chiark / gitweb /
Elide some fields from save game state where we can
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 18 Apr 2022 11:02:11 +0000 (12:02 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 18 Apr 2022 12:30:43 +0000 (13:30 +0100)
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 <ijackson@chiark.greenend.org.uk>
src/gamestate.rs
src/spec.rs

index 83a5f2aba92608c0303af755db445c703d9fcf72..a957382a27a7ed385aba1d00151ccfc44c044d8b 100644 (file)
@@ -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<PlayerId>,
   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<SpecialClientRendering>,
+
+  #[serde(default, skip_serializing_if="is_default")]
   pub multigrab: bool,
 }
 
index 47e211ec24cd2f80033f7b024c1a669d631830bd..7c27013342a5a1356e5253a32aeb69449e10b09b 100644 (file)
@@ -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;
 }