From: Ian Jackson Date: Sun, 28 Jun 2020 19:28:02 +0000 (+0100) Subject: wip update protocol X-Git-Tag: otter-0.2.0~1507 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=7bf27d047db1fdfb01aaec8d4d3f0ad45e74c120;p=otter.git wip update protocol --- diff --git a/src/bin/server.rs b/src/bin/server.rs index 9e6af503..3aa291e0 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -142,7 +142,7 @@ fn api_grab(form : Json) -> impl response::Responder<'static> { let u_gen = if client == p.lastclient { p.gen_lastclient } else { p.gen_before_lastclient }; - if p.gen > u_gen { Err(OpError::Conflict)? } + if u_gen > form.g { Err(OpError::Conflict)? } if p.held != None { Err(OpError::PieceHeld)? }; p.held = Some(iad.player); gs.gen += 1; @@ -153,16 +153,16 @@ fn api_grab(form : Json) -> impl response::Responder<'static> { } p.gen_lastclient = gen; for (tplayer, tpl) in g.gs.players { - for (tclient, cl) in ig.clients.get(tplayer) { + for (tclient, cl) in g.clients.get(tplayer) { if tclient == cl { cl.transmit_update(client, Update { gen, - u : GameUpdate::ClientSequence(form.s) + u : UpdatePayload::ClientSequence(form.s), }); } else { cl.transmit_update(client, Update { gen, - u : GameUpdate::PieceUpdate(p, p.update()), + u : UpdatePayload::PieceUpdate(p, p.update()), }); } } diff --git a/src/gamestate.rs b/src/gamestate.rs index 3d07b1c6..89cccf50 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -5,7 +5,7 @@ slotmap::new_key_type!{ pub struct PieceId; } -type Counter = u64; +pub type Counter = u64; visible_slotmap_key!{ VisiblePieceId('.') } @@ -72,7 +72,9 @@ pub fn xxx_gamestate_init() -> GameState { pos, p, face : 0.into(), held : None, - gen : 0, + lastclient : Default::default(), + gen_lastclient : 0, + gen_before_lastclient : 0, }; pieces.insert(pr); } diff --git a/src/imports.rs b/src/imports.rs index f960998a..4b5693b6 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -35,6 +35,7 @@ pub use crate::global::*; pub use crate::gamestate::*; pub use crate::pieces::*; pub use crate::keydata::*; +pub use crate::updates::*; pub type E = anyhow::Error; pub type AE = anyhow::Error; diff --git a/src/lib.rs b/src/lib.rs index 5096c9ee..57f7e6f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,3 +4,4 @@ pub mod global; pub mod pieces; pub mod gamestate; pub mod keydata; +pub mod updates; diff --git a/src/updates.rs b/src/updates.rs index 674ad79c..1f7b8c50 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -1,13 +1,32 @@ -struct Update { - gen : Counter, - u : UpdatePayload, +use crate::imports::*; + +#[derive(Debug,Deserialize,Serialize)] +#[serde(transparent)] +pub struct ClientSequence(String); + +#[derive(Debug,Serialize)] +pub struct Update { + pub gen : Counter, + pub u : UpdatePayload, +} + +#[derive(Debug,Serialize)] +pub struct PieceUpdate { + pub pos : Pos, + pub held : Option, + pub svg_piece : String, + pub svg_select : String, + pub svg_x_ids : VisiblePieceIdSvgIds, + pub svg_defs : String, } -enum UpdatePayload { +#[derive(Debug,Serialize)] +pub enum UpdatePayload { NoUpdate, - ClientSequence(ClientCounter), + ClientSequence(ClientSequence), PieceDelete(PieceId), PieceInsert(PieceId, PieceUpdate), PieceUpdate(PieceId, PieceUpdate), + PieceMove(PieceId, Pos), }