Skip to content

Commit 85155aa

Browse files
committed
Add plugins for testing currencies
1 parent 709f339 commit 85155aa

File tree

6 files changed

+137
-6
lines changed

6 files changed

+137
-6
lines changed

.build-config

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ PACKAGE_README_USE=0
55
PACKAGE_COMPILED_PLUGINS_USE=1
66
PACKAGE_COMPILED_PLUGINS_SAVE=0
77

8-
PACKAGE_PLUINGS_LIST_USE=0
8+
PACKAGE_PLUINGS_LIST_USE=1
9+
PACKAGE_PLUINGS_LIST_POSTFIX=MWallet
10+
PACKAGE_PLUINGS_LIST_SAVE=0
11+
912
PACKAGE_ASSETS_USE=0

amxmodx/scripting/MWallet-С-GameMoney.sma renamed to amxmodx/scripting/MWallet-C-GameMoney.sma

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <reapi>
33
#include <ModularWallet>
44
#include "MWallet/Utils"
5+
#include "MWallet/DebugMode"
56

67
#pragma semicolon 1
78
#pragma compress 1
@@ -26,28 +27,34 @@ public MWallet_OnInit() {
2627
}
2728

2829
Float:@OnGet(const UserId) {
30+
Dbg_Log("@OnGet(%n): %.0f", UserId, float(get_member(UserId, m_iAccount)));
2931
return float(get_member(UserId, m_iAccount));
3032
}
3133

3234
bool:@OnSet(const UserId, const Float:fAmount) {
3335
if (fAmount < 0) {
36+
Dbg_Log("@OnSet(%n, %.0f): false", UserId, fAmount);
3437
return false;
3538
}
3639

3740
rg_add_account(UserId, floatround(fAmount), AS_SET);
41+
Dbg_Log("@OnSet(%n, %.0f): true", UserId, fAmount);
3842
return true;
3943
}
4044

4145
bool:@OnDebit(const UserId, const Float:fAmount) {
4246
rg_add_account(UserId, floatround(fAmount), AS_ADD);
47+
Dbg_Log("@OnDebit(%n, %.0f): true", UserId, fAmount);
4348
return true;
4449
}
4550

4651
bool:@OnCredit(const UserId, const Float:fAmount) {
4752
if (@OnGet(UserId) < fAmount) {
53+
Dbg_Log("@OnCredit(%n, %.0f): false", UserId, fAmount);
4854
return false;
4955
}
5056

5157
rg_add_account(UserId, -floatround(fAmount), AS_ADD);
58+
Dbg_Log("@OnCredit(%n, %.0f): true", UserId, fAmount);
5259
return true;
5360
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <amxmodx>
2+
#include <ModularWallet>
3+
#include "MWallet/Utils"
4+
5+
new const __TestPrint_template[] = "[MWallet-Test] %s => %.0f (#%d)";
6+
#define TestPrint(%1,%2) \
7+
client_print(%1, print_console, __TestPrint_template, fmt(%2), GetUserBalance(UserId), g_iUserSelectedCurrency[UserId])
8+
9+
#define GetUserBalance(%1) \
10+
MWallet_Currency_Get(g_iUserSelectedCurrency[UserId], UserId)
11+
12+
#pragma semicolon 1
13+
#pragma compress 1
14+
15+
public stock const PluginName[] = "[MWallet] Test Commands";
16+
public stock const PluginVersion[] = _MWALLET_VERSION;
17+
public stock const PluginAuthor[] = "ArKaNeMaN";
18+
public stock const PluginURL[] = "t.me/arkaneman";
19+
public stock const PluginDescription[] = "Some commands for testing Modular Wallet";
20+
21+
new T_Currency:g_iUserSelectedCurrency[MAX_PLAYERS + 1] = {Invalid_Currency, ...};
22+
23+
public MWallet_OnInited() {
24+
RegisterPluginByVars();
25+
26+
register_clcmd("mwallet_test_select", "@Cmd_Select");
27+
register_clcmd("mwallet_test_get", "@Cmd_Get");
28+
register_clcmd("mwallet_test_set", "@Cmd_Set");
29+
register_clcmd("mwallet_test_credit", "@Cmd_Credit");
30+
register_clcmd("mwallet_test_debit", "@Cmd_Debit");
31+
register_clcmd("mwallet_test_enough", "@Cmd_Enough");
32+
}
33+
34+
@Cmd_Select(const UserId) {
35+
new sCurrencyName[MWALLET_CURRENCY_MAX_NAME_LEN];
36+
read_argv(1, sCurrencyName, charsmax(sCurrencyName));
37+
38+
g_iUserSelectedCurrency[UserId] = MWallet_Currency_Find(sCurrencyName);
39+
40+
if (g_iUserSelectedCurrency[UserId] == Invalid_Currency) {
41+
client_print(UserId, print_console, "[MWallet-Test] Currency `%s` not found.", sCurrencyName);
42+
} else {
43+
client_print(UserId, print_console, "[MWallet-Test] Select `%s` (#%d) currency.", sCurrencyName, g_iUserSelectedCurrency[UserId]);
44+
}
45+
}
46+
47+
@Cmd_Get(const UserId) {
48+
TestPrint(UserId, "Get", GetUserBalance(UserId));
49+
}
50+
51+
@Cmd_Set(const UserId) {
52+
new Float:fAmount = read_argv_float(1);
53+
if (MWallet_Currency_Set(g_iUserSelectedCurrency[UserId], UserId, fAmount)) {
54+
TestPrint(UserId, "Set %.0f", fAmount);
55+
} else {
56+
TestPrint(UserId, "Can`t Set %.0f", fAmount);
57+
}
58+
}
59+
60+
@Cmd_Credit(const UserId) {
61+
new Float:fAmount = read_argv_float(1);
62+
if (MWallet_Currency_Credit(g_iUserSelectedCurrency[UserId], UserId, fAmount)) {
63+
TestPrint(UserId, "Credit %.0f", fAmount);
64+
} else {
65+
TestPrint(UserId, "Can`t Credit %.0f", fAmount);
66+
}
67+
}
68+
69+
@Cmd_Debit(const UserId) {
70+
new Float:fAmount = read_argv_float(1);
71+
if (MWallet_Currency_Debit(g_iUserSelectedCurrency[UserId], UserId, fAmount)) {
72+
TestPrint(UserId, "Debit %.0f", fAmount);
73+
} else {
74+
TestPrint(UserId, "Can`t Debit %.0f", fAmount);
75+
}
76+
}
77+
78+
@Cmd_Enough(const UserId) {
79+
new Float:fAmount = read_argv_float(1);
80+
if (MWallet_Currency_IsEnough(g_iUserSelectedCurrency[UserId], UserId, fAmount)) {
81+
TestPrint(UserId, "Enough %.0f", fAmount);
82+
} else {
83+
TestPrint(UserId, "Not Enough %.0f", fAmount);
84+
}
85+
}

amxmodx/scripting/MWallet/Core/Currency.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ bool:Currency_Credit(const T_Currency:iCurrency, const UserId, const Float:fAmou
127127
new Currency[S_Currency];
128128
Currency_GetById(iCurrency, Currency);
129129

130-
Currency_CallOrDefault(Currency, Currency_OnIsEnough, [UserId, fAmount]) {
130+
Currency_CallOrDefault(Currency, Currency_OnCredit, [UserId, fAmount]) {
131131
if (Currency_IsEnough(iCurrency, UserId, fAmount)) {
132132
return false;
133133
} else {
@@ -142,7 +142,7 @@ bool:Currency_Debit(const T_Currency:iCurrency, const UserId, const Float:fAmoun
142142
new Currency[S_Currency];
143143
Currency_GetById(iCurrency, Currency);
144144

145-
Currency_CallOrDefault(Currency, Currency_OnIsEnough, [UserId, fAmount]) {
145+
Currency_CallOrDefault(Currency, Currency_OnDebit, [UserId, fAmount]) {
146146
return Currency_Set(iCurrency, UserId, Currency_Get(iCurrency, UserId) + fAmount);
147147
}
148148

amxmodx/scripting/MWallet/Core/Natives.inc

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@
99
#include "MWallet/Core/Currency"
1010

1111
T_Currency:get_param_currency(const iArg) {
12-
new T_Currency:iCurrency = T_Currency:get_param(iArg);
13-
assert iCurrency > Invalid_Currency && _:iCurrency < Currency_Count();
14-
return iCurrency;
12+
return T_Currency:get_param(iArg);
1513
}
1614

15+
#define ValidateCurrencyParam(%1,%2) CompositeMacros( \
16+
new T_Currency:__iCurrency = get_param_currency(%1); \
17+
if (__iCurrency <= Invalid_Currency || _:__iCurrency >= Currency_Count()) { \
18+
log_error(0, "Invalid currency handler (%d).", __iCurrency); \
19+
return %2; \
20+
} \
21+
)
22+
23+
#define ValidateUserParam(%1,%2) CompositeMacros( \
24+
new __UserId = get_param(%1); \
25+
if (!is_user_connected(__UserId)) { \
26+
log_error(0, "Invalid player index (%d).", __UserId); \
27+
return %2; \
28+
} \
29+
)
30+
1731
public plugin_natives() {
1832
Natives_Init(MWALLET_PREFIX);
1933

@@ -59,6 +73,8 @@ T_Currency:@_Currency_Find() {
5973
@_Currency_AddListener(const iPlugin) {
6074
enum {Arg_iCurrency = 1, Arg_iEvent, Arg_sCallback}
6175

76+
ValidateCurrencyParam(Arg_iCurrency, );
77+
6278
new T_Currency:iCurrency = T_Currency:get_param(Arg_iCurrency);
6379
new E_CurrencyEvents:iEvent = E_CurrencyEvents:get_param(Arg_iEvent);
6480

@@ -71,6 +87,9 @@ T_Currency:@_Currency_Find() {
7187
Float:@_Currency_Get() {
7288
enum {Arg_iCurrency = 1, Arg_UserId}
7389

90+
ValidateCurrencyParam(Arg_iCurrency, 0.0);
91+
ValidateUserParam(Arg_UserId, 0.0);
92+
7493
return Currency_Get(
7594
get_param_currency(Arg_iCurrency),
7695
get_param(Arg_UserId)
@@ -80,6 +99,9 @@ Float:@_Currency_Get() {
8099
bool:@_Currency_Set() {
81100
enum {Arg_iCurrency = 1, Arg_UserId, Arg_fAmount}
82101

102+
ValidateCurrencyParam(Arg_iCurrency, false);
103+
ValidateUserParam(Arg_UserId, false);
104+
83105
return Currency_Set(
84106
get_param_currency(Arg_iCurrency),
85107
get_param(Arg_UserId),
@@ -90,6 +112,9 @@ bool:@_Currency_Set() {
90112
bool:@_Currency_IsEnough() {
91113
enum {Arg_iCurrency = 1, Arg_UserId, Arg_fAmount}
92114

115+
ValidateCurrencyParam(Arg_iCurrency, false);
116+
ValidateUserParam(Arg_UserId, false);
117+
93118
return Currency_IsEnough(
94119
get_param_currency(Arg_iCurrency),
95120
get_param(Arg_UserId),
@@ -100,6 +125,9 @@ bool:@_Currency_IsEnough() {
100125
bool:@_Currency_Credit() {
101126
enum {Arg_iCurrency = 1, Arg_UserId, Arg_fAmount}
102127

128+
ValidateCurrencyParam(Arg_iCurrency, false);
129+
ValidateUserParam(Arg_UserId, false);
130+
103131
return Currency_Credit(
104132
get_param_currency(Arg_iCurrency),
105133
get_param(Arg_UserId),
@@ -110,6 +138,9 @@ bool:@_Currency_Credit() {
110138
bool:@_Currency_Debit() {
111139
enum {Arg_iCurrency = 1, Arg_UserId, Arg_fAmount}
112140

141+
ValidateCurrencyParam(Arg_iCurrency, false);
142+
ValidateUserParam(Arg_UserId, false);
143+
113144
return Currency_Debit(
114145
get_param_currency(Arg_iCurrency),
115146
get_param(Arg_UserId),

build-debug.bat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@echo off
2+
3+
set PACKAGE_DEBUG=1
4+
5+
call build

0 commit comments

Comments
 (0)