def test_dedent(self): #print "Did TestDedent run?" # Empty string, return empty string self.assertEqual("", utils.dedent("")) # No leading whitespace self.assertEqual("TestDedent", utils.dedent("TestDedent")) # Leading whitespace, single line self.assertEqual("TestDedent", utils.dedent(" TestDedent")) # Leading whitespace, multi line input_string = " hello\n world" expected_string = "hello\nworld" self.assertEqual(expected_string, utils.dedent(input_string))
def create_merchant_menutree(self, caller): if len(self.contents) < 1: self.tell_character(caller, "I do not have anything for sale currently.") return nodes = [] items_for_sale = self.contents character_attributes = caller.db.attributes table = PrettyTable(["Item", "Cost"]) for item in items_for_sale: table.add_row([item.name, item.value]) items_string = table.get_string() welcome_text = """ Hello there %s! Browse my goods: %s """ % (caller.name, items_string) welcome_text = utils.dedent(welcome_text) root_node = MenuNode("START", links=[i.name for i in items_for_sale], linktexts=["Buy %s" % i.name for i in items_for_sale], text=welcome_text) for item in items_for_sale: confirm_buy_node = MenuNode("buy-%s" % item.name, links=["END"], linktexts=['Exit Merchant Menu'], code="self.caller.buy_from_merchant(item='%s', merchant='%s')" % (item.name, self.name) ) item_node = MenuNode("%s" % item.name, links=["buy-%s" % item.name, "START", "END"], linktexts=["Buy %s" % item.name, "Back to Merchant Inventory", "Exit Merchant Menu"], text="Do you want to buy the %s?" % item.name) nodes.append(confirm_buy_node) nodes.append(item_node) nodes.append(root_node) menu = MenuTree(caller=caller, nodes=nodes) menu.start()
def create_trainer_menutree(self, caller): nodes = [] have_skills = 0 skills_and_costs = {} character_skills = [ i.name for i in caller.db.skills ] skills_i_can_train = [] for skill in self.db.skills_trained: if skill in character_skills: skills_i_can_train.append('%s' % skill) for skill in caller.db.skills: skills_and_costs['%s' % skill.name] = skill.db.cost_to_level skills_string = ''.join(["{b%s{n Cost: {y%s{n gp\n" % (k.title(),v) for k, v in skills_and_costs.items()]) skills_string = utils.dedent(skills_string) welcome_text = """ Welcome {g%s{n! I can train you in the following skills: %s """ % (caller.name, skills_string) welcome_text = utils.dedent(welcome_text) node0 = MenuNode("START", links=[k for k in skills_and_costs.keys()], linktexts=["Train %s" % k for k in skills_and_costs.keys()], text=welcome_text) #do some logic to find out which skills the character has and add some nodes for them. for skill in self.db.skills_trained: if skill in character_skills: have_skills = 1 node = MenuNode("%s" % skill, links=['END'], linktexts=['Exit Training Menu'], code="self.caller.level_skill('%s')" % skill ) #caller.msg("node.code looks like: %s" % node.code) nodes.append(node) #if we don't have skills then gracefully return. if have_skills != 1: self.tell_character(caller, "You have not trained any skills yet." % self.name) return nodes.append(node0) menu = MenuTree(caller=caller, nodes=nodes) menu.start()
def format_help_entry(title, help_text, aliases=None, suggested=None): """ This visually formats the help entry. """ string = SEP + "\n" if title: string += "{CHelp topic for {w%s{n" % title if aliases: string += " {C(aliases: {w%s{n{C){n" % (", ".join(aliases)) if help_text: string += "\n%s" % dedent(help_text.rstrip()) if suggested: string += "\n\n{CSuggested:{n " string += "{w%s{n" % fill(", ".join(suggested)) string.strip() string += "\n" + SEP return string