From ed277126d395364902c6b471a2c6053527ebc5b2 Mon Sep 17 00:00:00 2001 From: Sam Maselli Date: Sat, 12 Oct 2024 00:45:21 -0400 Subject: [PATCH] set_role_and_wincon --- server/src/game/components/cult.rs | 2 +- server/src/game/components/mafia.rs | 2 +- server/src/game/mod.rs | 4 ++-- server/src/game/player/player_helper_functions.rs | 4 +++- server/src/game/role/apostle.rs | 2 +- server/src/game/role/counterfeiter.rs | 2 +- server/src/game/role/doomsayer.rs | 2 +- server/src/game/role/fiends_wildcard.rs | 2 +- server/src/game/role/godfather.rs | 2 +- server/src/game/role/imposter.rs | 2 +- server/src/game/role/mafia_killing_wildcard.rs | 2 +- server/src/game/role/mafia_support_wildcard.rs | 2 +- server/src/game/role/politician.rs | 2 +- server/src/game/role/recruiter.rs | 2 +- server/src/game/role/retrainer.rs | 4 ++-- server/src/game/role/revolutionary.rs | 4 ++-- server/src/game/role/true_wildcard.rs | 2 +- server/src/game/role/wild_card.rs | 2 +- 18 files changed, 23 insertions(+), 21 deletions(-) diff --git a/server/src/game/components/cult.rs b/server/src/game/components/cult.rs index e50c436cb..336881e72 100644 --- a/server/src/game/components/cult.rs +++ b/server/src/game/components/cult.rs @@ -106,7 +106,7 @@ impl Cult{ }; if player_ref.role(game) == role.role() {continue} - player_ref.set_role(game, role); + player_ref.set_role_and_wincon(game, role); } game.set_cult(cult); diff --git a/server/src/game/components/mafia.rs b/server/src/game/components/mafia.rs index a53a710bc..ec57fdb27 100644 --- a/server/src/game/components/mafia.rs +++ b/server/src/game/components/mafia.rs @@ -67,7 +67,7 @@ impl Mafia{ let random_mafia = all_living_mafia.choose(&mut rand::thread_rng()); if let Some(random_mafia) = random_mafia { - random_mafia.set_role(game, role); + random_mafia.set_role_and_wincon(game, role); } } } \ No newline at end of file diff --git a/server/src/game/mod.rs b/server/src/game/mod.rs index 4c585eeba..0ce24d54c 100644 --- a/server/src/game/mod.rs +++ b/server/src/game/mod.rs @@ -204,7 +204,7 @@ impl Game { //on role creation needs to be called after all players roles are known for player_ref in PlayerReference::all_players(&game){ let role_data_copy = player_ref.role_state(&game).clone(); - player_ref.set_role(&mut game, role_data_copy); + player_ref.set_role_and_wincon(&mut game, role_data_copy); } for player_ref in PlayerReference::all_players(&game){ @@ -478,7 +478,7 @@ pub mod test { //on role creation needs to be called after all players roles are known for player_ref in PlayerReference::all_players(&game){ let role_data_copy = player_ref.role_state(&game).clone(); - player_ref.set_role(&mut game, role_data_copy); + player_ref.set_role_and_wincon(&mut game, role_data_copy); } OnGameStart::invoke(&mut game); diff --git a/server/src/game/player/player_helper_functions.rs b/server/src/game/player/player_helper_functions.rs index ef984156e..3a37e31b8 100644 --- a/server/src/game/player/player_helper_functions.rs +++ b/server/src/game/player/player_helper_functions.rs @@ -181,11 +181,13 @@ impl PlayerReference{ OnAnyDeath::new(*self) } /// Swaps this persons role, sends them the role chat message, and makes associated changes - pub fn set_role(&self, game: &mut Game, new_role_data: impl Into){ + pub fn set_role_and_wincon(&self, game: &mut Game, new_role_data: impl Into){ let new_role_data = new_role_data.into(); let old = self.role_state(game).clone(); + self.set_win_condition(game, new_role_data.clone().default_win_condition()); + BeforeRoleSwitch::new(*self, old.clone(), new_role_data.clone()).invoke(game); self.set_role_state(game, new_role_data.clone()); diff --git a/server/src/game/role/apostle.rs b/server/src/game/role/apostle.rs index e047ddb3d..84ba944b1 100644 --- a/server/src/game/role/apostle.rs +++ b/server/src/game/role/apostle.rs @@ -46,7 +46,7 @@ impl RoleStateImpl for Apostle { return } - target_ref.set_role(game, Zealot); + target_ref.set_role_and_wincon(game, Zealot); target_ref.set_win_condition(game, WinCondition::new_single_resolution_state(ResolutionState::Cult)); Cult::set_ability_used_last_night(game, Some(CultAbility::Convert)); } diff --git a/server/src/game/role/counterfeiter.rs b/server/src/game/role/counterfeiter.rs index d146e1e15..7c09dd2ab 100644 --- a/server/src/game/role/counterfeiter.rs +++ b/server/src/game/role/counterfeiter.rs @@ -199,7 +199,7 @@ impl RoleStateImpl for Counterfeiter { if !backup.alive(game){return} //convert backup to Counterfeiter - backup.set_role(game, RoleState::Counterfeiter(Counterfeiter{backup: None, ..self})); + backup.set_role_and_wincon(game, RoleState::Counterfeiter(Counterfeiter{backup: None, ..self})); } else if self.backup.is_some_and(|p|p == dead_player_ref) { actor_ref.set_role_state(game, RoleState::Counterfeiter(Counterfeiter{backup: None, ..self})); diff --git a/server/src/game/role/doomsayer.rs b/server/src/game/role/doomsayer.rs index ca9e289bd..cd3312d0e 100644 --- a/server/src/game/role/doomsayer.rs +++ b/server/src/game/role/doomsayer.rs @@ -162,7 +162,7 @@ impl Doomsayer{ player.alive(game) && DoomsayerGuess::convert_to_guess(player.role(game)).is_some() && *player != actor_ref ).count() < 3 { - actor_ref.set_role(game, RoleState::Jester(Jester::default())); + actor_ref.set_role_and_wincon(game, RoleState::Jester(Jester::default())); } } pub fn won(&self) -> bool { diff --git a/server/src/game/role/fiends_wildcard.rs b/server/src/game/role/fiends_wildcard.rs index 60fd2a0cb..b44b39710 100644 --- a/server/src/game/role/fiends_wildcard.rs +++ b/server/src/game/role/fiends_wildcard.rs @@ -56,7 +56,7 @@ impl FiendsWildcard { .collect::>() ) { - actor_ref.set_role(game, self.role.default_state()); + actor_ref.set_role_and_wincon(game, self.role.default_state()); }else{ actor_ref.add_private_chat_message(game, ChatMessageVariant::WildcardConvertFailed{role: self.role.clone()}) } diff --git a/server/src/game/role/godfather.rs b/server/src/game/role/godfather.rs index b8d734d34..c8dc632ea 100644 --- a/server/src/game/role/godfather.rs +++ b/server/src/game/role/godfather.rs @@ -114,7 +114,7 @@ impl RoleStateImpl for Godfather { if !backup.alive(game){return} //convert backup to godfather - backup.set_role(game, RoleState::Godfather(Godfather{backup: None})); + backup.set_role_and_wincon(game, RoleState::Godfather(Godfather{backup: None})); } else if self.backup.is_some_and(|p|p == dead_player_ref) { actor_ref.set_role_state(game, RoleState::Godfather(Godfather{backup: None})); diff --git a/server/src/game/role/imposter.rs b/server/src/game/role/imposter.rs index 8f4b2622c..98f2172e7 100644 --- a/server/src/game/role/imposter.rs +++ b/server/src/game/role/imposter.rs @@ -137,7 +137,7 @@ impl RoleStateImpl for Imposter { if !backup.alive(game){return} //convert backup to godfather - backup.set_role(game, RoleState::Imposter(Imposter{backup: None, ..self})); + backup.set_role_and_wincon(game, RoleState::Imposter(Imposter{backup: None, ..self})); } else if self.backup.is_some_and(|p|p == dead_player_ref) { actor_ref.set_role_state(game, RoleState::Imposter(Imposter{backup: None, ..self})); diff --git a/server/src/game/role/mafia_killing_wildcard.rs b/server/src/game/role/mafia_killing_wildcard.rs index 5b547817d..a8968786d 100644 --- a/server/src/game/role/mafia_killing_wildcard.rs +++ b/server/src/game/role/mafia_killing_wildcard.rs @@ -55,7 +55,7 @@ impl MafiaKillingWildcard { .collect::>() ) { - actor_ref.set_role(game, self.role.default_state()); + actor_ref.set_role_and_wincon(game, self.role.default_state()); }else{ actor_ref.add_private_chat_message(game, ChatMessageVariant::WildcardConvertFailed{role: self.role.clone()}) } diff --git a/server/src/game/role/mafia_support_wildcard.rs b/server/src/game/role/mafia_support_wildcard.rs index 71b613517..6b5adc569 100644 --- a/server/src/game/role/mafia_support_wildcard.rs +++ b/server/src/game/role/mafia_support_wildcard.rs @@ -55,7 +55,7 @@ impl MafiaSupportWildcard { .collect::>() ) { - actor_ref.set_role(game, self.role.default_state()); + actor_ref.set_role_and_wincon(game, self.role.default_state()); }else{ actor_ref.add_private_chat_message(game, ChatMessageVariant::WildcardConvertFailed{role: self.role.clone()}) } diff --git a/server/src/game/role/politician.rs b/server/src/game/role/politician.rs index 034bc3666..ec58193bd 100644 --- a/server/src/game/role/politician.rs +++ b/server/src/game/role/politician.rs @@ -34,7 +34,7 @@ impl RoleStateImpl for Politician { } fn on_role_creation(self, game: &mut Game, actor_ref: PlayerReference){ if self.should_suicide(game, actor_ref) { - actor_ref.set_role(game, RoleState::Jester(Jester::default())); + actor_ref.set_role_and_wincon(game, RoleState::Jester(Jester::default())); } } fn on_any_death(self, game: &mut Game, actor_ref: PlayerReference, _dead_player_ref: PlayerReference){ diff --git a/server/src/game/role/recruiter.rs b/server/src/game/role/recruiter.rs index e1997de1c..03404da66 100644 --- a/server/src/game/role/recruiter.rs +++ b/server/src/game/role/recruiter.rs @@ -157,7 +157,7 @@ impl RoleStateImpl for Recruiter { if !backup.alive(game){return} //convert backup to godfather - backup.set_role(game, RoleState::Recruiter(Recruiter{backup: None, ..self})); + backup.set_role_and_wincon(game, RoleState::Recruiter(Recruiter{backup: None, ..self})); } else if self.backup.is_some_and(|p|p == dead_player_ref) { actor_ref.set_role_state(game, RoleState::Recruiter(Recruiter{backup: None, ..self})); diff --git a/server/src/game/role/retrainer.rs b/server/src/game/role/retrainer.rs index 42648262e..6974f763e 100644 --- a/server/src/game/role/retrainer.rs +++ b/server/src/game/role/retrainer.rs @@ -125,7 +125,7 @@ impl RoleStateImpl for Retrainer { if !backup.alive(game){return} //convert backup to godfather - backup.set_role(game, RoleState::Retrainer(Retrainer{backup: None, ..self})); + backup.set_role_and_wincon(game, RoleState::Retrainer(Retrainer{backup: None, ..self})); } else if self.backup.is_some_and(|p|p == dead_player_ref) { actor_ref.set_role_state(game, RoleState::Retrainer(Retrainer{backup: None, ..self})); @@ -149,7 +149,7 @@ impl Retrainer { if let Some(backup) = retrainer.backup { if retrainer.retrains_remaining > 0 && backup.role(game) != role{ - backup.set_role(game, role.default_state()); + backup.set_role_and_wincon(game, role.default_state()); retrainer.retrains_remaining = retrainer.retrains_remaining.saturating_sub(1); } } diff --git a/server/src/game/role/revolutionary.rs b/server/src/game/role/revolutionary.rs index f0cc4975c..c7548e5dc 100644 --- a/server/src/game/role/revolutionary.rs +++ b/server/src/game/role/revolutionary.rs @@ -88,12 +88,12 @@ impl RoleStateImpl for Revolutionary { actor_ref.set_role_state(game, RoleState::Revolutionary(Revolutionary{target: RevolutionaryTarget::Target(*target)})); actor_ref.insert_role_label(game, *target); }else{ - actor_ref.set_role(game, RoleState::Jester(Jester::default())) + actor_ref.set_role_and_wincon(game, RoleState::Jester(Jester::default())) }; } fn on_any_death(self, game: &mut Game, actor_ref: PlayerReference, dead_player_ref: PlayerReference){ if Some(dead_player_ref) == self.target.get_target() && self.target != RevolutionaryTarget::Won { - actor_ref.set_role(game, RoleState::Jester(Jester::default())) + actor_ref.set_role_and_wincon(game, RoleState::Jester(Jester::default())) } } } diff --git a/server/src/game/role/true_wildcard.rs b/server/src/game/role/true_wildcard.rs index 8e2ce4eea..49723ba02 100644 --- a/server/src/game/role/true_wildcard.rs +++ b/server/src/game/role/true_wildcard.rs @@ -52,7 +52,7 @@ impl TrueWildcard { &[] ) { - actor_ref.set_role(game, self.role.default_state()); + actor_ref.set_role_and_wincon(game, self.role.default_state()); }else{ actor_ref.add_private_chat_message(game, ChatMessageVariant::WildcardConvertFailed{role: self.role.clone()}) } diff --git a/server/src/game/role/wild_card.rs b/server/src/game/role/wild_card.rs index 42786338f..4c2833068 100644 --- a/server/src/game/role/wild_card.rs +++ b/server/src/game/role/wild_card.rs @@ -54,7 +54,7 @@ impl Wildcard { .collect::>() ) { - actor_ref.set_role(game, self.role.default_state()); + actor_ref.set_role_and_wincon(game, self.role.default_state()); }else{ actor_ref.add_private_chat_message(game, ChatMessageVariant::WildcardConvertFailed{role: self.role.clone()}) }