chiark / gitweb /
hand generalisation: Introduce Sort
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 30 Mar 2021 19:04:55 +0000 (20:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 30 Mar 2021 19:05:14 +0000 (20:05 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/hand.rs
src/spec.rs

index 3bdf39ba0665e9deffec58cf42eab0ba43d21213..53f7c1c1556f01db38437922da5fa84fdbb199ed 100644 (file)
@@ -17,6 +17,7 @@ struct MagicOwner {
 struct Hand {
   shape: GenericSimpleShape<(), RectShape>,
   label: Option<PieceLabelLoaded>,
+  #[serde(default="Sort::backcompat_upgrade")] sort: Sort,
 }
 
 #[derive(Debug,Clone,Default,Serialize,Deserialize)]
@@ -24,6 +25,18 @@ struct HandState {
   owner: Option<MagicOwner>,
 }
 
+#[derive(Debug,Copy,Clone,Serialize,Deserialize)]
+enum Sort {
+  Hand,
+}
+
+impl Sort {
+  fn backcompat_upgrade() -> Self { Sort::Hand }
+  fn itemname(self) -> &'static str { use Sort::*; match self {
+    Hand => "magic-hand",
+  } }
+}
+
 impl HandState {
   fn player(&self) -> Option<PlayerId> {
     self.owner.as_ref().map(|o| o.player)
@@ -46,12 +59,9 @@ impl OutlineTrait for Hand {
   }
 }
 
-#[typetag::serde]
-impl PieceSpec for piece_specs::Hand {
+impl piece_specs::OwnedCommon {
   #[throws(SpecError)]
-  fn load(&self, _: usize, _: &mut GPiece,
-          _pcaliases: &PieceAliases, _ir: &InstanceRef)
-          -> PieceSpecLoaded {
+  fn load(&self, sort: Sort) -> PieceSpecLoaded {
     let common = SimpleCommon {
       itemname: None,
       faces: index_vec![self.colour.clone()],
@@ -65,16 +75,26 @@ impl PieceSpec for piece_specs::Hand {
     let shape = GenericSimpleShape::new(
       (),
       shape,
-      "magic-hand",
+      sort.itemname(),
       &common)?;
     let p = Box::new(Hand {
-      shape,
+      shape, sort,
       label: self.label.load()?,
     }) as Box<dyn PieceTrait>;
     PieceSpecLoaded { p, occultable: None }
   }
 }
 
+#[typetag::serde]
+impl PieceSpec for piece_specs::Hand {
+  #[throws(SpecError)]
+  fn load(&self, _: usize, _: &mut GPiece,
+          _pcaliases: &PieceAliases, _ir: &InstanceRef)
+          -> PieceSpecLoaded {
+    self.c.load(Sort::Hand)?
+  }
+}
+
 impl Hand {
   fn describe_html_inner(&self, xdata: Option<&HandState>) -> Html {
     if_chain! {
index 164193c822291cefefe2ec499c234cd24d35e3c7..c93b2f2cb4ed27ad28cbda8237884f6d2a14ba04 100644 (file)
@@ -241,6 +241,11 @@ pub mod piece_specs {
 
   #[derive(Debug,Serialize,Deserialize)]
   pub struct Hand {
+    #[serde(flatten)] pub c: OwnedCommon,
+  }
+
+  #[derive(Debug,Serialize,Deserialize)]
+  pub struct OwnedCommon {
     pub colour: ColourSpec,
     pub edge: Option<ColourSpec>,
     pub edge_width: Option<f64>,