chiark / gitweb /
currency: Implement op_multigrab and therefore splitting
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 18 Apr 2022 23:01:39 +0000 (00:01 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 30 Apr 2022 14:15:30 +0000 (15:15 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
ng

src/currency.rs
src/error.rs

index 8221dbc0b11861d823d29bd24e29aeed37ef9c41..6efd91aa825d5949b99db3810f6f79394073da00 100644 (file)
@@ -26,7 +26,7 @@ pub struct Spec {
   currency: String,
 }
 
-#[derive(Debug,Serialize,Deserialize)]
+#[derive(Debug,Clone,Serialize,Deserialize)]
 pub struct Banknote {
   itemname: String,
   image: Arc<dyn InertPieceTrait>,
@@ -121,4 +121,43 @@ impl PieceTrait for Banknote {
             label_y_adj, label_font_size,
             value.qty, &self.currency)?;
   }
+
+  #[throws(ApiPieceOpError)]
+  fn op_multigrab(&self, _: ApiPieceOpArgs, _: PieceRenderInstructions,
+                  take: MultigrabQty, new_z: &ZCoord) -> OpOutcomeThunk {
+    let currency = self.currency.clone();
+    let new_z = new_z.clone();
+    OpOutcomeThunk::Reborrow(Box::new(
+      move |ig: &mut InstanceGuard, player: PlayerId, tpiece: PieceId|
+  {
+    ig.fastsplit_split(player, tpiece, new_z,
+      move |ioccults: &IOccults, goccults: &GameOccults, gpl: &GPlayer,
+            tgpc: &mut GPiece, tipc: &IPiece,
+            ngpc: &mut GPiece|
+  {
+    let value: &mut Value = tgpc.xdata.get_mut_exp()?;
+    let remaining = value.qty.checked_sub(take)
+      .ok_or(Ia::CurrencyShortfall)?;
+
+    tgpc.held = Some(player);
+    ngpc.xdata_init(Value { qty: take })?;
+    tgpc.pinned = false;
+    ngpc.held = None;
+
+    let logents = log_did_to_piece(
+      ioccults, goccults, gpl, tgpc, tipc,
+      &format!("took {}{}, leaving {}{}",
+               take, &currency,
+               remaining, &currency)
+    )?;
+
+    let update = PieceUpdateOp::ModifyQuiet(());
+
+    Ok((
+      (WhatResponseToClientOp::Predictable,
+       update,
+       logents).into(),
+      default()
+    ))
+  })}))}
 }
index f84c966150c71e1c97c44c3bf0802dfdf4ec3dae..47cec919252280902cf9da115db15f68e3c98d19 100644 (file)
@@ -182,6 +182,7 @@ pub enum Inapplicable {
   #[error("overfull, cannot organise")]     OrganisedPlacementOverfull,
   #[error("overlapping occultation(s)")]    OverlappingOccultation,
   #[error("die was recently rolled, cannot flip or roll")] DieCooldown,
+  #[error("insufficient resources or currency")] CurrencyShortfall,
   #[error("UI operation not recognised")]   BadUiOperation,
   #[error("UI operation not valid in the curret piece state")]
                                             BadPieceStateForOperation,