Set event types in non-persistent data for the event handler

This commit is contained in:
Vincent Le Goff 2017-03-13 11:46:16 -07:00 committed by Griatch
parent 4bdee14adb
commit 88516630db
2 changed files with 31 additions and 18 deletions

View file

@ -10,6 +10,7 @@ from evennia import logger
from evennia import ScriptDB
hooks = []
event_types = []
def get_event_handler():
"""Return the event handler or None."""
@ -41,20 +42,7 @@ def create_event_type(typeclass, event_name, variables, help_text):
"""
typeclass_name = typeclass.__module__ + "." + typeclass.__name__
try:
script = ScriptDB.objects.get(db_key="event_handler")
except ScriptDB.DoesNotExist:
logger.log_err("Can't create event {} in typeclass {}, the " \
"script handler isn't defined".format(name, typeclass_name))
return
# Get the event types for this typeclass
if typeclass_name not in script.db.event_types:
script.db.event_types[typeclass_name] = {}
event_types = script.db.event_types[typeclass_name]
# Add or replace the event
event_types[event_name] = (variables, help_text)
event_types.append((typeclass_name, event_name, variables, help_text))
def del_event_type(typeclass, event_name):
"""
@ -79,7 +67,7 @@ def del_event_type(typeclass, event_name):
return
# Get the event types for this typeclass
event_types = script.db.event_types.get(typeclass_name, {})
event_types = script.ndb.event_types.get(typeclass_name, {})
if event_name in event_types:
del event_types[event_name]
@ -112,3 +100,27 @@ def patch_hooks():
typeclass, method_name, new_hook = hooks[0]
setattr(typeclass, method_name, new_hook)
del hooks[0]
def connect_event_types():
"""
Connect the event types when the script runs.
This method should be called automatically by the event handler
(the script).
"""
try:
script = ScriptDB.objects.get(db_key="event_handler")
except ScriptDB.DoesNotExist:
logger.log_err("Can't connect event types, the event handler " \
"cannot be found.")
return
for typeclass_name, event_name, variables, help_text in event_types:
# Get the event types for this typeclass
if typeclass_name not in script.ndb.event_types:
script.ndb.event_types[typeclass_name] = {}
types = script.ndb.event_types[typeclass_name]
# Add or replace the event
types[event_name] = (variables, help_text)

View file

@ -8,7 +8,7 @@ from Queue import Queue
from evennia import DefaultScript
from evennia import logger
from evennia.contrib.events.exceptions import InterruptEvent
from evennia.contrib.events.extend import patch_hooks
from evennia.contrib.events.extend import connect_event_types, patch_hooks
from evennia.contrib.events import typeclasses
from evennia.utils.utils import all_from_module
@ -22,11 +22,12 @@ class EventHandler(DefaultScript):
self.persistent = True
# Permanent data to be stored
self.db.event_types = {}
self.db.events = {}
def at_start(self):
"""Set up the event system."""
self.ndb.event_types = {}
connect_event_types()
patch_hooks()
def get_events(self, obj):
@ -48,7 +49,7 @@ class EventHandler(DefaultScript):
"""
types = {}
event_types = self.db.event_types
event_types = self.ndb.event_types
classes = Queue()
classes.put(type(obj))
while not classes.empty():