From 35d095a9a8581c2ff306b0ababf81568ba9bf13f Mon Sep 17 00:00:00 2001 From: AzerSD Date: Sat, 27 Apr 2024 10:37:48 +0200 Subject: [PATCH 1/3] fixed second game after refresh --- backend/game/consumers.py | 35 ++++++++++++++++++++++++++--------- docker-compose.yaml | 1 - 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/backend/game/consumers.py b/backend/game/consumers.py index 001700da4..45b16bffc 100644 --- a/backend/game/consumers.py +++ b/backend/game/consumers.py @@ -27,7 +27,6 @@ class GameConsumer(AsyncWebsocketConsumer): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.game_state = {} - self.condition = asyncio.Condition() user_ids = {} @@ -44,24 +43,29 @@ async def connect(self): logger.info("User is not authenticated!!!!!") raise StopConsumer("User is not authenticated") logger.info(f"connected clients: {self.connected_clients}") + user = self.scope["user"] if self.room_name in self.connected_clients: + self.game_instance = self.connected_clients[self.room_name] if self.user_ids[self.room_name][0] != user.id: - self.game_instance = self.connected_clients[self.room_name] self.user_ids[self.room_name][1] = user.id self.users[self.room_name][1] = user self.game_state[self.room_name] = "starting" else: self.user_ids[self.room_name] = [user.id, None] self.users[self.room_name] = [user, None] - self.connected_clients[self.room_name] = GameInstance() - self.game_state[self.room_name] = "waiting" + if ( + self.room_name not in self.connected_clients + or self.connected_clients[self.room_name] is None + ): + self.connected_clients[self.room_name] = GameInstance() self.game_instance = self.connected_clients[self.room_name] + self.game_state[self.room_name] = "waiting" await self.channel_layer.group_add(self.room_group_name, self.channel_name) await self.accept() - # if self.room_name not in self.game_state: - # self.game_state[self.room_name] = "waiting" + if self.room_name not in self.game_state: + self.game_state[self.room_name] = "waiting" if (self.game_state[self.room_name] != "waiting"): await self.send_room_info_to_group() @@ -84,9 +88,20 @@ async def receive(self, text_data): game_event = text_data cmd = str(game_event[:2]) userid = int(game_event[2:]) + # if connected client < 2, then create a new game instance + logger.info(f"connected clientssssssss: {self.connected_clients}") + if (self.room_name not in self.connected_clients and len(self.connected_clients) < 2): + # add user to the room + self.user_ids[self.room_name][1] = userid + self.users[self.room_name][1] = self.scope["user"] + self.game_instance = self.connected_clients[self.room_name] + + logger.info(f"ccccccconnected clients: {self.connected_clients}") await self.handleInput(cmd, userid) async def handleInput(self, cmd, userid): + logger.info(f"users in room: {self.users[self.room_name]}") + logger.info(f"User {userid} sent command: {cmd}") if cmd == "pw" and userid == self.user_ids[self.room_name][0]: await self.game_instance.move_p0_up("press") elif cmd == "ps" and userid == self.user_ids[self.room_name][0]: @@ -95,7 +110,6 @@ async def handleInput(self, cmd, userid): await self.game_instance.move_p0_up("release") elif cmd == "rs" and userid == self.user_ids[self.room_name][0]: await self.game_instance.move_p0_down("release") - elif cmd == "pw" and userid == self.user_ids[self.room_name][1]: await self.game_instance.move_p1_up("press") elif cmd == "ps" and userid == self.user_ids[self.room_name][1]: @@ -112,6 +126,9 @@ async def disconnect(self, close_code): if not self.connected_users: self.game_state = {} self.connected_clients = {} + # self.game_instance = None + self.game_tasks = {} + # self.connected_users = set() await self.channel_layer.group_discard(self.room_group_name, self.channel_name) @@ -125,9 +142,9 @@ async def startGame(self): self.connected_clients[self.room_name] = GameInstance() self.game_instance = self.connected_clients[self.room_name] if self.room_name not in self.game_tasks: - self.game_tasks[self.room_name] = asyncio.create_task(self.game_loop()) + self.game_tasks[self.room_name] = asyncio.create_task(self.gameLoop()) - async def game_loop(self): + async def gameLoop(self): while self.game_state[self.room_name] == "starting" or self.game_state[self.room_name] == "waiting": await self.game_instance.update_game( self.game_state, diff --git a/docker-compose.yaml b/docker-compose.yaml index 8d9b1f2f2..a614ba1f7 100755 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -54,7 +54,6 @@ services: args: FRONTEND_URL: ${FRONTEND_URL} BACKEND_URL: ${BACKEND_URL} - BACKEND_URLL: ${BACKEND_URLL} SERVER: ${SERVER} LOCATION: ${LOCATION} entrypoint: /app/docker/nginx/nginx.sh From 461cb2aed36b46e32061c29143996c43b6c4c8df Mon Sep 17 00:00:00 2001 From: AzerSD Date: Sat, 27 Apr 2024 10:39:58 +0200 Subject: [PATCH 2/3] fixed second after refresh --- backend/game/consumers.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/backend/game/consumers.py b/backend/game/consumers.py index 45b16bffc..0327990d0 100644 --- a/backend/game/consumers.py +++ b/backend/game/consumers.py @@ -34,6 +34,7 @@ def __init__(self, *args, **kwargs): game_tasks = {} users = {} connected_users = set() + async def connect(self): self.connected_users.add(self.scope["user"].id) self.room_name = self.scope["url_route"]["kwargs"]["room_name"] @@ -67,7 +68,7 @@ async def connect(self): if self.room_name not in self.game_state: self.game_state[self.room_name] = "waiting" - if (self.game_state[self.room_name] != "waiting"): + if self.game_state[self.room_name] != "waiting": await self.send_room_info_to_group() await self.startGame() @@ -90,7 +91,10 @@ async def receive(self, text_data): userid = int(game_event[2:]) # if connected client < 2, then create a new game instance logger.info(f"connected clientssssssss: {self.connected_clients}") - if (self.room_name not in self.connected_clients and len(self.connected_clients) < 2): + if ( + self.room_name not in self.connected_clients + and len(self.connected_clients) < 2 + ): # add user to the room self.user_ids[self.room_name][1] = userid self.users[self.room_name][1] = self.scope["user"] @@ -145,7 +149,10 @@ async def startGame(self): self.game_tasks[self.room_name] = asyncio.create_task(self.gameLoop()) async def gameLoop(self): - while self.game_state[self.room_name] == "starting" or self.game_state[self.room_name] == "waiting": + while ( + self.game_state[self.room_name] == "starting" + or self.game_state[self.room_name] == "waiting" + ): await self.game_instance.update_game( self.game_state, self.room_name, From fa4a2843dc2a78476559357df8f2171cbdb90cb6 Mon Sep 17 00:00:00 2001 From: AzerSD Date: Sat, 27 Apr 2024 10:40:54 +0200 Subject: [PATCH 3/3] changed score --- backend/game/consumers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/game/consumers.py b/backend/game/consumers.py index 0327990d0..cde5c5034 100644 --- a/backend/game/consumers.py +++ b/backend/game/consumers.py @@ -280,7 +280,7 @@ def __init__(self): self.p0_moving = 0 self.p1_moving = 0 self.ball_speed = 0 - self.score_to_win = 2 + self.score_to_win = 10 async def move_paddle(self, paddle, direction, state): if state == "press":