Skip to content

Commit

Permalink
Merge branch 'develop' into feature/tournament-waring
Browse files Browse the repository at this point in the history
  • Loading branch information
massahito authored Oct 20, 2024
2 parents 479e1e7 + 6ebb8f4 commit 95e95d8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
11 changes: 7 additions & 4 deletions pong/realtime_pong_game/consumers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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(
{
Expand All @@ -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(
{
Expand All @@ -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(
{
Expand Down Expand Up @@ -108,6 +108,7 @@ async def connect(self):
}
)
)
del self.room_manager
await self.close()
return

Expand Down Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions pong/realtime_pong_game/ponggame.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,28 @@ 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)
self.player1_paddle = Paddle()
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",
Expand Down
19 changes: 17 additions & 2 deletions pong/realtime_pong_game/roommanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}"
)
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 95e95d8

Please sign in to comment.