Skip to content

Commit

Permalink
End game when game ends
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Papel committed Dec 6, 2023
1 parent 94456b9 commit c56c6ed
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
2 changes: 2 additions & 0 deletions client/src/game/gameManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ export function createGameManager(): GameManager {
},

tick(timePassedMs) {
if (!gameManager.gameState.ongoing) return;

const newTimeLeft = gameManager.gameState.timeLeftMs - timePassedMs;
if (Math.floor(newTimeLeft / 1000) < Math.floor(gameManager.gameState.timeLeftMs / 1000)) {
gameManager.invokeStateListeners("tick");
Expand Down
2 changes: 2 additions & 0 deletions client/src/game/gameState.d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type GameState = {
roleList: RoleOutline[],
excludedRoles: RoleOutline[],
phaseTimes: PhaseTimes

ongoing: boolean
}
export default GameState;

Expand Down
2 changes: 2 additions & 0 deletions client/src/game/gameState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export function createGameState(): GameState {
evening: 7,
night: 37,
},

ongoing: true,
}
}

Expand Down
1 change: 1 addition & 0 deletions client/src/game/messageListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export default function messageListener(packet: ToClientPacket){
GAME_MANAGER.gameState.graves.push(packet.grave);
break;
case "gameOver":
GAME_MANAGER.gameState.ongoing = false;
switch(packet.reason) {
case "ReachedMaxDay":
// alert("Game Over: Reached the maximum day!");
Expand Down
31 changes: 20 additions & 11 deletions server/src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct Game {
pub teams: Teams,

phase_machine : PhaseStateMachine,

pub ongoing: bool
}

impl Game {
Expand Down Expand Up @@ -77,6 +79,7 @@ impl Game {
drop(roles); // Ensure we don't use the order of roles anywhere

let mut game = Self{
ongoing: true,
players: players.into_boxed_slice(),
graves: Vec::new(),
teams: Teams::default(),
Expand Down Expand Up @@ -122,7 +125,7 @@ impl Game {
(guilty, innocent)
}

pub fn game_is_over(&self)->bool{
pub fn winner(&self) -> Option<EndGameCondition> {
let mut winning_team = None;

for player_ref in PlayerReference::all_players(self){
Expand All @@ -133,13 +136,14 @@ impl Game {
if let Some(ref winning_team) = winning_team{
//if there are two different teams alive then nobody won
if *winning_team != egc{
return false;
return None;
}
}else{
} else {
winning_team = Some(egc.clone());
}
}
return true;

winning_team
}

pub fn current_phase(&self) -> &PhaseState {
Expand All @@ -151,15 +155,23 @@ impl Game {
}

pub fn tick(&mut self, time_passed: Duration){
if self.game_is_over() {
for player_ref in PlayerReference::all_players(self){
player_ref.tick(self, time_passed)
}

if !self.ongoing { return }

if let Some(_winner) = self.winner() {
self.add_message_to_chat_group(ChatGroup::All, ChatMessage::GameOver);
self.send_packet_to_all(ToClientPacket::GameOver{ reason: GameOverReason::Draw });

self.ongoing = false;
return;
}

if self.phase_machine.day_number == u8::MAX {
self.add_message_to_chat_group(ChatGroup::All, ChatMessage::GameOver);
self.send_packet_to_all(ToClientPacket::GameOver{ reason: GameOverReason::ReachedMaxDay });
// TODO, clean up the lobby. Stop the ticking
self.ongoing = false;
return;
}

Expand All @@ -169,10 +181,6 @@ impl Game {
self.start_phase(new_phase);
}

for player_ref in PlayerReference::all_players(self){
player_ref.tick(self, time_passed)
}

self.phase_machine.time_remaining = self.phase_machine.time_remaining.saturating_sub(time_passed);
}

Expand Down Expand Up @@ -260,6 +268,7 @@ pub mod test {
drop(roles);

let mut game = Game{
ongoing: true,
players: players.into_boxed_slice(),
graves: Vec::new(),
teams: Teams::default(),
Expand Down
6 changes: 5 additions & 1 deletion server/src/game/player/player_send_packet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use crate::{game::{Game, available_buttons::AvailableButtons, phase::PhaseState}, packet::ToClientPacket, websocket_connections::connection::ClientSender};
use crate::{game::{Game, available_buttons::AvailableButtons, phase::PhaseState}, packet::{ToClientPacket, GameOverReason}, websocket_connections::connection::ClientSender};

use super::{PlayerReference, ClientConnection, DISCONNECT_TIMER_SECS};

Expand Down Expand Up @@ -55,6 +55,10 @@ impl PlayerReference{
}
]);

if !game.ongoing {
self.send_packet(game, ToClientPacket::GameOver { reason: GameOverReason::Draw })
}

if let PhaseState::Testimony { player_on_trial, .. }
| PhaseState::Judgement { player_on_trial, .. }
| PhaseState::Evening { player_on_trial: Some(player_on_trial) } = game.current_phase() {
Expand Down
2 changes: 1 addition & 1 deletion server/src/lobby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl Lobby {
player.1.send(packet.clone());
}
}
#[allow(unused)]

fn send_players_game(game: &mut Game, players: &HashMap<PlayerID, GamePlayer>){

let mut players: Vec<_> = players.iter().collect();
Expand Down

1 comment on commit c56c6ed

@vercel
Copy link

@vercel vercel bot commented on c56c6ed Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mafia-game – ./

mafia-game-git-00x-main-itssammym.vercel.app
mafia-game.vercel.app
mafia-game-itssammym.vercel.app

Please sign in to comment.