Skip to content

Commit

Permalink
godfather and imposter backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSammyM committed Dec 9, 2024
1 parent 4f00b8c commit ee0e52d
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 213 deletions.
137 changes: 77 additions & 60 deletions server/src/game/ability_input/saved_controllers_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use crate::{
game::{
chat::ChatMessageVariant, components::{
forfeit_vote::ForfeitVote, insider_group::InsiderGroupID, mafia::Mafia, pitchfork::Pitchfork, syndicate_gun_item::SyndicateGunItem
}, event::on_validated_ability_input_received::OnValidatedAbilityInputReceived, phase::PhaseType, player::PlayerReference, Game
},
event::{
on_controller_selection_changed::OnControllerSelectionChanged,
on_validated_ability_input_received::OnValidatedAbilityInputReceived
},
phase::PhaseType, player::PlayerReference, Game
}, packet::ToClientPacket, vec_map::VecMap, vec_set::VecSet
};

Expand Down Expand Up @@ -62,7 +67,7 @@ impl SavedControllersMap{
}


// new mutators
// mutators
fn update_controllers_from_parameters(game: &mut Game){
let mut new_controller_parameters_map = ControllerParametersMap::default();

Expand Down Expand Up @@ -90,6 +95,75 @@ impl SavedControllersMap{
}
}

pub fn send_selection_message(
game: &mut Game,
player_ref: PlayerReference,
id: ControllerID,
selection: AbilitySelection
){
let chat_message = ChatMessageVariant::AbilityUsed{
player: player_ref.index(),
ability_id: id,
selection: selection.clone()
};

let mut target_message_sent = false;
for insider_group in InsiderGroupID::all_insider_groups_with_player(game, player_ref){
game.add_message_to_chat_group( insider_group.get_insider_chat_group(), chat_message.clone());
target_message_sent = true;
}
if !target_message_sent{
player_ref.add_private_chat_message(game, chat_message);
}
}

/// Keeps old selection if its valid, otherwise uses default_selection,
/// even if default selection is invalid
fn set_controller_parameters(game: &mut Game, new_controller_parameters_map: ControllerParametersMap){

let controller_ids_to_remove = game.saved_controllers.controller_parameters().controller_parameters().keys()
.filter(|id| !new_controller_parameters_map.controller_parameters().contains_key(id))
.cloned()
.collect::<Vec<_>>();

for id in controller_ids_to_remove{
game.saved_controllers.saved_controllers.remove(&id);
}

for (id, controller_parameters) in new_controller_parameters_map.controller_parameters().iter(){
let mut new_selection = controller_parameters.default_selection().clone();

let mut kept_old_selection = false;


if let Some(SavedController{selection: old_selection, ..}) = game.saved_controllers.saved_controllers.get(&id) {
if
controller_parameters.validate_selection(game, old_selection) &&
!controller_parameters.dont_save() &&
!controller_parameters.grayed_out()
{
new_selection = old_selection.clone();
kept_old_selection = true;
}
}


game.saved_controllers.saved_controllers.insert(
id.clone(),
SavedController::new(
new_selection,
controller_parameters.clone()
)
);

if !kept_old_selection {
OnControllerSelectionChanged::new(id.clone()).invoke(game);
}
}

Self::send_saved_controllers_to_clients(game);
}

/// return true if selection was valid
pub fn set_selection_in_controller(
game: &mut Game,
Expand Down Expand Up @@ -125,6 +199,7 @@ impl SavedControllersMap{

if !available_ability_data.dont_save() {
*saved_selection = selection.clone();
OnControllerSelectionChanged::new(id).invoke(game);
}

true
Expand Down Expand Up @@ -286,64 +361,6 @@ impl SavedControllersMap{
)
}

//mutators
/// Keeps old selection if its valid, otherwise uses default_selection,
/// even if default selection is invalid
fn set_controller_parameters(game: &mut Game, new_controller_parameters_map: ControllerParametersMap){

let controller_ids_to_remove = game.saved_controllers.controller_parameters().controller_parameters().keys()
.filter(|id| !new_controller_parameters_map.controller_parameters().contains_key(id))
.cloned()
.collect::<Vec<_>>();

for id in controller_ids_to_remove{
game.saved_controllers.saved_controllers.remove(&id);
}

for (id, controller_parameters) in new_controller_parameters_map.controller_parameters().iter(){
let mut new_selection = controller_parameters.default_selection().clone();

if !controller_parameters.dont_save() && !controller_parameters.grayed_out(){
if let Some(SavedController{selection: old_selection, ..}) = game.saved_controllers.saved_controllers.get(&id) {
if controller_parameters.validate_selection(game, old_selection){
new_selection = old_selection.clone()
}
}
}

game.saved_controllers.saved_controllers.insert(
id.clone(),
SavedController::new(
new_selection,
controller_parameters.clone()
)
);
}

Self::send_saved_controllers_to_clients(game);
}

pub fn send_selection_message(
game: &mut Game,
player_ref: PlayerReference,
id: ControllerID,
selection: AbilitySelection
){
let chat_message = ChatMessageVariant::AbilityUsed{
player: player_ref.index(),
ability_id: id,
selection: selection.clone()
};

let mut target_message_sent = false;
for insider_group in InsiderGroupID::all_insider_groups_with_player(game, player_ref){
game.add_message_to_chat_group( insider_group.get_insider_chat_group(), chat_message.clone());
target_message_sent = true;
}
if !target_message_sent{
player_ref.add_private_chat_message(game, chat_message);
}
}


// game stuff
Expand Down
22 changes: 21 additions & 1 deletion server/src/game/components/mafia.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rand::seq::SliceRandom;

use crate::{game::{
ability_input::{AbilitySelection, AvailableAbilitySelection, ControllerID, ControllerParametersMap, PlayerListSelection}, attack_power::AttackPower, chat::{ChatGroup, ChatMessageVariant}, grave::GraveKiller, phase::PhaseType, player::PlayerReference, role::{Priority, RoleState}, role_list::RoleSet, visit::Visit, Game
ability_input::{AbilitySelection, AvailableAbilitySelection, ControllerID, ControllerParametersMap, PlayerListSelection}, attack_power::AttackPower, chat::{ChatGroup, ChatMessageVariant}, grave::GraveKiller, phase::PhaseType, player::PlayerReference, role::{Priority, RoleState}, role_list::RoleSet, tag::Tag, visit::Visit, Game
}, vec_set::{vec_set, VecSet}};

use super::{detained::Detained, insider_group::InsiderGroupID, night_visits::NightVisits, syndicate_gun_item::SyndicateGunItem};
Expand Down Expand Up @@ -150,6 +150,26 @@ impl Mafia{
}
}

pub fn on_controller_selection_changed(game: &mut Game, controller_id: ControllerID){
if controller_id != ControllerID::syndicate_choose_backup() {return};

let backup =
game.saved_controllers.get_controller_current_selection_player_list(controller_id)
.map(|b|b.0.first().cloned())
.flatten();


for player_ref in PlayerReference::all_players(game){
if !InsiderGroupID::Mafia.is_player_in_revealed_group(game, player_ref) {continue}
player_ref.remove_player_tag_on_all(game, Tag::GodfatherBackup);
}
if let Some(backup) = backup{
for player_ref in PlayerReference::all_players(game){
if !InsiderGroupID::Mafia.is_player_in_revealed_group(game, player_ref) {continue}
player_ref.push_player_tag(game, backup, Tag::GodfatherBackup);
}
}
}

/// - This must go after rolestate on any death
/// - Godfathers backup should become godfather if godfather dies as part of the godfathers ability
Expand Down
1 change: 1 addition & 0 deletions server/src/game/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod on_remove_role_label;
pub mod before_initial_role_creation;
pub mod on_ability_input_received;
pub mod on_validated_ability_input_received;
pub mod on_controller_selection_changed;
pub mod on_tick;


Expand Down
16 changes: 16 additions & 0 deletions server/src/game/event/on_controller_selection_changed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::game::{
ability_input::ControllerID, components::mafia::Mafia, Game
};

#[must_use = "Event must be invoked"]
pub struct OnControllerSelectionChanged{
id: ControllerID,
}
impl OnControllerSelectionChanged{
pub fn new(id: ControllerID) -> Self{
Self{id}
}
pub fn invoke(self, game: &mut Game){
Mafia::on_controller_selection_changed(game, self.id);
}
}
Loading

0 comments on commit ee0e52d

Please sign in to comment.