Atrinik Server  4.0
bank.c
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
25 #include <global.h>
26 #include <check.h>
27 #include <checkstd.h>
28 #include <check_proto.h>
29 #include <toolkit/string.h>
30 #include <arch.h>
31 #include <object.h>
32 
33 START_TEST(test_bank_find_info)
34 {
35  mapstruct *map;
36  object *pl;
37  check_setup_env_pl(&map, &pl);
38  ck_assert_ptr_eq(bank_find_info(pl), NULL);
39  object *money = object_insert_into(arch_get("ambercoin"), pl, 0);
40  money->nrof = 1;
41  int64_t value;
42  ck_assert_int_eq(bank_deposit(pl, "1 a", &value), BANK_SUCCESS);
43  ck_assert_uint_eq(value, money->value);
44  ck_assert_uint_eq(bank_get_balance(pl), money->value);
45  ck_assert_uint_eq(shop_get_money(pl), money->value);
46  ck_assert_ptr_ne(bank_find_info(pl), NULL);
47  ck_assert_uint_eq(bank_find_info(pl)->value, money->value);
48 }
49 END_TEST
50 
51 START_TEST(test_bank_deposit)
52 {
53  mapstruct *map;
54  object *pl;
55  check_setup_env_pl(&map, &pl);
56  int64_t value;
57  ck_assert_int_eq(bank_deposit(pl, "", &value), BANK_SYNTAX_ERROR);
58  ck_assert_uint_eq(value, 0);
59  ck_assert_int_eq(bank_deposit(pl, "1 a", &value), BANK_DEPOSIT_AMBER);
60  ck_assert_uint_eq(value, 0);
61  ck_assert_int_eq(bank_deposit(pl, "1 m", &value), BANK_DEPOSIT_MITHRIL);
62  ck_assert_uint_eq(value, 0);
63  ck_assert_int_eq(bank_deposit(pl, "1 j", &value), BANK_DEPOSIT_JADE);
64  ck_assert_uint_eq(value, 0);
65  ck_assert_int_eq(bank_deposit(pl, "1 g", &value), BANK_DEPOSIT_GOLD);
66  ck_assert_uint_eq(value, 0);
67  ck_assert_int_eq(bank_deposit(pl, "1 s", &value), BANK_DEPOSIT_SILVER);
68  ck_assert_uint_eq(value, 0);
69  ck_assert_int_eq(bank_deposit(pl, "1 c", &value), BANK_DEPOSIT_COPPER);
70  ck_assert_uint_eq(value, 0);
71  ck_assert_int_eq(bank_deposit(pl, "all", &value), BANK_SUCCESS);
72  ck_assert_uint_eq(value, 0);
73  ck_assert_uint_eq(bank_get_balance(pl), 0);
74  object *money = object_insert_into(arch_get("ambercoin"), pl, 0);
75  money->nrof = 1;
76  ck_assert_int_eq(bank_deposit(pl, "1 a", &value), BANK_SUCCESS);
77  ck_assert_uint_eq(value, money->value);
78  int64_t total = money->value;
79  ck_assert_uint_eq(bank_get_balance(pl), total);
80  ck_assert_uint_eq(shop_get_money(pl), total);
81  money = object_insert_into(arch_get("coppercoin"), pl, 0);
82  money->nrof = 58;
83  ck_assert_int_eq(bank_deposit(pl, "50 copper", &value), BANK_SUCCESS);
84  ck_assert_uint_eq(value, money->value * 50);
85  total += money->value * 50;
86  ck_assert_uint_eq(bank_get_balance(pl), total);
87  ck_assert_uint_eq(shop_get_money(pl), total + money->value * 8);
88  ck_assert_uint_eq(money->nrof, 8);
89  ck_assert_int_eq(bank_deposit(pl, "9 copper", &value), BANK_DEPOSIT_COPPER);
90  ck_assert_uint_eq(value, 0);
91  tag_t money_tag = money->count;
92  ck_assert_int_eq(bank_deposit(pl, "8 copper", &value), BANK_SUCCESS);
93  ck_assert_uint_eq(value, money->value * 8);
94  total += money->value * 8;
95  ck_assert_uint_eq(bank_get_balance(pl), total);
96  ck_assert_uint_eq(shop_get_money(pl), total);
97  ck_assert(!OBJECT_VALID(money, money_tag));
98 
99  money = object_insert_into(arch_get("goldcoin"), pl, 0);
100  money->nrof = 5923;
101  int64_t val = money->value * money->nrof;
102  money = object_insert_into(arch_get("silvercoin"), pl, 0);
103  money->nrof = 5091;
104  val += money->value * money->nrof;
105  money = object_insert_into(arch_get("coppercoin"), pl, 0);
106  money->nrof = 5829104;
107  val += money->value * money->nrof;
108  money = object_insert_into(arch_get("jadecoin"), pl, 0);
109  money->nrof = 5938;
110  val += money->value * money->nrof;
111  total += val;
112 
113  ck_assert_int_eq(bank_deposit(pl, "all", &value), BANK_SUCCESS);
114  ck_assert_uint_eq(value, val);
115  ck_assert_uint_eq(bank_get_balance(pl), total);
116  ck_assert_uint_eq(shop_get_money(pl), total);
117 
118  ck_assert_int_eq(bank_deposit(pl, "all", &value), BANK_SUCCESS);
119  ck_assert_uint_eq(value, 0);
120  ck_assert_uint_eq(bank_get_balance(pl), total);
121  ck_assert_uint_eq(shop_get_money(pl), total);
122 }
123 END_TEST
124 
125 START_TEST(test_bank_withdraw)
126 {
127  mapstruct *map;
128  object *pl;
129  check_setup_env_pl(&map, &pl);
130  int64_t value;
131  ck_assert_int_eq(bank_withdraw(pl, "", &value), BANK_WITHDRAW_MISSING);
132  ck_assert_uint_eq(value, 0);
133  ck_assert_int_eq(bank_withdraw(pl, "1 a", &value), BANK_WITHDRAW_MISSING);
134  ck_assert_uint_eq(value, 0);
135  ck_assert_int_eq(bank_withdraw(pl, "1 m", &value), BANK_WITHDRAW_MISSING);
136  ck_assert_uint_eq(value, 0);
137  ck_assert_int_eq(bank_withdraw(pl, "1 j", &value), BANK_WITHDRAW_MISSING);
138  ck_assert_uint_eq(value, 0);
139  ck_assert_int_eq(bank_withdraw(pl, "1 g", &value), BANK_WITHDRAW_MISSING);
140  ck_assert_uint_eq(value, 0);
141  ck_assert_int_eq(bank_withdraw(pl, "1 s", &value), BANK_WITHDRAW_MISSING);
142  ck_assert_uint_eq(value, 0);
143  ck_assert_int_eq(bank_withdraw(pl, "1 c", &value), BANK_WITHDRAW_MISSING);
144  ck_assert_uint_eq(value, 0);
145  ck_assert_int_eq(bank_withdraw(pl, "all", &value), BANK_WITHDRAW_MISSING);
146  ck_assert_uint_eq(value, 0);
147  ck_assert_uint_eq(bank_get_balance(pl), 0);
148  object *money = object_insert_into(arch_get("ambercoin"), pl, 0);
149  money->nrof = 50;
150  int64_t val = money->value * money->nrof;
151  ck_assert_int_eq(bank_deposit(pl, "all", &value), BANK_SUCCESS);
152  ck_assert_uint_eq(value, val);
153  int64_t total = val;
154  ck_assert_uint_eq(bank_get_balance(pl), total);
155  ck_assert_uint_eq(shop_get_money(pl), total);
156  ck_assert_int_eq(bank_withdraw(pl, "", &value), BANK_SYNTAX_ERROR);
157  ck_assert_uint_eq(value, 0);
158  ck_assert_int_eq(bank_withdraw(pl, "10000000 a", &value),
160  ck_assert_uint_eq(value, 0);
161  ck_assert_uint_eq(bank_get_balance(pl), total);
162  ck_assert_uint_eq(shop_get_money(pl), total);
163  ck_assert_int_eq(bank_withdraw(pl, "10000000 m", &value),
165  ck_assert_uint_eq(value, 0);
166  ck_assert_uint_eq(bank_get_balance(pl), total);
167  ck_assert_uint_eq(shop_get_money(pl), total);
168  ck_assert_int_eq(bank_withdraw(pl, "10000000 j", &value),
170  ck_assert_uint_eq(value, 0);
171  ck_assert_uint_eq(bank_get_balance(pl), total);
172  ck_assert_uint_eq(shop_get_money(pl), total);
173  ck_assert_int_eq(bank_withdraw(pl, "10000000 g", &value),
175  ck_assert_uint_eq(value, 0);
176  ck_assert_uint_eq(bank_get_balance(pl), total);
177  ck_assert_uint_eq(shop_get_money(pl), total);
178  ck_assert_int_eq(bank_withdraw(pl, "10000000 s", &value),
180  ck_assert_uint_eq(value, 0);
181  ck_assert_uint_eq(bank_get_balance(pl), total);
182  ck_assert_uint_eq(shop_get_money(pl), total);
183  ck_assert_int_eq(bank_withdraw(pl, "10000000 c", &value),
185  ck_assert_uint_eq(value, 0);
186  ck_assert_uint_eq(bank_get_balance(pl), total);
187  ck_assert_uint_eq(shop_get_money(pl), total);
188 
189  ck_assert_int_eq(bank_withdraw(pl, "1000000 c", &value),
191  ck_assert_uint_eq(value, 0);
192  ck_assert_uint_eq(bank_get_balance(pl), total);
193  ck_assert_uint_eq(shop_get_money(pl), total);
194 
195  int64_t in_bank = total;
196  ck_assert_int_eq(bank_withdraw(pl, "1 c 1 s 1 g", &value), BANK_SUCCESS);
197  int64_t withdrawn = 10101;
198  in_bank -= withdrawn;
199  ck_assert_uint_eq(value, withdrawn);
200  ck_assert_uint_eq(bank_get_balance(pl), in_bank);
201  ck_assert_uint_eq(shop_get_money(pl), total);
202 
203  ck_assert_int_eq(bank_withdraw(pl, "5983 copper 83 silver 500 gold",
204  &value), BANK_SUCCESS);
205  withdrawn = 5014283;
206  in_bank -= withdrawn;
207  ck_assert_uint_eq(value, withdrawn);
208  ck_assert_uint_eq(bank_get_balance(pl), in_bank);
209  ck_assert_uint_eq(shop_get_money(pl), total);
210 
211  ck_assert_int_eq(bank_withdraw(pl, "all", &value), BANK_SUCCESS);
212  ck_assert_uint_eq(value, in_bank);
213  ck_assert_uint_eq(bank_get_balance(pl), 0);
214  ck_assert_uint_eq(shop_get_money(pl), total);
215 
216  ck_assert_int_eq(bank_withdraw(pl, "all", &value), BANK_WITHDRAW_MISSING);
217  ck_assert_uint_eq(value, 0);
218  ck_assert_uint_eq(bank_get_balance(pl), 0);
219  ck_assert_uint_eq(shop_get_money(pl), total);
220 }
221 END_TEST
222 
223 static Suite *suite(void)
224 {
225  Suite *s = suite_create("bank");
226  TCase *tc_core = tcase_create("Core");
227 
228  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
229  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
230 
231  suite_add_tcase(s, tc_core);
232  tcase_add_test(tc_core, test_bank_find_info);
233  tcase_add_test(tc_core, test_bank_deposit);
234  tcase_add_test(tc_core, test_bank_withdraw);
235 
236  return s;
237 }
238 
239 void check_server_bank(void)
240 {
241  check_run_suite(suite(), __FILE__);
242 }
object * bank_find_info(object *op)
Definition: bank.c:227
object * arch_get(const char *name)
Definition: arch.c:430
int bank_deposit(object *op, const char *text, int64_t *value)
Definition: bank.c:300
#define BANK_WITHDRAW_OVERWEIGHT
Definition: global.h:59
#define BANK_DEPOSIT_SILVER
Definition: global.h:64
#define BANK_DEPOSIT_MITHRIL
Definition: global.h:70
int64_t shop_get_money(object *op)
Definition: shop.c:251
#define BANK_DEPOSIT_COPPER
Definition: global.h:62
#define BANK_DEPOSIT_GOLD
Definition: global.h:66
object * object_insert_into(object *op, object *where, int flag)
Definition: object.c:2158
#define BANK_WITHDRAW_HIGH
Definition: global.h:55
#define BANK_SYNTAX_ERROR
Definition: global.h:50
uint32_t nrof
Definition: object.h:264
#define BANK_WITHDRAW_MISSING
Definition: global.h:57
#define BANK_DEPOSIT_JADE
Definition: global.h:68
int bank_withdraw(object *op, const char *text, int64_t *value)
Definition: bank.c:402
#define BANK_SUCCESS
Definition: global.h:52
tag_t count
Definition: object.h:142
#define OBJECT_VALID(_ob_, _count_)
Definition: object.h:548
Definition: map.h:536
int64_t value
Definition: object.h:240
#define BANK_DEPOSIT_AMBER
Definition: global.h:72
int64_t bank_get_balance(object *op)
Definition: bank.c:278