示例#1
0
    def func(self):
        """
        Handle the looking - add fallback to details.
        """
        caller = self.caller
        args = self.args
        if args:
            looking_at_obj = caller.search(args, use_nicks=True, quiet=True)
            if not looking_at_obj:
                # no object found. Check if there is a matching
                # detail at location.
                location = caller.location
                if location and hasattr(location, "return_detail") and callable(location.return_detail):
                    detail = location.return_detail(args)
                    if detail:
                        # we found a detail instead. Show that.
                        caller.msg(detail)
                        return
                # no detail found. Trigger delayed error messages
                _AT_SEARCH_RESULT(caller, args, looking_at_obj, False)
                return
            else:
                # we need to extract the match manually.
                looking_at_obj = utils.make_iter(looking_at_obj)[0]
        else:
            looking_at_obj = caller.location
            if not looking_at_obj:
                caller.msg("You have no location to look at!")
                return

        if not hasattr(looking_at_obj, "return_appearance"):
            # this is likely due to us having a player instead
            looking_at_obj = looking_at_obj.character
        if not looking_at_obj.access(caller, "view"):
            caller.msg("Could not find '%s'." % args)
            return
        # get object's appearance
        caller.msg(looking_at_obj.return_appearance(caller))
        # the object's at_desc() method.
        looking_at_obj.at_desc(looker=caller)
示例#2
0
    def func(self):
        """
        Handle the looking - add fallback to details.
        """
        caller = self.caller
        args = self.args
        if args:
            looking_at_obj = caller.search(args, use_nicks=True, quiet=True)
            if not looking_at_obj:
                # no object found. Check if there is a matching
                # detail at location.
                location = caller.location
                if location and hasattr(location, "return_detail") and callable(location.return_detail):
                    detail = location.return_detail(args)
                    if detail:
                        # we found a detail instead. Show that.
                        caller.msg(detail)
                        return
                # no detail found. Trigger delayed error messages
                _AT_SEARCH_RESULT(caller, args, looking_at_obj, False)
                return
            else:
                # we need to extract the match manually.
                looking_at_obj = utils.make_iter(looking_at_obj)[0]
        else:
            looking_at_obj = caller.location
            if not looking_at_obj:
                caller.msg("You have no location to look at!")
                return

        if not hasattr(looking_at_obj, 'return_appearance'):
            # this is likely due to us having a player instead
            looking_at_obj = looking_at_obj.character
        if not looking_at_obj.access(caller, "view"):
            caller.msg("Could not find '%s'." % args)
            return
        # get object's appearance
        caller.msg(looking_at_obj.return_appearance(caller))
        # the object's at_desc() method.
        looking_at_obj.at_desc(looker=caller)
示例#3
0
    def format_output(self, obj, avail_cmdset):
        """
        Helper function that creates a nice report about an object.

        returns a string.
        """

        string = "\n{wName/key{n: {c%s{n (%s)" % (obj.name, obj.dbref)
        if hasattr(obj, "aliases") and obj.aliases.all():
            string += "\n{wAliases{n: %s" % (", ".join(utils.make_iter(str(obj.aliases))))
        if hasattr(obj, "sessid") and obj.sessid:
            string += "\n{wsession{n: %s" % obj.sessid
        elif hasattr(obj, "sessions") and obj.sessions:
            string += "\n{wsession(s){n: %s" % (", ".join(str(sess.sessid) for sess in obj.sessions))
        if hasattr(obj, "has_player") and obj.has_player:
            string += "\n{wPlayer{n: {c%s{n" % obj.player.name
            perms = obj.player.permissions
            if obj.player.is_superuser:
                perms = ["<Superuser>"]
            elif not perms:
                perms = ["<None>"]
            string += "\n{wPlayer Perms{n: %s" % (", ".join(perms))
        string += "\n{wTypeclass{n: %s (%s)" % (obj.typeclass.typename, obj.typeclass_path)
        if hasattr(obj, "location"):
            string += "\n{wLocation{n: %s" % obj.location
            if obj.location:
                string += " (#%s)" % obj.location.id
        if hasattr(obj, "destination"):
            string += "\n{wDestination{n: %s" % obj.destination
            if obj.destination:
                string += " (#%s)" % obj.destination.id
        perms = obj.permissions
        if perms:
            perms_string = (", ".join(perms))
        else:
            perms_string = "Default"
        if obj.is_superuser:
            perms_string += " [Superuser]"

        string += "\n{wPermissions{n: %s" % perms_string
        locks = str(obj.locks)
        if locks:
            locks_string = utils.fill("; ".join([lock for lock in locks.split(';')]), indent=6)
        else:
            locks_string = " Default"


        string += "\n{wLocks{n:%s" % locks_string

        if not (len(obj.cmdset.all()) == 1 and obj.cmdset.current.key == "Empty"):
            # list the current cmdsets
            all_cmdsets = obj.cmdset.all() + (hasattr(obj, "player") and obj.player and obj.player.cmdset.all() or [])
            all_cmdsets.sort(key=lambda x:x.priority, reverse=True)
            string += "\n{wCurrent Cmdset(s){n:\n %s" % ("\n ".join("%s (prio %s)" % (cmdset.path, cmdset.priority) for cmdset in all_cmdsets))

            # list the commands available to this object
            avail_cmdset = sorted([cmd.key for cmd in avail_cmdset if cmd.access(obj, "cmd")])

            cmdsetstr = utils.fill(", ".join(avail_cmdset), indent=2)
            string += "\n{wCommands available to %s (all cmdsets + exits and external cmds){n:\n %s" % (obj.key, cmdsetstr)

        if hasattr(obj, "scripts") and hasattr(obj.scripts, "all") and obj.scripts.all():
            string += "\n{wScripts{n:\n %s" % obj.scripts
        # add the attributes
        string += self.format_attributes(obj)
        # add the contents
        exits = []
        pobjs = []
        things = []
        if hasattr(obj, "contents"):
            for content in obj.contents:
                if content.destination:
                    exits.append(content)
                elif content.player:
                    pobjs.append(content)
                else:
                    things.append(content)
            if exits:
                string += "\n{wExits{n: %s" % ", ".join([exit.name for exit in exits])
            if pobjs:
                string += "\n{wCharacters{n: %s" % ", ".join(["{c%s{n" % pobj.name for pobj in pobjs])
            if things:
                string += "\n{wContents{n: %s" % ", ".join([cont.name for cont in obj.contents
                                                           if cont not in exits and cont not in pobjs])
        separator = "-"*78
        #output info
        return '%s\n%s\n%s' % ( separator, string.strip(), separator )
示例#4
0
def prompt_choice(caller, question="", prompts=None, choicefunc=None, force_choose=False):
    """
    This sets up a simple choice questionnaire. Question will be
    asked, followed by a serie of prompts. Note that this isn't
    making use of the menu node system.

    caller - the object calling and being offered the choice
    question - text describing the offered choice
    prompts - list of choices
    choicefunc - functions callback to be called as func(self) when
                 make choice (self.caller is available) The function's definision
                 should be like func(self, menu_node), and menu_node.key is user's
                 choice.
    force_choose - force user to make a choice or not
    """

    # creating and defining commands
    count = 0
    choices = ""
    commands = []

    for choice in utils.make_iter(prompts):
        count += 1
        choices += "\n{lc%d{lt[%d]{le %s" % (count, count, choice)

        cmdfunc = CmdMenuNode(key="%d" % count)
        if choicefunc:
            cmdfunc.choicefunc = choicefunc
            def _choicefunc(self):
                self.caller.cmdset.delete('menucmdset')
                del self.caller.db._menu_data
                self.choicefunc(self)
            cmdfunc.callback = MethodType(_choicefunc, cmdfunc, CmdMenuNode)

        commands.append(cmdfunc)

    if not force_choose:
        choices += "\n{lc{lt[No choice]{le"

    prompt = question + choices + "\nPlease choose one."

    errorcmd = CmdMenuNode(key=CMD_NOMATCH)
    if force_choose:
        def _errorcmd(self):
            self.caller.msg("You can only choose given choices.")
    else:
        if choicefunc:
            errorcmd.choicefunc = choicefunc
            def _errorcmd(self):
                self.caller.msg("No choice.")
                self.caller.cmdset.delete('menucmdset')
                del self.caller.db._menu_data
                self.choicefunc(self)
    errorcmd.callback = MethodType(_errorcmd, errorcmd, CmdMenuNode)

    defaultcmd = CmdMenuNode(key=CMD_NOINPUT)
    if force_choose:
        def _defaultcmd(self):
            caller.msg(prompt)
    else:
        if choicefunc:
            defaultcmd.choicefunc = choicefunc
            def _defaultcmd(self):
                self.caller.msg("No choice.")
                self.caller.cmdset.delete('menucmdset')
                del self.caller.db._menu_data
                self.choicefunc(self)
    defaultcmd.callback = MethodType(_defaultcmd, defaultcmd, CmdMenuNode)

    # creating cmdset (this will already have look/help commands)
    choicecmdset = MenuCmdSet()
    for cmdfunc in commands: choicecmdset.add(cmdfunc)
    choicecmdset.add(errorcmd)
    choicecmdset.add(defaultcmd)
    choicecmdset.add(CmdMenuLook())
    choicecmdset.add(CmdMenuHelp())

    # assinging menu data flags to caller.
    caller.db._menu_data = {"help": "Please select.",
                            "look": prompt}

    # assign cmdset and ask question
    caller.cmdset.add(choicecmdset)
    caller.msg(prompt)