123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- '''
- auxiliary_example.py
- Provides a simple example of how one might install and interact with
- auxiliary data in python. This module installs a new piece of auxiliary data,
- and sets up two new commands that allow people to interact with that auxiliary
- data.
- '''
- import auxiliary, storage, mud, mudsys
- class ExampleAux:
- '''example auxiliary data class. Stores a list of key : value pairs.'''
- def __init__(self, set = None):
- '''Create a new instance of the auxiliary data. If a storage set is
- supplied, read our values from that'''
-
- self.pairs = { }
-
-
-
-
- if set != None and set.contains("pairs"):
-
-
- for one_pair in set.readList("pairs").sets():
- key = one_pair.readString("key")
- val = one_pair.readString("val")
- self.pairs[key] = val
- def copyTo(self, to):
- '''copy the pairs in this aux data to the another.'''
- to.pairs = self.pairs.copy()
- def copy(self):
- '''create a duplicate of this aux data.'''
- newVal = ExampleAux()
- self.copyTo(newVal)
- return newVal
- def store(self):
- '''returns a storage set representation of the auxiliary data'''
- set = storage.StorageSet()
- list = storage.StorageList()
-
- for key, val in self.pairs.iteritems():
- one_pair = storage.StorageSet()
- one_pair.storeString("key", key)
- one_pair.storeString("val", val)
- list.add(one_pair)
- set.storeList("pairs", list)
- return set
- def cmd_getaux(ch, cmd, arg):
- '''allows people to peek at the value stored in their ExampleAux data'''
- if not arg in ch.aux("example_aux").pairs:
- ch.send("There is no value for '%s'" % arg)
- else:
- ch.send("The val is '%s'" % ch.aux("example_aux").pairs[arg])
- def cmd_setaux(ch, cmd, arg):
- '''allows people to set a value stored in their aux data. If no value is
- specified, instead delete a key.'''
- try:
- key, val = mud.parse_args(ch, True, cmd, arg, "word(key) | string(val)")
- except: return
-
- if val == None:
- if key in ch.aux("example_aux").pairs:
- del ch.aux("example_aux").pairs[key]
- ch.send("Key deleted.")
- else:
- ch.aux("example_aux").pairs[key] = val
- ch.send("Key '%s' set to '%s'." % (key, val))
- auxiliary.install("example_aux", ExampleAux, "character")
- mudsys.add_cmd("getaux", None, cmd_getaux, "admin", False)
- mudsys.add_cmd("setaux", None, cmd_setaux, "admin", False)
|