chiark / gitweb /
at-currency: Break out move_money
[otter.git] / apitest / at-currency.rs
1 // Copyright 2020-2021 Ian Jackson and contributors to Otter
2 // SPDX-License-Identifier: AGPL-3.0-or-later
3 // There is NO WARRANTY.
4
5 use crate::*;
6
7 type Ctx = UsualCtx;
8
9 #[throws(Explode)]
10 fn move_money<PI:Idx>(alice: &mut Session,
11                       a_pieces: &mut Pieces<PI>, piece: PI,
12                       qty: i32, z: &str, pos: Pos) {
13   alice.api_piece_op_single(PuSynch((&mut *a_pieces, piece)).id(), (
14     "multigrab", json!({ "n": qty, 'z': z })
15   ))?;
16
17   alice.api_piece(GH::Ungrab, PuSynch((&mut *a_pieces, piece)), pos)?;
18 }
19
20 impl Ctx {
21   #[throws(Explode)]
22   fn multigrab(&mut self) {
23     let mut alice = self.connect_player(&self.alice)?;
24     let mut a_pieces = alice.pieces::<PIA>()?;
25
26     let bank = a_pieces.find_by_desc_glob("*400ƒ*");
27
28     let pile_pos = PosC::new(40,20);
29     let temp_pos = PosC::new(40,10);
30
31     alice.api_piece_op_single(PuSynch((&mut a_pieces, bank)).id(), (
32       "multigrab", json!({ "n": 50, 'z': "q000000000" })
33     ))?;
34     alice.synchu(&mut a_pieces)?;
35
36     let pile = bank;
37     let bank = a_pieces.find_by_desc_glob("* 350ƒ*");
38     a_pieces[pile].assert_desc_contains(" 50ƒ");
39
40     alice.api_piece(GH::Ungrab, PuSynch((&mut a_pieces, pile)), pile_pos)?;
41     alice.synchu(&mut a_pieces)?;
42
43     alice.api_piece_op_single(PuSynch((&mut a_pieces, bank)).id(), (
44       "multigrab", json!({ "n": 13, 'z': "t000000000" })
45     ))?;
46     let moved = bank;
47     alice.api_piece(GH::Ungrab, PuSynch((&mut a_pieces, moved)), temp_pos)?;
48     alice.synchu(&mut a_pieces)?;
49     let bank = a_pieces.find_by_desc_glob("* 337ƒ*");
50     a_pieces[moved].assert_desc_contains(" 13ƒ");
51
52     alice.api_piece(GH::With, PuSynch((&mut a_pieces, moved)), pile_pos)?;
53     alice.synchu(&mut a_pieces)?;
54     assert!(a_pieces[moved].info.is_null());
55     a_pieces[pile].assert_desc_contains(" 63ƒ");
56
57     // This saves us some complaints
58     let _ = moved;
59     let _ = bank;
60     let _ = pile;
61   }
62
63   #[throws(Explode)]
64   fn occult(&mut self) {
65     self.clear_reset_to_demo()?;
66
67     let mut alice = self.connect_player(&self.alice)?;
68     let mut a_pieces = alice.pieces::<PIA>()?;
69
70     let mut bob = self.connect_player(&self.bob)?;
71
72     let hand = a_pieces.find_by_desc_glob(otter::hand::UNCLAIMED_HAND_DESC);
73     alice.api_piece(GH::With, PuSynch(&mut (&mut a_pieces, hand)),
74                     ("k", json!({ "opname": "claim",
75                                    "wrc": WRC::Unpredictable })))?;
76     let hand_pos = a_pieces[hand].pos;
77
78     alice.synchu(&mut a_pieces)?;
79
80     let bank = a_pieces.find_by_desc_glob("*400ƒ*");
81
82     move_money(&mut alice, &mut a_pieces, bank, 399, "u000000000", hand_pos)?;
83     alice.synchu(&mut a_pieces)?;
84     
85     let _ = &mut bob;
86     let _ = bob;
87   }
88 }
89
90 #[throws(Explode)]
91 fn tests(mut c: Ctx) {
92   test!(c, "multigrab",                     c.multigrab()              ?);
93   test!(c, "occult",                        c.occult()                 ?);
94 }
95
96 #[throws(Explode)]
97 pub fn main() {
98   tests(Ctx::setup()?)?;
99 }