From: Ian Jackson Date: Sun, 26 Jul 2020 15:08:41 +0000 (+0100) Subject: finish rework X-Git-Tag: otter-0.2.0~1245 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=cd3d8a042ad851e72bed07b5a75ba1f22a15b5e9;p=otter.git finish rework result of this and previous commit is to break out PrepareUpdatesBuffer code --- diff --git a/src/api.rs b/src/api.rs index b99e1eac..59245137 100644 --- a/src/api.rs +++ b/src/api.rs @@ -14,11 +14,11 @@ trait ApiPieceOp : Debug { #[throws(GameError)] fn op(&self, gs: &mut GameState, player: PlayerId, piece: PieceId, lens: &dyn Lens /* used for LogEntry and PieceId but not Pos */) - -> (PieceUpdateOp<(),()>, Vec); + -> (PieceUpdateOp<()>, Vec); } pub trait Lens { - fn visible_pieceid(&self, piece: PieceId) -> VisiblePieceId; + fn pieceid2visible(&self, piece: PieceId) -> VisiblePieceId; fn log_pri(&self, piece: PieceId, pc: &PieceState) -> PieceRenderInstructions; fn svg_pri(&self, piece: PieceId, pc: &PieceState, player: PlayerId) @@ -29,13 +29,13 @@ pub trait Lens { } struct TransparentLens { } impl Lens for TransparentLens { - fn visible_pieceid(&self, piece: PieceId) -> VisiblePieceId { + fn pieceid2visible(&self, piece: PieceId) -> VisiblePieceId { let kd : slotmap::KeyData = piece.into(); - VisiblePieceId(kd); + VisiblePieceId(kd) } fn log_pri(&self, piece: PieceId, pc: &PieceState) -> PieceRenderInstructions { - let id = self.make_piece_visible(piece); + let id = self.pieceid2visible(piece); PieceRenderInstructions { id, face : pc.face } } fn svg_pri(&self, piece: PieceId, pc: &PieceState, _player: PlayerId) @@ -92,8 +92,8 @@ fn api_piece_op(form : Json>) let mut buf = PrepareUpdatesBuffer::new(g, client, form.cseq, 1 + logents.len()); - buf.piece_update(piece, update, &lens); - buf.log_updates(logents); + buf.piece_update(piece, update, &lens)?; + buf.log_updates(logents)?; eprintln!("API {:?} OK", &form); } @@ -114,7 +114,7 @@ impl ApiPieceOp for ApiPieceGrab { #[throws(GameError)] fn op(&self, gs: &mut GameState, player: PlayerId, piece: PieceId, lens: &dyn Lens) - -> (PieceUpdateOp<(),()>, Vec) { + -> (PieceUpdateOp<()>, Vec) { let pl = gs.players.byid(player).unwrap(); let pc = gs.pieces.byid_mut(piece).unwrap(); @@ -146,7 +146,7 @@ impl ApiPieceOp for ApiPieceUngrab { #[throws(GameError)] fn op(&self, gs: &mut GameState, player: PlayerId, piece: PieceId, lens: &dyn Lens) - -> (PieceUpdateOp<(),()>, Vec) { + -> (PieceUpdateOp<()>, Vec) { let pl = gs.players.byid(player).unwrap(); let pc = gs.pieces.byid_mut(piece).unwrap(); @@ -179,7 +179,7 @@ impl ApiPieceOp for ApiPieceRaise { #[throws(GameError)] fn op(&self, gs: &mut GameState, _: PlayerId, piece: PieceId, _: &dyn Lens) - -> (PieceUpdateOp<(),()>, Vec) { + -> (PieceUpdateOp<()>, Vec) { let pc = gs.pieces.byid_mut(piece).unwrap(); pc.zlevel = ZLevel { z : self.z, zg : gs.gen }; let update = PieceUpdateOp::SetZLevel(pc.zlevel); @@ -198,7 +198,7 @@ impl ApiPieceOp for ApiPieceMove { #[throws(GameError)] fn op(&self, gs: &mut GameState, _: PlayerId, piece: PieceId, _lens: &dyn Lens) - -> (PieceUpdateOp<(),()>, Vec) { + -> (PieceUpdateOp<()>, Vec) { let pc = gs.pieces.byid_mut(piece).unwrap(); pc.pos = self.0; diff --git a/src/gamestate.rs b/src/gamestate.rs index a3517c49..ce6633e3 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -163,7 +163,6 @@ impl PieceState { pub fn prep_piecestate(&self, pri : &PieceRenderInstructions) -> PreparedPieceState { PreparedPieceState { - piece : pri.id, pos : self.pos, held : self.held, svg : self.make_defs(pri)?, diff --git a/src/updates.rs b/src/updates.rs index f17fc7a2..abcffdfa 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -29,14 +29,14 @@ pub enum PreparedUpdateEntry { Piece { client : ClientId, sameclient_cseq : ClientSequence, - op : PieceUpdateOp, + piece : VisiblePieceId, + op : PieceUpdateOp, }, Log (Arc), } #[derive(Debug,Serialize)] pub struct PreparedPieceState { - pub piece : VisiblePieceId, pub pos : Pos, pub svg : String, pub held : Option, @@ -47,12 +47,12 @@ pub struct PreparedPieceState { // ---------- piece updates ---------- #[derive(Debug,Serialize)] -pub enum PieceUpdateOp { - Delete(ID), +pub enum PieceUpdateOp { + Delete(), Insert(NS), Modify(NS), - Move(ID,Pos), - SetZLevel(ID,ZLevel), + Move(Pos), + SetZLevel(ZLevel), } // ---------- for traansmission ---------- @@ -71,7 +71,8 @@ enum TransmitUpdateEntry<'u> { zg : Option, }, Piece { - op : &'u PieceUpdateOp, + piece : VisiblePieceId, + op : &'u PieceUpdateOp, }, Log (&'u LogEntry), } @@ -110,59 +111,43 @@ impl PreparedUpdateEntry { // ---------- PieceUpdatesOp ---------- -impl PieceUpdateOp { +impl PieceUpdateOp { pub fn new_state(&self) -> Option<&NS> { use PieceUpdateOp::*; match self { - Delete(_) => None, + Delete() => None, Insert(ns) => Some(ns), Modify(ns) => Some(ns), - Move(..) => None, - SetZLevel(..) => None, + Move(_) => None, + SetZLevel(_) => None, } } - pub fn try_map Result, - NSF: FnOnce(NS) -> Result> - (self, f:NSF, idf:IDF) -> Result,E> + pub fn try_map_new_state Result> + (self, f:F) -> Result,E> { use PieceUpdateOp::*; Ok(match self { - Delete(i) => Delete(idf(i)?), + Delete() => Delete(), Insert(ns) => Insert(f(ns)?), Modify(ns) => Modify(f(ns)?), - Move(i,pos) => Move(idf(i)?,pos), - SetZLevel(i,zl) => SetZLevel(idf(i)?,zl), + Move(pos) => Move(pos), + SetZLevel(zl) => SetZLevel(zl), }) } - pub fn map ID2, - NSF: FnOnce(NS) -> NS2> - (self, nsf:NSF, idf:IDF) -> PieceUpdateOp - { - #[derive(Error,Debug)] enum Never { } - self.try_map( - |ns| >::Ok(nsf(ns)), - |id| >::Ok(idf(id)), - ).unwrap() + pub fn map_new_state NS2>(self, f:F) + -> PieceUpdateOp { + #[derive(Error,Debug)] + enum Never { } + self.try_map_new_state(|ns| >::Ok(f(ns))).unwrap() } pub fn new_z_generation(&self) -> Option { use PieceUpdateOp::*; match self { - Delete(_) => None, + Delete() => None, Insert(_) => None, Modify(_) => None, - Move(..) => None, - SetZLevel(_,ZLevel{zg,..}) => Some(*zg), - } - } - pub fn pieceid<'ns>(&'ns self) -> ID where &'ns NS : Into, ID : Copy { - use PieceUpdateOp::*; - match self { - Delete(i) => *i, - Insert(ns) | Modify(ns) => ns.into(), - Move(i,_) => *i, - SetZLevel(i,_) => *i, + Move(_) => None, + SetZLevel(ZLevel{zg,..}) => Some(*zg), } } } @@ -187,12 +172,13 @@ impl<'r> PrepareUpdatesBuffer<'r> { } } - pub fn piece_update(&mut self, piece: PieceId, update: PieceUpdateOp<(),()>, + #[throws(SVGProcessingError)] + pub fn piece_update(&mut self, piece: PieceId, update: PieceUpdateOp<()>, lens: &dyn Lens) { let gs = &mut self.g.gs; - let update = match gs.pieces.byid_mut(piece) { - Some(pc) => { + let (update, piece) = match gs.pieces.byid_mut(piece) { + Ok(pc) => { if self.by_client != pc.lastclient { pc.gen_before_lastclient = pc.gen; pc.lastclient = self.by_client; @@ -202,31 +188,33 @@ impl<'r> PrepareUpdatesBuffer<'r> { let pri_for_all = lens.svg_pri(piece,pc,Default::default()); - let update = update.try_map( + let update = update.try_map_new_state( |_|{ let mut ns = pc.prep_piecestate(&pri_for_all)?; lens.massage_prep_piecestate(&mut ns); >::Ok(ns) }, - |_|{ - >::Ok(pri_for_all.id) - }, )?; - update + (update, pri_for_all.id) }, - None => { - PieceUpdateOp::Delete(lens.make_piece_visible(piece)) + Err(GameError::PieceGone) => { + (PieceUpdateOp::Delete(), lens.pieceid2visible(piece)) + } + Err(e) => { + panic!(format!("unexpected error {:?} from pices.byid_mut", &e)); } }; self.us.push(PreparedUpdateEntry::Piece { + piece, client : self.by_client, sameclient_cseq : self.cseq, op : update, }); } + #[throws(SVGProcessingError)] pub fn log_updates(&mut self, logents: Vec) { for logentry in logents { let logentry = Arc::new(logentry); @@ -238,7 +226,10 @@ impl<'r> PrepareUpdatesBuffer<'r> { impl<'r> Drop for PrepareUpdatesBuffer<'r> { fn drop(&mut self) { - let update = PreparedUpdate { gen: self.gen, us: self.us.take(), }; + let update = PreparedUpdate { + gen: self.gen, + us: mem::take(&mut self.us), + }; let update = Arc::new(update); eprintln!("UPDATE {:?}", &update);