From d4b419ecf193cb667f7f5f35dee8a51064aa699d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 18 Apr 2022 12:02:11 +0100 Subject: [PATCH] Elide some fields from save game state where we can This saves writing out the field names as well as just the vslues. We would like to do this for the updates sent to the client, but that would really depend on the JS nullable coalesce operator "??" which is not available the version of typescript I'm using, nor our ES compatibility target. Newer typescript would do a polyfill but that maybe wouldn't be super-fast? So postpone that. Signed-off-by: Ian Jackson --- src/gamestate.rs | 9 ++++++++- src/spec.rs | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/gamestate.rs b/src/gamestate.rs index 83a5f2ab..a957382a 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -63,9 +63,12 @@ pub struct GPlayer { // usual variable: gpl #[derive(Debug,Serialize,Deserialize)] pub struct GPiece { // usual variable: gpc pub pos: Pos, + #[serde(default, skip_serializing_if="is_default")] pub face: FaceId, + #[serde(default, skip_serializing_if="is_default")] pub held: Option, pub zlevel: ZLevel, + #[serde(default, skip_serializing_if="is_default")] pub pinned: bool, pub occult: PieceOccult, pub angle: PieceAngle, @@ -73,6 +76,7 @@ pub struct GPiece { // usual variable: gpc pub lastclient: ClientId, #[serde(default)] pub last_released: ClientId, pub gen_before_lastclient: Generation, + #[serde(default, skip_serializing_if="Option::is_none")] pub xdata: PieceXDataState, pub moveable: PieceMoveable, #[serde(default)] pub rotateable: bool, @@ -292,9 +296,12 @@ pub type PieceSpecLoadedOccultable = /// /// These remain constant after the piece has been loaded, /// so they are mostly "can/do we do this thing". -#[derive(Debug,Clone,Default,Serialize,Deserialize)] +#[derive(Debug,Clone,ConstDefault,Default,Serialize,Deserialize)] pub struct PieceSpecialProperties { + #[serde(default, skip_serializing_if="Option::is_none")] pub rendering: Option, + + #[serde(default, skip_serializing_if="is_default")] pub multigrab: bool, } diff --git a/src/spec.rs b/src/spec.rs index 47e211ec..7c270133 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -14,6 +14,7 @@ use std::hash::Hash; use std::convert::TryFrom; use std::time::Duration; +use const_default::ConstDefault; use enum_map::Enum; use fehler::{throw,throws}; use index_vec::{define_index_type, IndexVec}; @@ -42,7 +43,7 @@ pub struct RawToken(pub String); pub type RawFaceId = u8; define_index_type! { - #[derive(Default)] + #[derive(Default,ConstDefault)] pub struct FaceId = RawFaceId; IMPL_RAW_CONVERSIONS = true; } -- 2.30.2