From d18e787213ee528d72a9ea3ad71f49311977991e Mon Sep 17 00:00:00 2001 From: Giovani Date: Tue, 6 Jul 2021 01:59:14 -0400 Subject: [PATCH] feat: add client id to connection --- tetris/online.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tetris/online.py b/tetris/online.py index 7f71931..0771756 100644 --- a/tetris/online.py +++ b/tetris/online.py @@ -1,8 +1,8 @@ import asyncio -from os import stat import websockets import json import queue # refer to https://docs.python.org/3/library/queue.html for multi-threading +import uuid from typing import Dict from threading import Thread @@ -11,6 +11,7 @@ class MultiplayerService(): _receive_piece_queue = queue.Queue() _send_piece_queue = queue.Queue() _current_game_id = None + _client_id = str(uuid.uuid4()) @classmethod def init(cls) -> None: @@ -70,7 +71,9 @@ class _NetworkConnectionService(): if not cls._join_game: return - json_message = json.dumps({"action": "enter_game", "gameId": MultiplayerService._current_game_id}) + json_message = json.dumps({"action": "enter_game",\ + "clientId": MultiplayerService._client_id,\ + "gameId": MultiplayerService._current_game_id}) await cls._websocket.send(json_message) json_response = await cls._websocket.recv() @@ -88,12 +91,13 @@ class _NetworkConnectionService(): piece = MultiplayerService._send_piece_queue.get() # construct json message - message = {} - message["action"] = "send_piece" - message["gameId"] = MultiplayerService._current_game_id - message["piece"] = piece.__dict__() + json_message = json.dumps({"action": "send_piece",\ + "clientId": MultiplayerService._client_id,\ + "gameId": MultiplayerService._current_game_id,\ + "piece": piece.__dict__}) - await cls._websocket.send(json.dumps(message)) + await cls._websocket.send(json_message) + MultiplayerService._send_piece_queue.task_done() @classmethod async def _try_receive_message(cls) -> None: