def process_clients(SERVER_RUN, OPList, CLIENT_LIST, CLIENT_DATA): """ Check each client, if client.cmd_ready == True then there is a line of input available via client.get_command(). """ for client in CLIENT_LIST: if client.active and client.cmd_ready: msg = client.get_command() # the string recieved from the client if msg.startswith("'"): msg = msg[:1] + " " + msg[1:] lmsg = msg.lower() # an all-lowercase version of the string recieved from the client if msg == '': cmd = '' # cmd is the first word of the string from the client. It represents the command sent by the client, to be processed below else: cmd = lmsg.split()[0] args = msg.split()[1:] # args is everything following the first word, not including the first word(cmd) #print "args: " + str(args) clientDataID = str(client.addrport()) # location of client's data in CLIENT_DATA #prompt = CLIENT_DATA[clientDataID].prompt commandSuccess = False #--------------------- # Command Definitions #--------------------- if CLIENT_DATA[clientDataID].name == "none" or CLIENT_DATA[clientDataID].name == '': # client just logged in and doesn't have a name assigned. Accept the first input and assign it to the client as it's name. CLIENT_DATA[clientDataID].name = str(msg) CLIENT_DATA[clientDataID].lastCmd = str(msg) path = "data/client/" + CLIENT_DATA[clientDataID].name + "/" + CLIENT_DATA[clientDataID].name if os.path.isfile(path): client.send("\nWelcome back, %s!\n" %CLIENT_DATA[clientDataID].name) client.send("Please enter your password.\n") return else: client.send("\nHello, %s!\n" % CLIENT_DATA[clientDataID].name) client.send("Choose a password. It can be anything, just be sure to remember it. If you lose your password, your character is gone forever!\n") return while CLIENT_DATA[clientDataID].authSuccess == False: path = "data/client/" + CLIENT_DATA[clientDataID].name + "/" + CLIENT_DATA[clientDataID].name if os.path.isfile(path): with open(path, 'r') as f: CLIENT_DATA[clientDataID].password = f.readline() CLIENT_DATA[clientDataID].password = CLIENT_DATA[clientDataID].password[:-1] else: CLIENT_DATA[clientDataID].password = '******' if CLIENT_DATA[clientDataID].password == ' ': CLIENT_DATA[clientDataID].password = sha256_crypt.encrypt(str(msg)) # print CLIENT_DATA[clientDataID].password # print "pwl:" + str(len(CLIENT_DATA[clientDataID].password)) CLIENT_DATA[clientDataID].authSuccess = True else: # path = "data/client/" + CLIENT_DATA[clientDataID].name try: with open(path, 'r') as f: password = f.readline() password = password[:-1] # print password # print "pwl:" + str(len(CLIENT_DATA[clientDataID].password)) CLIENT_DATA[clientDataID].authSuccess = sha256_crypt.verify(str(msg), password) # print authSuccess CLIENT_DATA[clientDataID].password = password except: raise if CLIENT_DATA[clientDataID].authSuccess == False: if CLIENT_DATA[clientDataID].numTries == 2: client.send("Last attempt is final before kick.\n") elif CLIENT_DATA[clientDataID].numTries > 2: CLIENT_DATA[clientDataID].numTries = 0 client.send("Too many failed passwords. Goodbye.\n") print "-- Disconnected " + str(clientDataID) + " (too many failed pw)" client.active = False client.send("Incorrect password. Please try again.\n") CLIENT_DATA[clientDataID].numTries += 1 CLIENT_DATA[clientDataID].lastCmd = str(msg) return if CLIENT_DATA[clientDataID].authSuccess == True and CLIENT_DATA[clientDataID].loadFinish == False: for cli in CLIENT_DATA: # print CLIENT_DATA[cli].name # print CLIENT_DATA[cli].clientDataID # print CLIENT_DATA[clientDataID].name # print CLIENT_DATA[clientDataID].clientDataID # print "****" if CLIENT_DATA[cli].name == CLIENT_DATA[clientDataID].name and CLIENT_DATA[cli].clientDataID != CLIENT_DATA[clientDataID].clientDataID: CLIENT_DATA[cli].client.send("You have been kicked because your username logged in again.\n") print "-- Kicked " + CLIENT_DATA[cli].clientDataID + " " + CLIENT_DATA[cli].name + " (duplicate login)" kickTimer = World.Timer(Globals.TIMERS, 1, None, actionArgs = [], attachedTo = None, respawns = False) kickTimer.actionFunction = timerActions.kick(CLIENT_DATA[cli].client, kickTimer, Globals.TIMERS) print "** " + str(client.addrport()) + " identified as " + str(CLIENT_DATA[clientDataID].name) # client.send(prompt) mortalComponent = World.mortal(hp=100,maxHp=100,pp=10,maxPp=10,level=1,exp=0,money=0,offense=1,defense=1,speed=1,guts=1,luck=1,vitality=1,IQ=1,inventory=[]) if os.path.isfile('data/client/'+str(CLIENT_DATA[clientDataID].name) + '/' + str(CLIENT_DATA[clientDataID].name)): #print "cl:" + str(CLIENT_LIST) #CLIENT_DATA[clientDataID].avatar = World.Player(description='Just another traveler.', currentRoom = Globals.startingRoom, name=CLIENT_DATA[clientDataID].name, client=client, clientDataID = clientDataID, kind=mortalComponent) SysInit.clientDataLoad(client, CLIENT_LIST, CLIENT_DATA, Globals.TIMERS, mortalComponent) #Globals.startingRoom.players.remove(CLIENT_DATA[clientDataID].avatar) # print 'sp:' + str(Globals.startingRoom.players) + " sr:" + str(Globals.startingRoom.name) + ' ' + str(Globals.startingRoom) # print 'ava:' + str(CLIENT_DATA[clientDataID].avatar.currentRoom.name)+ ' asrp:'+ str(CLIENT_DATA[clientDataID].avatar.currentRoom.players) # print 'avacr:' + str(CLIENT_DATA[clientDataID].avatar.currentRoom) #print "cl2:" + str(CLIENT_LIST) else: os.mkdir('data/client/'+str(CLIENT_DATA[clientDataID].name) + '/') CLIENT_DATA[clientDataID].avatar = World.Player(description='Just another traveler.', currentRoom = Globals.startingRoom, name=CLIENT_DATA[clientDataID].name, client=client, clientDataID = clientDataID, kind=mortalComponent) Globals.startingRoom.players.append(CLIENT_DATA[clientDataID].avatar) # print 'starting;' + str(Globals.startingRoom.players) with open(path, 'w') as f: f.write(str(CLIENT_DATA[clientDataID].password) + '\n') # print CLIENT_DATA[clientDataID].password player = CLIENT_DATA[clientDataID].avatar for playerName in OPList: #print str(CLIENT_DATA[clientDataID].name) #print str(playerName) if playerName.endswith('\n'): playerName = playerName[:-1] if str(CLIENT_DATA[clientDataID].name) == str(playerName): CLIENT_DATA[clientDataID].op = True #print "op true" #print CLIENT_DATA[clientDataID].op cMove.alert(client, CLIENT_DATA, ("\n^g^!%s appeared.^~\n" %player.name)) #print Rooms.startingRoom.players if CLIENT_DATA[clientDataID].gameState == 'normal': cInfo.render_room(client=client, player=CLIENT_DATA[clientDataID].avatar, room=CLIENT_DATA[clientDataID].avatar.currentRoom, CLIENT_DATA=CLIENT_DATA) elif CLIENT_DATA[clientDataID].gameState == 'battle': cInfo.battleLook(client=client, args=[], CLIENT_LIST=Globals.CLIENT_LIST, CLIENT_DATA=CLIENT_DATA) CLIENT_DATA[clientDataID].loadFinish = True commandSuccess = True elif cmd == 'say': ## If the client sends a 'say' command echo it to the room cChat.say(client, args, CLIENT_LIST, CLIENT_DATA) commandSuccess = True elif cmd == 'shout': ## If the client sends a 'shout' command echo it to the region cChat.shout(client, args, CLIENT_LIST, CLIENT_DATA) commandSuccess = True elif cmd == 'tell' or cmd == 't': cChat.tell(client, args, CLIENT_LIST, CLIENT_DATA) commandSuccess = True elif cmd == 'chat' or cmd == 'c': ## If the client sends a 'chat' command echo it to the chat channel cChat.chat(client, args, CLIENT_LIST, CLIENT_DATA) CLIENT_DATA[str(client.addrport())].replyTo = None commandSuccess = True elif cmd == "'": if CLIENT_DATA[client.addrport()].replyTo is None: cChat.chat(client, args, CLIENT_LIST, CLIENT_DATA) else: args.insert(0, CLIENT_DATA[(CLIENT_DATA[client.addrport()].replyTo).addrport()].name) isOnline = cChat.tell(client, args, CLIENT_LIST, CLIENT_DATA) if isOnline == False: CLIENT_DATA[str(client.addrport())].replyTo = None #print CLIENT_DATA[client.addrport()].replyTo commandSuccess = True elif cmd == 'channel': ## if the client sends a 'channel' command, create a new chat channel cChat.Channel() commandSuccess = True elif cmd == 'who': ## display who is online cInfo.who(client, args, CLIENT_LIST, CLIENT_DATA) commandSuccess = True elif cmd == 'title': ## set player title to args cPersonal.title(client,args,CLIENT_LIST,CLIENT_DATA) commandSuccess = True elif cmd == 'inventory' or cmd == 'i': ## If the client sends an 'inventory' command, display the contents of the client avatar's inventory cInfo.inventory(client, args, CLIENT_LIST, CLIENT_DATA) commandSuccess = True elif cmd == 'use' or cmd == 'u': ## Use an item in a player's inventory cInteractions.use(client, args, CLIENT_LIST, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.currentRoom) commandSuccess = True elif cmd == 'equip': ## Equip a bit of kit cInteractions.equip(client, args, CLIENT_LIST, clientDataID, CLIENT_DATA) commandSuccess = True elif cmd == 'remove': ## Remove a bit of kit cInteractions.remove(client, args, CLIENT_LIST, clientDataID, CLIENT_DATA) commandSuccess = True elif cmd == 'status' or cmd == 'st': cInfo.status(client, args, CLIENT_LIST, CLIENT_DATA) commandSuccess = True elif CLIENT_DATA[clientDataID].op == True: # OP-only commands. if cmd == 'spawn': # don't spawn anything but top level items for testing, otherwise it breaks things item = args[0] if len(args)>1: active = args[1] #print active if active == 'on': active = True elif active == 'off': active = False # elif active == '': # active = False else: client.send("Second argument should be 'on' to turn on object spawning, else 'off' or nothing.\n") return if len(args)>2: container = args[2] else: active = False container = None newObject = cmdSpawnObject(item, CLIENT_DATA[clientDataID].avatar.currentRoom, active) if container is not None: for item in CLIENT_DATA[clientDataID].avatar.currentRoom.objects: if item.name == str(container): if hasattr(newObject, 'kind'): if hasattr(newObject.kind, 'objectSpawner'): if newObject.kind.objectSpawner is not None: newObject.kind.objectSpawner.container = item.kind if newObject.kind is not None: newObject.kind.spawnContainer = item item.kind.inventory.append(newObject) CLIENT_DATA[clientDataID].avatar.currentRoom.objects.remove(newObject) # elif item.inventory != []: # for item in item.inventory: # if item.name == str(container): # if newObject.kind.objectSpawner: # newObject.kind.objectSpawner.container = item # item.kind.inventory.append(newObject) # CLIENT_DATA[clientDataID].avatar.currentRoom.objects.inventory.remove(newObject) commandSuccess = True elif cmd == 'shutdown': ## shutdown the server (needs to be protected or removed) print "** Shutdown request received from %s." % CLIENT_DATA[clientDataID].name return 'shutdown' commandSuccess = True # State-dependant commands if CLIENT_DATA[clientDataID].gameState == 'normal': if cmd in CLIENT_DATA[clientDataID].avatar.currentRoom.exits: ## player used a room exit name as a command, move to room associated with the exit cMove.move(client, cmd, args, CLIENT_LIST, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.currentRoom.exits) elif cmd in ['1','2','3','4','5','6','7','8','9']: exitList = [] for exit in CLIENT_DATA[clientDataID].avatar.currentRoom.exits: exitList.append(exit) if len(CLIENT_DATA[clientDataID].avatar.currentRoom.exits) >= int(cmd): cMove.move(client, exitList[int(cmd)-1], args, CLIENT_LIST, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.currentRoom.exits) else: if len(CLIENT_DATA[clientDataID].avatar.currentRoom.exits) == 0: client.send("You don't see any exits!.\n") elif len(CLIENT_DATA[clientDataID].avatar.currentRoom.exits) == 1: client.send("You only see 1 exit.\n") else: client.send("You only see " + str(len(CLIENT_DATA[clientDataID].avatar.currentRoom.exits)) + " exits.\n") elif cmd == 'get': ## pick up an item in the room cInteractions.get(client, args, clientDataID, CLIENT_DATA, (CLIENT_DATA[clientDataID].avatar.currentRoom)) elif cmd == 'check': cInteractions.check(client, args, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.currentRoom) elif cmd == 'drop': ## drop an item in the room cInteractions.drop(client, args, clientDataID, CLIENT_DATA, (CLIENT_DATA[clientDataID].avatar.currentRoom)) elif cmd == 'fight' or cmd == 'f': ## start a battle with a mob fighting = cInteractions.startBattle(client, args, clientDataID, CLIENT_DATA, (CLIENT_DATA[clientDataID].avatar.currentRoom)) if fighting: CLIENT_DATA[clientDataID].gameState = 'battle' elif cmd == 'look' or cmd == 'l': ## If the client sends a 'look' command, send back the description cInfo.look(client, args, CLIENT_LIST, CLIENT_DATA) elif cmd == 'lh': ## alias for 'look harder' cInfo.look(client, ['harder'], CLIENT_LIST, CLIENT_DATA) elif cmd == 'examine' or cmd == 'ex': ## If the client sends an 'examine' command, send back the long description cInfo.examine(client, args, CLIENT_LIST, CLIENT_DATA) elif cmd == 'quit': ## client is disconnecting client.send("Disconnected.") #CLIENT_LIST.remove(client) client.active = False elif cmd == '': ## client just send a return ony, with no information at all client.send("\nDid you mean to tell me something?\n\n") elif commandSuccess == False: ## command does not exist or is badly formed client.send("\nHuh? I don't know what '%s' means.\n\n" % msg) elif CLIENT_DATA[clientDataID].gameState == 'battle': if cmd == 'look' or cmd == 'l': cInfo.battleLook(client, args, CLIENT_LIST, CLIENT_DATA) elif cmd == 'examine' or cmd == 'ex': cInfo.battleExamine(client, args, CLIENT_LIST, CLIENT_DATA) elif cmd == 'lh': cInfo.battleExamine(client, ['lh'], CLIENT_LIST, CLIENT_DATA) elif cmd == 'flee': cMove.fleeBattle(client, args, CLIENT_LIST, CLIENT_DATA) elif cmd == 'bash' or cmd == 'b': battleCommands.bash(client, args, CLIENT_LIST, CLIENT_DATA) elif cmd in CLIENT_DATA[clientDataID].avatar.battleCommands: if cmd == 'identify': commandFunction = battleCommands.identify commandFunction(client, args, CLIENT_LIST, CLIENT_DATA) elif cmd == '': ## client just send a return ony, with no information at all client.send("\nDid you mean to tell me something?\n\n") elif commandSuccess == False: ## command does not exist or is badly formed client.send("\nI can't '%s' in a battle.\n\n" % msg) elif CLIENT_DATA[clientDataID].gameState == 'levelup': if cmd == 'a' or cmd == 'A': statRaiser(client, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.lvlchoiceslist[0]) elif cmd == 'b' or cmd == 'B': statRaiser(client, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.lvlchoiceslist[1]) elif cmd == 'c' or cmd == 'C': if len(CLIENT_DATA[clientDataID].lvlchoiceslist) > 2: statRaiser(client, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.lvlchoiceslist[2]) else: client.send("C is not an available choice.\n") elif cmd == 'd' or cmd == 'D': if len(CLIENT_DATA[clientDataID].lvlchoiceslist) > 3: statRaiser(client, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.lvlchoiceslist[3]) else: client.send("D is not an available choice.\n") elif cmd == 'e' or cmd == 'E': if len(CLIENT_DATA[clientDataID].lvlchoiceslist) > 4: statRaiser(client, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.lvlchoiceslist[4]) else: client.send("E is not an available choice.\n") elif cmd == 'f' or cmd == 'F': if len(CLIENT_DATA[clientDataID].lvlchoiceslist) > 5: statRaiser(client, clientDataID, CLIENT_DATA, CLIENT_DATA[clientDataID].avatar.lvlchoiceslist[5]) else: client.send("F is not an available choice.\n") else: client.send("Please select using one letter from : A,B,C,D,E,F.\n") elif cmd == '': ## client just send a return ony, with no information at all client.send("\nDid you mean to tell me something?\n\n") elif commandSuccess == False: ## command does not exist or is badly formed client.send("\nHuh? I don't know what '%s' means.\n\n" % msg) CLIENT_DATA[clientDataID].lastCmd = str(msg)
def loadMobFromFile(file): ''' handles loading a single mob from a given mob definition file into the world ''' print file if str(file).endswith('~'): print '\n' return path = 'blueprints/mob/' + file with open(path, 'r') as f: fileData = f.readlines() newMob = World.Mob('none', 'none', 'none') newMob.mobID = '' print fileData splitFile = file.split("/") mobID = None name = 'none' species = None currentRoom = None region = None description = '' longDescription = '' hp = 0 exp = 0 inventory = [] inventorySize = 0 equipment = {} kind = None expirator = None inventoryItems = [] currentRoomString = '' moveAI = None battleAI = None newMob.kind = World.mortal(hp=0,maxHp=0,pp=0,maxPp=0,level=0,exp=0,money=0,offense=0,defense=0,speed=0,guts=0,luck=0,vitality=0,IQ=0,inventory=[],inventorySize=0,equipment={}) newMob.region = splitFile[0] for Data in fileData: if Data.startswith('mobID='): IDstring = Data[6:-1] if IDstring != '': newMob.mobID = int(IDstring) if Data.startswith('name='): newMob.name = Data[5:-1] if Data.startswith('species='): newMob.species = Data[8:-1] if Data.startswith('currentRoom='): currentRoomString = Data[12:-1] if Data.startswith('description='): newMob.description = Data[12:-1] if Data.startswith('longDescription='): newMob.longDescription = Data[16:-1] if Data.startswith('speech='): newMob.speech = Data[7:-1] if Data.startswith('expirator='): expirator = Data[10:-1] if expirator != '': expirator = int(expirator) if Data.startswith('moveAI='): text = Data[7:-1] moveAI = text.split(":") if Data.startswith('battleAI='): text = Data[9:-1] if text == 'basicBash': battleAI = aiBattle.basicBash else: battleAI = '' if Data.startswith('kind.hp='): newMob.kind.hp = int(Data[8:-1]) if Data.startswith('kind.maxHp='): newMob.kind.maxHp = int(Data[11:-1]) if Data.startswith('kind.pp='): newMob.kind.pp = int(Data[8:-1]) if Data.startswith('kind.maxPp='): newMob.kind.maxPp = int(Data[11:-1]) if Data.startswith('kind.level='): newMob.kind.level = int(Data[11:-1]) if Data.startswith('kind.exp='): newMob.kind.exp = int(Data[9:-1]) if Data.startswith('kind.money='): newMob.kind.money = int(Data[11:-1]) if Data.startswith('kind.offense='): newMob.kind.offense = int(Data[13:-1]) if Data.startswith('kind.defense='): newMob.kind.defense = int(Data[13:-1]) if Data.startswith('kind.speed='): newMob.kind.speed = int(Data[11:-1]) if Data.startswith('kind.guts='): newMob.kind.guts = int(Data[10:-1]) if Data.startswith('kind.luck='): newMob.kind.luck = int(Data[10:-1]) if Data.startswith('kind.vitality='): newMob.kind.vitality = int(Data[14:-1]) if Data.startswith('kind.IQ='): newMob.kind.IQ = int(Data[8:-1]) if Data.startswith('kind.inventory='): invString = Data[15:-1] if invString != '': #print "invString:" + invString invList = invString.split(', ') #print 'invList:' + str(invList) for item in invList: for ob in Globals.fromFileList: if item == ob.name: inventoryItems.append(item) else: inventoryItems = [] if Data.startswith('kind.inventorySize='): newMob.kind.inventorySize = int(Data[19:-1]) if currentRoomString != '': currentRoomCoords = currentRoomString.split(":") newMob.currentRoom = Globals.regionListDict[currentRoomCoords[0]][currentRoomCoords[1]] else: # newMob.currentRoom = Globals.regionListDict[newMob.region]['bullpen'] newMob.currentRoom = None if expirator != None and expirator != '': expiratorComponent = World.expirator(newMob, expirator) newMob.expirator = expiratorComponent if moveAI != None and moveAI != []: newMoveAI = aiMove.movementAI(newMob, int(moveAI[1])) if moveAI[0] == 'basicRandom': newMoveAI.Timer.actionFunction = newMoveAI.basicRandom elif moveAI[0] == 'introvertRandom': newMoveAI.Timer.actionFunction = newMoveAI.introvertRandom elif moveAI[0] == 'extrovertRandom': newMoveAI.Timer.actionFunction = newMoveAI.extrovertRandom elif moveAI[0] == 'doNotMove': newMoveAI.Timer.actionFunction = newMoveAI.doNotMove newMob.aiMove = newMoveAI Globals.MoveTIMERS.remove(newMob.aiMove.Timer) if battleAI != None: newMob.aiBattle = battleAI #print 'invItems:' + str(inventoryItems) for item in inventoryItems: #print 'invitem:' + str(item) removed = False newItem = Engine.cmdSpawnObject(item, newMob.currentRoom, alert=False, whereFrom='mobinv') newMob.kind.inventory.append(newItem) # for obj in newMob.currentRoom.objects: # if obj.name == item: # newMob.currentRoom.objects.remove(obj) if newMob.currentRoom is not None: newMob.currentRoom.objects.remove(newItem) if newMob.currentRoom is not None: newMob.currentRoom.mobs.append(newMob) if expirator != None and expirator != '': Globals.TIMERS.remove(newMob.expirator.Timer) #newMob.expirator.Timer = None Globals.mobsFromFile.append(newMob)