banned/src/events.py
skizzerz 0287b05bcd Split shaman
This has a number of related changes in other files due to needing to
re-prioritize events and fixing other bits of code to play more nicely
with split-off totems. Alpha wolf handling was moved after protection
handling because it should've been that way to begin with, and as part
of splitting off protection totem it needed to be split at the same
time. Same with fallen angel changes with how protection now adjusts the
killers lists.
2016-09-02 13:56:26 -05:00

36 lines
1.1 KiB
Python

# event system
from collections import defaultdict
from types import SimpleNamespace
EVENT_CALLBACKS = defaultdict(list)
__all__ = ["add_listener", "remove_listener", "Event"]
def add_listener(event, callback, priority=5):
if (priority, callback) not in EVENT_CALLBACKS[event]:
EVENT_CALLBACKS[event].append((priority, callback))
EVENT_CALLBACKS[event].sort(key = lambda x: x[0])
def remove_listener(event, callback, priority = 5):
if (priority, callback) in EVENT_CALLBACKS[event]:
EVENT_CALLBACKS[event].remove((priority, callback))
class Event:
def __init__(self, name, data, **kwargs):
self.stop_processing = False
self.prevent_default = False
self.name = name
self.data = data
self.params = SimpleNamespace(**kwargs)
def dispatch(self, *args, **kwargs):
self.stop_processing = False
self.prevent_default = False
for item in list(EVENT_CALLBACKS[self.name]):
item[1](self, *args, **kwargs)
if self.stop_processing:
break
return not self.prevent_default
# vim: set sw=4 expandtab: