Advertisement
Guest User

npcshop

a guest
Apr 1st, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.34 KB | None | 0 0
  1. from evennia.utils import evmenu
  2.  
  3. def menunode_shopfront(caller):
  4.     "This is the top-menu screen."
  5.  
  6.     shopname = caller.location.key
  7.     wares = caller.location.db.storeroom.contents
  8.  
  9.     # Wares includes all items inside the storeroom, including the
  10.     # door! Let's remove that from our for sale list.
  11.     wares = [ware for ware in wares if ware.key.lower() != "door"]
  12.  
  13.     text = "*** Welcome to %s! ***\n" % shopname
  14.     if wares:
  15.         text += "   Things for sale (choose 1-%i to inspect);" \
  16.                 " quit to exit:" % len(wares)
  17.     else:
  18.         text += "   There is nothing for sale; quit to exit."
  19.  
  20.     options = []
  21.     for ware in wares:
  22.         # add an option for every ware in store
  23.         options.append({"desc": "%s (%s money)" %
  24.                              (ware.key, ware.db.money_value or 1),
  25.                         "goto": "menunode_inspect_and_buy"})
  26.     return text, options
  27.  
  28. def menunode_inspect_and_buy(caller, raw_string):
  29.     "Sets up the buy menu screen."
  30.  
  31.     wares = caller.location.db.storeroom.contents
  32.     # Don't forget, we will need to remove that pesky door again!
  33.     wares = [ware for ware in wares if ware.key.lower() != "door"]
  34.     iware = int(raw_string) - 1
  35.     ware = wares[iware]
  36.     value = ware.db.money_value or 1
  37.     wealth = caller.db.money or 0
  38.     text = "You inspect %s:\n\n%s" % (ware.key, ware.db.desc)
  39.  
  40.     def buy_ware_result(caller):
  41.         "This will be executed first when choosing to buy."
  42.         if wealth >= value:
  43.             rtext = "You pay %i money and purchase %s!" % \
  44.                          (value, ware.key)
  45.             caller.db.money -= value
  46.             ware.move_to(caller, quiet=True)
  47.         else:
  48.             rtext = "You cannot afford %i money for %s!" % \
  49.                           (value, ware.key)
  50.         caller.msg(rtext)
  51.  
  52.     options = ({"desc": "Buy %s for %s money" % \
  53.                         (ware.key, ware.db.money_value or 1),
  54.                 "goto": "menunode_shopfront",
  55.                 "exec": buy_ware_result},
  56.                {"desc": "Look for something else",
  57.                 "goto": "menunode_shopfront"})
  58.  
  59.     return text, options
  60.  
  61. from evennia import Command
  62.  
  63. class CmdBuy(Command):
  64.     """
  65.    Start to do some shopping
  66.  
  67.    Usage:
  68.      buy
  69.      shop
  70.      browse
  71.  
  72.    This will allow you to browse the wares of the
  73.    current shop and buy items you want.
  74.    """
  75.     key = "buy"
  76.     aliases = ("shop", "browse")
  77.  
  78.     def func(self):
  79.         "Starts the shop EvMenu instance"
  80.         evmenu.EvMenu(self.caller,
  81.                       "typeclasses.npcshop",
  82.                       startnode="menunode_shopfront")
  83. #----------------------------------------------------------------------
  84. #
  85. #                    Builder code for ease of use
  86. #
  87. #
  88. #----------------------------------------------------------------------
  89.  
  90. from evennia import DefaultRoom, DefaultExit, DefaultObject
  91. from evennia.utils.create import create_object
  92. from typeclasses.rooms import Room
  93.  
  94. # class for our front shop room
  95. class NPCShop(DefaultRoom):
  96.     def at_object_creation(self):
  97.         # we could also use add(ShopCmdSet, permanent=True)
  98.         self.cmdset.add_default(ShopCmdSet)
  99.         self.db.storeroom = None
  100.  
  101. # command to build a complete shop (the Command base class
  102. # should already have been imported earlier in this file)
  103. class CmdBuildShop(Command):
  104.     """
  105.    Build a new shop
  106.  
  107.    Usage:
  108.        @buildshop shopname
  109.  
  110.    This will create a new NPCshop room
  111.    as well as a linked store room (named
  112.    simply <storename>-storage) for the
  113.    wares on sale. The store room will be
  114.    accessed through a locked door in
  115.    the shop.
  116.    """
  117.     key = "@buildshop"
  118.     locks = "cmd:perm(Builders)"
  119.     help_category = "Builders"
  120.  
  121.     def func(self):
  122.         "Create the shop rooms"
  123.         if not self.args:
  124.             self.msg("Usage: @buildshop <storename>")
  125.             return
  126.         # create the shop and storeroom
  127.         shopname = self.args.strip()
  128.         shop = create_object(NPCShop,
  129.                              key=shopname,
  130.                              location=None)
  131.         storeroom = create_object(DefaultRoom,
  132.                              key="%s-storage" % shopname,
  133.                              location=None)
  134.         shop.db.storeroom = storeroom
  135.         # create a door between the two
  136.         shop_exit = create_object(DefaultExit,
  137.                                   key="back door",
  138.                                   aliases=["storage", "store room"],
  139.                                   location=shop,
  140.                                   destination=storeroom)
  141.         storeroom_exit = create_object(DefaultExit,
  142.                                   key="door",
  143.                                   location=storeroom,
  144.                                   destination=shop)
  145.         # make a key for accessing the store room
  146.         storeroom_key_name = "%s-storekey" % shopname
  147.         storeroom_key = create_object(DefaultObject,
  148.                                        key=storeroom_key_name,
  149.                                        location=shop)
  150.         # only allow chars with this key to enter the store room
  151.         shop_exit.locks.add("traverse:holds(%s)" % storeroom_key_name)
  152.  
  153.         # inform the builder about progress
  154.         self.caller.msg("The shop %s was created!" % shop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement