diff --git a/helpers.py b/helpers.py index d4b192c..bfe83d1 100644 --- a/helpers.py +++ b/helpers.py @@ -1,4 +1,7 @@ import json +import time + +import constants HEADER_LENGTH = 10 @@ -17,3 +20,15 @@ def prepare_json(object) -> bytes: :return: Encoded JSON """ return prepare(json.dumps(object)) + + +def prepare_server_message(nickname: str, message: str, color: str, msgtime: int = None): + return prepare_json( + { + 'type': constants.Types.MESSAGE, + 'nickname': nickname, + 'content': message, + 'color': color, + 'time': msgtime or int(time.time()) + } + ) diff --git a/oserver.py b/oserver.py index 19b7fcf..2e48207 100644 --- a/oserver.py +++ b/oserver.py @@ -57,14 +57,10 @@ def handle(client_id): nickname = message['nickname'] if not clients[client_id]['has_nickname']: print("Nickname is {}".format(nickname)) - broadcast_data(helpers.prepare_json( - { - 'type': constants.Types.MESSAGE, - 'nickname': 'Server', - 'content': f'{nickname} joined!', - 'color': constants.Colors.PINK, - 'time': int(time.time()) - } + broadcast_data(helpers.prepare_server_message( + nickname='Server', + message=f'{nickname} joined!', + color=constants.Colors.BLACK )) clients[client_id]['has_nickname'] = True else: @@ -72,19 +68,7 @@ def handle(client_id): clients[client_id]['nickname'] = nickname elif message['type'] == constants.Types.MESSAGE: - if message['content'] == '/reroll': - color = random.choice(constants.Colors.ALL) - colorname = constants.Colors.ALL_NAMES[constants.Colors.ALL.index(color)] - clients[client_id]['color'] = color - broadcast_data(helpers.prepare_json( - { - 'type': constants.Types.MESSAGE, - 'nickname': 'Server', - 'content': f'Changed your color to {colorname} ({color})', - 'color': constants.Colors.PINK, - 'time': int(time.time()) - } - )) + # Echo message back to all other users broadcast_data(helpers.prepare_json( { 'type': constants.Types.MESSAGE, @@ -94,19 +78,26 @@ def handle(client_id): 'time': int(time.time()) } )) + + # Basic command processing + if message['content'] == '/reroll': + color = random.choice(constants.Colors.ALL) + colorName = constants.Colors.ALL_NAMES[constants.Colors.ALL.index(color)] + clients[client_id]['color'] = color + broadcast_data(helpers.prepare_server_message( + nickname='Server', + message=f'Changed your color to {colorName} ({color})', + color=constants.Colors.BLACK + )) except: traceback.print_exc() print(f'Closing Client {clients[client_id]["nickname"]}') client.close() del clients[client_id] - broadcast_data(helpers.prepare_json( - { - 'type': constants.Types.MESSAGE, - 'nickname': 'Server', - 'content': f'{nickname} left!', - 'color': constants.Colors.PINK, - 'time': int(time.time()) - } + broadcast_data(helpers.prepare_server_message( + nickname='Server', + message=f'{nickname} left!', + color=constants.Colors.BLACK )) break