def do_command(self, nickname, command, args): player = self.players[nickname] # inter-context commands if '.' in command: parts = command.split('.') if len(parts) != 2: player.tell("Inter-context commands take the form: context.command arg1 ... argN") return context_name, command = parts if context_name not in ['build', 'admin']: player.tell("Context must be one of: build, admin") return context_name = {'build':'builder', 'admin':'administration'}[context_name] # convert to true name ctxcls = contexts.get(context_name) if not ctxcls: player.tell("The %s context could not be loaded remotely." % context_name) return contextual = "com_%s" % command if not hasattr(ctxcls, contextual) or contextual == "com_exit": player.tell("The %s context has no %s command." % (context_name, command)) return context = ctxcls(self) contextual = getattr(context, contextual) # run validation try: data = validation.command(self, contextual, args) # run if valid contextual(player.session, data) db.commit() return except validation.ValidationError, e: self.tell(player, e.message) return except Exception, e: self.tell(player, "Sorry, that command resulted in an error on the server.") return
# Let context handle input if it wants if player.session.context.on_input(player.session, command, args): db.commit() return # determine the usable commands for this player clocals, cglobals = commands.get_allowed(player) if command in clocals+cglobals: # only respond to allowed commands # format for context based commands contextual = "com_%s" % command # session contextual command if hasattr(player.session.context, contextual): # get the command contextual = getattr(player.session.context, contextual) # validate passed arguments against schema try: data = validation.command(self, contextual, args) # run the comand if validated contextual(player.session, data) db.commit() except validation.ValidationError, e: self.tell(player, e.message) except Exception, e: self.tell(player, "Sorry, that command resulted in an error on the server.") dtrace("Context command caused an error : %s %s" % (command, args)) else: # its not contextual so check dynamic commands comm_cls = commands.get(command) if comm_cls: # validate passed arguments against schema try: data = validation.command(self, comm_cls, args)
def handle_TAB(self): if self.nickname: # only tab once logged in parts = ''.join(self.lineBuffer).split(' ') player = self.app.players[self.nickname] comname = parts[0] if self.tabchoices and self.tabarg != None: # if queue, cycle self.tabchoices.append( self.tabchoices.pop(0) ) choice = self.tabchoices[0] end = len(self.lineBuffer) diff = end - self.lineBufferIndex for x in xrange(diff): self.handle_RIGHT() for x in range(end): HistoricRecvLine.handle_BACKSPACE(self) parts[self.tabarg] = choice newline = ' '.join(parts) end = len(' '.join(parts[:self.tabarg + 1])) for c in newline: HistoricRecvLine.characterReceived(self, c, None) diff = abs(end - self.lineBufferIndex) for x in xrange(diff): self.handle_LEFT() else: # determine tabchoices # complete commands if len(parts) == 1: if '.' in parts[0]: _parts = parts[0].split('.') if len(_parts) != 2: player.tell("Inter-context commands take the form: context.command arg1 ... argN") return context_name, command = _parts if context_name not in ['build', 'admin'] and command != 'exit': player.tell("Context must be one of: build, admin") return _context_name = {'build':'builder', 'admin':'administration'}[context_name] # convert to true name ctxcls = contexts.get(_context_name) if not ctxcls: player.tell("The %s context could not be loaded remotely." % _context_name) return contextual = "com_%s" % command context = ctxcls(self) self.tabchoices = ["%s.%s" % (context_name, attribute[4:]) for attribute in dir(context) if attribute.startswith(contextual) and attribute != 'com_exit'] if self.tabchoices: self.tabarg = 0 return self.handle_TAB() elif isinstance(player.session.context, contexts.get('battle')): self.tabchoices = [move.selector.encode('utf8') for move in player.character.moves if move.selector.startswith(parts[0])] callowed, cglobals = get_allowed(player) self.tabchoices += [cname for cname in callowed if cname.startswith(parts[0])] if self.tabchoices: self.tabarg = 0 return self.handle_TAB() else: callowed, cglobals = get_allowed(player) self.tabchoices = [cname for cname in callowed+cglobals if cname.startswith(parts[0])] if self.tabchoices: self.tabarg = 0 return self.handle_TAB() # complete arguments schema = None comobj = None callowed, cglobals = get_allowed(player) if len(parts) > 1: if '.' in comname: _parts = comname.split('.') if len(_parts) == 2: context_name, command = _parts if context_name in ['build', 'admin'] and command != exit: _context_name = {'build':'builder', 'admin':'administration'}[context_name] # convert to true name ctxcls = contexts.get(_context_name) if ctxcls: contextual = "com_%s" % command context = ctxcls(self) if hasattr(context, contextual): comobj = getattr(context, contextual) # battle move arg elif isinstance(player.session.context, contexts.get('battle')) and len(parts) == 2: moves = [move for move in player.character.moves if move.selector == comname] if moves: move = moves[0] self.tabchoices = [] for fighter in self.app.game.fighters.itervalues(): try: self.app.game.validate_target(player, fighter, move) except ValidationError: pass else: self.tabchoices.append(fighter.nickname.encode('utf8')) if self.tabchoices: self.tabarg = 1 self.handle_TAB() # contextual elif comname in callowed: contextual = "com_%s" % comname if hasattr(player.session.context, contextual): # get the command comobj = getattr(player.session.context, contextual) # global elif comname in cglobals: comobj = get(comname) # do complete via exception if comobj: try: data = v.command(self.app, comobj, parts[1:], player=player) except v.ValidationError, e: if e.choices: self.tabchoices = [c.encode('utf8') for c in e.choices] if self.tabchoices: self.tabarg = e.argnum self.handle_TAB()