def activate_object(self, object_id, character): # Instantiate the scripted object and call its activate thing. retval = [] for o in ScriptedObject.objects(id=object_id): # TODO: Break this block into a method. for script in o.scripts: scriptclass = script.split('.')[-1] module = __import__(script, globals(), locals(), [scriptclass], -1) # For each entry in the script's dir() for key in dir(module): C = getattr(module, key) try: if not issubclass(C, Script): # If it isn't a subclass of Script, skip it. continue except TypeError: # If C isn't a class at all, skip it. continue # Finally activate the script. script_val = C(o).activate(character) if script_val: # Only return it if the script actually returns something. # So clients should only react to activating scripts that # return something meaningful. retval.append(script_val) return retval
def load_scripts(self): '''(Re)Load scripts for objects in this zone.''' self.scripts = {} # Query DB for a list of all objects' script names, # ordered according to proximity to players logger.info(ScriptedObject.objects) for o in ScriptedObject.objects(scripts__exists=True): logger.info("Scripted Object: {0}".format(o.name)) # Store list of script names in self # For each script name in the list: for script in o.scripts: logger.info("Importing %s" % script) if script not in self.scripts: self.scripts[script] = [] # Import those by name via __import__ scriptclass = script.split('.')[-1] module = __import__(script, globals(), locals(), [scriptclass], -1) # For each entry in the script's dir() for key in dir(module): C = getattr(module, key) # No sense in instantiating the default Script instance. if C == Script: continue try: # Does this object have the attributes that scripts need? if not all((C.tick, C.activate, C.create)): continue except AttributeError: continue # Store object instance in a list. self.scripts[script].append(C(mongo_engine_object=o)) return self.scripts