From: Ian Jackson Date: Mon, 6 Jul 2020 14:15:17 +0000 (+0100) Subject: wip logging X-Git-Tag: otter-0.2.0~1429 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=482d47bd7e13c983a30d4e7af2228a5d980b1a5e;p=otter.git wip logging --- diff --git a/src/bin/server.rs b/src/bin/server.rs index 508c2839..0f21f7dd 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -130,10 +130,13 @@ fn api_grab(form : Json) -> 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) -> 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) -> 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(); } diff --git a/src/gamestate.rs b/src/gamestate.rs index 0855700c..5e7704b9 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -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) -> 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)] diff --git a/src/global.rs b/src/global.rs index 8f86452a..a049b968 100644 --- a/src/global.rs +++ b/src/global.rs @@ -32,6 +32,7 @@ pub enum PreparedUpdateEntry { op : PieceUpdateOp, }, Log { + msg : Arc, }, } 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 } } } diff --git a/src/pieces.rs b/src/pieces.rs index 4f59851a..4c6cf4c3 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -7,6 +7,7 @@ define_index_type! { #[derive(Debug)] struct SimpleShape { + desc : String, shape : String, colours : IndexVec, } @@ -28,17 +29,26 @@ impl Piece for SimpleShape { fn svg_x_defs(&self, pri : &PieceRenderInstructions) -> String { format!(r#"{}"#, pri.id_x("base"), self.shape) } + fn describe_html(&self, face : Option) -> 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)> { vec![ ([ 90, 80 ], Box::new(SimpleShape { + desc : "circle".to_owned(), shape : r#""#.to_owned(), colours : index_vec![ "red".to_string(), "grey".to_string() ], })), ([ 90, 60 ], Box::new(SimpleShape { + desc : "square".to_owned(), shape : r#""#.to_owned(), colours : index_vec![ "blue".to_string(), "grey".to_string() ], })), diff --git a/src/sse.rs b/src/sse.rs index 6d7b14ef..936f041a 100644 --- a/src/sse.rs +++ b/src/sse.rs @@ -57,6 +57,7 @@ enum TransmitUpdate<'u> { op : &'u PieceUpdateOp, }, 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; diff --git a/templates/script.js b/templates/script.js index 55570587..19487341 100644 --- a/templates/script.js +++ b/templates/script.js @@ -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;