chiark / gitweb /
wip initial data from server
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Jun 2020 20:32:11 +0000 (21:32 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Jun 2020 20:32:11 +0000 (21:32 +0100)
src/bin/server.rs
src/gamestate.rs
src/pieces.rs
templates/test.tera

index f0f1c2e520e2d2b79d4ec3c677b904b4dab5daf3..634f936e50e93168167b8cdf34c632625bc33aea 100644 (file)
@@ -53,6 +53,7 @@ fn loading(token : InstanceAccess) -> Result<Template,RE> {
 #[derive(Serialize,Debug)]
 struct SessionRenderContext {
   clientid : u64,
+  defs : Vec<String>,
 }
 
 #[derive(Deserialize)]
@@ -68,7 +69,21 @@ fn session(form : Json<SessionForm>) -> Result<Template,RE> {
     let user = g.users.get_mut(iad.user).ok_or_else(|| anyhow!("user deleted"))?;
     let client = Client { };
     let clientid : slotmap::KeyData = user.clients.insert(client).into();
-    SessionRenderContext { clientid : clientid.as_ffi() }
+
+    let mut defs = vec![];
+    for (gpid, pr) in &g.gs.pieces {
+      let id : slotmap::KeyData = gpid.into();
+      let pri = PieceRenderInstructions {
+        id : id.as_ffi(),
+        face : pr.face,
+      };
+      defs.push(pr.p.svg_defs(&pri));
+    }
+
+    SessionRenderContext {
+      clientid : clientid.as_ffi(),
+      defs,
+    }
   };
   Ok(Template::render("test",&c))
 }
@@ -111,7 +126,7 @@ fn updates(token : &RawStr, clientid : u64) -> impl response::Responder<'static>
   let _ = {
     let mut g = iad.i.lock().map_err(|e| anyhow!("lock poison {:?}",&e))?;
     let user = g.users.get_mut(iad.user).ok_or_else(|| anyhow!("user deleted"))?;
-    let client = user.clients.get_mut(clientid).ok_or_else(|| anyhow!("client deleted"))?;
+    let _client = user.clients.get_mut(clientid).ok_or_else(|| anyhow!("client deleted"))?;
   };
   let tc = TestCounterInner { next : 0 };
   let tc = BufReader::new(tc);
index bca051fa89844dc4930419c72fb650c3ea9ddf4a..ceb2d67b0d3bf3ee4e8abc8f0c61f27b67631da0 100644 (file)
@@ -5,15 +5,23 @@ slotmap::new_key_type!{
   pub struct PieceId;
 }
 
+type VisiblePieceId = u64;
+
+pub struct PieceRenderInstructions {
+  pub id : VisiblePieceId,
+  pub face : FaceId,
+}
+
 pub trait Piece : Send + Debug {
+  fn svg_defs(&self, pri : &PieceRenderInstructions) -> String;
 }
 
 #[derive(Debug)]
 pub struct PieceRecord {
-  pos : Pos,
-  p : Box<dyn Piece>,
-  face : FaceId,
-  held : Option<UserId>,
+  pub pos : Pos,
+  pub p : Box<dyn Piece>,
+  pub face : FaceId,
+  pub held : Option<UserId>,
 }
 
 #[derive(Debug)]
index 4fc36f5f4cf68cf4e92b70f3e26fa360f12b9d9c..3c3d1c99116996a7512fc13d48153e9e36fbfe29 100644 (file)
@@ -12,16 +12,19 @@ struct SimpleShape {
 }
 
 impl Piece for SimpleShape {
+  fn svg_defs(&self, pri : &PieceRenderInstructions) -> String {
+    format!(r#"<g id=base{}>{}</g>"#, pri.id, self.shape)
+  }
 }
 
 pub fn xxx_make_pieces() -> Vec<(Pos, Box<dyn Piece>)> {
   vec![
-    ([ 50, 80 ],
+    ([ 90, 80 ],
      Box::new(SimpleShape {
        shape : r#"<circle cx="0" cy="0" r="10"/>"#.to_owned(),
        colours : index_vec![ "red".to_string(), "grey".to_string() ],
      })),
-    ([ 50, 60 ],
+    ([ 90, 60 ],
      Box::new(SimpleShape {
        shape : r#"<rect x="-10" y="-10" width="20" height="20"/>"#.to_owned(),
        colours : index_vec![ "blue".to_string(), "grey".to_string() ],
index 8e1980b561b470eac61288095f234b05719b6cc4..ff9a676aa1459ceffd2b0b756b5e2d998c5dba9a 100644 (file)
@@ -14,6 +14,7 @@
       <use href="#piece43" data-p="43" x="50" y="60" />
       <use href="#select43" x="50" y="60" />
       <defs>
+{% for piece in defs %}{{piece}}{% endfor %}
        <g id="piece42">
          <circle
            cx="0" cy="0"