chiark / gitweb /
api: Introduce and use ApiPieceOpArgs::pri
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 18 Apr 2022 12:08:44 +0000 (13:08 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 18 Apr 2022 12:08:44 +0000 (13:08 +0100)
We're going to have a new call site, too.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/api.rs
src/gamestate.rs

index c5e0850f6b8e6af4f98af03792cdc9eba86cb7f8..206b9a709f96e7ca9d9d6b9d97f83e8a028f55ca 100644 (file)
@@ -320,7 +320,8 @@ api_route!{
 
   impl op::Simple as {
     #[throws(ApiPieceOpError)]
-    fn op(&self, a: ApiPieceOpArgs) -> PieceUpdate {
+    fn op(&self, mut a: ApiPieceOpArgs) -> PieceUpdate {
+      let pri = a.pri()?;
       let ApiPieceOpArgs { gs,ioccults,player,piece,ipc, .. } = a;
       let gpc = gs.pieces.byid_mut(piece)?;
       let players = &mut gs.players;
@@ -329,8 +330,6 @@ api_route!{
       let was = was.map(|was| htmlescape::encode_minimal(&was.nick));
 
       let gpl = players.byid_mut(player)?;
-      let pri = piece_pri(ioccults, &gs.occults, player, gpl, piece, gpc, ipc)
-        .ok_or(Ia::PieceGone)?;
 
       let pcs = pri.describe(ioccults,&gs.occults, gpc, ipc);
 
@@ -575,13 +574,9 @@ api_route!{
   impl op::Complex as {
     #[throws(ApiPieceOpError)]
     fn op_complex(&self, mut a: ApiPieceOpArgs) -> UpdateFromOpComplex {
+      let pri = a.pri()?;
       let ApiPieceOpArgs { ioccults,player,piece,ipc, .. } = a;
       let gs = &mut a.gs;
-      let pri = piece_pri(ioccults, &gs.occults,
-                          player, gs.players.byid_mut(player)?,
-                          piece, gs.pieces.byid(piece)?,
-                          ipc)
-        .ok_or(Ia::PieceGone)?;
       let y = pri.fully_visible().ok_or(Ia::Occultation)?;
 
       '_normal_global_ops__not_loop: loop {
index 9f2f33996a7673f68ed694e5fd41f6ac3711f050..71d6af678239f25923ed7cf824d2614643543fa3 100644 (file)
@@ -396,6 +396,19 @@ impl<T:Debug> From<PosOffTableError<T>> for MgmtError {
   fn from(pote: PosOffTableError<T>) -> MgmtError { ME::BadSpec(pote.into()) }
 }
 
+// ---------- ApiPieceOpArgs ----------
+
+impl ApiPieceOpArgs<'_> {
+  #[throws(ApiPieceOpError)]
+  pub fn pri(&mut self) -> PieceRenderInstructions {
+    let ApiPieceOpArgs { gs,ioccults,player,piece,ipc, .. } = self;
+    let gpc = gs.pieces.byid(*piece)?;
+    let gpl = gs.players.byid_mut(*player)?;
+    piece_pri(ioccults, &gs.occults, *player, gpl, *piece, gpc, ipc)
+      .ok_or(Ia::PieceGone)?
+  }
+}
+
 // ---------- game state - rendering etc. ----------
 
 impl GPiece {