Skip to content

Commit

Permalink
move files around
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSammyM committed Nov 27, 2024
1 parent 24b1489 commit 76c733e
Show file tree
Hide file tree
Showing 25 changed files with 429 additions and 365 deletions.
62 changes: 62 additions & 0 deletions server/src/game/ability_input/ability_selection.rs
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);
},
}
}
}

This file was deleted.

69 changes: 45 additions & 24 deletions server/src/game/ability_input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,69 @@
pub mod common_selection;

use common_selection::{
one_player_option_selection::OnePlayerOptionSelection,
role_option_selection::RoleOptionSelection,
two_player_option_selection::TwoPlayerOptionSelection,
two_role_option_selection::TwoRoleOptionSelection,
two_role_outline_option_selection::TwoRoleOutlineOptionSelection,
BooleanSelection
};
pub mod selection_type;
pub mod ability_selection;
pub mod saved_ability_inputs;

use ability_selection::{AbilitySelection, AvailableAbilitySelection};

use serde::{Deserialize, Serialize};
use crate::vec_map::VecMap;

use super::{
event::on_ability_input_received::OnAbilityInputReceived,
player::PlayerReference,
Game
};

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct AbilityInput(AbilityID, AbilitySelection);


pub type RoleAbilityID = u8;
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord)]
#[serde(rename_all = "camelCase")]
#[serde(tag="type")]
pub enum AbilityID{
Role{
role_ability_id: RoleAbilityID,
},
Role{role_ability_id: RoleAbilityID},
ForfeitVote,
PitchforkVote,
SyndicateGunItemShoot,
SyndicateGunItemGive,
}


#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord)]
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)]
pub struct AbilityInput{
abilities: VecMap<AbilityID, AbilitySelection>
}


#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AvailableAbilityInput{
abilities: VecMap<AbilityID, AvailableAbilitySelection>
}
impl AvailableAbilityInput{
pub fn new(abilities: VecMap<AbilityID, AvailableAbilitySelection>)->Self{
Self{abilities}
}
}





pub trait ValidateAvailableSelection{
type Selection;
fn validate_selection(&self, selection: &Self::Selection)->bool;
}







// #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord)]
// #[serde(rename_all = "camelCase", tag="type")]
// pub enum AbilityInput{
Expand Down
207 changes: 207 additions & 0 deletions server/src/game/ability_input/saved_ability_inputs.rs
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);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,4 @@ pub struct BooleanSelection(pub bool);



pub trait AvailableSelection{
type Selection;
fn validate_selection(&self, selection: &Self::Selection)->bool;
}





Loading

0 comments on commit 76c733e

Please sign in to comment.