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()
def characterReceived(self, ch, moreCharactersComing): self.reset_tab() HistoricRecvLine.characterReceived(self, ch, moreCharactersComing)