From 4494aa3e8bd2378cb4213c849486040136a3aac9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 22 Mar 2021 03:25:41 +0000 Subject: [PATCH] deck: Implement ui_operation This is most of the actual functionality Signed-off-by: Ian Jackson --- src/deck.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/deck.rs b/src/deck.rs index 868062e1..34df7b6f 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -2,10 +2,9 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // There is NO WARRANTY. -// This is otter::hidden::magic - use crate::prelude::*; +pub const CORE_DESC: &str = "a pickup deck"; pub const ENABLED_DESC: &str = "a pickup deck (enabled)"; pub const DISABLED_DESC: &str = "a pickup deck (disabled)"; @@ -111,4 +110,71 @@ impl PieceTrait for Deck { } ) } + + #[throws(ApiPieceOpError)] + fn ui_operation(&self, a: ApiPieceOpArgs<'_>, + opname: &str, wrc: WhatResponseToClientOp) + -> UpdateFromOpComplex { + let ApiPieceOpArgs { gs,player,piece,ipieces,ioccults,to_permute,.. } = a; + let gen = &mut gs.gen; + let gplayers = &mut gs.players; + let gpieces = &mut gs.pieces; + + let goccults = &mut gs.occults; + let gpc = gpieces.byid_mut(piece)?; + + let gpl = gplayers.byid_mut(player)?; + let nick = Html(htmlescape::encode_minimal(&gpl.nick)); + + dbgc!("ui op k entry", &opname); + + let (xupdates, did) = + match (opname, self.enabled(gpc)) + { + ("activate", false) => { + dbgc!("claiming"); + let (region, views) = (||{ + let region = self.shape.outline.region(gpc.pos)?; + let views = UniformOccultationView(OccKG::Scrambled).views()?; + Ok::<_,IE>((region, views)) + })().map_err(|ie| ApiPieceOpError::ReportViaResponse(ie.into()))?; + + // actually do things: + dbgc!("creating occ"); + let xupdates = + create_occultation(&mut gen.unique_gen(), &mut gs.max_z, + gplayers, gpieces, goccults, ipieces, ioccults, + to_permute, + region, piece, views)?; + + dbgc!("creating occ done", &xupdates); + (xupdates, format!("enabled {}", CORE_DESC)) + } + ("deactivate", true) => { + let xupdates = + remove_occultation(&mut gen.unique_gen(), + gplayers, gpieces, goccults, ipieces, ioccults, + to_permute, piece) + .map_err(|ie| ApiPieceOpError::ReportViaResponse(ie.into()))?; + + (xupdates, format!("disabled {}", CORE_DESC)) + } + ("claim", true) | + ("deactivate", false) => { + throw!(OE::PieceHeld); + } + _ => { + throw!(OE::BadOperation); + } + }; + + let log = vec![ LogEntry { html: Html(format!("{} {}", nick.0, did)) }]; + + dbgc!("ui op k did main"); + + (PieceUpdate { + wrc, log, + ops: PUOs::Simple(PUO::Modify(())), + }, xupdates, None) + } } -- 2.30.2