From 2230c209b63278ac462a1e2b6deb9c6c366dd9a8 Mon Sep 17 00:00:00 2001 From: massahito <110250805+massahito@users.noreply.github.com> Date: Sat, 19 Oct 2024 18:17:44 +0900 Subject: [PATCH 1/3] feat: refs #203 enable user to reconnect-ponggame-room. --- pong/realtime_pong_game/roommanager.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pong/realtime_pong_game/roommanager.py b/pong/realtime_pong_game/roommanager.py index 635b0ff5..56f004f2 100644 --- a/pong/realtime_pong_game/roommanager.py +++ b/pong/realtime_pong_game/roommanager.py @@ -62,6 +62,7 @@ def __init__(self, room_name, number_of_players): self.room_name = room_name self.room_state = RoomState.Not_All_Participants_Connected self.participants = [] + self.participants_connection = dict() self.participant_nickname_dict = dict() self.participants_state = dict() self.max_of_participants = int(number_of_players) @@ -79,6 +80,13 @@ def set_participant_state(self, participant, new_participant_state): def add_new_participant(self, new_participant, new_participant_nickname): with self.instance_lock: + if ( + new_participant in self.participants + and new_participant in self.participants_connection + and self.participants_connection[new_participant] is False + ): + self.participants_connection[new_participant] = True + return True if self.room_state != RoomState.Not_All_Participants_Connected: return False if new_participant in self.participants: @@ -91,6 +99,7 @@ def add_new_participant(self, new_participant, new_participant_nickname): return True def remove_participant(self, participant): + self.participants_connection[participant] = False if ( self.room_state != RoomState.Not_All_Participants_Connected and self.room_state != RoomState.Finished @@ -107,7 +116,10 @@ def remove_participant(self, participant): async def on_user_connected(self, user, user_nickname): if not self.add_new_participant(user, user_nickname): return False - if len(self.participants) == self.max_of_participants: + if ( + len(self.participants) == self.max_of_participants + and self.room_state == RoomState.Not_All_Participants_Connected + ): self.set_room_state(RoomState.Display_Tournament) await self.send_room_state_to_group() asyncio.new_event_loop().run_in_executor( From 675fa960e06ac20be44aac7ec63798d41888931b Mon Sep 17 00:00:00 2001 From: massahito <110250805+massahito@users.noreply.github.com> Date: Sat, 19 Oct 2024 19:02:45 +0900 Subject: [PATCH 2/3] fix: refs #203 update participant_connection when user enter room. --- pong/realtime_pong_game/roommanager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pong/realtime_pong_game/roommanager.py b/pong/realtime_pong_game/roommanager.py index 56f004f2..2f0abbc9 100644 --- a/pong/realtime_pong_game/roommanager.py +++ b/pong/realtime_pong_game/roommanager.py @@ -92,6 +92,7 @@ def add_new_participant(self, new_participant, new_participant_nickname): if new_participant in self.participants: return False self.participants.append(new_participant) + self.participants_connection[new_participant] = True self.participant_nickname_dict[new_participant] = ( f"{new_participant_nickname} #{len(self.participants)}" ) From c8e502eca394ff0c21bd930d4f0157c446c0291e Mon Sep 17 00:00:00 2001 From: massahito <110250805+massahito@users.noreply.github.com> Date: Sun, 20 Oct 2024 17:11:53 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20refs=20#203=20=E5=86=8D=E6=8E=A5?= =?UTF-8?q?=E7=B6=9A=E6=99=82=E3=81=ABscore=E3=81=8C=E5=8F=8D=E6=98=A0?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pong/realtime_pong_game/consumers.py | 11 +++++++---- pong/realtime_pong_game/ponggame.py | 15 +++++++++++++++ pong/realtime_pong_game/roommanager.py | 6 ++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/pong/realtime_pong_game/consumers.py b/pong/realtime_pong_game/consumers.py index f8f451b1..7333022c 100644 --- a/pong/realtime_pong_game/consumers.py +++ b/pong/realtime_pong_game/consumers.py @@ -1,7 +1,7 @@ import json from channels.generic.websocket import AsyncWebsocketConsumer from django.contrib.auth.models import AnonymousUser -from realtime_pong_game.roommanager import RoomManager +from realtime_pong_game.roommanager import RoomManager, RoomState class PlayerConsumer(AsyncWebsocketConsumer): @@ -33,7 +33,7 @@ async def connect(self): ) self.user_nickname = self.user.nickname # if url path is not valid, close connection - if self.room_name == None or self.user_role == None: + if self.room_name is None or self.user_role is None: await self.send( text_data=json.dumps( { @@ -51,7 +51,7 @@ async def connect(self): self.room_manager = RoomManager.host_room( self.room_name, self.number_of_players ) - if self.room_manager == None: + if self.room_manager is None: await self.send( text_data=json.dumps( { @@ -66,7 +66,7 @@ async def connect(self): # if user want to guest, confirm that room can be entered. if not, connection closed. elif self.user_role == "guest": self.room_manager = RoomManager.guest_room(self.room_name) - if self.room_manager == None: + if self.room_manager is None: await self.send( text_data=json.dumps( { @@ -108,6 +108,7 @@ async def connect(self): } ) ) + del self.room_manager await self.close() return @@ -135,6 +136,8 @@ async def receive(self, text_data=None, bytes_data=None): } ) ) + if self.room_manager.room_state == RoomState.In_Game: + await self.room_manager.pong_game.on_user_connected() return await self.room_manager.on_receive_user_message(self.user, message_json) diff --git a/pong/realtime_pong_game/ponggame.py b/pong/realtime_pong_game/ponggame.py index 79c57a8c..58a67b4a 100644 --- a/pong/realtime_pong_game/ponggame.py +++ b/pong/realtime_pong_game/ponggame.py @@ -60,6 +60,19 @@ def __init__(self, room_name): self.player1_score = 0 self.player2_score = 0 + async def on_user_connected(self): + await self.send_message_to_group( + "send_game_information", + { + "sender": "PongGame", + "type": "player-scored", + "contents": { + "player1": {"name": self.player1_name, "score": self.player1_score}, + "player2": {"name": self.player2_name, "score": self.player2_score}, + }, + }, + ) + # execture pong game and return the scores def execute(self, player1_name, player2_name): self.ball = Ball(FIELD_WIDTH / 2, FIELD_HEIGHT / 2) @@ -67,6 +80,8 @@ def execute(self, player1_name, player2_name): self.player2_paddle = Paddle() self.player1_score = 0 self.player2_score = 0 + self.player1_name = player1_name + self.player2_name = player2_name while self.player1_score < 5 and self.player2_score < 5: async_to_sync(self.send_message_to_group)( "send_game_information", diff --git a/pong/realtime_pong_game/roommanager.py b/pong/realtime_pong_game/roommanager.py index 2f0abbc9..e167f067 100644 --- a/pong/realtime_pong_game/roommanager.py +++ b/pong/realtime_pong_game/roommanager.py @@ -100,7 +100,6 @@ def add_new_participant(self, new_participant, new_participant_nickname): return True def remove_participant(self, participant): - self.participants_connection[participant] = False if ( self.room_state != RoomState.Not_All_Participants_Connected and self.room_state != RoomState.Finished @@ -132,7 +131,10 @@ async def on_user_connected(self, user, user_nickname): # delete user from Room async def on_user_disconnected(self, participant): - self.remove_participant(participant) + if participant in self.participants_connection: + self.participants_connection[participant] = False + if self.room_state == RoomState.Not_All_Participants_Connected: + self.remove_participant(participant) if len(self.participants) == 0: RoomManager.remove_instance(self.room_name) return True