-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
429 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{selection_type::{ | ||
one_player_option_selection::{AvailableOnePlayerOptionSelection, OnePlayerOptionSelection}, role_option_selection::{AvailableRoleOptionSelection, RoleOptionSelection}, two_player_option_selection::{AvailableTwoPlayerOptionSelection, TwoPlayerOptionSelection}, two_role_option_selection::{AvailableTwoRoleOptionSelection, TwoRoleOptionSelection}, two_role_outline_option_selection::{AvailableTwoRoleOutlineOptionSelection, TwoRoleOutlineOptionSelection}, BooleanSelection | ||
}, ValidateAvailableSelection}; | ||
|
||
|
||
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord)] | ||
#[serde(rename_all = "camelCase")] | ||
#[serde(tag="type")] | ||
pub enum AbilitySelection{ | ||
Unit, | ||
BooleanSelection{selection: BooleanSelection}, | ||
OnePlayerOption{selection: OnePlayerOptionSelection}, | ||
TwoPlayerOption{selection: TwoPlayerOptionSelection}, | ||
RoleOption{selection: RoleOptionSelection,}, | ||
TwoRoleOption{selection: TwoRoleOptionSelection}, | ||
TwoRoleOutlineOption{selection: TwoRoleOutlineOptionSelection}, | ||
} | ||
|
||
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
#[serde(rename_all = "camelCase")] | ||
#[serde(tag="type")] | ||
pub enum AvailableAbilitySelection{ | ||
Unit, | ||
BooleanSelection, | ||
OnePlayerOption{selection: AvailableOnePlayerOptionSelection}, | ||
TwoPlayerOption{selection: AvailableTwoPlayerOptionSelection}, | ||
RoleOption{selection: AvailableRoleOptionSelection}, | ||
TwoRoleOption{selection: AvailableTwoRoleOptionSelection}, | ||
TwoRoleOutlineOption{selection: AvailableTwoRoleOutlineOptionSelection}, | ||
} | ||
impl ValidateAvailableSelection for AvailableAbilitySelection{ | ||
type Selection = AbilitySelection; | ||
|
||
fn validate_selection(&self, selection: &Self::Selection)->bool { | ||
match self { | ||
Self::Unit => {true}, | ||
Self::BooleanSelection => {true}, | ||
Self::OnePlayerOption{ selection: available } => { | ||
let AbilitySelection::OnePlayerOption{selection} = selection else {return false}; | ||
return available.validate_selection(selection); | ||
}, | ||
Self::TwoPlayerOption{ selection: available } => { | ||
let AbilitySelection::TwoPlayerOption{selection} = selection else {return false}; | ||
return available.validate_selection(selection); | ||
}, | ||
Self::RoleOption{ selection: available } => { | ||
let AbilitySelection::RoleOption{selection} = selection else {return false}; | ||
return available.validate_selection(selection); | ||
}, | ||
Self::TwoRoleOption{ selection: available } => { | ||
let AbilitySelection::TwoRoleOption{selection} = selection else {return false}; | ||
return available.validate_selection(selection); | ||
}, | ||
Self::TwoRoleOutlineOption{ selection: available } => { | ||
let AbilitySelection::TwoRoleOutlineOption{selection} = selection else {return false}; | ||
return available.validate_selection(selection); | ||
}, | ||
} | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
server/src/game/ability_input/common_selection/role_option_selection.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
use crate::{game::{chat::ChatMessageVariant, components::insider_group::InsiderGroupID, phase::PhaseType, player::PlayerReference, Game}, packet::ToClientPacket, vec_map::VecMap}; | ||
|
||
use super::{ability_selection::{AbilitySelection, AvailableAbilitySelection}, AbilityID, AbilityInput, AvailableAbilityInput, ValidateAvailableSelection}; | ||
|
||
|
||
|
||
//actual component | ||
#[derive(Default)] | ||
struct SavedAbilityInput{ | ||
save: VecMap<AbilityID, (Option<AbilitySelection>, AvailableAbilitySelection)> | ||
} | ||
impl SavedAbilityInput{ | ||
fn new(save: VecMap<AbilityID, (Option<AbilitySelection>, AvailableAbilitySelection)>)->Self{ | ||
Self{save} | ||
} | ||
fn set_ability_save(&mut self, id: AbilityID, selection: Option<AbilitySelection>, available: AvailableAbilitySelection){ | ||
let Some(selection) = selection else { | ||
self.save.insert(id, (None, available)); | ||
return; | ||
}; | ||
|
||
if available.validate_selection(&selection) { | ||
self.save.insert(id, (Some(selection), available)); | ||
}else{ | ||
|
||
self.save.insert(id, (None, available)); | ||
} | ||
} | ||
} | ||
|
||
|
||
//all players components | ||
#[derive(Default)] | ||
pub struct SavedAbilityInputs{ | ||
players_saved_inputs: VecMap<PlayerReference, SavedAbilityInput> | ||
} | ||
|
||
|
||
|
||
impl SavedAbilityInputs{ | ||
pub fn on_ability_input_received( | ||
game: &mut Game, | ||
actor: PlayerReference, | ||
ability_input: AbilityInput | ||
){ | ||
|
||
for (id, incoming_selection) in ability_input.abilities{ | ||
|
||
let Some(saved_ability_input) = | ||
game.saved_ability_inputs.players_saved_inputs.get_mut(&actor) else {return}; | ||
|
||
let Some((saved_selection, available_selection)) = | ||
saved_ability_input.save.get_mut(&id) else {continue}; | ||
|
||
|
||
|
||
if | ||
!available_selection.validate_selection(&incoming_selection) || | ||
saved_selection.as_ref().is_some_and(|s| *s == incoming_selection) | ||
{ | ||
continue; | ||
} | ||
|
||
*saved_selection = Some(incoming_selection.clone()); | ||
|
||
let out_packet = ToClientPacket::YourSavedAbilityInput{selection: | ||
saved_ability_input.save | ||
.iter() | ||
.filter_map(|(id, (selection, _))|{ | ||
if let Some(selection) = selection{ | ||
Some((id.clone(), selection.clone())) | ||
}else{ | ||
None | ||
} | ||
}) | ||
.collect() | ||
}; | ||
|
||
actor.send_packet(game, out_packet); | ||
Self::send_selection_message(game, actor, id, incoming_selection); | ||
} | ||
} | ||
|
||
|
||
|
||
pub fn on_phase_start(game: &mut Game, phase: PhaseType){ | ||
if phase != PhaseType::Obituary {return} | ||
game.saved_ability_inputs.players_saved_inputs.clear(); | ||
for player in PlayerReference::all_players(game){ | ||
player.send_packet(game, ToClientPacket::YourSavedAbilityInput { selection: vec![].into_iter().collect() }); | ||
} | ||
} | ||
|
||
pub fn on_tick(game: &mut Game){ | ||
for player in PlayerReference::all_players(game){ | ||
let new_available_selection = | ||
player.role_state(game).clone().available_ability_input(game, player); | ||
|
||
let current = Self::current_available_ability_input(game, player); | ||
|
||
if | ||
current.is_none() || | ||
current.is_some_and(|c| c != new_available_selection) | ||
{ | ||
Self::set_available_ability_input(game, player, new_available_selection); | ||
} | ||
} | ||
} | ||
|
||
|
||
fn set_available_ability_input( | ||
game: &mut Game, | ||
player: PlayerReference, | ||
available_selection: AvailableAbilityInput | ||
){ | ||
|
||
if let Some(player_saved_input) = game.saved_ability_inputs.players_saved_inputs.get_mut(&player){ | ||
|
||
for (id, available_selection) in available_selection.abilities{ | ||
|
||
let selection = player_saved_input.save | ||
.get(&id) | ||
.map(|x| x.0.clone()) | ||
.flatten(); | ||
|
||
player_saved_input.set_ability_save(id, selection, available_selection); | ||
} | ||
}else{ | ||
game.saved_ability_inputs.players_saved_inputs.insert(player, SavedAbilityInput::new( | ||
available_selection.abilities | ||
.iter() | ||
.map(|(id, available_selection)| (id.clone(), (None, available_selection.clone()))) | ||
.collect() | ||
)); | ||
} | ||
|
||
|
||
|
||
|
||
let Some(player_saved_input) = game.saved_ability_inputs.players_saved_inputs.get(&player) else {return}; | ||
|
||
player.send_packet(game, ToClientPacket::YourSavedAbilityInput{selection: | ||
player_saved_input.save | ||
.iter() | ||
.filter_map(|(id, (selection, _))|{ | ||
if let Some(selection) = selection{ | ||
Some((id.clone(), selection.clone())) | ||
}else{ | ||
None | ||
} | ||
}) | ||
.collect() | ||
}); | ||
|
||
player.send_packet(game, ToClientPacket::YourAvailableAbilityInput{selection: | ||
player_saved_input.save | ||
.iter() | ||
.map(|(id, (_, available))|{ | ||
(id.clone(), available.clone()) | ||
}) | ||
.collect() | ||
}); | ||
} | ||
|
||
fn current_available_ability_input( | ||
game: &Game, | ||
player_ref: PlayerReference | ||
)->Option<AvailableAbilityInput>{ | ||
game.saved_ability_inputs.players_saved_inputs | ||
.get(&player_ref) | ||
.map(|data| | ||
AvailableAbilityInput::new( | ||
data.save | ||
.iter() | ||
.map(|(id, (_, available_selection))| | ||
(id.clone(), available_selection.clone()) | ||
) | ||
.collect() | ||
) | ||
) | ||
} | ||
|
||
|
||
pub fn send_selection_message( | ||
game: &mut Game, | ||
player_ref: PlayerReference, | ||
id: AbilityID, | ||
selection: AbilitySelection | ||
){ | ||
let chat_message = ChatMessageVariant::AbilityUsed{ | ||
player: player_ref.index(), | ||
role: Some(player_ref.role(game)), | ||
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); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.