Encapsulate player

This commit is contained in:
Wiktor Mazur
2019-12-25 14:14:58 +01:00
parent bef54dee9b
commit 75238241e6
2 changed files with 15 additions and 7 deletions
+5 -5
View File
@@ -30,7 +30,7 @@ impl Session {
fn from(session: &Self) -> Self { fn from(session: &Self) -> Self {
let mut players = HashMap::new(); let mut players = HashMap::new();
for (player_name, player) in &session.players { for (player_name, player) in &session.players {
let mut new_player = Player::new(player.id, player.main); let mut new_player = Player::new(player.id(), player.main());
if let CombatState::InCombat = player.combat_state() { if let CombatState::InCombat = player.combat_state() {
new_player.enter_combat(); new_player.enter_combat();
} }
@@ -59,7 +59,7 @@ impl Session {
fame_per_hour: player.fame_per_hour(), fame_per_hour: player.fame_per_hour(),
items: player.items(), items: player.items(),
idle: player.idle(), idle: player.idle(),
main_player_stats: player.main, main_player_stats: player.main(),
}) })
.collect(), .collect(),
) )
@@ -69,7 +69,7 @@ impl Session {
let without_dmg = self let without_dmg = self
.players .players
.iter() .iter()
.filter(|(_, player)| !player.main && player.damage() == 0.0 && player.fame() == 0.0) .filter(|(_, player)| !player.main() && player.damage() == 0.0 && player.fame() == 0.0)
.map(|(name, _)| name.clone()) .map(|(name, _)| name.clone())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
for w in without_dmg { for w in without_dmg {
@@ -82,7 +82,7 @@ impl Session {
} }
fn get_player_by_id(&mut self, player_id: usize) -> Option<&mut Player> { fn get_player_by_id(&mut self, player_id: usize) -> Option<&mut Player> {
self.players.values_mut().find(|p| p.id == player_id) self.players.values_mut().find(|p| p.id() == player_id)
} }
fn add_player(&mut self, player_name: &str, player_id: usize, main: bool) { fn add_player(&mut self, player_name: &str, player_id: usize, main: bool) {
@@ -125,7 +125,7 @@ impl Meter {
fn stats_filter(&self, player: &(&String, &Player)) -> bool { fn stats_filter(&self, player: &(&String, &Player)) -> bool {
if self.config.skip_non_party_members { if self.config.skip_non_party_members {
let is_main_player = if let Some(id) = &self.main_player_id { *id == player.1.id } else { false }; let is_main_player = if let Some(id) = &self.main_player_id { *id == player.1.id() } else { false };
let is_in_party = if let Some(party) = &self.party { party.includes(&player.0) } else { false }; let is_in_party = if let Some(party) = &self.party { party.includes(&player.0) } else { false };
return is_main_player || is_in_party; return is_main_player || is_in_party;
+10 -2
View File
@@ -30,7 +30,7 @@ impl CombatTime {
#[derive(Debug)] #[derive(Debug)]
pub struct Player { pub struct Player {
pub id: usize, id: usize,
damage_dealt: f32, damage_dealt: f32,
combat_time: CombatTime, combat_time: CombatTime,
combat_state: CombatState, combat_state: CombatState,
@@ -38,7 +38,7 @@ pub struct Player {
fame: f32, fame: f32,
items: Items, items: Items,
idle: bool, idle: bool,
pub main: bool main: bool
} }
@@ -60,6 +60,14 @@ impl Player {
pub fn idle(&self) -> bool { pub fn idle(&self) -> bool {
self.idle self.idle
} }
pub fn id(&self) -> usize {
self.id
}
pub fn main(&self) -> bool {
self.main
}
} }
impl DamageDealer for Player { impl DamageDealer for Player {