From: Ian Jackson Date: Mon, 18 Apr 2022 23:01:39 +0000 (+0100) Subject: currency: Implement op_multigrab and therefore splitting X-Git-Tag: otter-1.1.0~406 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8997e378510330ceb3d897a1ae9c9735e5ad36c5;p=otter.git currency: Implement op_multigrab and therefore splitting Signed-off-by: Ian Jackson ng --- diff --git a/src/currency.rs b/src/currency.rs index 8221dbc0..6efd91aa 100644 --- a/src/currency.rs +++ b/src/currency.rs @@ -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, @@ -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, ¤cy, + remaining, ¤cy) + )?; + + let update = PieceUpdateOp::ModifyQuiet(()); + + Ok(( + (WhatResponseToClientOp::Predictable, + update, + logents).into(), + default() + )) + })}))} } diff --git a/src/error.rs b/src/error.rs index f84c9661..47cec919 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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,