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 = 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):
class SendHandler(Handler):
def on_message(self, frame):
if self.connection.src == '@':
self.connection.add_text_to_buf('', '* You are not logged in')
message ='message', None)
embed ='embed', None)
action ='action', None)
react ='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'groups', []):
......@@ -63,8 +86,8 @@ class RecvHandler:
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} {!r}')
t = frame.type
if t == 'message':
if self.src == '@':
self.add_text_to_buf('', '* You are not logged in')
message ='message', None)
embed ='embed', None)
action ='action', None)
react ='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}')
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