Skip to content

Commit

Permalink
feat: refs #224 PongGameで一時停止を可能にする
Browse files Browse the repository at this point in the history
  • Loading branch information
massahito committed Oct 20, 2024
1 parent aa34db8 commit d7699c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pong/pong/static/pong/components/PongGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export class PongGame extends Component {
case "tournament-winner":
this.setRouteContext("TournamentWinner", message.contents);
break;
case "timeout":
if (document.getElementById("timeoutMessage")) {
document.getElementById("timeoutMessage").innerHTML =
"Timeout requested.";
}
break;
}
};

Expand Down
6 changes: 6 additions & 0 deletions pong/pong/static/pong/components/PongGameHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export class PongGameHome extends Component {
alert(message.contents);
this.goNextPage("/error");
break;
case "timeout":
if (document.getElementById("timeoutMessage")) {
document.getElementById("timeoutMessage").innerHTML =
"Timeout requested.";
}
break;
}
};

Expand Down
19 changes: 18 additions & 1 deletion pong/pong/static/pong/components/PongGameTournament.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class PongGameTournament extends Component {
alert("connection failed.");
this.goNextPage("/");
}
this.connection = this.getRouteContext("PongGameWebSocket");
const tournamentContext = this.getRouteContext("Tournament");
this.bracket = new PongGameTournamentBracket(
this.route,
Expand All @@ -20,12 +21,28 @@ export class PongGameTournament extends Component {
);
this.element.appendChild(this.bracket.element);
this.unsetRouteContext("Tournament");
this.timeoutButton = document.getElementById("timeoutButton");
if (this.timeoutButton) {
this.timeoutButton.onclick = this.onClickTimeoutButton;
}
}

onClickTimeoutButton = async () => {
this.connection.send(
JSON.stringify({
sender: "player",
type: "timeout",
contents: "timeout",
}),
);
};

get html() {
return `
<main class="text-center p-5>
<main class="text-center" p-5>
<h1>Pong-Game Tournament</h1>
<button id="timeoutButton" class="btn btn-primary">TimeOut</button>
<h2 id="timeoutMessage"></h2>
</main>
`;
}
Expand Down
18 changes: 17 additions & 1 deletion pong/realtime_pong_game/roommanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, room_name, number_of_players):
self.participants_state = dict()
self.max_of_participants = int(number_of_players)
self.game_results = []
self.is_timeout = False

def set_room_state(self, new_room_state):
with self.instance_lock:
Expand Down Expand Up @@ -167,6 +168,19 @@ async def send_room_state_to_group(self):
async def on_receive_user_message(self, participant, message_json):
if self.room_state == RoomState.In_Game:
await self.handle_game_action(participant, message_json)
elif self.room_state == RoomState.Display_Tournament:
self.is_timeout = True
await self.channel_layer.group_send(
self.room_name,
{
"type": "send_room_information",
"contents": {
"sender": "room-manager",
"type": "timeout",
"contents": "timeout",
},
},
)

def change_participants_state_for_game(self, player1, player2):
for participant in self.participants_state.keys():
Expand Down Expand Up @@ -236,7 +250,9 @@ def game_dispatcher(self, dummy_data):
# change room state to Display_Tournament
self.set_room_state(RoomState.Display_Tournament)
async_to_sync(self.send_room_state_to_group)()
time.sleep(3)
time.sleep(5)
if self.is_timeout is True:
time.sleep(30)
# change room state to In_Game
self.set_room_state(RoomState.In_Game)
async_to_sync(self.send_room_state_to_group)()
Expand Down

0 comments on commit d7699c0

Please sign in to comment.