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