forked from Headline/Gangs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhl_gangs_credits.sp
434 lines (355 loc) · 10.2 KB
/
hl_gangs_credits.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
/* [CS:GO] Gangs Credits
*
* Copyright (C) 2016 Michael Flaherty // michaelwflaherty.com // [email protected]
*
* 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/.
*/
#include <sourcemod>
#include <autoexecconfig>
#define REQUIRE_PLUGIN
#include <hl_gangs>
#pragma semicolon 1
#pragma newdecls required
#define PLUGIN_VERSION "1.0"
/* ConVars */
ConVar gcv_iCreditAmmount;
ConVar gcv_iIntervalAmmount;
/* Client Vars */
int ga_iCredits[MAXPLAYERS + 1] = {0, ...};
bool ga_bLoaded[MAXPLAYERS + 1] = {false, ...};
char ga_sSteamID[MAXPLAYERS + 1][32];
/* Database Globals */
Database g_hDatabase = null;
char g_sDatabaseName[60];
/* Plugin Load Status */
bool g_bLateLoad = false;
bool g_bGangsEnabled = false;
public APLRes AskPluginLoad2(Handle hMyself, bool bLate, char[] sError, int err_max)
{
/* Don't yell at us if we don't use it */
MarkNativeAsOptional("Gangs_Message");
RegPluginLibrary("hl_gangs_credits");
CreateNative("Gangs_GetCredits", Native_GetCredits);
CreateNative("Gangs_SetCredits", Native_SetCredits);
g_bLateLoad = bLate;
return APLRes_Success;
}
public int Native_GetCredits(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if (!IsValidClient(client))
{
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%i)", client);
}
return ga_iCredits[client];
}
public int Native_SetCredits(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
int credits = GetNativeCell(2);
if (!IsValidClient(client))
{
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%i)", client);
}
ga_iCredits[client] = credits;
return 0;
}
public Plugin myinfo =
{
name = "[ANY] Blank Credits System",
author = "Headline",
description = "A generic unfeatured credit system",
version = PLUGIN_VERSION,
url = "http://michaelwflaherty.com"
};
public void OnPluginStart()
{
/* Determine if the library exists */
g_bGangsEnabled = LibraryExists("hl_gangs_library");
AutoExecConfig_SetFile("hl_gangs_credits");
AutoExecConfig_CreateConVar("hl_gangs_credits_version", PLUGIN_VERSION, "Headline's Gangs Plugin : Version", FCVAR_NOTIFY|FCVAR_DONTRECORD);
gcv_iCreditAmmount = AutoExecConfig_CreateConVar("hl_gangs_credits_ammount", "1", "Credit ammount per interval", FCVAR_NOTIFY, true, 0.0, true, 100.0);
gcv_iIntervalAmmount = AutoExecConfig_CreateConVar("hl_gangs_credits_interval", "2", "Interval between credit delieveries (minutes)", FCVAR_NOTIFY, true, 0.0, true, 100.0);
AutoExecConfig_ExecuteFile();
AutoExecConfig_CleanFile();
/* Set DB handle */
SetDB();
RegAdminCmd("sm_givecredits", Command_GiveCredits, ADMFLAG_ROOT, "Gives a player credits!");
RegAdminCmd("sm_givecred", Command_GiveCredits, ADMFLAG_ROOT, "Gives a player credits!");
RegConsoleCmd("sm_credits", Command_Credits, "View your credits!");
}
public void OnLibraryAdded(const char[] name)
{
if (StrEqual(name, "hl_gangs_library"))
{
g_bGangsEnabled = true;
}
}
public void OnLibraryRemoved(const char[] name)
{
if (StrEqual(name, "hl_gangs_library"))
{
g_bGangsEnabled = false;
}
}
public Action Command_Credits(int client, int args)
{
if (args != 0)
{
ReplyToCommand(client, "[SM] Usage: sm_credits");
return Plugin_Handled;
}
if (!IsValidClient(client))
{
if (g_bGangsEnabled)
{
Gangs_Message(client, "You must be ingame to use this command!");
}
else
{
PrintToChat(client, "You must be ingame to use this command!");
}
return Plugin_Handled;
}
if (g_bGangsEnabled)
{
Gangs_Message(client, "You have %i credits!", ga_iCredits[client]);
}
else
{
PrintToChat(client, "You have %i credits!", ga_iCredits[client]);
}
return Plugin_Handled;
}
public Action Command_GiveCredits(int client, int args)
{
if (args != 2)
{
ReplyToCommand(client, "[SM] Usage: sm_givecredits <target> <ammount>");
return Plugin_Handled;
}
char sArg1[MAX_NAME_LENGTH];
GetCmdArg(1, sArg1, sizeof(sArg1));
int target = FindTarget(client, sArg1, true);
char sArg2[32];
GetCmdArg(2, sArg2, sizeof(sArg2));
int credits = StringToInt(sArg2);
if (g_bGangsEnabled)
{
Gangs_Message(client, "%i credits given to %N", credits, target);
}
else
{
PrintToChat(client, "%i credits given to %N", credits, target);
}
ga_iCredits[target] += credits;
return Plugin_Handled;
}
public void OnClientPostAdminCheck(int client)
{
LoadSQL(client);
}
public void OnClientConnected(int client)
{
ResetVariables(client);
}
public void OnClientDisconnect(int client)
{
UpdateSQL(client);
ResetVariables(client);
}
public void OnConfigsExecuted()
{
if (g_hDatabase == null)
{
SetDB();
}
if (g_bLateLoad)
{
for (int i = 1; i <= MaxClients; i++)
{
if (IsValidClient(i))
{
GetClientAuthId(i, AuthId_Steam2, ga_sSteamID[i], sizeof(ga_sSteamID[]));
if (StrContains(ga_sSteamID[i], "STEAM_", true) != -1)
{
LoadSQL(i);
}
else
{
CreateTimer(10.0, RefreshSteamID, GetClientUserId(i), TIMER_FLAG_NO_MAPCHANGE);
}
}
}
}
}
public void OnMapStart()
{
CreateTimer((gcv_iIntervalAmmount.IntValue * 60.0), Timer_GiveCredits, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}
public Action Timer_GiveCredits(Handle timer)
{
for (int i = 1; i <= MaxClients; i++)
{
if (IsValidClient(i) && ga_bLoaded[i])
{
ga_iCredits[i] += gcv_iCreditAmmount.IntValue;
if (g_bGangsEnabled)
{
Gangs_Message(i, "You have gained %i credits! You now have %i", gcv_iCreditAmmount.IntValue, ga_iCredits[i]);
}
else
{
PrintToChat(i, "You have gained %i credits! You now have %i", gcv_iCreditAmmount.IntValue, ga_iCredits[i]);
}
}
}
}
void LoadSQL(int client)
{
if (!IsValidClient(client))
{
return;
}
GetClientAuthId(client, AuthId_Steam2, ga_sSteamID[client], sizeof(ga_sSteamID[]));
if (StrContains(ga_sSteamID[client], "STEAM_", true) == -1) //if ID is invalid
{
CreateTimer(10.0, RefreshSteamID, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
if (g_hDatabase == null) //connect not loaded - retry to give it time
{
CreateTimer(1.0, RepeatCheckRank, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
else
{
char sQuery[300];
Format(sQuery, sizeof(sQuery), "SELECT credits FROM hl_gangs_credits WHERE steamid=\"%s\"", ga_sSteamID[client]);
g_hDatabase.Query(SQLCallback_CheckPlayer, sQuery, GetClientUserId(client));
}
}
void UpdateSQL(int client)
{
if (ga_bLoaded[client] && !StrEqual(ga_sSteamID[client], "", false))
{
char sQuery[300];
Format(sQuery, sizeof(sQuery), "UPDATE hl_gangs_credits SET credits=%i WHERE steamid=\"%s\"", ga_iCredits[client], ga_sSteamID[client]);
g_hDatabase.Query(SQLCallback_Void, sQuery, GetClientUserId(client));
}
}
void ResetVariables(int client)
{
ga_iCredits[client] = 0;
ga_sSteamID[client] = "";
ga_bLoaded[client] = false;
}
void SetDB()
{
ConVar convar;
if (g_hDatabase == null)
{
convar = FindConVar("hl_gangs_database_name");
if (convar == null)
{
strcopy(g_sDatabaseName, sizeof(g_sDatabaseName), "hl_gangs");
}
else
{
convar.GetString(g_sDatabaseName, sizeof(g_sDatabaseName));
delete convar;
}
Database.Connect(SQLCallback_Connect, g_sDatabaseName);
}
}
/***********************************************************
*********************** SQL CALLBACKS **********************
************************************************************/
/* SQL Callback On First Connection */
public void SQLCallback_Connect(Database db, const char[] error, any data)
{
if (db == null)
{
SetDB();
}
else
{
g_hDatabase = db;
g_hDatabase.Query(SQLCallback_Void, "CREATE TABLE IF NOT EXISTS `hl_gangs_credits` (`id` int(20) NOT NULL AUTO_INCREMENT, `steamid` varchar(32) NOT NULL, `credits` int(16) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", 1);
}
}
public void SQLCallback_CheckPlayer(Database db, DBResultSet results, const char[] error, int data)
{
if (db == null)
{
SetDB();
}
int client = GetClientOfUserId(data);
if (!IsValidClient(client))
{
return;
}
if (results.RowCount == 0)
{
char sQuery[300];
Format(sQuery, sizeof(sQuery), "INSERT INTO hl_gangs_credits (credits, steamid) VALUES(0, \"%s\")", ga_sSteamID[client]);
g_hDatabase.Query(SQLCallback_Void, sQuery, GetClientUserId(client));
}
else
{
results.FetchRow();
ga_iCredits[client] = results.FetchInt(0);
}
ga_bLoaded[client] = true;
}
public void SQLCallback_Void(Database db, DBResultSet results, const char[] error, int data)
{
if (db == null)
{
LogError("Error (%i): %s", data, error);
}
}
/***********************************************************
************************ SQL TIMERS ************************
************************************************************/
public Action RepeatCheckRank(Handle timer, int iUserID)
{
int client = GetClientOfUserId(iUserID);
LoadSQL(client);
}
public Action RefreshSteamID(Handle hTimer, int iUserID)
{
int client = GetClientOfUserId(iUserID);
if (!IsValidClient(client))
{
return;
}
GetClientAuthId(client, AuthId_Steam2, ga_sSteamID[client], sizeof(ga_sSteamID[]));
if (StrContains(ga_sSteamID[client], "STEAM_", true) == -1) //still invalid - retry again
{
CreateTimer(10.0, RefreshSteamID, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
else
{
LoadSQL(client);
}
}
/***********************************************************
********************* HELPER FUNCTIONS *********************
************************************************************/
bool IsValidClient(int client, bool bAllowBots = false, bool bAllowDead = true)
{
if (!(1 <= client <= MaxClients) || !IsClientInGame(client) || (IsFakeClient(client) && !bAllowBots) || IsClientSourceTV(client) || IsClientReplay(client) || (!bAllowDead && !IsPlayerAlive(client)))
{
return false;
}
return true;
}