chiark / gitweb /
total reorg, does not work any more
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Jul 2020 23:29:49 +0000 (00:29 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Jul 2020 23:29:49 +0000 (00:29 +0100)
src/bin/server.rs
src/gamestate.rs
src/global.rs
src/sse.rs
src/updates.rs

index 0da139fffb646b8d194cbad63bc882afaa906497..bcd0d126a2cf44f8583c6714c048fd9647a9a9ee 100644 (file)
@@ -152,16 +152,15 @@ fn api_grab(form : Json<ApiGrab>) -> impl response::Responder<'static> {
       id : vpiece, 
       face : p.face,
     };
-    let json = UpdatePayload::PieceUpdate(vpiece, p.mk_update(&pri));
-    let json = serde_json::to_string(&json).expect("convert to json");
+    let op = PieceUpdateOp::Modify(p.prep_piecestate(&pri));
     let update = PreparedUpdate {
       gen,
-      u : PreparedPieceUpdate {
+      us : vec![ PreparedUpdateEntry::Piece {
         client,
         sameclient_cseq : form.cseq,
         piece : vpiece,
-        json,
-      },
+        op,
+      }],
     };
     let update = Arc::new(update);
     // split vie wthing would go here, see also update.piece
index 928a0eef85a5c74ff42dee165796f2dad0aa73d0..0855700cefe005bb8dac1e3739d7d5e65b8abaf5 100644 (file)
@@ -61,6 +61,13 @@ pub struct PieceRecord {
   pub gen_before_lastclient : Generation,
 }
 
+#[derive(Debug,Serialize)]
+pub struct PreparedPieceState {
+  pub pos : Pos,
+  pub held : Option<PlayerId>,
+  pub svg : String,
+}
+
 impl PieceRecord {
   pub fn make_defs(&self, pri : &PieceRenderInstructions) -> String {
     let pr = self;
@@ -75,11 +82,12 @@ impl PieceRecord {
     defs
   }
 
-  pub fn mk_update(&self, pri : &PieceRenderInstructions) -> PieceUpdate {
-    PieceUpdate {
+  pub fn prep_piecestate(&self, pri : &PieceRenderInstructions)
+                         -> PreparedPieceState {
+    PreparedPieceState {
       pos        : self.pos,
       held       : self.held,
-      svgs       : self.make_defs(pri),
+      svg        : self.make_defs(pri),
       // xxx want all piece's stuff in the same def
     }
   }
index b90c371f3318bd1b39496480a1344b3c784683a0..8f86452a08b9c97946cb0b6d3aa1bd93d47949e3 100644 (file)
@@ -18,34 +18,61 @@ pub struct Client {
   pub player : PlayerId,
 }
 
-impl Client {
-  pub fn transmit_update(&mut self, u : &Update) {
-    eprintln!("XXX not transmitting {:?}", &u);
-  }
-}
-
 #[derive(Debug)]
 pub struct PreparedUpdate {
   pub gen : Generation,
-  pub u : PreparedUpdatePayload,
+  pub us : Vec<PreparedUpdateEntry>,
 }
 #[derive(Debug)]
-pub enum PreparedUpdatePayload {
-  PreparedPieceUpdate {
+pub enum PreparedUpdateEntry {
+  Piece {
     client : ClientId,
     sameclient_cseq : ClientSequence,
     piece : VisiblePieceId,
-    json : String,
+    op : PieceUpdateOp<PreparedPieceState>,
+  },
+  Log {
   },
 }
-pub use PreparedUpdatePayload::*;
-
-impl PreparedUpdatePayload {
-  pub fn json_len(&self) -> usize { match self {
-    &PreparedPieceUpdate { ref json, .. } => json.len(),
-  } }
+impl PreparedUpdateEntry {
+  pub fn json_len(&self) -> usize {
+    use PreparedUpdateEntry::*;
+    match self {
+      Piece { ref op, .. } => {
+        50 +
+        op.new_state().map(|x| x.svg.len()).unwrap_or(0)
+      },
+      Log { .. } => {
+        todo!("json length of log")
+      }
+    }
+  }
+}
+impl PreparedUpdate {
+  pub fn json_len(&self) -> usize {
+    self.us.iter().map(|u| 20 + u.json_len()).sum()
+  }
 }
 
+#[derive(Debug,Serialize)]
+pub enum PieceUpdateOp<NS> {
+  Delete,
+  Insert(NS),
+  Modify(NS),
+  Move(Pos),
+}
+impl<NS> PieceUpdateOp<NS> {
+  pub fn new_state(&self) -> Option<&NS> {
+    use PieceUpdateOp::*;
+    match self {
+      Delete => None,
+      Insert(ns) => Some(ns),
+      Modify(ns) => Some(ns),
+      Move(_) => None,
+    }
+  }
+}      
+
 #[derive(Debug,Default)]
 pub struct PlayerUpdates {
   pub log : StableIndexVecDeque<Arc<PreparedUpdate>,sse::UpdateId>,
index 49e2c815c1b8695c76ca6c4cd88bebc131c729e9..af6d62b144b37601227e7fea02cb8e67464cef9e 100644 (file)
@@ -46,11 +46,18 @@ struct UpdateReader {
   ami : Arc<Mutex<Instance>>,
 }
 
-#[derive(Serialize)]
-struct RecordedConfirmation {
-  gen : Generation,
-  piece : VisiblePieceId,
-  cseq : ClientSequence,
+#[derive(Debug,Serialize)]
+enum TransmitUpdate<'u> {
+  Recorded {
+    piece : VisiblePieceId,
+    cseq : ClientSequence,
+  },
+  Piece {
+    piece : VisiblePieceId,
+    op : &'u PieceUpdateOp<PreparedPieceState>,
+  },
+  Log {
+  },
 }
 
 #[derive(Error,Debug)]
@@ -81,31 +88,33 @@ impl Read for UpdateReader {
       let next = match pu.log.get(self.to_send) {
         Some(next) => next,  None => { break }
       };
-      let next_len = UPDATE_MAX_FRAMING_SIZE + next.u.json_len();
+      let next_len = UPDATE_MAX_FRAMING_SIZE + next.json_len();
       if next_len > buf.len() { break }
 
-      match &next.u {
-        &PreparedPieceUpdate {
-          piece, client : uclient, sameclient_cseq : cseq, ..
-        } if uclient== self.client => {
-          write!(buf, "event: recorded\n\
-                       data: ")?;
-          serde_json::to_writer(&mut buf, &RecordedConfirmation {
-            gen : next.gen,
-            piece : piece,
-            cseq : cseq,
-          })?;
-          write!(buf, "\n\n")?;
-        },
-        PreparedPieceUpdate { json, .. } => {
-          write!(buf, "id: {}\n\
-                       data: {}\n\n",
-                 &self.to_send,
-                 json)?;
-        },
+      write!(buf, "data: [")?;
+      for u in &next.us {
+        let tu = match u {
+          &PreparedUpdateEntry::Piece
+          { piece, client, sameclient_cseq : cseq, .. }
+          if client== self.client => {
+            TransmitUpdate::Recorded { piece, cseq }
+          },
+          &PreparedUpdateEntry::Piece { piece, ref op, .. } => {
+            TransmitUpdate::Piece { piece, op }
+          },
+          PreparedUpdateEntry::Log { } => {
+            TransmitUpdate::Log { }
+          },
+        };
+        serde_json::to_writer(&mut buf, &tu)?;
+        write!(buf,",")?;
       }
-      self.to_send.try_increment().unwrap();
+      serde_json::to_writer(&mut buf, &next.gen)?;
+      write!(buf, "]\n\
+                   id: {}\n\n",
+             &self.to_send)?;
     }
+
     loop {
       let generated = orig_wanted - buf.len();
       if generated > 0 {
index 872cd8ecfe470e7723ba25316b1df5d53fd8ce6c..0d1b6d8a49197268db101862c59046e78317c0ff 100644 (file)
@@ -4,7 +4,7 @@ use crate::imports::*;
 #[derive(Debug,Copy,Clone,Eq,PartialEq,Deserialize,Serialize)]
 #[serde(transparent)]
 pub struct ClientSequence(u64);
-
+/*
 #[derive(Debug,Serialize)]
 pub struct Update {
   pub gen : Generation,
@@ -12,18 +12,6 @@ pub struct Update {
 }
 
 #[derive(Debug,Serialize)]
-pub struct PieceUpdate {
-  pub pos : Pos,
-  pub held : Option<PlayerId>,
-  pub svgs : String,
-}
-
-#[derive(Debug,Serialize)]
-pub enum UpdatePayload {
-  NoUpdate,
-  ClientSequence(PieceId, ClientSequence),
-  PieceDelete(VisiblePieceId),
-  PieceInsert(VisiblePieceId, PieceUpdate),
-  PieceUpdate(VisiblePieceId, PieceUpdate),
-  PieceMove(VisiblePieceId, Pos),
+pub enum PieceUpdate {
 }
+*/