def menunode_examine_and_buy(caller, raw_string): """Examine and buy an item.""" char = caller.new_char prototypes = spawn(return_parents=True) items, item = _EQUIPMENT_CATEGORIES[caller.ndb._menutree.item_category][1:], None raw_string = raw_string.strip() if raw_string.isdigit() and int(raw_string) <= len(items): item = prototypes[items[int(raw_string) - 1].lower()] if item: text = _format_item_details(item) text += "You currently have {}. Purchase |w{}|n?".format( as_price(char.db.wallet), item['key'] ) help = "Choose carefully. Purchases are final." def purchase_item(session): """Process item purchase.""" try: # this will raise exception if caller doesn't # have enough funds in their `db.wallet` print(item) transfer_funds(char, None, item['value']) ware = spawn(item).pop() ware.move_to(char, quiet=True) ware.at_get(char) rtext = "You pay {} and purchase {}".format( as_price(ware.db.value), ware.key ) except InsufficientFunds: rtext = "You do not have enough money to buy {}.".format( item['key']) session.msg(rtext) options = ({"key": ("Yes", "ye", "y"), "desc": "Purchase {} for {}".format( item['key'], as_price(item['value']) ), "exec": purchase_item, "goto": "menunode_equipment_cats"}, {"key": ("No", "n", "_default"), "desc": "Go back to category list", "goto": "menunode_equipment_list"} ) return (text, help), options else: assert False
def menunode_equipment_list(caller, raw_string): """Initial equipment "shopping" - list items in a category""" text = "You currently have {}.\n".format( as_price(caller.new_char.db.wallet)) text += "Select an item to view details and buy:" raw_string = raw_string.strip() if raw_string.isdigit() and int(raw_string) <= len(_CATEGORY_LIST): caller.ndb._menutree.item_category = _CATEGORY_LIST[int(raw_string) - 1] category = (caller.ndb._menutree.item_category if hasattr( caller.ndb._menutree, 'item_category') else '') help = _CATEGORY_HELP[category] prototypes = spawn(return_prototypes=True) options = [] for proto in _EQUIPMENT_CATEGORIES[category][1:]: options.append({ "desc": _format_menuitem_desc(prototypes[proto]), "goto": "menunode_examine_and_buy" }) options.append({ "key": ("Back", "_default"), "desc": "to category list", "goto": "menunode_equipment_cats", }) return (text, help), options
def menunode_equipment_list(caller, raw_string): """Initial equipment "shopping" - list items in a category""" text = "You currently have {}.\n".format(as_price(caller.new_char.db.wallet)) text += "Select an item to view details and buy:" raw_string = raw_string.strip() if raw_string.isdigit() and int(raw_string) <= len(_CATEGORY_LIST): caller.ndb._menutree.item_category = _CATEGORY_LIST[int(raw_string) - 1] category = (caller.ndb._menutree.item_category if hasattr(caller.ndb._menutree, 'item_category') else '') help = _CATEGORY_HELP[category] prototypes = spawn(return_parents=True) options = [] for proto in _EQUIPMENT_CATEGORIES[category][1:]: options.append({ "desc": _format_menuitem_desc(prototypes[proto.lower()]), "goto": "menunode_examine_and_buy" }) options.append({ "key": ("Back", "_default"), "desc": "to category list", "goto": "menunode_equipment_cats", }) return (text, help), options
def func(self): try: item_name, value = self.args.split() except: self.caller.msg("Syntax: price <item> <value>") return sellable_items = [ware for ware in self.caller.location.contents if inherits_from(ware, Item)] item = self.caller.search(item_name, location=self.caller.location, candidates=sellable_items, quiet=True) if len(item) > 1: self.caller.msg("Which '{}' did you mean?".format(item_name)) return elif len(item): item = item[0] try: value = int(value) except: self.caller.msg("Your price can only be an integer.") return item.db.value = value self.caller.msg("You set the price of {} to {}.".format( item, as_price(value) )) else: self.caller.msg("There is no '{}' available here to sell.".format(item_name))
def func(self): try: item_name, value = self.args.split() except: self.caller.msg("Syntax: price <item> <value>") return sellable_items = [ ware for ware in self.caller.location.contents if inherits_from(ware, Item) ] item = self.caller.search(item_name, location=self.caller.location, candidates=sellable_items, quiet=True) if len(item) > 1: self.caller.msg("Which '{}' did you mean?".format(item_name)) return elif len(item): item = item[0] try: value = int(value) except: self.caller.msg("Your price can only be an integer.") return item.db.value = value self.caller.msg("You set the price of {} to {}.".format( item, as_price(value))) else: self.caller.msg( "There is no '{}' available here to sell.".format(item_name))
def _format_item_details(item): print(item) # The hackiest solution in the world # Todo: Evaluate replacing this method value = [i for i in item['attrs'] if i[0] == 'value'][0][1] weight = [i for i in item['attrs'] if i[0] == 'weight'][0][1] """Returns a piece of equipment's details and description.""" stats = [[" |CPrice|n: {}".format(as_price(value)), " |CWeight|n: |w{}|n".format(weight)], []] col1, col2 = stats # this is somewhat awkward because we're using prototype dicts instead # of instances of these classes. if item['typeclass'] in ("typeclasses.weapons.Weapon", "typeclasses.weapons.TwoHandedWeapon", "typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged", "typeclasses.armors.Shield"): col2.append(" |CHandedness|n: |c{}H|n".format( 2 if 'TwoHanded' in item['typeclass'] else 1 )) if item['typeclass'] in ("typeclasses.weapons.Weapon", "typeclasses.weapons.TwoHandedWeapon", "typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged"): col2.append(" |CDamage|n: |r{}|n".format(item['damage'])) if item['typeclass'] in ("typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged"): col2.append(" |CRange|n: |G{}|n".format( ", ".join([r.capitalize() for r in item['range']]))) if item['typeclass'] in ("typeclasses.armors.Armor", "typeclasses.armors.Shield"): col2.append(" |CToughness|n: |y{}|n".format(item['toughness'])) if 'quantity' in item: col2.append(" |CQuantity|n: |w{}|n".format(item['quantity'])) table = EvTable(header=False, table=stats, border=None) text = "|Y{name}|n\n" text += "{desc}\n" text += "{stats}\n" if 'ammunition' in item: text += " This weapon requires ammunition: |w{ammo}|n\n" return text.format(name=item['key'].title(), desc=fill(item['desc']), stats=table, ammo=item.get('ammunition', ''))
def _format_item_details(item): print(item) # The hackiest solution in the world # Todo: Evaluate replacing this method value = [i for i in item['attrs'] if i[0] == 'value'][0][1] weight = [i for i in item['attrs'] if i[0] == 'weight'][0][1] """Returns a piece of equipment's details and description.""" stats = [[ " |CPrice|n: {}".format(as_price(value)), " |CWeight|n: |w{}|n".format(weight) ], []] col1, col2 = stats # this is somewhat awkward because we're using prototype dicts instead # of instances of these classes. if item['typeclass'] in ("typeclasses.weapons.Weapon", "typeclasses.weapons.TwoHandedWeapon", "typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged", "typeclasses.armors.Shield"): col2.append(" |CHandedness|n: |c{}H|n".format( 2 if 'TwoHanded' in item['typeclass'] else 1)) if item['typeclass'] in ("typeclasses.weapons.Weapon", "typeclasses.weapons.TwoHandedWeapon", "typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged"): col2.append(" |CDamage|n: |r{}|n".format(item['damage'])) if item['typeclass'] in ("typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged"): col2.append(" |CRange|n: |G{}|n".format(", ".join( [r.capitalize() for r in item['range']]))) if item['typeclass'] in ("typeclasses.armors.Armor", "typeclasses.armors.Shield"): col2.append(" |CToughness|n: |y{}|n".format(item['toughness'])) if 'quantity' in item: col2.append(" |CQuantity|n: |w{}|n".format(item['quantity'])) table = EvTable(header=False, table=stats, border=None) text = "|Y{name}|n\n" text += "{desc}\n" text += "{stats}\n" if 'ammunition' in item: text += " This weapon requires ammunition: |w{ammo}|n\n" return text.format(name=item['key'].title(), desc=fill(item['desc']), stats=table, ammo=item.get('ammunition', ''))
def _format_item_details(item, item_attributes): """Returns a piece of equipment's details and description.""" item_typeclass = item['typeclass'] price_value = as_price(item_attributes.get('value', 0)) weight = item_attributes.get('weight', 0) stats = [[ f" |CPrice|n: {price_value}", f" |CWeight|n: |w{weight}|n"], []] col1, col2 = stats # this is somewhat awkward because we're using prototype dicts instead # of instances of these classes. if item_typeclass in _HANDED_TYPECLASSES: handed_str = 2 if 'TwoHanded' in item_typeclass else 1 col2.append(f" |CHandedness|n: |c{handed_str}H|n") if item_typeclass in _WEAPON_TYPECLASSES: damage = item_attributes.get('damage', 0) col2.append(f" |CDamage|n: |r{damage}|n") if item_typeclass in _RANGED_TYPECLASSES: item_range = item_attributes.get('range', []) item_range_str = ", ".join([r.capitalize() for r in item_range]) col2.append(f" |CRange|n: |G{item_range_str}|n") if item_typeclass in _ARMOR_TYPECLASSES | _SHIELD_TYPECLASSES: toughness = item_attributes.get('toughness', 0) col2.append(f" |CToughness|n: |y{toughness}|n") quantity = item_attributes.get('quantity') if quantity: col2.append(f" |CQuantity|n: |w{quantity}|n") stats_table = EvTable(header=False, table=stats, border=None) name_title = item['key'].title() desc = item_attributes.get('desc', '') text = f"|Y{name_title}|n\n" \ f"{fill(desc)}\n" \ f"{stats_table}\n" ammunition = item_attributes.get('ammunition') if ammunition: text += f" This weapon requires ammunition: |w{ammunition}|n\n" return text
def purchase_item(session): """Process item purchase.""" try: # this will raise exception if caller doesn't # have enough funds in their `db.wallet` transfer_funds(char, None, item['value']) ware = spawn(item).pop() ware.move_to(char, quiet=True) ware.at_get(char) rtext = "You pay {} and purchase {}".format( as_price(ware.db.value), ware.key) except InsufficientFunds: rtext = "You do not have enough money to buy {}.".format( item['key']) session.msg(rtext)
def purchase_item(caller): """Process item purchase.""" try: # this will raise exception if # caller doesn't have enough funds if ware.location == caller.location.db.storeroom and ware.db.value: transfer_funds(caller, None, ware.db.value) ware.move_to(caller, quiet=True) ware.at_get(caller) rtext = "You pay {} and purchase {}".format( as_price(ware.db.value), ware.key) else: rtext = "{} is no longer available.".format( ware.key.capitalize()) except InsufficientFunds: rtext = "You do not have enough money to buy {}.".format(ware.key) caller.msg(rtext)
def menunode_equipment_cats(caller, raw_string): """Initial equipment "shopping" - choose a category""" text = raw_string if raw_string and raw_string[0] == 'F' else "" text += "\n\nNext, purchase your starting equipment.\n" text += "You have |w{coins}|n.\n" text += "Select a category of equipment to view:" text = text.format(coins=as_price(caller.new_char.db.wallet)) help = fill("Equipment is grouped into categories. Select one to view" "the items in that category.") help += "\n\n" help += fill("Money in Ainneve is represented as Copper Coins (CC)," "Silver Coins (SC), and Gold Coins (GC), with a conversion" "rate of 100 CC = 1 SC and 100 SC = 1 GC") def show_inventory(session): """display the character's inventory We achieve this by "monkey patching" the session's `msg` method onto the new char to catch the output of the 'inventory' command. """ session.msg('\n') old_msg = session.new_char.msg session.new_char.msg = session.msg session.new_char.execute_cmd('inventory') session.new_char.msg = old_msg options = [{ "desc": cat, "goto": "menunode_equipment_list" } for cat in _CATEGORY_LIST] options.append({ "key": ("Inventory", "inv", "i"), "desc": "Show your current inventory", "exec": show_inventory, "goto": "menunode_equipment_cats" }) options.append({ "key": "Done", "desc": "Continue to character description", "goto": "menunode_character_sdesc" }) return (text, help), options
def purchase_item(s): """Process item purchase.""" try: # this will raise exception if caller doesn't # have enough funds in their `db.wallet` transfer_funds(char, None, item['value']) ware = spawn(item).pop() ware.move_to(char, quiet=True) ware.at_get(char) rtext = "You pay {} and purchase {}".format( as_price(ware.db.value), ware.key ) except InsufficientFunds: rtext = "You do not have enough money to buy {}.".format( item['key']) s.msg(rtext)
def menunode_inspect_and_buy(caller, raw_input): """ Sets up the buy menu screen, or informs the player that the ware is no longer available """ iware = int(raw_input) - 1 ware = caller.ndb._menutree.npc_shop_wares[iware] def purchase_item(caller): """Process item purchase.""" try: # this will raise exception if # caller doesn't have enough funds if ware.location == caller.location.db.storeroom and ware.db.value: transfer_funds(caller, None, ware.db.value) ware.move_to(caller, quiet=True) ware.at_get(caller) rtext = "You pay {} and purchase {}".format( as_price(ware.db.value), ware.key ) else: rtext = "{} is no longer available.".format( ware.key.capitalize()) except InsufficientFunds: rtext = "You do not have enough money to buy {}.".format( ware.key) caller.msg(rtext) if ware.location == caller.location.db.storeroom and ware.db.value: # If the item's still in stock and has not been removed from sale text = "You inspect %s:\n\n%s" % (ware.key, ware.db.desc) options = ({"desc": "Buy %s for %s" % \ (ware.key, as_price(ware.db.value) or 1), "goto": "menunode_shopfront", "exec": purchase_item}, {"desc": "Look for something else", "goto": "menunode_shopfront"}) else: text = "{} is no longer available".format(ware.key.capitalize()) options = ({"desc": "Look for something else", "goto": "menunode_shopfront"}) return text, options
def menunode_inspect_and_buy(caller, raw_input): """ Sets up the buy menu screen, or informs the player that the ware is no longer available """ iware = int(raw_input) - 1 ware = caller.ndb._menutree.npc_shop_wares[iware] def purchase_item(caller): """Process item purchase.""" try: # this will raise exception if # caller doesn't have enough funds if ware.location == caller.location.db.storeroom and ware.db.value: transfer_funds(caller, None, ware.db.value) ware.move_to(caller, quiet=True) ware.at_get(caller) rtext = "You pay {} and purchase {}".format( as_price(ware.db.value), ware.key) else: rtext = "{} is no longer available.".format( ware.key.capitalize()) except InsufficientFunds: rtext = "You do not have enough money to buy {}.".format(ware.key) caller.msg(rtext) if ware.location == caller.location.db.storeroom and ware.db.value: # If the item's still in stock and has not been removed from sale text = "You inspect %s:\n\n%s" % (ware.key, ware.db.desc) options = ({"desc": "Buy %s for %s" % \ (ware.key, as_price(ware.db.value) or 1), "goto": "menunode_shopfront", "exec": purchase_item}, {"desc": "Look for something else", "goto": "menunode_shopfront"}) else: text = "{} is no longer available".format(ware.key.capitalize()) options = ({ "desc": "Look for something else", "goto": "menunode_shopfront" }) return text, options
def purchase_item(caller): """Process item purchase.""" try: # this will raise exception if # caller doesn't have enough funds if ware.location == caller.location.db.storeroom and ware.db.value: transfer_funds(caller, None, ware.db.value) ware.move_to(caller, quiet=True) ware.at_get(caller) rtext = "You pay {} and purchase {}".format( as_price(ware.db.value), ware.key ) else: rtext = "{} is no longer available.".format( ware.key.capitalize()) except InsufficientFunds: rtext = "You do not have enough money to buy {}.".format( ware.key) caller.msg(rtext)
def _format_menuitem_desc(item): """Returns a piece of equipment formatted as a one-line menu item.""" template = "|w{name}|n Cost: ({price}) " if item['typeclass'] in ("typeclasses.weapons.Weapon", "typeclasses.weapons.TwoHandedWeapon", "typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged"): template += "|c{handed}H|n [|rDmg: {damage}|n]" elif item['typeclass'] in ("typeclasses.armors.Shield", ): template += "|c{handed}H|n [|yDef: {toughness}|n]" elif item['typeclass'] in ("typeclasses.armors.Armor", ): template += "[|yDef: {toughness}|n]" return template.format(name=article_re.sub('', item['key']).title(), price=as_price(item.get('value', {})), handed=2 if 'TwoHanded' in item['typeclass'] else 1, damage=item.get('damage', ''), toughness=item.get('toughness', ''))
def menunode_shopfront(caller): "This is the top-menu screen." shopname = caller.location.key caller.ndb._menutree.npc_shop_wares = get_wares(caller) text = "*** Welcome to %s! ***\n" % shopname if caller.ndb._menutree.npc_shop_wares: text += " Things for sale (choose 1-%i to inspect);" \ " quit to exit:" % len(caller.ndb._menutree.npc_shop_wares) else: text += " There is nothing for sale; quit to exit." options = [] for ware in caller.ndb._menutree.npc_shop_wares: # add an option for every ware in store options.append({"desc": "%s (%s)" % (ware.key, as_price(ware.db.value)), "goto": "menunode_inspect_and_buy"}) return text, options
def menunode_shopfront(caller): "This is the top-menu screen." shopname = caller.location.key caller.ndb._menutree.npc_shop_wares = get_wares(caller) text = "*** Welcome to %s! ***\n" % shopname if caller.ndb._menutree.npc_shop_wares: text += " Things for sale (choose 1-%i to inspect);" \ " quit to exit:" % len(caller.ndb._menutree.npc_shop_wares) else: text += " There is nothing for sale; quit to exit." options = [] for ware in caller.ndb._menutree.npc_shop_wares: # add an option for every ware in store options.append({ "desc": "%s (%s)" % (ware.key, as_price(ware.db.value)), "goto": "menunode_inspect_and_buy" }) return text, options
def menunode_equipment_cats(caller, raw_string): """Initial equipment "shopping" - choose a category""" text = raw_string if raw_string and raw_string[0] == 'F' else "" text += "\n\nNext, purchase your starting equipment.\n" text += "You have |w{coins}|n.\n" text += "Select a category of equipment to view:" text = text.format(coins=as_price(caller.new_char.db.wallet)) help = fill("Equipment is grouped into categories. Select one to view" "the items in that category.") help += "\n\n" help += fill("Money in Ainneve is represented as Copper Coins (CC)," "Silver Coins (SC), and Gold Coins (GC), with a conversion" "rate of 100 CC = 1 SC and 100 SC = 1 GC") def show_inventory(session): """display the character's inventory We achieve this by "monkey patching" the session's `msg` method onto the new char to catch the output of the 'inventory' command. """ session.msg('\n') old_msg = session.new_char.msg session.new_char.msg = session.msg session.new_char.execute_cmd('inventory') session.new_char.msg = old_msg options = [{"desc": cat, "goto": "menunode_equipment_list"} for cat in _CATEGORY_LIST] options.append({"key": ("Inventory", "inv", "i"), "desc": "Show your current inventory", "exec": show_inventory, "goto": "menunode_equipment_cats"}) options.append({"key": "Done", "desc": "Continue to character description", "goto": "menunode_character_sdesc"}) return (text, help), options
def _format_menuitem_desc(item): """Returns a piece of equipment formatted as a one-line menu item.""" template = "|w{name}|n Cost: ({price}) " if item['typeclass'] in ("typeclasses.weapons.Weapon", "typeclasses.weapons.TwoHandedWeapon", "typeclasses.weapons.RangedWeapon", "typeclasses.weapons.TwoHandedRanged"): template += "|c{handed}H|n [|rDmg: {damage}|n]" elif item['typeclass'] in ("typeclasses.armors.Shield",): template += "|c{handed}H|n [|yDef: {toughness}|n]" elif item['typeclass'] in ("typeclasses.armors.Armor",): template += "[|yDef: {toughness}|n]" return template.format( name=article_re.sub('', item['key']).title(), price=as_price(item.get('value', {})), handed=2 if 'TwoHanded' in item['typeclass'] else 1, damage=item.get('damage', ''), toughness=item.get('toughness', '') )
def _format_menuitem_desc(item): """Returns a piece of equipment formatted as a one-line menu item.""" item_attributes = prototypeutils.create_attribute_dict(item) item_typeclass = item['typeclass'] type_class_info = '' if item_typeclass in _WEAPON_TYPECLASSES: damage = item_attributes.get('damage', 0) handed_str = 2 if 'TwoHanded' in item_typeclass else 1 type_class_info = f"|c{handed_str}H|n [|rDmg: {damage}|n]" elif item_typeclass in _SHIELD_TYPECLASSES: handed_str = 2 if 'TwoHanded' in item_typeclass else 1 toughness = item_attributes.get('toughness', 0) type_class_info = f"|c{handed_str}H|n [|yDef: {toughness}]" elif item_typeclass in _ARMOR_TYPECLASSES: toughness = item_attributes.get('toughness', 0) type_class_info = f"[|yDef: {toughness}|n]" name_title = article_re.sub('', item['key']).title() cost = as_price(item_attributes.get('value', 0)) return f"|w{name_title}|n Cost: ({cost}) {type_class_info}"