Commit d254ddc7 authored by Elizabeth Myers's avatar Elizabeth Myers 💬
Browse files

clean up dispatch code more

parent 2a4d5e2a
......@@ -34,17 +34,40 @@ def parse_input(string):
return Frame.create(src, dest, command, data=keyvals)
class RecvHandler:
class Handler:
def __init__(self, window, name, connection):
self.window = window
self.name = name
self.connection = connection
def dispatch(self, frame):
func = getattr(self, f'on_{frame_type}', None)
func = getattr(self, f'on_{frame.type}', None)
if callable(func):
func()
func(frame)
class SendHandler(Handler):
def on_message(self, frame):
if self.connection.src == '@':
self.connection.add_text_to_buf('', '* You are not logged in')
return
message = frame.data.get('message', None)
embed = frame.data.get('embed', None)
action = frame.data.get('action', None)
react = frame.data.get('react', None)
if message:
self.connection.add_text_to_buf(frame.dst, f'<{self.connection.src[1:]}> {message}')
if embed:
self.connection.add_text_to_buf(frame.dst, f'-!- ({self.connection.src[1:]}) embed: {embed}')
if action:
self.connection.add_text_to_buf(frame.dst, f'* ({self.connection.src[1:]}) {action}')
if react:
self.connection.add_text_to_buf(frame.dst, f'-!- ({self.connection.src[1:]}) react: {react}')
class RecvHandler(Handler):
def on_listgroups(self, frame):
if frame.dst[0] == '@':
for group in frame.data.get('groups', []):
......@@ -63,8 +86,8 @@ class RecvHandler:
self.connection.member_bufs[frame.dst].add(member)
def on_login(self, frame):
self.connection.src = frame.dst
self.connection.send(Frame.create(self.connection.src, '$', 'listgroups'))
src = self.connection.src = frame.dst
self.connection.send(Frame.create(src, src, 'listgroups'))
def on_join(self, frame):
if frame.dst not in self.member_bufs:
......@@ -116,6 +139,7 @@ class RecvHandler:
class NetworkConnection:
def __init__(self, window, name, url):
self.send_handler = SendHandler(window, name, self)
self.recv_handler = RecvHandler(window, name, self)
self.window = window
......@@ -220,26 +244,7 @@ class NetworkConnection:
def send(self, frame):
self.add_text_to_buf('', f'→ {frame.type} {frame.src} {frame.dst} {frame.data!r}')
t = frame.type
if t == 'message':
if self.src == '@':
self.add_text_to_buf('', '* You are not logged in')
return
message = frame.data.get('message', None)
embed = frame.data.get('embed', None)
action = frame.data.get('action', None)
react = frame.data.get('react', None)
if message:
self.add_text_to_buf(frame.dst, f'<{self.src[1:]}> {message}')
if embed:
self.add_text_to_buf(frame.dst, f'-!- ({self.src[1:]}) embed: {embed}')
if action:
self.add_text_to_buf(frame.dst, f'* ({self.src[1:]}) {action}')
if react:
self.add_text_to_buf(frame.dst, f'-!- ({self.src[1:]}) react: {react}')
self.send_handler.dispatch(frame)
self.socket.sendBinaryMessage(frame.to_ubjson())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment