From 5c7f4f46b87569213aa1947102bd8b6cc5bdc232 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 2 Apr 2021 22:54:40 +0100 Subject: [PATCH] movehist: Abbreviate many names Now this is a module, this is nicer. Signed-off-by: Ian Jackson --- daemon/main.rs | 6 ++--- daemon/session.rs | 2 +- src/gamestate.rs | 4 +-- src/movehist.rs | 64 +++++++++++++++++++++++------------------------ src/prelude.rs | 3 --- src/updates.rs | 4 +-- 6 files changed, 40 insertions(+), 43 deletions(-) diff --git a/daemon/main.rs b/daemon/main.rs index e5b831aa..84c46ca8 100644 --- a/daemon/main.rs +++ b/daemon/main.rs @@ -124,9 +124,9 @@ fn loading(layout: Option, ia: PlayerQueryString) game: g.name.to_string(), ptoken: &ia.raw_token, debug_js_inject: config().debug_js_inject.clone(), - movehist_lens: JsonString(MOVEHIST_LENS), - movehist_len_i: MOVEHIST_LEN_DEF_I, - movehist_len_max: MOVEHIST_LEN_MAX, + movehist_lens: JsonString(movehist::LENS), + movehist_len_i: movehist::LEN_DEF_I, + movehist_len_max: movehist::LEN_MAX, layout, }; Template::render("loading", &c) diff --git a/daemon/session.rs b/daemon/session.rs index 38223588..29af6336 100644 --- a/daemon/session.rs +++ b/daemon/session.rs @@ -24,7 +24,7 @@ struct SessionRenderContext { links: Html, player_info_pane: Html, fake_rng: bool, - movehist: GMoveHist, + movehist: movehist::PlHist, } #[derive(Debug,Serialize)] diff --git a/src/gamestate.rs b/src/gamestate.rs index 5a0aabec..f58c83ff 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -55,8 +55,8 @@ pub struct GPlayer { // usual variable: gpl pub nick: String, pub layout: PresentationLayout, pub idmap: PerPlayerIdMap, - #[serde(default)] pub moveheld: GMoveHeld, - #[serde(default)] pub movehist: GMoveHist, + #[serde(default)] pub movehist: movehist::PlHist, + #[serde(default)] pub moveheld: movehist::PlHeld, } #[derive(Debug,Serialize,Deserialize)] diff --git a/src/movehist.rs b/src/movehist.rs index 75e2c7cd..77dfe2c2 100644 --- a/src/movehist.rs +++ b/src/movehist.rs @@ -4,78 +4,78 @@ use super::*; // we are otter::updates::movehist -pub const MOVEHIST_LENS: &[usize] = &[ 0, 1, 3, 10 ]; -pub const MOVEHIST_LEN_MAX: usize = 10; -pub const MOVEHIST_LEN_DEF_I: usize = 1; -pub const MOVEHIST_MIN_DIST: f64 = 7.5; +pub const LENS: &[usize] = &[ 0, 1, 3, 10 ]; +pub const LEN_MAX: usize = 10; +pub const LEN_DEF_I: usize = 1; +pub const MIN_DIST: f64 = 7.5; #[test] fn movehist_lens() { assert_eq!( - MOVEHIST_LENS.iter().max(), - Some(&MOVEHIST_LEN_MAX), + LENS.iter().max(), + Some(&LEN_MAX), ); - assert!( MOVEHIST_LENS.get(MOVEHIST_LEN_DEF_I).is_some() ); - assert_eq!( MOVEHIST_LENS.iter().cloned().fold(None, |b, i| { + assert!( LENS.get(LEN_DEF_I).is_some() ); + assert_eq!( LENS.iter().cloned().fold(None, |b, i| { let i = Some(i); assert!(i > b); i }), - Some(MOVEHIST_LEN_MAX) ); + Some(LEN_MAX) ); } #[derive(Debug,Copy,Clone,Serialize,Deserialize)] -pub struct MoveHistPosx { // usual variable: posx +pub struct Posx { // usual variable: posx pub pos: Pos, pub angle: CompassAngle, pub facehint: Option, } #[derive(Debug,Clone,Serialize,Deserialize)] -pub struct MoveHistEnt { +pub struct Ent { pub held: PlayerId, - pub posx: OldNew, - pub diff: MoveHistDiffToShow, + pub posx: OldNew, + pub diff: DiffToShow, } #[derive(Debug,Copy,Clone,Serialize,Deserialize)] -pub enum MoveHistDiffToShow { +pub enum DiffToShow { Moved { d: f64 }, } #[derive(Debug,Clone,Serialize,Deserialize)] -pub struct GMoveHist { - pub hist: VecDeque, +pub struct PlHist { + pub hist: VecDeque, } // ---------- non-public structs ---------- #[derive(Debug,Clone,Serialize,Deserialize,Default)] -pub struct GMoveHeld { - held: SparseSecondaryMap, +pub struct PlHeld { + held: SparseSecondaryMap, } #[derive(Debug,Copy,Clone,Serialize,Deserialize)] -struct GMoveHistLast { +struct PlHistLast { held: PlayerId, - posx: MoveHistPosx, + posx: Posx, } -impl Default for GMoveHist { - fn default() -> GMoveHist { GMoveHist { - hist: VecDeque::with_capacity(MOVEHIST_LEN_MAX), +impl Default for PlHist { + fn default() -> PlHist { PlHist { + hist: VecDeque::with_capacity(LEN_MAX), } } } -impl MoveHistPosx { - fn differs_significantly(&self, other: &MoveHistPosx) - -> Option { - use MoveHistDiffToShow as D; +impl Posx { + fn differs_significantly(&self, other: &Posx) + -> Option { + use DiffToShow as D; match (|| Ok::<_,CoordinateOverflow> ({ (self.pos - other.pos)?.len()? }))() { - Ok(d) if d >= MOVEHIST_MIN_DIST as f64 => return Some(D::Moved{ d }), + Ok(d) if d >= MIN_DIST as f64 => return Some(D::Moved{ d }), _ => {}, } @@ -96,7 +96,7 @@ pub fn peek_prep_update(gs: &mut GameState, peek: &PreparedUpdateEntry) if let Some(gpl) = wants!( gs.players.get_mut(player), ?player); if let Some(mut ent) = wants!( gpl.moveheld.held.entry(piece), ?piece); let &PreparedPieceState { pos, angle, facehint, .. } = ns; - let new_posx = MoveHistPosx { pos, angle, facehint }; + let new_posx = Posx { pos, angle, facehint }; then { if let slotmap::sparse_secondary::Entry::Occupied(ref mut oe) = ent { @@ -105,12 +105,12 @@ pub fn peek_prep_update(gs: &mut GameState, peek: &PreparedUpdateEntry) if let Some(diff) = new_posx.differs_significantly(&last.posx) { // Generate an update - let histent = MoveHistEnt { + let histent = Ent { held: last.held, posx: OldNew::from([last.posx, new_posx]), diff, }; - if gpl.movehist.hist.len() == MOVEHIST_LEN_MAX { + if gpl.movehist.hist.len() == LEN_MAX { gpl.movehist.hist.pop_front(); } gpl.movehist.hist.push_back(histent.clone()); @@ -119,7 +119,7 @@ pub fn peek_prep_update(gs: &mut GameState, peek: &PreparedUpdateEntry) } if let Some(held) = ns.held { - ent.insert(GMoveHistLast { held: held, posx: new_posx }); + ent.insert(PlHistLast { held: held, posx: new_posx }); } else { ent.remove(); } diff --git a/src/prelude.rs b/src/prelude.rs index 784fba5c..caeeaf04 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -145,9 +145,6 @@ pub use crate::sse; pub use crate::toml_de; pub use crate::tz::*; pub use crate::updates::*; -pub use crate::updates::movehist::{self, GMoveHeld, GMoveHist, MoveHistEnt}; -pub use crate::updates::movehist::{MOVEHIST_LENS, MOVEHIST_LEN_MAX}; -pub use crate::updates::movehist::{MOVEHIST_LEN_DEF_I}; pub use crate::utils::*; pub use crate::ui::*; diff --git a/src/updates.rs b/src/updates.rs index e939268b..828e14c7 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -56,7 +56,7 @@ pub struct PreparedUpdate { pub enum PreparedUpdateEntry { Piece(PreparedUpdateEntry_Piece), Image(PreparedUpdateEntry_Image), - MoveHistEnt(SecondarySlotMap), + MoveHistEnt(SecondarySlotMap), SetTableSize(Pos), SetTableColour(Colour), SetLinks(Arc), @@ -223,7 +223,7 @@ enum TransmitUpdateEntry<'u> { }, Piece(TransmitUpdateEntry_Piece<'u>), Image(TransmitUpdateEntry_Image<'u>), - MoveHistEnt(&'u MoveHistEnt), + MoveHistEnt(&'u movehist::Ent), RecordedUnpredictable { piece: VisiblePieceId, cseq: ClientSequence, -- 2.30.2