chiark / gitweb /
wip logging
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 6 Jul 2020 14:15:17 +0000 (15:15 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 6 Jul 2020 14:15:17 +0000 (15:15 +0100)
src/bin/server.rs
src/gamestate.rs
src/global.rs
src/pieces.rs
src/sse.rs
templates/script.js

index 508c28397341c8aeabddaa55be3a28b82b3aa8e1..0f21f7dd62710c59c4b122681dfe79584659d64e 100644 (file)
@@ -130,10 +130,13 @@ fn api_grab(form : Json<ApiGrab>) -> impl response::Responder<'static> {
   let cl = &g.clients.byid(client)?;
   // ^ can only fail if we raced
   let player = cl.player;
+  let pl = g.gs.players.byid(player)?;
+  let g_updates = &mut g.updates;
+  let gs_pieces = &mut g.gs.pieces;
+  let gs_gen = &mut g.gs.gen;
   let r : Result<(),GameError> = (||{
     let piece = decode_visible_pieceid(form.piece);
-    let gs = &mut g.gs;
-    let p = gs.pieces.byid_mut(piece)?;
+    let p = gs_pieces.byid_mut(piece)?;
     let q_gen = form.gen;
     let u_gen =
       if client == p.lastclient { p.gen_lastclient }
@@ -141,8 +144,8 @@ fn api_grab(form : Json<ApiGrab>) -> impl response::Responder<'static> {
     if u_gen > q_gen { Err(GameError::Conflict)? }
     if p.held != None { Err(GameError::PieceHeld)? };
     p.held = Some(player);
-    gs.gen.increment();
-    let gen = gs.gen;
+    gs_gen.increment();
+    let gen = *gs_gen;
     if client != p.lastclient {
       p.gen_before_lastclient = p.gen_lastclient;
       p.lastclient = client;
@@ -155,18 +158,28 @@ fn api_grab(form : Json<ApiGrab>) -> impl response::Responder<'static> {
     let op = PieceUpdateOp::Modify(p.prep_piecestate(&pri));
     let update = PreparedUpdate {
       gen,
-      us : vec![ PreparedUpdateEntry::Piece {
-        client,
-        sameclient_cseq : form.cseq,
-        piece : vpiece,
-        op,
-      }],
+      us : vec![
+        PreparedUpdateEntry::Piece {
+          client,
+          sameclient_cseq : form.cseq,
+          piece : vpiece,
+          op,
+        },
+        PreparedUpdateEntry::Log {
+          msg : Arc::new(format!("{} grasped {}",
+                                 &htmlescape::encode_minimal(&pl.nick),
+                                 p.describe_html(&pri)
+                                 // split view: pri should be global
+                                 // (currently log is one global view)
+          )),
+        },
+      ],
     };
     let update = Arc::new(update);
     eprintln!("UPDATE {:?}", &update);
     // split vie wthing would go here, see also update.piece
     p.gen_lastclient = gen;
-    for (_tplayer, tplupdates) in &mut g.updates {
+    for (_tplayer, tplupdates) in g_updates {
       tplupdates.log.push_back(update.clone());
       tplupdates.cv.notify_all();
     }
index 0855700cefe005bb8dac1e3739d7d5e65b8abaf5..5e7704b943ba822b8c982f3cb47be7a1b8356a64 100644 (file)
@@ -48,6 +48,7 @@ pub trait Piece : Send + Debug {
   fn svg_select(&self, pri : &PieceRenderInstructions) -> String;
   fn svg_x_ids(&self) -> VisiblePieceIdSvgIds;
   fn svg_x_defs(&self, pri : &PieceRenderInstructions) -> String;
+  fn describe_html(&self, face : Option<FaceId>) -> String;
 }
 
 #[derive(Debug)]
@@ -91,6 +92,11 @@ impl PieceRecord {
       // xxx want all piece's stuff in the same def
     }
   }
+
+  pub fn describe_html(&self, pri : &PieceRenderInstructions) -> String {
+    // xxx want to be able to hide things
+    self.p.describe_html(Some(pri.face))
+  }
 }
 
 #[derive(Debug)]
index 8f86452a08b9c97946cb0b6d3aa1bd93d47949e3..a049b9681fa31abb3a094d6eac96b2a6bed3d437 100644 (file)
@@ -32,6 +32,7 @@ pub enum PreparedUpdateEntry {
     op : PieceUpdateOp<PreparedPieceState>,
   },
   Log {
+    msg : Arc<String>,
   },
 }
 impl PreparedUpdateEntry {
@@ -42,8 +43,8 @@ impl PreparedUpdateEntry {
         50 +
         op.new_state().map(|x| x.svg.len()).unwrap_or(0)
       },
-      Log { .. } => {
-        todo!("json length of log")
+      Log { msg } => {
+        msg.as_bytes().len() * 3
       }
     }
   }
index 4f59851ad5824aaf92ce9c3d27601184934691fb..4c6cf4c325425a80ead73776f2cadc2658cc4431 100644 (file)
@@ -7,6 +7,7 @@ define_index_type! {
 
 #[derive(Debug)]
 struct SimpleShape {
+  desc : String,
   shape : String,
   colours : IndexVec<FaceId,Colour>,
 }
@@ -28,17 +29,26 @@ impl Piece for SimpleShape {
   fn svg_x_defs(&self, pri : &PieceRenderInstructions) -> String {
     format!(r#"<g id={}>{}</g>"#, pri.id_x("base"), self.shape)
   }
+  fn describe_html(&self, face : Option<FaceId>) -> String {
+    if let Some(face) = face {
+      format!("a {} {}", self.colours[face], self.desc)
+    } else {
+      format!("a {}", self.desc)
+    }
+  }
 }
 
 pub fn xxx_make_pieces() -> Vec<(Pos, Box<dyn Piece>)> {
   vec![
     ([ 90, 80 ],
      Box::new(SimpleShape {
+       desc : "circle".to_owned(),
        shape : r#"<circle cx="0" cy="0" r="10"/>"#.to_owned(),
        colours : index_vec![ "red".to_string(), "grey".to_string() ],
      })),
     ([ 90, 60 ],
      Box::new(SimpleShape {
+       desc : "square".to_owned(),
        shape : r#"<rect x="-10" y="-10" width="20" height="20"/>"#.to_owned(),
        colours : index_vec![ "blue".to_string(), "grey".to_string() ],
      })),
index 6d7b14ef2a3d8463456f80e193ae0d9d6347ecc7..936f041a94da5ffb1346f05ab7768e9ae652ff5b 100644 (file)
@@ -57,6 +57,7 @@ enum TransmitUpdate<'u> {
     op : &'u PieceUpdateOp<PreparedPieceState>,
   },
   Log {
+    msg : &'u str,
   },
 }
 
@@ -102,8 +103,8 @@ impl Read for UpdateReader {
           &PreparedUpdateEntry::Piece { piece, ref op, .. } => {
             TransmitUpdate::Piece { piece, op }
           },
-          PreparedUpdateEntry::Log { } => {
-            TransmitUpdate::Log { }
+          PreparedUpdateEntry::Log { msg } => {
+            TransmitUpdate::Log { msg }
           },
         };
         serde_json::to_writer(&mut buf, &tu)?;
@@ -131,6 +132,8 @@ impl Read for UpdateReader {
         return Err(io::Error::new(io::ErrorKind::WouldBlock,
                                   FlushWouldBlockError{}));
       }
+      // xxx this endless stream is a leak
+      // restart it occasionally
 
       amig = cv.wait_timeout(amig, UPDATE_KEEPALIVE)
         .map_err(|_| em("poison"))?.0;
index 555705870de9fc0b8f84648493bf607da289c8c5..194873413b7299a9cad8f641c29e8b2832a6bafd 100644 (file)
@@ -213,7 +213,7 @@ function drag_cancel() {
 
 // ----- logs -----
 
-messages.LogUpdate = function(data) {
+messages.Log = function(j) {
   lastent = logdiv.lastElementChild;
   in_scrollback =
     // inspired by
@@ -223,7 +223,7 @@ messages.LogUpdate = function(data) {
     lastent.getBoundingClientRect().bottom >
     logdiv.getBoundingClientRect().bottom;
 
-  console.log('LOG UPDATE ',in_scrollback,data);
+  console.log('LOG UPDATE ',in_scrollback, j);
 
   if (!in_scrollback) {
     lastent = logdiv.lastElementChild;