def execute(self, sid, command, oid, *args): #@startTime = time.time() log.debug('execute', sid, oid, command, args) # check client id session = self.clientMngr.getSession(sid) if not session.cid: raise ige.SecurityException('No player object selected.') if not self.validateClient(session): raise ige.GameException('Wrong version of client.') # check game status (admin is allowed anytime) if self.status != Const.GS_RUNNING and session.cid != Const.OID_ADMIN: raise ige.ServerStatusException(self.status) # check existence of the commander if not self.db.has_key(session.cid): raise ige.GameException( 'This player does not exist. He/she could lose.') # update client's liveness session.touch() # find correct object type try: obj = self.db[oid] except KeyError: raise ige.NoSuchObjectException('Object %d does not exist.' % oid) cmdObj = getattr(self.cmdPool[obj.type], command) if not hasattr(cmdObj, 'public') or not cmdObj.public: raise ige.SecurityException( 'Access denied - method is not public.') # get acces level of the commander accLevel = Const.AL_NONE if obj.owner == session.cid: accLevel = Const.AL_OWNER if session.cid == Const.OID_ADMIN: accLevel = Const.AL_ADMIN #@log.debug('access rights', accLevel, cmdObj.accLevel) if cmdObj.accLevel > accLevel: raise ige.SecurityException('Access denied - low access level.') # create transaction (TODO - cache it!) tran = Transaction(self, session.cid, session) # invoke command on it result = cmdObj(*(tran, obj) + args) # commit transaction tran.commit() #@log.debug('result', result) # session messages #@log.debug('Messages:', session.messages.items()) messages = session.messages.items() session.messages.clear() #@log.debug("Execution time", time.time() - startTime) return result, messages
def selectPlayer(self, sid, playerID): """ Selects which of the player objects of the account is going to be used for this particular session.""" session = self.clientMngr.getSession(sid) if session.cid: raise ige.GameException('You already selected a player object.') try: accounts_player_objects = self.db[Const.OID_I_LOGIN2OID].get( session.login, []) except AttributeError: raise ige.SecurityException('Not logged in.') if playerID not in accounts_player_objects: raise ige.NoAccountException('Player object not on this account.') log.debug('Adding cid to session', playerID) session.cid = playerID # validate client if not self.validateClient(session): raise ige.GameException('Wrong version of client.') # notify object, that player has logged in player = self.db[playerID] self.cmdPool[player.type].loggedIn(Transaction(self), player) return True, None
def backup(self, sid, basename): session = self.clientMngr.getSession(sid) if session.login != Const.ADMIN_LOGIN: raise ige.SecurityException('You cannot issue this command.') self.db.backup(basename) self.clientMngr.backup(basename) self.msgMngr.backup(basename) return True, None
def commitDatabases(self, sid): session = self.clientMngr.getSession(sid) if session.login != Const.ADMIN_LOGIN: raise ige.SecurityException('You cannot issue this command.') self.db.checkpoint() self.clientMngr.checkpoint() self.msgMngr.checkpoint() return True, None
def processTurn(self, sid, turns=1): session = self.clientMngr.getSession(sid) if session.login != Const.ADMIN_LOGIN: raise ige.SecurityException('You cannot issue this command.') for turn in range(turns): log.message("--- TURN PROCESSING STARTED ---") # commit player's changes #if ige.igeRuntimeMode: # self.db.checkpoint() # get turn phases turn, turnspec, data = self.getTurnData(sid)[0] log.debug('Processing turn %d' % turn) tran = Transaction(self, session.cid, session) counter = 0 # phases for objIDs, phases in turnspec: # process all objects for objID in objIDs: # process all phases for phase in phases: todo = [objID] t0 = time.time() cnt0 = self.db.statCount log.debug('Processing turn %d phase %d.%s' % (turn, objID, phase)) while todo: tmpID = todo.pop(0) #@log.debug('Processing obj', tmpID) try: counter += 1 obj = self.db[tmpID] method = getattr( self.cmdPool[obj.type], 'process%sPhase' % phase, ) result = method(tran, obj, data) if result: todo.extend(result) obj = None except: log.warning('Cannot execute %s on %d' % (phase, tmpID)) log.debug('STATS -- time: %.3f sec, db accesses: %d' % (time.time() - t0, tran.db.statCount - cnt0)) log.message('Processed commands:', counter) # turn processing has finished self.turnFinished(sid) log.message("--- TURN PROCESSING FINISHED ---") return 1, None