-
Notifications
You must be signed in to change notification settings - Fork 3
/
rpg_tconomy.sp
497 lines (399 loc) · 15 KB
/
rpg_tconomy.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
T-RP
Copyright (C) 2017 Christian Ziegler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma semicolon 1
#define PLUGIN_AUTHOR "Totenfluch"
#define PLUGIN_VERSION "1.00"
#include <sourcemod>
#include <sdktools>
#include <smlib>
#include <multicolors>
#include <rpg_licensing>
#include <sha1>
#include <tStocks>
#pragma newdecls required
int g_iMoney[MAXPLAYERS + 1];
int g_iBankedMoney[MAXPLAYERS + 1];
bool g_bLoaded[MAXPLAYERS + 1];
char dbconfig[] = "gsxh_multiroot";
Database g_DB;
int g_iStartMoney = 300;
char currencyName[] = "tCoins";
public Plugin myinfo =
{
name = "[T-RP] tConomy",
author = PLUGIN_AUTHOR,
description = "Enonmy System for T-RP",
version = PLUGIN_VERSION,
url = "https://totenfluch.de"
};
public void OnPluginStart() {
RegConsoleCmd("sm_money", cmdMoney, "Displays your current Money");
RegAdminCmd("sm_givemoney", cmdGiveMoney, ADMFLAG_ROOT, "Give a Client Currency");
RegAdminCmd("sm_givebankedmoney", cmdGiveBankedMoney, ADMFLAG_ROOT, "Give a Client Banked Currency");
char error[255];
g_DB = SQL_Connect(dbconfig, true, error, sizeof(error));
SQL_SetCharset(g_DB, "utf8");
char createTableQuery[4096];
Format(createTableQuery, sizeof(createTableQuery),
"CREATE TABLE IF NOT EXISTS `t_rpg_tConomy` ( \
`Id` bigint(20) NOT NULL AUTO_INCREMENT, \
`playerid` varchar(20) COLLATE utf8_bin NOT NULL, \
`playername` varchar(64) COLLATE utf8_bin NOT NULL, \
`currency` int(11) NOT NULL, \
`bankCurrency` int(11) NOT NULL, \
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, \
PRIMARY KEY (`Id`), \
UNIQUE KEY `playerid` (`playerid`) \
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_bin;");
SQL_TQuery(g_DB, SQLErrorCheckCallback, createTableQuery);
char createTableQuery2[4096];
Format(createTableQuery2, sizeof(createTableQuery2), "CREATE TABLE IF NOT EXISTS `t_rpg_tConomy_log` ( `Id` INT NOT NULL AUTO_INCREMENT , `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , `playerid` VARCHAR(20) NOT NULL , `amount` INT NOT NULL , `reason` VARCHAR(512) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , PRIMARY KEY (`Id`)) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_bin;");
SQL_TQuery(g_DB, SQLErrorCheckCallback, createTableQuery2);
}
public bool liCheck() {
char licenseKey[64];
char shaKey[128];
licensing_getChecksums(licenseKey, shaKey);
char checksum[128];
char tochecksum[128];
int t = GetTime();
int w = t / 10000 + (24 * 60 * 60) * 3;
Format(tochecksum, sizeof(tochecksum), "|||success %i %s|||", w, licenseKey);
SHA1String(tochecksum, checksum, true);
return StrEqual(checksum, shaKey);
}
public void licensing_OnTokenRefreshed(char serverToken[64], char sha1Token[128]) {
if (!licensing_isValid() || !liCheck())
SetFailState("Invalid License");
}
public void OnClientDisconnect(int client) {
g_bLoaded[client] = false;
}
public Action cmdMoney(int client, int args) {
CPrintToChat(client, "{green}[{purple}tConomy{green}] {orange}You have {purple}%i{orange} {purple}%s", g_iMoney[client], currencyName);
return Plugin_Handled;
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) {
/*
Adds Currency to a Players Account
@Param1 -> int client
@Param2 -> int amount
@Param3 -> char reason[256]
@return money after change
*/
CreateNative("tConomy_addCurrency", Native_addCurrency);
/*
Removes Currency to a Players Account
@Param1 -> int client
@Param2 -> int amount
@Param3 -> char reason[256]
@return money after change
*/
CreateNative("tConomy_removeCurrency", Native_removeCurrency);
/*
Sets Currency on a Players Account
@Param1 -> int client
@Param2 -> int amount
@Param3 -> char reason[256]
@return money after change
*/
CreateNative("tConomy_setCurrency", Native_setCurrency);
/*
Gets the Clients Currency amount
@Param1 -> int client
@return money owned
*/
CreateNative("tConomy_getCurrency", Native_getCurrency);
/*
Adds Currency to a Players Bank Account
@Param1 -> int client
@Param2 -> int amount
@Param3 -> char reason[256]
@return money after change
*/
CreateNative("tConomy_addBankCurrency", Native_addBankCurrency);
/*
Removes Currency to a Players Bank Account
@Param1 -> int client
@Param2 -> int amount
@Param3 -> char reason[256]
@return money after change
*/
CreateNative("tConomy_removeBankCurrency", Native_removeBankCurrency);
/*
Sets Currency on a Players Bank Account
@Param1 -> int client
@Param2 -> int amount
@Param3 -> char reason[256]
@return money after change
*/
CreateNative("tConomy_setBankCurrency", Native_setBankCurrency);
/*
Gets the Clients Bank Currency amount
@Param1 -> int client
@return money owned
*/
CreateNative("tConomy_getBankCurrency", Native_getBankCurrency);
/*
Return if a client is loaded
@Param1 -> int client
@return true if loaded
*/
CreateNative("tConomy_isClientLoaded", Native_isClientLoaded);
}
public int Native_addCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
int amount = GetNativeCell(2);
char reason[256];
GetNativeString(3, reason, sizeof(reason));
if(!addCurrency(client, amount, reason, false))
ThrowNativeError(SP_ERROR_ABORTED, "The Client is not loaded yet OR (client money + amount) < 0 (negative money bug prevented)");
return g_iMoney[client];
}
public int Native_removeCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
int amount = GetNativeCell(2);
char reason[256];
GetNativeString(3, reason, sizeof(reason));
if(!removeCurrency(client, amount, reason, false))
ThrowNativeError(SP_ERROR_ABORTED, "The Client is not loaded yet OR (client money - amount) < 0 (negative money bug prevented)");
return g_iMoney[client];
}
public int Native_setCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
int amount = GetNativeCell(2);
char reason[256];
GetNativeString(3, reason, sizeof(reason));
if(!setCurrency(client, amount, reason, false))
ThrowNativeError(SP_ERROR_ABORTED, "The Client is not loaded yet OR the amount is negative");
return g_iMoney[client];
}
public int Native_addBankCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
int amount = GetNativeCell(2);
char reason[256];
GetNativeString(3, reason, sizeof(reason));
addCurrency(client, amount, reason, true);
return g_iBankedMoney[client];
}
public int Native_removeBankCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
int amount = GetNativeCell(2);
char reason[256];
GetNativeString(3, reason, sizeof(reason));
removeCurrency(client, amount, reason, true);
return g_iBankedMoney[client];
}
public int Native_setBankCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
int amount = GetNativeCell(2);
char reason[256];
GetNativeString(3, reason, sizeof(reason));
setCurrency(client, amount, reason, true);
return g_iBankedMoney[client];
}
public int Native_getCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
return g_iMoney[client];
}
public int Native_getBankCurrency(Handle plugin, int numParams) {
int client = GetNativeCell(1);
return g_iBankedMoney[client];
}
public int Native_isClientLoaded(Handle plugin, int numParams) {
int client = GetNativeCell(1);
return g_bLoaded[client];
}
public void OnClientPostAdminCheck(int client) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char addClientQuery[512];
char playername[MAX_NAME_LENGTH + 8];
GetClientName(client, playername, sizeof(playername));
char clean_playername[MAX_NAME_LENGTH * 2 + 16];
SQL_EscapeString(g_DB, playername, clean_playername, sizeof(clean_playername));
Format(addClientQuery, sizeof(addClientQuery), "INSERT IGNORE INTO `t_rpg_tConomy` (`Id`, `playerid`, `playername`, `currency`, `bankCurrency`, `timestamp`) VALUES (NULL, '%s', '%s', '%i', '0', CURRENT_TIMESTAMP);", playerid, clean_playername, g_iStartMoney);
SQL_TQuery(g_DB, SQLErrorCheckCallback, addClientQuery);
g_bLoaded[client] = false;
loadMoney(client);
}
public bool addCurrency(int client, int amount, char reason[256], bool isBank) {
if (!g_bLoaded[client])
return false;
if(!isBank && (g_iMoney[client] + amount < 0))
return false;
logtConomyAction(client, amount, reason, isBank);
if (isBank)
g_iBankedMoney[client] += amount;
else
g_iMoney[client] += amount;
forceCurrencyAndNameUpdateQuery(client);
return true;
}
public bool removeCurrency(int client, int amount, char reason[256], bool isBank) {
if (!g_bLoaded[client])
return false;
if(!isBank && (g_iMoney[client] - amount < 0))
return false;
logtConomyAction(client, -amount, reason, isBank);
if (isBank)
g_iBankedMoney[client] -= amount;
else
g_iMoney[client] -= amount;
forceCurrencyAndNameUpdateQuery(client);
return true;
}
public bool setCurrency(int client, int amount, char reason[256], bool isBank) {
if (!g_bLoaded[client])
return false;
if(!isBank && amount < 0)
return false;
char reason2[256];
Format(reason2, sizeof(reason2), "SET %s", reason);
logtConomyAction(client, amount, reason2, isBank);
if (isBank)
g_iBankedMoney[client] = amount;
else
g_iMoney[client] = amount;
forceCurrencyAndNameUpdateQuery(client);
return true;
}
public int getCurrency(int client, bool isBank) {
if (!g_bLoaded[client])return -1;
if (isBank)
return g_iBankedMoney[client];
else
return g_iMoney[client];
}
public void loadMoney(int client) {
if (!IsClientConnected(client))
return;
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char loadMoneyQuery[512];
Format(loadMoneyQuery, sizeof(loadMoneyQuery), "SELECT currency,bankCurrency FROM t_rpg_tConomy WHERE playerid = '%s'", playerid);
SQL_TQuery(g_DB, SQLLoadMoneyQueryCallback, loadMoneyQuery, GetClientUserId(client));
}
public void logtConomyAction(int client, int amount, char reason[256], bool isBank) {
if (amount == 0 && StrContains(reason, "SET") == -1)
return;
char playerLog[256];
if (isBank)
Format(playerLog, sizeof(playerLog), "{green}[{purple}tConomy{green}] {orange}Your Banked {purple}%s{orange} have been changed by {purple}%i{orange}{orange} (%s)", currencyName, amount, reason);
else
Format(playerLog, sizeof(playerLog), "{green}[{purple}tConomy{green}] {orange}Your {purple}%s{orange} have been changed by {purple}%i {orange} (%s)", currencyName, amount, reason);
CPrintToChat(client, playerLog);
char realReason[256];
if (isBank)
Format(realReason, sizeof(realReason), "BANK %s", reason);
else
Format(realReason, sizeof(realReason), "Currency %s", reason);
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char logQuery[512];
Format(logQuery, sizeof(logQuery), "INSERT INTO `t_rpg_tConomy_log` (`Id`, `timestamp`, `playerid`, `amount`, `reason`) VALUES (NULL, CURRENT_TIMESTAMP, '%s', '%i', '%s');", playerid, amount, realReason);
SQL_TQuery(g_DB, SQLErrorCheckCallback, logQuery);
}
public void forceCurrencyAndNameUpdateQuery(int client) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char playername[MAX_NAME_LENGTH + 8];
GetClientName(client, playername, sizeof(playername));
char clean_playername[MAX_NAME_LENGTH * 2 + 16];
SQL_EscapeString(g_DB, playername, clean_playername, sizeof(clean_playername));
char CurrencyUpdateQuery[512];
Format(CurrencyUpdateQuery, sizeof(CurrencyUpdateQuery), "UPDATE t_rpg_tConomy SET currency = %i, bankCurrency = %i, playername = '%s' WHERE playerid = '%s';", g_iMoney[client], g_iBankedMoney[client], clean_playername, playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, CurrencyUpdateQuery);
}
public void SQLErrorCheckCallback(Handle owner, Handle hndl, const char[] error, any data) {
if (!StrEqual(error, ""))
LogError(error);
}
public Action cmdGiveMoney(int client, int args) {
if (args < 2) {
ReplyToCommand(client, "[SM tConomy] Usage: sm_givemoney <target> <amount>");
return Plugin_Handled;
}
char tempCurrencyString[64];
GetCmdArg(2, tempCurrencyString, sizeof(tempCurrencyString));
int tempCurrency = StringToInt(tempCurrencyString);
if (tempCurrency < -100000 || tempCurrency > 100000) {
ReplyToCommand(client, "Invalid Amount | < -100000 || > 100000");
return Plugin_Handled;
}
char pattern[MAX_NAME_LENGTH + 8];
char buffer[MAX_NAME_LENGTH + 8];
GetCmdArg(1, pattern, sizeof(pattern));
int targets[64];
bool ml = false;
int count = ProcessTargetString(pattern, client, targets, sizeof(targets), COMMAND_FILTER_ALIVE, buffer, sizeof(buffer), ml);
char reason[256];
Format(reason, sizeof(reason), "Given by %N", client);
if (count <= 0)
ReplyToCommand(client, "Invalid or Bad Target");
else {
for (int i = 0; i < count; i++) {
int target = targets[i];
if (tempCurrency >= 0)
addCurrency(target, tempCurrency, reason, false);
else
removeCurrency(target, -tempCurrency, reason, false);
}
}
return Plugin_Handled;
}
public Action cmdGiveBankedMoney(int client, int args) {
if (args < 2) {
ReplyToCommand(client, "[SM tConomy] Usage: sm_givebankedmoney <target> <amount>");
return Plugin_Handled;
}
char tempCurrencyString[64];
GetCmdArg(2, tempCurrencyString, sizeof(tempCurrencyString));
int tempCurrency = StringToInt(tempCurrencyString);
if (tempCurrency < -100000 || tempCurrency > 100000) {
ReplyToCommand(client, "Invalid Amount | < -100000 || > 100000");
return Plugin_Handled;
}
char pattern[MAX_NAME_LENGTH + 8];
char buffer[MAX_NAME_LENGTH + 8];
GetCmdArg(1, pattern, sizeof(pattern));
int targets[64];
bool ml = false;
int count = ProcessTargetString(pattern, client, targets, sizeof(targets), COMMAND_FILTER_ALIVE, buffer, sizeof(buffer), ml);
char reason[256];
Format(reason, sizeof(reason), "Given by %N", client);
if (count <= 0)
ReplyToCommand(client, "Invalid or Bad Target");
else {
for (int i = 0; i < count; i++) {
int target = targets[i];
if (tempCurrency >= 0)
addCurrency(target, tempCurrency, reason, true);
else
removeCurrency(target, -tempCurrency, reason, true);
}
}
return Plugin_Handled;
}
public void SQLLoadMoneyQueryCallback(Handle owner, Handle hndl, const char[] error, any data) {
int client = GetClientOfUserId(data);
while (SQL_FetchRow(hndl)) {
g_iMoney[client] = SQL_FetchIntByName(hndl, "curency");
if(g_iMoney[client] < 0)
setCurrency(client, 0, "Reset of Negative Money", false);
g_iBankedMoney[client] = SQL_FetchIntByName(hndl, "bankCurrency");
g_bLoaded[client] = true;
}
}