-
Notifications
You must be signed in to change notification settings - Fork 3
/
rpg_tcrime.sp
361 lines (277 loc) · 11.4 KB
/
rpg_tcrime.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
/*
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 <tStocks>
#pragma newdecls required
char dbconfig[] = "gsxh_multiroot";
Database g_DB;
#define MAX_COOLDOWN 60
enum crimeProperties {
cCrime,
cCooldown,
String:cFlags[64]
}
int g_ePlayerCrime[MAXPLAYERS + 1][crimeProperties];
public Plugin myinfo =
{
name = "[T-RP] tCrime",
author = PLUGIN_AUTHOR,
description = "Crime System for T-RP",
version = PLUGIN_VERSION,
url = "https://totenfluch.de"
};
public void OnPluginStart()
{
char error[255];
g_DB = SQL_Connect(dbconfig, true, error, sizeof(error));
SQL_SetCharset(g_DB, "utf8");
char CreateCrimeTableQuery[512];
Format(CreateCrimeTableQuery, sizeof(CreateCrimeTableQuery), "CREATE TABLE IF NOT EXISTS `t_rpg_tcrime` ( `Id` BIGINT NOT NULL AUTO_INCREMENT , `timestamp` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , `playername` VARCHAR(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `playerid` VARCHAR(20) NOT NULL , `crime` INT NOT NULL , `flags` VARCHAR(64) NOT NULL , PRIMARY KEY (`Id`, `playerid`), UNIQUE KEY `playerid` (`playerid`)) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_bin;");
SQL_TQuery(g_DB, SQLErrorCheckCallback, CreateCrimeTableQuery);
char createTableQuery2[4096];
Format(createTableQuery2, sizeof(createTableQuery2), "CREATE TABLE IF NOT EXISTS `t_rpg_tcrime_log` ( `Id` INT NOT NULL AUTO_INCREMENT , `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , `playerid` VARCHAR(20) NOT NULL , `amount` INT NOT NULL, PRIMARY KEY (`Id`)) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_bin;");
SQL_TQuery(g_DB, SQLErrorCheckCallback, createTableQuery2);
RegAdminCmd("sm_resetCrime", cmdResetCrime, ADMFLAG_ROOT, "resets the crime");
}
public Action cmdResetCrime(int client, int args) {
for (int i = 1; i < MAXPLAYERS; i++) {
if (!isValidClient(i))
continue;
setCrime(i, 0);
}
}
public void OnMapStart() {
CreateTimer(1.0, refreshTimer, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
}
public Action refreshTimer(Handle Timer) {
for (int i = 1; i < MAXPLAYERS; i++) {
if (!isValidClient(i))
continue;
if (GetClientTeam(i) != 2 && GetClientTeam(i) != 3)
continue;
if (g_ePlayerCrime[i][cCrime] == -1)
continue;
if (g_ePlayerCrime[i][cCooldown] > 0)
g_ePlayerCrime[i][cCooldown]--;
if (g_ePlayerCrime[i][cCrime] > 0 && g_ePlayerCrime[i][cCooldown] == 0)
decreaseCrime(i, 1);
}
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) {
/*
Get the crime amount of client
@Param1 -> client index
@return crime amount
*/
CreateNative("tCrime_getCrime", Native_getCrime);
/*
Sets the Crime amount on client
@Param1 -> client index
@Param2 -> crime amount
@return -
*/
CreateNative("tCrime_setCrime", Native_setCrime);
/*
Adds the Crime amount on client
@Param1 -> client index
@Param2 -> crime amount
@return -
*/
CreateNative("tCrime_addCrime", Native_addCrime);
/*
Removes the Crime amount on client
@Param1 -> client index
@Param2 -> crime amount
@return -
*/
CreateNative("tCrime_removeCrime", Native_removeCrime);
/*
Adds flags to the client
@Param1 -> client index
@Param2 -> char flags[64]
@return -
*/
CreateNative("tCrime_addFlags", Native_addFlags);
/*
Removes flags from the client
@Param1 -> client index
@Param2 -> char flags[64]
@return -
*/
CreateNative("tCrime_removeFlags", Native_removeFlags);
/*
Clears flags from the client
@Param1 -> client index
@return -
*/
CreateNative("tCrime_clearFlags", Native_clearFlags);
/*
Sets flags from the client
@Param1 -> client index
@return -
*/
CreateNative("tCrime_setFlags", Native_setFlags);
/*
Sets flags from the client
@Param1 -> client index
@Param2 -> char flagsbuffer[64]
@return -
*/
CreateNative("tCrime_getFlags", Native_getFlags);
}
public int Native_getCrime(Handle plugin, int numParams) {
return g_ePlayerCrime[GetNativeCell(1)][cCrime];
}
public int Native_setCrime(Handle plugin, int numParams) {
setCrime(GetNativeCell(1), GetNativeCell(2));
}
public int Native_addCrime(Handle plugin, int numParams) {
increaseCrime(GetNativeCell(1), GetNativeCell(2));
}
public int Native_removeCrime(Handle plugin, int numParams) {
decreaseCrime(GetNativeCell(1), GetNativeCell(2));
}
public int Native_addFlags(Handle plugin, int numParams) {
char flagBuffer[64];
GetNativeString(2, flagBuffer, sizeof(flagBuffer));
addFlags(GetNativeCell(1), flagBuffer);
}
public int Native_removeFlags(Handle plugin, int numParams) {
char flagBuffer[64];
GetNativeString(2, flagBuffer, sizeof(flagBuffer));
removeFlags(GetNativeCell(1), flagBuffer);
}
public int Native_setFlags(Handle plugin, int numParams) {
char flagBuffer[64];
GetNativeString(2, flagBuffer, sizeof(flagBuffer));
setFlags(GetNativeCell(1), flagBuffer);
}
public int Native_clearFlags(Handle plugin, int numParams) {
clearFlags(GetNativeCell(1));
}
public int Native_getFlags(Handle plugin, int numParams) {
int client = GetNativeCell(1);
char flags[64];
strcopy(flags, 64, g_ePlayerCrime[client][cFlags]);
SetNativeString(2, flags, sizeof(flags), false);
}
public void OnClientAuthorized(int client) {
g_ePlayerCrime[client][cCooldown] = MAX_COOLDOWN;
g_ePlayerCrime[client][cCrime] = -1;
strcopy(g_ePlayerCrime[client][cFlags], 64, "");
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 insertPlayerQuery[512];
Format(insertPlayerQuery, sizeof(insertPlayerQuery), "INSERT IGNORE INTO `t_rpg_tcrime` (`Id`, `timestamp`, `playername`, `playerid`, `crime`, `flags`) VALUES (NULL, CURRENT_TIMESTAMP, '%s', '%s', '0', '');", clean_playername, playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, insertPlayerQuery);
loadCrime(client);
}
public void OnClientDisconnect(int client) {
g_ePlayerCrime[client][cCrime] = -1;
g_ePlayerCrime[client][cCooldown] = -1;
strcopy(g_ePlayerCrime[client][cFlags], 64, "");
}
public void loadCrime(int client) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char LoadCrimeQuery[512];
Format(LoadCrimeQuery, sizeof(LoadCrimeQuery), "SELECT crime,flags FROM t_rpg_tcrime WHERE playerid = '%s';", playerid);
SQL_TQuery(g_DB, SQLLoadCrimeCallback, LoadCrimeQuery, GetClientUserId(client));
}
public void SQLLoadCrimeCallback(Handle owner, Handle hndl, const char[] error, any data) {
int client = GetClientOfUserId(data);
while (SQL_FetchRow(hndl)) {
SQL_FetchStringByName(hndl, "flags", g_ePlayerCrime[client][cFlags], 64);
g_ePlayerCrime[client][cCrime] = SQL_FetchIntByName(hndl, "crime");
}
}
public void SQLErrorCheckCallback(Handle owner, Handle hndl, const char[] error, any data) {
if (!StrEqual(error, ""))
LogError(error);
}
public void increaseCrime(int client, int amount) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
g_ePlayerCrime[client][cCrime] += amount;
g_ePlayerCrime[client][cCooldown] = MAX_COOLDOWN;
char updateCrimeQuery[512];
Format(updateCrimeQuery, sizeof(updateCrimeQuery), "UPDATE t_rpg_tcrime SET crime = %i WHERE playerid = '%s'", g_ePlayerCrime[client][cCrime], playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateCrimeQuery);
Format(updateCrimeQuery, sizeof(updateCrimeQuery), "INSERT INTO `t_rpg_tcrime_log` (`Id`, `timestamp`, `playerid`, `amount`) VALUES (NULL, CURRENT_TIMESTAMP, '%s', '%i');", playerid, amount);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateCrimeQuery);
}
public void decreaseCrime(int client, int amount) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
g_ePlayerCrime[client][cCrime] -= amount;
char updateCrimeQuery[512];
Format(updateCrimeQuery, sizeof(updateCrimeQuery), "UPDATE t_rpg_tcrime SET crime = %i WHERE playerid = '%s'", g_ePlayerCrime[client][cCrime], playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateCrimeQuery);
if (amount > 1) {
Format(updateCrimeQuery, sizeof(updateCrimeQuery), "INSERT INTO `t_rpg_tcrime_log` (`Id`, `timestamp`, `playerid`, `amount`) VALUES (NULL, CURRENT_TIMESTAMP, '%s', '%i');", playerid, -amount);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateCrimeQuery);
}
}
public void setCrime(int client, int amount) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
g_ePlayerCrime[client][cCrime] = amount;
char updateCrimeQuery[512];
Format(updateCrimeQuery, sizeof(updateCrimeQuery), "UPDATE t_rpg_tcrime SET crime = %i WHERE playerid = '%s'", g_ePlayerCrime[client][cCrime], playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateCrimeQuery);
}
public void addFlags(int client, char flags[64]) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char finalFlags[64];
Format(finalFlags, sizeof(finalFlags), "%s%s", g_ePlayerCrime[client][cFlags], flags);
strcopy(g_ePlayerCrime[client][cFlags], 64, finalFlags);
char updateFlagsQuery[512];
Format(updateFlagsQuery, sizeof(updateFlagsQuery), "UPDATE t_rpg_tcrime SET flags = %s WHERE playerid = '%s'", g_ePlayerCrime[client][cFlags], playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateFlagsQuery);
}
public void removeFlags(int client, char flags[64]) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
ReplaceString(g_ePlayerCrime[client][cFlags], 64, flags, "", true);
char updateFlagsQuery[512];
Format(updateFlagsQuery, sizeof(updateFlagsQuery), "UPDATE t_rpg_tcrime SET flags = %s WHERE playerid = '%s'", g_ePlayerCrime[client][cFlags], playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateFlagsQuery);
}
public void clearFlags(int client) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
strcopy(g_ePlayerCrime[client][cFlags], 64, "");
char updateFlagsQuery[512];
Format(updateFlagsQuery, sizeof(updateFlagsQuery), "UPDATE t_rpg_tcrime SET flags = %s WHERE playerid = '%s'", g_ePlayerCrime[client][cFlags], playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateFlagsQuery);
}
public void setFlags(int client, char flags[64]) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
strcopy(g_ePlayerCrime[client][cFlags], 64, flags);
char updateFlagsQuery[512];
Format(updateFlagsQuery, sizeof(updateFlagsQuery), "UPDATE t_rpg_tcrime SET flags = %s WHERE playerid = '%s'", g_ePlayerCrime[client][cFlags], playerid);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateFlagsQuery);
}