"dummy" handlers and connect and disconnect handlers

This commit is contained in:
Ben 2019-08-23 21:49:27 +01:00
parent 9806bcf82e
commit 2f0da637d2
4 changed files with 45 additions and 20 deletions

View File

@ -64,7 +64,9 @@ player_in_room = handlers.player_in_room
class CommandManager(_AbstractManager):
def load(self, module):
async def setup(self, module):
raise NotImplementedError('Commands can only be loaded from plugins')
async def load(self, module):
command_objects = inspect.getmembers(module, is_command)
if not isinstance(module, plugins.IPlugin):

View File

@ -15,8 +15,8 @@ class AuthorityError(Exception):
class _Packet:
__slots__ = ['id']
def __init__(self):
self.id = None
def __init__(self, packet_id):
self.id = packet_id
def __eq__(self, other):
return self.id == other.id
@ -27,17 +27,15 @@ class _Packet:
class XTPacket(_Packet):
def __init__(self, *packet_id):
super().__init__()
self.id = '#'.join(packet_id)
def __hash__(self):
return hash(self.id)
super().__init__('#'.join(packet_id))
class XMLPacket(_Packet):
def __init__(self, packet_id):
super().__init__()
self.id = packet_id
pass
class DummyEventPacket(_Packet):
pass
class Priority(enum.Enum):
@ -107,7 +105,15 @@ class _XMLListener(_Listener):
return await self.callback(*handler_call_arguments)
class _DummyListener(_Listener):
async def __call__(self, p, *_):
handler_call_arguments = [self.instance, p] if self.instance is not None else [p]
return await self.callback(*handler_call_arguments)
class _ListenerManager(_AbstractManager):
ListenerClass = _Listener
def __init__(self, server):
self.strict_load = None
self.exclude_load = None
@ -153,19 +159,19 @@ class _ListenerManager(_AbstractManager):
@classmethod
def is_listener(cls, listener):
return issubclass(type(listener), _Listener)
return issubclass(type(listener), cls.ListenerClass)
class XTListenerManager(_ListenerManager):
@classmethod
def is_listener(cls, listener):
return issubclass(type(listener), _XTListener)
ListenerClass = _XTListener
class XMLListenerManager(_ListenerManager):
@classmethod
def is_listener(cls, listener):
return issubclass(type(listener), _XMLListener)
ListenerClass = _XMLListener
class DummyEventListenerManager(_ListenerManager):
ListenerClass = _DummyListener
def handler(packet, **kwargs):
@ -176,6 +182,10 @@ def handler(packet, **kwargs):
return _listener(listener_class, packet, **kwargs)
connected = _listener(_DummyListener, DummyEventPacket('connected'))
disconnected = _listener(_DummyListener, DummyEventPacket('disconnected'))
def cooldown(per=1.0, rate=1, bucket_type=BucketType.Default, callback=None):
def decorator(handler_function):
handler_function.__cooldown = _CooldownMapping(callback, _Cooldown(per, rate, bucket_type))

View File

@ -35,7 +35,7 @@ except ImportError:
import houdini.handlers
import houdini.plugins
from houdini.handlers import XTListenerManager, XMLListenerManager
from houdini.handlers import XTListenerManager, XMLListenerManager, DummyEventListenerManager
from houdini.plugins import PluginManager
from houdini.commands import CommandManager
@ -70,6 +70,7 @@ class Houdini:
self.xt_listeners = XTListenerManager(self)
self.xml_listeners = XMLListenerManager(self)
self.dummy_event_listeners = DummyEventListenerManager(self)
self.commands = CommandManager(self)
self.plugins = PluginManager(self)

View File

@ -5,7 +5,7 @@ from asyncio import IncompleteReadError, CancelledError
import defusedxml.cElementTree as Et
from xml.etree.cElementTree import Element, SubElement, tostring
from houdini.handlers import AuthorityError
from houdini.handlers import AuthorityError, DummyEventPacket
from houdini.converters import ChecklistError
from houdini.cooldown import CooldownError
@ -136,10 +136,22 @@ class Spheniscidae:
async def _client_connected(self):
self.logger.info('Client %s connected', self.peer_name)
dummy_event = DummyEventPacket('connected')
if dummy_event in self.server.dummy_event_listeners:
dummy_event_listeners = self.server.dummy_event_listeners[dummy_event]
for listener in dummy_event_listeners:
await listener(self)
async def _client_disconnected(self):
del self.server.peers_by_ip[self.peer_name]
self.logger.info('Client %s disconnected', self.peer_name)
dummy_event = DummyEventPacket('disconnected')
if dummy_event in self.server.dummy_event_listeners:
dummy_event_listeners = self.server.dummy_event_listeners[dummy_event]
for listener in dummy_event_listeners:
await listener(self)
async def __data_received(self, data):
data = data.decode()[:-1]
try: