diff --git a/pong/realtime_pong_game/consumers.py b/pong/realtime_pong_game/consumers.py index f8f451b..7333022 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 79c57a8..58a67b4 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 7808683..c25067f 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,11 +80,19 @@ 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: return False self.participants.append(new_participant) + self.participants_connection[new_participant] = True self.participant_nickname_dict[new_participant] = ( f"{new_participant_nickname}" ) @@ -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( @@ -119,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