def character(app, name, argsleft, player=None): """Validate argument to an existing character.""" selector = argsleft.pop(0) kwargs = {"selector": selector} print "CONTEXT BUILDER:", contexts.get("builder") if player and not isinstance(player.session.context, contexts.get("builder")): kwargs["finalized"] = 1 char = Character.filter(**kwargs) if char: return char[0], argsleft else: del kwargs["selector"] choices = [c.selector for c in Character.filter(**kwargs) if c.selector.startswith(selector)] raise ValidationError("'%s' is not a valid character selector." % selector, character, choices)
def switch(self, context): ctxcls = contexts.get(context) if ctxcls: if self.context: self.last_context_name = self.context_name self.context.leave(self) self.context_name = context self.context = ctxcls(self) self.context.enter(self)
def _get_status(self): if self.nickname and self.nickname in self.app.players: player = self.app.players[self.nickname] if isinstance(player.session.context, contexts.get('battle')): ready = player.current_move.name.capitalize() if player.current_move else "READY" status = "# {p.health}HP:{p.magicpoints}MP:{p.superpoints}SP #-{ready}".format(p=player, ready=ready) result = status + "{0:-^{1}}".format('-', MLW - len(status)) return result else: return "{0:-^{1}}".format("Ultra Relay Battle 0.9", MLW + 2) # +2 for no prompt else: return "{0:-^{1}}".format("Ultra Relay Battle 0.9", MLW + 2)
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
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()