def command_help(request, cmd_key): from commands.default_cmdsets import AccountCmdSet, CharacterCmdSet from commands.cmdsets.situational import SituationalCmdSet user = request.user cmd_key = cmd_key.lower() matches = [ ob for ob in AccountCmdSet() if ob.key.lower() == cmd_key and ob.access(user, "cmd") ] matches += [ ob for ob in CharacterCmdSet() if ob.key.lower() == cmd_key and ob.access(user, "cmd") ] matches += [ ob for ob in SituationalCmdSet() if ob.key.lower() == cmd_key and ob.access(user, "cmd") ] return render( request, "help_topics/command_help.html", {"matches": matches, "page_title": cmd_key}, )
def list_commands(request): from commands.default_cmdsets import AccountCmdSet, CharacterCmdSet from commands.cmdsets.situational import SituationalCmdSet user = request.user def sort_name(cmd): cmdname = cmd.key.lower() cmdname = cmdname.lstrip("+").lstrip("@") return cmdname def check_cmd_access(cmdset): cmd_list = [] for cmd in cmdset: try: if cmd.access(user, 'cmd'): cmd_list.append(cmd) except (AttributeError, ValueError, TypeError): continue return sorted(cmd_list, key=sort_name) player_cmds = check_cmd_access(AccountCmdSet()) char_cmds = check_cmd_access(CharacterCmdSet()) situational_cmds = check_cmd_access(SituationalCmdSet()) return render( request, 'help_topics/list_commands.html', { 'player_cmds': player_cmds, 'character_cmds': char_cmds, 'situational_cmds': situational_cmds, 'page_title': 'commands' })
def list_commands(request): from commands.default_cmdsets import AccountCmdSet, CharacterCmdSet from commands.cmdsets.situational import SituationalCmdSet user = request.user def sort_name(cmd): cmdname = cmd.key.lower() cmdname = cmdname.lstrip("+").lstrip("@") return cmdname player_cmds = sorted( [ob for ob in AccountCmdSet() if ob.access(user, 'cmd')], key=sort_name) char_cmds = sorted( [ob for ob in CharacterCmdSet() if ob.access(user, 'cmd')], key=sort_name) situational_cmds = sorted( [ob for ob in SituationalCmdSet() if ob.access(user, 'cmd')], key=sort_name) return render( request, 'help_topics/list_commands.html', { 'player_cmds': player_cmds, 'character_cmds': char_cmds, 'situational_cmds': situational_cmds, 'page_title': 'commands' })
def func(self): """ Run the dynamic help entry creator. """ query, cmdset = self.args, self.cmdset caller = self.caller unavailable = False suggestion_cutoff = 0.6 suggestion_maxnum = 5 if not query: query = "all" # removing doublets in cmdset, caused by cmdhandler # having to allow doublet commands to manage exits etc. cmdset.make_unique(caller) # retrieve all available commands and database topics all_cmds = [ cmd for cmd in cmdset if cmd.auto_help and cmd.access(caller) ] player = caller if not hasattr(caller, 'character'): player = caller.player if not player.character: try: char_cmds = [ cmd for cmd in player.char_ob.cmdset.all()[0] if cmd.auto_help and cmd.access(caller) ] all_cmds += char_cmds except Exception: pass all_topics = [ topic for topic in HelpEntry.objects.all() if topic.access(caller, 'view', default=True) ] all_categories = list( set([cmd.help_category.lower() for cmd in all_cmds] + [topic.help_category.lower() for topic in all_topics])) if query in ("list", "all"): # we want to list all available help entries, grouped by category hdict_cmd = defaultdict(list) hdict_topic = defaultdict(list) # create the dictionaries {category:[topic, topic ...]} required by format_help_list [hdict_cmd[cmd.help_category].append(cmd.key) for cmd in all_cmds] [ hdict_topic[topic.help_category].append(topic.key) for topic in all_topics ] # report back self.msg(format_help_list(hdict_cmd, hdict_topic, brief=True)) return # Try to access a particular command # build vocabulary of suggestions and rate them by string similarity. vocabulary = [cmd.key for cmd in all_cmds if cmd ] + [topic.key for topic in all_topics] + all_categories [vocabulary.extend(cmd.aliases) for cmd in all_cmds] suggestions = [ sugg for sugg in string_suggestions(query, set(vocabulary), cutoff=suggestion_cutoff, maxnum=suggestion_maxnum) if sugg != query ] if not suggestions: suggestions = [ sugg for sugg in vocabulary if sugg != query and sugg.startswith(query) ] found_match = False # try an exact command auto-help match match = [cmd for cmd in all_cmds if cmd == query] # Account for prefixes if not match: if (query[0] == '+'): match = [ cmd for cmd in all_cmds if ((cmd == query[1:]) or (cmd == "@%s" % query[1:])) ] elif (query[0] == '@'): match = [ cmd for cmd in all_cmds if ((cmd == query[1:]) or (cmd == "+%s" % query[1:])) ] else: match = [ cmd for cmd in all_cmds if ((cmd == "@%s" % query) or (cmd == "+%s" % query)) ] if not match: match = [cmd for cmd in SituationalCmdSet() if cmd == query] unavailable = True if len(match) == 1: if not [ob for ob in self.cmdset if ob == match[0]]: unavailable = True doc_text = match[0].get_help(caller, cmdset) try: tags = match[0].help_entry_tags except AttributeError: tags = None self.msg( format_help_entry(match[0].key, doc_text, aliases=match[0].aliases, suggested=suggestions, unavailable=unavailable, related_tags=tags)) found_match = True # try an exact database help entry match match = list(HelpEntry.objects.find_topicmatch(query, exact=True)) match = [ topic for topic in match if topic.access(caller, 'view', default=True) ] if len(match) == 1: self.msg( format_help_entry(match[0].key, match[0].entrytext, suggested=suggestions)) found_match = True # try to see if a category name was entered if query in all_categories: # fixed bug - wouldn't match if category name was capitalized self.msg( format_help_list( { query: [ cmd.key for cmd in all_cmds if cmd.help_category.lower() == query ] }, { query: [ topic.key for topic in all_topics if topic.help_category.lower() == query ] })) return if found_match: return # no exact matches found. Just give suggestions. self.msg( format_help_entry("", "No help entry found for '%s'" % query, None, suggested=suggestions))