auxiliary_example.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. '''
  2. auxiliary_example.py
  3. Provides a simple example of how one might install and interact with
  4. auxiliary data in python. This module installs a new piece of auxiliary data,
  5. and sets up two new commands that allow people to interact with that auxiliary
  6. data.
  7. '''
  8. import auxiliary, storage, mud, mudsys
  9. ################################################################################
  10. # auxiliary data implementation
  11. ################################################################################
  12. class ExampleAux:
  13. '''example auxiliary data class. Stores a list of key : value pairs.'''
  14. def __init__(self, set = None):
  15. '''Create a new instance of the auxiliary data. If a storage set is
  16. supplied, read our values from that'''
  17. # make sure our table of pairs are created
  18. self.pairs = { }
  19. # read in our key/value pairs from the list contained in the storage
  20. # set. NOTE: Storage Lists are NOT Python lists. They are
  21. # intermediaries for storage, and should not be kept stored on your
  22. # auxiliary data, only read from or stored to
  23. if set != None and set.contains("pairs"):
  24. # notice the sets() at the end? This will return the list as a
  25. # Python list of storage sets -- the contents of the Storae List
  26. for one_pair in set.readList("pairs").sets():
  27. key = one_pair.readString("key")
  28. val = one_pair.readString("val")
  29. self.pairs[key] = val
  30. def copyTo(self, to):
  31. '''copy the pairs in this aux data to the another.'''
  32. to.pairs = self.pairs.copy()
  33. def copy(self):
  34. '''create a duplicate of this aux data.'''
  35. newVal = ExampleAux()
  36. self.copyTo(newVal)
  37. return newVal
  38. def store(self):
  39. '''returns a storage set representation of the auxiliary data'''
  40. set = storage.StorageSet()
  41. list = storage.StorageList()
  42. # convert our table to a storage list of key:val pairs
  43. for key, val in self.pairs.iteritems():
  44. one_pair = storage.StorageSet()
  45. one_pair.storeString("key", key)
  46. one_pair.storeString("val", val)
  47. list.add(one_pair)
  48. set.storeList("pairs", list)
  49. return set
  50. ################################################################################
  51. # player commands
  52. ################################################################################
  53. def cmd_getaux(ch, cmd, arg):
  54. '''allows people to peek at the value stored in their ExampleAux data'''
  55. if not arg in ch.aux("example_aux").pairs:
  56. ch.send("There is no value for '%s'" % arg)
  57. else:
  58. ch.send("The val is '%s'" % ch.aux("example_aux").pairs[arg])
  59. def cmd_setaux(ch, cmd, arg):
  60. '''allows people to set a value stored in their aux data. If no value is
  61. specified, instead delete a key.'''
  62. try:
  63. key, val = mud.parse_args(ch, True, cmd, arg, "word(key) | string(val)")
  64. except: return
  65. # are we trying to delete a key?
  66. if val == None:
  67. if key in ch.aux("example_aux").pairs:
  68. del ch.aux("example_aux").pairs[key]
  69. ch.send("Key deleted.")
  70. else:
  71. ch.aux("example_aux").pairs[key] = val
  72. ch.send("Key '%s' set to '%s'." % (key, val))
  73. ################################################################################
  74. # initialization
  75. ################################################################################
  76. # install our auxiliary data on characters when this module is loaded.
  77. # auxiliary data can also be installed onto rooms and objects. You can install
  78. # auxiliary data onto more than one type of thing by comma-separating them in
  79. # the third argument of this method.
  80. auxiliary.install("example_aux", ExampleAux, "character")
  81. # add in our two commands
  82. mudsys.add_cmd("getaux", None, cmd_getaux, "admin", False)
  83. mudsys.add_cmd("setaux", None, cmd_setaux, "admin", False)