Skip to content

Commit a7feb62

Browse files
committed
game_is_over function
1 parent f74c7e8 commit a7feb62

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

server/src/game/end_game_condition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Debug, PartialEq, Eq, Clone)]
1+
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
22
pub enum EndGameCondition {
33
Mafia,
44
Town,

server/src/game/mod.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,20 @@ impl Game {
129129
(guilty, innocent)
130130
}
131131

132-
pub fn winner(&self) -> Option<EndGameCondition> {
133-
let mut winning_team = None;
132+
pub fn game_is_over(&self) -> bool {
133+
//find list of all remaining teams
134+
let remaining_teams: Vec<EndGameCondition> =
135+
PlayerReference::all_players(self).into_iter()
136+
.filter(|p|p.alive(self)).map(|p|p.end_game_condition(self)).collect();
134137

135-
for player_ref in PlayerReference::all_players(self){
136-
if !player_ref.alive(self) {continue;}
137-
let egc = player_ref.end_game_condition(self);
138-
if egc == EndGameCondition::None {continue;}
139-
140-
if let Some(ref winning_team) = winning_team{
141-
//if there are two different teams alive then nobody won
142-
if *winning_team != egc{
143-
return None;
144-
}
145-
} else {
146-
winning_team = Some(egc.clone());
147-
}
148-
}
149-
150-
winning_team
138+
//remove all duplicates from remaining_teams
139+
let remaining_teams = remaining_teams.into_iter()
140+
.collect::<std::collections::HashSet<EndGameCondition>>().into_iter().collect::<Vec<EndGameCondition>>();
141+
142+
//if there is only one team left, then the game is over
143+
remaining_teams.len() <= 1
144+
145+
//it doesnt matter if the final team is none, because this doesnt care who won, just that game is over
151146
}
152147

153148
pub fn current_phase(&self) -> &PhaseState {
@@ -165,7 +160,7 @@ impl Game {
165160

166161
if !self.ongoing { return }
167162

168-
if let Some(_winner) = self.winner() {
163+
if self.game_is_over() {
169164
self.add_message_to_chat_group(ChatGroup::All, ChatMessage::GameOver);
170165
self.send_packet_to_all(ToClientPacket::GameOver{ reason: GameOverReason::Draw });
171166
self.ongoing = false;

0 commit comments

Comments
 (0)