255 lines
8.0 KiB
Rust
255 lines
8.0 KiB
Rust
//! Implementation of all needed traits for the Entity struct
|
|
mod from_impl;
|
|
use std::hash::Hash;
|
|
|
|
use crate::output::symbol;
|
|
use crate::{
|
|
Displayable,
|
|
entity::Entity,
|
|
epoch::{EpochInfo, epoch_traits::Diffable},
|
|
stats::{self, Symbols},
|
|
};
|
|
use colored::*;
|
|
use uuid::Uuid; // [3] - colored-rs examples
|
|
mod accesors;
|
|
mod methods;
|
|
|
|
// ---------- Implementation ----------
|
|
impl Displayable for Entity {
|
|
// Detailed multi-line view (used e.g. in logs)
|
|
fn print_full(&self, epoch_info: &EpochInfo) {
|
|
println!("{}", self.to_pretty_tree(epoch_info),);
|
|
if let Ok(c) = self.stats_to_table() {
|
|
println!("{c}");
|
|
}
|
|
}
|
|
|
|
/// One-liner used for auto-generation of `Vec<T>` displays
|
|
// TODO: URGENT: Change this display
|
|
fn oneline_display(&self) -> String {
|
|
let capital = self
|
|
.metadata
|
|
.capital
|
|
.as_ref()
|
|
.map(|c| format!("Capital: {}{} ", symbol(" ", ""), c.to_symbol().blue()))
|
|
.unwrap_or_default();
|
|
|
|
let governor = self
|
|
.metadata
|
|
.governor
|
|
.as_ref()
|
|
.map(|g| format!("Governor: {}{}", symbol(" ", ""), g.to_symbol().blue()))
|
|
.unwrap_or_default();
|
|
|
|
let prestige = if *self.metadata.prestige > 0 {
|
|
format!(
|
|
" {:2}{}",
|
|
self.metadata.prestige,
|
|
symbol(" ", " prestige").yellow()
|
|
)
|
|
} else {
|
|
String::new()
|
|
};
|
|
|
|
// ---- build the two lines ----
|
|
let line1 = format!(
|
|
"├──┬─ {} {:23} {} ──────> {} {}",
|
|
self.id.to_string().split_at(7).0.blue(),
|
|
self.name.green(),
|
|
self.entity_type.to_symbol(),
|
|
self.metadata.position.to_symbol(),
|
|
self.duration.to_symbol().yellow(),
|
|
);
|
|
|
|
let (junction, line3) = if !capital.is_empty() | !governor.is_empty() {
|
|
("├", format!("│ └─ {}{}\n", capital, governor,))
|
|
} else {
|
|
("└", "".to_string())
|
|
};
|
|
let line2 = format!(
|
|
"│ {}─ Faction: {} │ Effect: {} {}, Stats: {}{}",
|
|
junction,
|
|
self.metadata.faction.to_symbol(),
|
|
self.effect_nature.to_symbol(),
|
|
self.effect_bounds.to_symbol(),
|
|
self.stats.to_symbol(),
|
|
prestige,
|
|
);
|
|
|
|
format!("{}\n{}\n{}│", line1, line2, line3)
|
|
}
|
|
}
|
|
|
|
/// Short utility to make a symbol for an entity from it's Uuid
|
|
fn uuid_to_symbol(id: Uuid, epoch_info: &EpochInfo, e_type: &stats::EntityType) -> String {
|
|
epoch_info
|
|
.find_entity_strict(id, Some(*e_type))
|
|
.map(|e| e.to_symbol())
|
|
.unwrap_or(format!(
|
|
"{}{} is missing {}",
|
|
symbol(" ", "missing ").red(),
|
|
id.to_symbol().red(),
|
|
e_type.clone().to_shortest().blue()
|
|
))
|
|
}
|
|
|
|
// impl Entity {
|
|
// /// Create a new Event
|
|
// fn new_event(name: String, description: String){}
|
|
// }
|
|
|
|
/// Replace a statistic in `stats` with the value from `pair`.
|
|
///
|
|
/// This function performs a direct overwrite: the existing value is
|
|
/// discarded and the new value from `pair` is written in its place.
|
|
#[inline]
|
|
fn overwrite_stat(
|
|
stats: &mut stats::OverallStats,
|
|
pair: stats::StatPair,
|
|
delta: stats::Modification,
|
|
) {
|
|
match delta {
|
|
stats::Modification::Delta => {
|
|
let affected_stat = stats.get_field_mut(&pair.stat);
|
|
let current_value = *affected_stat;
|
|
*affected_stat = stats::GenericStat(current_value.saturating_add(*pair.value));
|
|
}
|
|
stats::Modification::Replace => {
|
|
let affected_stat = stats.get_field_mut(&pair.stat);
|
|
*affected_stat = pair.value;
|
|
}
|
|
stats::Modification::Delete => {
|
|
let affected_stat = stats.get_field_mut(&pair.stat);
|
|
*affected_stat = stats::GenericStat::default();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Compare a field on `self` with the same field on `other`.
|
|
/// If they are equal → `None`, otherwise → the value from `other`.
|
|
macro_rules! diff_fields {
|
|
// Two arguments: the two objects you want to compare
|
|
($self:ident, $other:ident, $( $field:ident ),* $(,)?) => {
|
|
$(
|
|
let $field = if $self.$field == $other.$field {
|
|
None
|
|
} else {
|
|
$other.$field.clone()
|
|
};
|
|
)*
|
|
};
|
|
}
|
|
impl Diffable for Entity {
|
|
fn delta(&self, other: &Self) -> Self {
|
|
let name = if self.name == other.name {
|
|
self.name.clone()
|
|
} else {
|
|
format!("{}->{}", self.name, other.name)
|
|
};
|
|
Self {
|
|
name,
|
|
stats: self.stats.clone() - other.stats.clone(),
|
|
temp_stats: self.temp_stats.delta(&other.temp_stats),
|
|
metadata: self.metadata.delta(&other.metadata),
|
|
entity_type: other.entity_type,
|
|
duration: other.duration.clone(),
|
|
effect_nature: other.effect_nature.clone(),
|
|
effect_bounds: other.effect_bounds.clone(),
|
|
id: Uuid::new_v4(),
|
|
description: other.description.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Diffable for Option<stats::OverallStats> {
|
|
fn delta(&self, other: &Self) -> Self {
|
|
match (self, other) {
|
|
(None, None) => None,
|
|
(None, Some(a)) => Some(a.clone()),
|
|
(Some(a), None) => Some(a.clone()),
|
|
(Some(a), Some(b)) => Some(a.clone() - b.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Diffable for stats::MetaStats {
|
|
fn delta(&self, other: &Self) -> Self {
|
|
diff_fields!(self, other, capital, governor);
|
|
Self {
|
|
position: self.position.clone() - other.position.clone(),
|
|
capital,
|
|
governor,
|
|
faction: {
|
|
match (self.faction.as_ref(), other.faction.as_ref()) {
|
|
(None, None) => None,
|
|
(None, Some(b)) => Some(*b),
|
|
(Some(a), None) => Some(*a),
|
|
(Some(_), Some(b)) => Some(*b),
|
|
}
|
|
},
|
|
links: self
|
|
.links
|
|
.clone()
|
|
.into_iter()
|
|
.filter(|t1| other.links.iter().any(|t2| t1 == t2))
|
|
.collect(),
|
|
prestige: self.prestige - other.prestige,
|
|
reach: stats::Reach::None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl stats::Symbols for Entity {
|
|
fn to_symbol(&self) -> String {
|
|
format!(
|
|
"{} {} {}",
|
|
self.entity_type.to_symbol().blue(),
|
|
self.id.to_symbol().blue(),
|
|
self.name
|
|
)
|
|
}
|
|
}
|
|
|
|
impl stats::Symbols for Uuid {
|
|
fn to_symbol(&self) -> String {
|
|
format!("[{}]", self.to_string().split_at(7).0)
|
|
}
|
|
|
|
fn to_shortest(&self) -> String {
|
|
self.to_string().split_at(7).0.to_string()
|
|
}
|
|
}
|
|
|
|
impl Hash for Entity {
|
|
/// Massive speedup at the cost of collision on updates, making eq for finding but not true equality
|
|
/// However a .deep_eq() should be used for this
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
// self.name.hash(state);
|
|
// self.description.hash(state);
|
|
self.id.hash(state);
|
|
// self.stats.hash(state);
|
|
// self.temp_stats.hash(state);
|
|
// self.metadata.hash(state);
|
|
// self.entity_type.hash(state);
|
|
// self.duration.hash(state);
|
|
// self.effect_nature.hash(state);
|
|
// self.effect_bounds.hash(state);
|
|
}
|
|
}
|
|
|
|
impl PartialEq for Entity {
|
|
/// Time save and clash if not .deep_eq
|
|
fn eq(&self, other: &Self) -> bool {
|
|
// self.name == other.name
|
|
// && self.description == other.description
|
|
self.id == other.id
|
|
// && self.stats == other.stats
|
|
// && self.temp_stats == other.temp_stats
|
|
// && self.metadata == other.metadata
|
|
// && self.entity_type == other.entity_type
|
|
// && self.duration == other.duration
|
|
// && self.effect_nature == other.effect_nature
|
|
// && self.effect_bounds == other.effect_bounds
|
|
}
|
|
}
|