From: Ian Jackson Date: Sat, 11 Jul 2020 22:44:03 +0000 (+0100) Subject: gamestate.rs: reorg nfc X-Git-Tag: otter-0.2.0~1383 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=6cb1eb604a93c2faa10d7b999eaec1138da37f38;p=otter.git gamestate.rs: reorg nfc --- diff --git a/src/gamestate.rs b/src/gamestate.rs index 3478217c..9b5c26eb 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -1,6 +1,8 @@ use crate::imports::*; +// ---------- newtypes and type aliases ---------- + slotmap::new_key_type!{ pub struct PieceId; } @@ -10,21 +12,64 @@ slotmap::new_key_type!{ #[serde(transparent)] pub struct Generation (pub u64); -impl Generation { - pub fn increment(&mut self) { self.0 += 1 } +visible_slotmap_key!{ VisiblePieceId('.') } + +#[derive(Debug,Copy,Clone,PartialEq,PartialOrd)] +#[derive(Serialize,Deserialize)] +#[serde(into="f64")] +#[serde(try_from="f64")] +pub struct ZCoord(f64); + +// ---------- general data types ---------- + +#[derive(Debug,Copy,Clone,Serialize,Deserialize,Eq,PartialEq,Ord,PartialOrd)] +pub struct ZLevel { + pub z: ZCoord, + pub zg: Generation, } -impl Display for Generation { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - Display::fmt(&self.0,f) - } + +// ---------- game state ---------- + +#[derive(Debug)] +pub struct GameState { + pub pieces : DenseSlotMap, + pub players : DenseSlotMap, + pub gen : Generation, + pub log : Vec<(Generation, Arc)>, } -visible_slotmap_key!{ VisiblePieceId('.') } +#[derive(Debug)] +pub struct PieceRecord { + pub pos : Pos, + pub p : Box, + pub face : FaceId, + pub held : Option, + pub zlevel : ZLevel, + pub gen : Generation, + pub lastclient : ClientId, + pub gen_before_lastclient : Generation, +} -pub fn make_pieceid_visible(p : PieceId) -> VisiblePieceId { - // xxx need to do censorship mapping here - let kd : slotmap::KeyData = p.into(); - VisiblePieceId(kd) +#[derive(Debug)] +pub struct Player { + pub nick : String, +} + +#[derive(Debug,Serialize)] +pub struct LogEntry { + pub html : String, +} + +// ---------- piece trait, and rendering ---------- + +pub trait Piece : Send + Debug { + fn svg_piece(&self, pri : &PieceRenderInstructions) -> String; + fn svg_select(&self, pri : &PieceRenderInstructions) -> String; + fn svg_x_ids(&self) -> VisiblePieceIdSvgIds; + fn svg_x_defs(&self, pri : &PieceRenderInstructions) -> String; + fn thresh_dragraise(&self, pri : &PieceRenderInstructions) + -> Option; + fn describe_html(&self, face : Option) -> String; } #[derive(Debug,Copy,Clone)] @@ -42,21 +87,19 @@ impl PieceRenderInstructions { pub fn id_x(&self, w : &str) -> String { format!("def.{}.{}", self.id, w) } } -pub trait Piece : Send + Debug { - fn svg_piece(&self, pri : &PieceRenderInstructions) -> String; - fn svg_select(&self, pri : &PieceRenderInstructions) -> String; - fn svg_x_ids(&self) -> VisiblePieceIdSvgIds; - fn svg_x_defs(&self, pri : &PieceRenderInstructions) -> String; - fn thresh_dragraise(&self, pri : &PieceRenderInstructions) - -> Option; - fn describe_html(&self, face : Option) -> String; +// ========== implementations ========== + +// ---------- simple data types ---------- + +impl Generation { + pub fn increment(&mut self) { self.0 += 1 } +} +impl Display for Generation { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + Display::fmt(&self.0,f) + } } -#[derive(Debug,Copy,Clone,PartialEq,PartialOrd)] -#[derive(Serialize,Deserialize)] -#[serde(into="f64")] -#[serde(try_from="f64")] -pub struct ZCoord(f64); impl TryFrom for ZCoord { type Error = OnlineError; #[throws(OnlineError)] @@ -80,23 +123,7 @@ impl Display for ZCoord { } } -#[derive(Debug,Copy,Clone,Serialize,Deserialize,Eq,PartialEq,Ord,PartialOrd)] -pub struct ZLevel { - pub z: ZCoord, - pub zg: Generation, -} - -#[derive(Debug)] -pub struct PieceRecord { - pub pos : Pos, - pub p : Box, - pub face : FaceId, - pub held : Option, - pub zlevel : ZLevel, - pub gen : Generation, - pub lastclient : ClientId, - pub gen_before_lastclient : Generation, -} +// ---------- game state ---------- impl PieceRecord { pub fn make_defs(&self, pri : &PieceRenderInstructions) -> String { @@ -134,22 +161,12 @@ impl PieceRecord { } } -#[derive(Debug)] -pub struct GameState { - pub pieces : DenseSlotMap, - pub players : DenseSlotMap, - pub gen : Generation, - pub log : Vec<(Generation, Arc)>, -} - -#[derive(Debug)] -pub struct Player { - pub nick : String, -} +// ========== ad-hoc and temporary ========== -#[derive(Debug,Serialize)] -pub struct LogEntry { - pub html : String, +pub fn make_pieceid_visible(p : PieceId) -> VisiblePieceId { + // xxx need to do censorship mapping here + let kd : slotmap::KeyData = p.into(); + VisiblePieceId(kd) } pub fn xxx_gamestate_init() -> GameState {