Exemplo n.º 1
0
 def perform(self):
     clocals, cglobals = commands.get_allowed(self.player)
     context = self.player.session.context
     try:
         command = self.args["command"]
     except KeyError:
         if context.__doc__:
             self.player.tell("", fmt="-<")
             for wline in context.__doc__.splitlines():
                 for line in wrap(wline, self.player.linewidth):
                     self.player.tell(line)
             #                self.player.tell("", fmt="-<")
             self.player.tell("AVAILABLE COMMANDS ('all' for more)", fmt="-<")
             available = ["help", "all"] + clocals
             available = ", ".join(available)
             for line in wrap(available, self.player.linewidth):
                 self.player.tell(line)
     else:
         comobj = None
         if command in clocals + cglobals:
             try:
                 comobj = getattr(context, "com_%s" % command)
             except:
                 comobj = commands.get(command)
             finally:
                 if comobj:
                     if comobj.__doc__ or hasattr(comobj, "schema"):
                         for line in self._get_help(comobj):
                             self.player.tell(line)
                     else:
                         self.player.tell("Sorry, no help is available for '%s'." % command)
                 else:
                     self.player.tell("Sorry, %s is not a command." % command)
Exemplo n.º 2
0
     # 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)
             # create live command object
             new_comm = comm_cls(self, player, data)
             # let command verify submission
             new_comm.verify()
             new_comm.perform()
             db.commit()
             return
         except validation.ValidationError, e:
             self.tell(player, e.message)
         except Exception, e:
             self.tell(player, 
Exemplo n.º 3
0
    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()