forked from zzzznn5000/bTimes-csgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbTimes-core.sp
More file actions
1002 lines (819 loc) · 23.8 KB
/
bTimes-core.sp
File metadata and controls
1002 lines (819 loc) · 23.8 KB
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma semicolon 1
#include <bTimes-core>
public Plugin:myinfo =
{
name = "[bTimes] Core",
author = "blacky",
description = "The root of bTimes",
version = VERSION,
url = "http://steamcommunity.com/id/blaackyy/"
}
#include <sourcemod>
#include <sdktools>
#include <scp>
#include <smlib/clients>
#include <bTimes-timer>
enum
{
GameType_CSS,
GameType_CSGO
};
new g_GameType;
new Handle:g_hCommandList,
bool:g_bCommandListLoaded;
new Handle:g_DB;
new String:g_sMapName[64],
g_PlayerID[MAXPLAYERS+1],
Handle:g_MapList,
Handle:g_hDbMapNameList,
Handle:g_hDbMapIdList,
bool:g_bDbMapsLoaded,
Float:g_fMapStart;
new Float:g_fSpamTime[MAXPLAYERS+1];
// Chat
new String:g_msg_start[128] = {""};
new String:g_msg_varcol[128] = {"\x07B4D398"};
new String:g_msg_textcol[128] = {"\x01"};
// Forwards
new Handle:g_fwdMapIDPostCheck,
Handle:g_fwdMapListLoaded,
Handle:g_fwdPlayerIDLoaded;
// PlayerID retrieval data
new Handle:g_hPlayerID,
Handle:g_hUser,
bool:g_bPlayerListLoaded;
public OnPluginStart()
{
decl String:sGame[64];
GetGameFolderName(sGame, sizeof(sGame));
if(StrEqual(sGame, "cstrike"))
g_GameType = GameType_CSS;
else if(StrEqual(sGame, "csgo"))
g_GameType = GameType_CSGO;
else
SetFailState("This timer does not support this game (%s)", sGame);
// Database
DB_Connect();
g_fMapStart = GetEngineTime();
AutoExecConfig(true, "core", "timer");
// Events
HookEvent("player_changename", Event_PlayerChangeName, EventHookMode_Pre);
// Commands
RegConsoleCmdEx("sm_mostplayed", SM_TopMaps, "Displays the most played maps");
RegConsoleCmdEx("sm_lastplayed", SM_LastPlayed, "Shows the last played maps");
RegConsoleCmdEx("sm_playtime", SM_Playtime, "Shows the people who played the most.");
RegConsoleCmdEx("sm_search", SM_Search, "Search the command list for the given string of text.");
// Makes FindTarget() work properly
LoadTranslations("common.phrases");
}
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
CreateNative("GetClientID", Native_GetClientID);
CreateNative("IsSpamming", Native_IsSpamming);
CreateNative("SetIsSpamming", Native_SetIsSpamming);
CreateNative("RegisterCommand", Native_RegisterCommand);
CreateNative("GetMapIdFromMapName", Native_GetMapIdFromMapName);
CreateNative("GetMapNameFromMapId", Native_GetMapNameFromMapId);
CreateNative("GetNameFromPlayerID", Native_GetNameFromPlayerID);
CreateNative("GetSteamIDFromPlayerID", Native_GetSteamIDFromPlayerID);
g_fwdMapIDPostCheck = CreateGlobalForward("OnMapIDPostCheck", ET_Event);
g_fwdPlayerIDLoaded = CreateGlobalForward("OnPlayerIDLoaded", ET_Event, Param_Cell);
g_fwdMapListLoaded = CreateGlobalForward("OnDatabaseMapListLoaded", ET_Event);
return APLRes_Success;
}
public OnMapStart()
{
GetCurrentMap(g_sMapName, sizeof(g_sMapName));
//g_fMapStart = GetEngineTime();
if(g_MapList != INVALID_HANDLE)
{
CloseHandle(g_MapList);
}
g_MapList = ReadMapList();
// Creates map if it doesn't exist, sets map as recently played, and loads map playtime
CreateCurrentMapID();
}
public OnMapEnd()
{
DB_SaveMapPlaytime();
DB_SetMapLastPlayed();
}
public OnClientDisconnect(client)
{
// Save player's play time
if(g_PlayerID[client] != 0 && !IsFakeClient(client))
{
DB_SavePlaytime(client);
}
// Reset the playerid for the client index
g_PlayerID[client] = 0;
}
public OnClientAuthorized(client)
{
if(!IsFakeClient(client) && g_bPlayerListLoaded == true)
{
CreatePlayerID(client);
}
}
public OnTimerChatChanged(MessageType, String:Message[])
{
if(MessageType == 0)
{
Format(g_msg_start, sizeof(g_msg_start), Message);
ReplaceMessage(g_msg_start, sizeof(g_msg_start));
}
else if(MessageType == 1)
{
Format(g_msg_varcol, sizeof(g_msg_varcol), Message);
ReplaceMessage(g_msg_varcol, sizeof(g_msg_varcol));
}
else if(MessageType == 2)
{
Format(g_msg_textcol, sizeof(g_msg_textcol), Message);
ReplaceMessage(g_msg_textcol, sizeof(g_msg_textcol));
}
}
ReplaceMessage(String:message[], maxlength)
{
if(g_GameType == GameType_CSS)
{
ReplaceString(message, maxlength, "^", "\x07", false);
}
else if(g_GameType == GameType_CSGO)
{
ReplaceString(message, maxlength, "^A", "\x0A");
ReplaceString(message, maxlength, "^1", "\x01");
ReplaceString(message, maxlength, "^2", "\x02");
ReplaceString(message, maxlength, "^3", "\x03");
ReplaceString(message, maxlength, "^4", "\x04");
ReplaceString(message, maxlength, "^5", "\x05");
ReplaceString(message, maxlength, "^6", "\x06");
ReplaceString(message, maxlength, "^7", "\x07");
}
}
public Action:OnChatMessage(&author, Handle:recipients, String:name[], String:message[])
{
if(IsChatTrigger())
{
return Plugin_Stop;
}
/*
decl String:sType[16], String:sStyle[32], String:sCommand[48];
for(new Type; Type < MAX_TYPES; Type++)
{
GetTypeAbbr(Type, sType, sizeof(sType), true);
for(new Style; Style < MAX_STYLES; Style++)
{
GetStyleAbbr(Style, sStyle, sizeof(sStyle), true);
Format(sCommand, sizeof(sCommand), "%srank%s", sType, sStyle);
if(StrEqual(message, sCommand, true))
{
FakeClientCommand(author, "sm_%s", message);
return Plugin_Stop;
}
}
}
*/
return Plugin_Continue;
}
public Action:SM_TopMaps(client, args)
{
if(!IsSpamming(client))
{
SetIsSpamming(client, 1.0);
decl String:query[256];
Format(query, sizeof(query), "SELECT MapName, MapPlaytime FROM maps ORDER BY MapPlaytime DESC");
SQL_TQuery(g_DB, TopMaps_Callback, query, client);
}
return Plugin_Handled;
}
public TopMaps_Callback(Handle:owner, Handle:hndl, String:error[], any:client)
{
if(hndl != INVALID_HANDLE)
{
if(IsClientInGame(client))
{
new Handle:menu = CreateMenu(Menu_TopMaps);
SetMenuTitle(menu, "Most played maps\n---------------------------------------");
new rows = SQL_GetRowCount(hndl);
if(rows > 0)
{
decl String:mapname[64], String:timeplayed[32], String:display[128], iTime;
for(new i, j; i < rows; i++)
{
SQL_FetchRow(hndl);
iTime = SQL_FetchInt(hndl, 1);
if(iTime != 0)
{
SQL_FetchString(hndl, 0, mapname, sizeof(mapname));
if(FindStringInArray(g_MapList, mapname) != -1)
{
FormatPlayerTime(float(iTime), timeplayed, sizeof(timeplayed), false, 1);
SplitString(timeplayed, ".", timeplayed, sizeof(timeplayed));
Format(display, sizeof(display), "#%d: %s - %s", ++j, mapname, timeplayed);
AddMenuItem(menu, display, display);
}
}
}
SetMenuExitButton(menu, true);
DisplayMenu(menu, client, MENU_TIME_FOREVER);
}
}
}
else
{
LogError(error);
}
}
public Menu_TopMaps(Handle:menu, MenuAction:action, param1, param2)
{
if(action == MenuAction_Select)
{
decl String:info[32];
GetMenuItem(menu, param2, info, sizeof(info));
FakeClientCommand(param1, "sm_nominate %s", info);
}
else if(action == MenuAction_End)
CloseHandle(menu);
}
public Action:SM_LastPlayed(client, argS)
{
if(!IsSpamming(client))
{
SetIsSpamming(client, 1.0);
decl String:query[256];
Format(query, sizeof(query), "SELECT MapName, LastPlayed FROM maps ORDER BY LastPlayed DESC");
SQL_TQuery(g_DB, LastPlayed_Callback, query, client);
}
return Plugin_Handled;
}
public LastPlayed_Callback(Handle:owner, Handle:hndl, String:error[], any:client)
{
if(hndl != INVALID_HANDLE)
{
if(IsClientInGame(client))
{
new Handle:menu = CreateMenu(Menu_LastPlayed);
SetMenuTitle(menu, "Last played maps\n---------------------------------------");
decl String:sMapName[64], String:sDate[32], String:sTimeOfDay[32], String:display[256], iTime;
new rows = SQL_GetRowCount(hndl);
for(new i=1; i<=rows; i++)
{
SQL_FetchRow(hndl);
iTime = SQL_FetchInt(hndl, 1);
if(iTime != 0)
{
SQL_FetchString(hndl, 0, sMapName, sizeof(sMapName));
if(FindStringInArray(g_MapList, sMapName) != -1)
{
FormatTime(sDate, sizeof(sDate), "%x", iTime);
FormatTime(sTimeOfDay, sizeof(sTimeOfDay), "%X", iTime);
Format(display, sizeof(display), "%s - %s - %s", sMapName, sDate, sTimeOfDay);
AddMenuItem(menu, display, display);
}
}
}
SetMenuExitButton(menu, true);
DisplayMenu(menu, client, MENU_TIME_FOREVER);
}
}
else
{
LogError(error);
}
}
public Menu_LastPlayed(Handle:menu, MenuAction:action, param1, param2)
{
if (action == MenuAction_Select)
{
decl String:info[32];
GetMenuItem(menu, param2, info, sizeof(info));
FakeClientCommand(param1, "sm_nominate %s", info);
}
else if(action == MenuAction_End)
CloseHandle(menu);
}
public Action:Event_PlayerChangeName(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if(!IsFakeClient(client) && g_PlayerID[client] != 0)
{
decl String:sNewName[MAX_NAME_LENGTH];
GetEventString(event, "newname", sNewName, sizeof(sNewName));
UpdateName(client, sNewName);
}
}
DB_Connect()
{
if(g_DB != INVALID_HANDLE)
CloseHandle(g_DB);
decl String:error[255];
g_DB = SQL_Connect("timer", true, error, sizeof(error));
if(g_DB == INVALID_HANDLE)
{
LogError(error);
CloseHandle(g_DB);
}
else
{
decl String:query[512];
//UNICODE!!!!!!!!!!!!!111111111111!!!!!!!!!111111111111111111
SQL_SetCharset(g_DB, "utf8");
// Create maps table
Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS maps(MapID INTEGER NOT NULL AUTO_INCREMENT, MapName TEXT, MapPlaytime INTEGER NOT NULL, LastPlayed INTEGER NOT NULL, PRIMARY KEY (MapID))");
SQL_TQuery(g_DB, DB_Connect_Callback, query);
// Create zones table
Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS zones(RowID INTEGER NOT NULL AUTO_INCREMENT, MapID INTEGER, Type INTEGER, point00 REAL, point01 REAL, point02 REAL, point10 REAL, point11 REAL, point12 REAL, flags INTEGER, PRIMARY KEY (RowID))");
SQL_TQuery(g_DB, DB_Connect_Callback, query);
// Create players table
Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS players(PlayerID INTEGER NOT NULL AUTO_INCREMENT, SteamID TEXT, User Text, Playtime INTEGER NOT NULL, ccname TEXT, ccmsgcol TEXT, ccuse INTEGER, PRIMARY KEY (PlayerID))");
SQL_TQuery(g_DB, DB_Connect_Callback, query);
// Create times table
Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS times(rownum INTEGER NOT NULL AUTO_INCREMENT, MapID INTEGER, Type INTEGER, Style INTEGER, PlayerID INTEGER, Time REAL, Jumps INTEGER, Strafes INTEGER, Points REAL, Timestamp INTEGER, Sync REAL, SyncTwo REAL, PRIMARY KEY (rownum))");
SQL_TQuery(g_DB, DB_Connect_Callback, query);
LoadPlayers();
LoadDatabaseMapList();
}
}
public DB_Connect_Callback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(hndl == INVALID_HANDLE)
{
LogError(error);
}
}
LoadDatabaseMapList()
{
decl String:query[256];
FormatEx(query, sizeof(query), "SELECT MapID, MapName FROM maps");
SQL_TQuery(g_DB, LoadDatabaseMapList_Callback, query);
}
public LoadDatabaseMapList_Callback(Handle:owner, Handle:hndl, String:error[], any:data)
{
if(hndl != INVALID_HANDLE)
{
if(g_bDbMapsLoaded == false)
{
g_hDbMapNameList = CreateArray(ByteCountToCells(64));
g_hDbMapIdList = CreateArray();
}
decl String:sMapName[64];
while(SQL_FetchRow(hndl))
{
SQL_FetchString(hndl, 1, sMapName, sizeof(sMapName));
PushArrayString(g_hDbMapNameList, sMapName);
PushArrayCell(g_hDbMapIdList, SQL_FetchInt(hndl, 0));
}
Call_StartForward(g_fwdMapListLoaded);
Call_Finish();
}
else
{
LogError(error);
}
}
LoadPlayers()
{
g_hPlayerID = CreateArray(ByteCountToCells(32));
g_hUser = CreateArray(ByteCountToCells(MAX_NAME_LENGTH));
decl String:query[128];
FormatEx(query, sizeof(query), "SELECT SteamID, PlayerID, User FROM players");
SQL_TQuery(g_DB, LoadPlayers_Callback, query);
}
public LoadPlayers_Callback(Handle:owner, Handle:hndl, String:error[], any:data)
{
if(hndl != INVALID_HANDLE)
{
decl String:sName[32], String:sAuth[32];
new RowCount = SQL_GetRowCount(hndl), PlayerID, iSize;
for(new Row; Row < RowCount; Row++)
{
SQL_FetchRow(hndl);
SQL_FetchString(hndl, 0, sAuth, sizeof(sAuth));
PlayerID = SQL_FetchInt(hndl, 1);
SQL_FetchString(hndl, 2, sName, sizeof(sName));
iSize = GetArraySize(g_hPlayerID);
if(PlayerID >= iSize)
{
ResizeArray(g_hPlayerID, PlayerID + 1);
ResizeArray(g_hUser, PlayerID + 1);
}
SetArrayString(g_hPlayerID, PlayerID, sAuth);
SetArrayString(g_hUser, PlayerID, sName);
}
g_bPlayerListLoaded = true;
for(new client = 1; client <= MaxClients; client++)
{
if(IsClientConnected(client) && !IsFakeClient(client))
{
if(IsClientAuthorized(client))
{
CreatePlayerID(client);
}
}
}
}
else
{
LogError(error);
}
}
CreateCurrentMapID()
{
new Handle:pack = CreateDataPack();
WritePackString(pack, g_sMapName);
decl String:query[512];
FormatEx(query, sizeof(query), "INSERT INTO maps (MapName) SELECT * FROM (SELECT '%s') AS tmp WHERE NOT EXISTS (SELECT MapName FROM maps WHERE MapName = '%s') LIMIT 1",
g_sMapName,
g_sMapName);
SQL_TQuery(g_DB, DB_CreateCurrentMapID_Callback, query, pack);
}
public DB_CreateCurrentMapID_Callback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(hndl != INVALID_HANDLE)
{
if(SQL_GetAffectedRows(hndl) > 0)
{
ResetPack(data);
decl String:sMapName[64];
ReadPackString(data, sMapName, sizeof(sMapName));
new MapID = SQL_GetInsertId(hndl);
LogMessage("MapID for %s created (%d)", sMapName, hndl);
if(g_bDbMapsLoaded == false)
{
g_hDbMapNameList = CreateArray(ByteCountToCells(64));
g_hDbMapIdList = CreateArray();
}
PushArrayString(g_hDbMapNameList, sMapName);
PushArrayCell(g_hDbMapIdList, MapID);
}
Call_StartForward(g_fwdMapIDPostCheck);
Call_Finish();
}
else
{
LogError(error);
}
CloseHandle(data);
}
CreatePlayerID(client)
{
decl String:sName[MAX_NAME_LENGTH];
GetClientName(client, sName, sizeof(sName));
decl String:sAuth[32];
//GetClientAuthString(client, sAuth, sizeof(sAuth));
GetClientAuthId(client, AuthId_Steam2, sAuth, sizeof(sAuth));
new idx = FindStringInArray(g_hPlayerID, sAuth);
if(idx != -1)
{
g_PlayerID[client] = idx;
decl String:sOldName[MAX_NAME_LENGTH];
GetArrayString(g_hUser, idx, sOldName, sizeof(sOldName));
if(!StrEqual(sName, sOldName))
{
UpdateName(client, sName);
}
Call_StartForward(g_fwdPlayerIDLoaded);
Call_PushCell(client);
Call_Finish();
}
else
{
decl String:sEscapeName[(2 * MAX_NAME_LENGTH) + 1];
SQL_LockDatabase(g_DB);
SQL_EscapeString(g_DB, sName, sEscapeName, sizeof(sEscapeName));
SQL_UnlockDatabase(g_DB);
new Handle:pack = CreateDataPack();
WritePackCell(pack, GetClientUserId(client));
WritePackString(pack, sAuth);
WritePackString(pack, sName);
decl String:query[128];
FormatEx(query, sizeof(query), "INSERT INTO players (SteamID, User) VALUES ('%s', '%s')",
sAuth,
sEscapeName);
SQL_TQuery(g_DB, CreatePlayerID_Callback, query, pack);
}
}
public CreatePlayerID_Callback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(hndl != INVALID_HANDLE)
{
ResetPack(data);
new client = GetClientOfUserId(ReadPackCell(data));
decl String:sAuth[32];
ReadPackString(data, sAuth, sizeof(sAuth));
decl String:sName[MAX_NAME_LENGTH];
ReadPackString(data, sName, sizeof(sName));
new PlayerID = SQL_GetInsertId(hndl);
new iSize = GetArraySize(g_hPlayerID);
if(PlayerID >= iSize)
{
ResizeArray(g_hPlayerID, PlayerID + 1);
ResizeArray(g_hUser, PlayerID + 1);
}
SetArrayString(g_hPlayerID, PlayerID, sAuth);
SetArrayString(g_hUser, PlayerID, sName);
if(client != 0)
{
g_PlayerID[client] = PlayerID;
Call_StartForward(g_fwdPlayerIDLoaded);
Call_PushCell(client);
Call_Finish();
}
}
else
{
LogError(error);
}
}
UpdateName(client, const String:sName[])
{
SetArrayString(g_hUser, g_PlayerID[client], sName);
decl String:sEscapeName[(2 * MAX_NAME_LENGTH) + 1];
SQL_LockDatabase(g_DB);
SQL_EscapeString(g_DB, sName, sEscapeName, sizeof(sEscapeName));
SQL_UnlockDatabase(g_DB);
decl String:query[128];
FormatEx(query, sizeof(query), "UPDATE players SET User='%s' WHERE PlayerID=%d",
sEscapeName,
g_PlayerID[client]);
SQL_TQuery(g_DB, UpdateName_Callback, query);
}
public UpdateName_Callback(Handle:owner, Handle:hndl, const String:error[], any:userid)
{
if(hndl == INVALID_HANDLE)
LogError(error);
}
public Native_GetClientID(Handle:plugin, numParams)
{
return g_PlayerID[GetNativeCell(1)];
}
DB_SavePlaytime(client)
{
if(IsClientInGame(client))
{
new PlayerID = GetPlayerID(client);
if(PlayerID != 0)
{
decl String:query[128];
Format(query, sizeof(query), "UPDATE players SET Playtime=(SELECT Playtime FROM (SELECT * FROM players) AS x WHERE PlayerID=%d)+%d WHERE PlayerID=%d",
PlayerID,
RoundToFloor(GetClientTime(client)),
PlayerID);
SQL_TQuery(g_DB, DB_SavePlaytime_Callback, query);
}
}
}
public DB_SavePlaytime_Callback(Handle:owner, Handle:hndl, String:error[], any:data)
{
if(hndl == INVALID_HANDLE)
LogError(error);
}
DB_SaveMapPlaytime()
{
decl String:query[256];
Format(query, sizeof(query), "UPDATE maps SET MapPlaytime=(SELECT MapPlaytime FROM (SELECT * FROM maps) AS x WHERE MapName='%s' LIMIT 0, 1)+%d WHERE MapName='%s'",
g_sMapName,
RoundToFloor(GetEngineTime()-g_fMapStart),
g_sMapName);
SQL_TQuery(g_DB, DB_SaveMapPlaytime_Callback, query);
}
public DB_SaveMapPlaytime_Callback(Handle:owner, Handle:hndl, String:error[], any:data)
{
if(hndl == INVALID_HANDLE)
LogError(error);
}
DB_SetMapLastPlayed()
{
decl String:query[128];
Format(query, sizeof(query), "UPDATE maps SET LastPlayed=%d WHERE MapName='%s'",
GetTime(),
g_sMapName);
SQL_TQuery(g_DB, DB_SetMapLastPlayed_Callback, query);
}
public DB_SetMapLastPlayed_Callback(Handle:owner, Handle:hndl, String:error[], any:data)
{
if(hndl == INVALID_HANDLE)
LogError(error);
}
public Action:SM_Playtime(client, args)
{
if(!IsSpamming(client))
{
SetIsSpamming(client, 1.0);
if(args == 0)
{
if(g_PlayerID[client] != 0)
{
DB_ShowPlaytime(client, g_PlayerID[client]);
}
}
else
{
decl String:sArg[MAX_NAME_LENGTH];
GetCmdArgString(sArg, sizeof(sArg));
new target = FindTarget(client, sArg, true, false);
if(target != -1)
{
if(g_PlayerID[target] != 0)
{
DB_ShowPlaytime(client, g_PlayerID[target]);
}
}
}
}
return Plugin_Handled;
}
DB_ShowPlaytime(client, PlayerID)
{
new Handle:pack = CreateDataPack();
WritePackCell(pack, GetClientUserId(client));
WritePackCell(pack, PlayerID);
decl String:query[512];
Format(query, sizeof(query), "SELECT (SELECT Playtime FROM players WHERE PlayerID=%d) AS TargetPlaytime, User, Playtime, PlayerID FROM players ORDER BY Playtime DESC LIMIT 0, 100",
PlayerID);
SQL_TQuery(g_DB, DB_ShowPlaytime_Callback, query, pack);
}
public DB_ShowPlaytime_Callback(Handle:owner, Handle:hndl, String:error[], any:data)
{
if(hndl != INVALID_HANDLE)
{
ResetPack(data);
new client = GetClientOfUserId(ReadPackCell(data));
if(client != 0)
{
new rows = SQL_GetRowCount(hndl);
if(rows != 0)
{
new TargetPlayerID = ReadPackCell(data);
new Handle:menu = CreateMenu(Menu_ShowPlaytime);
decl String:sName[MAX_NAME_LENGTH], String:sTime[32], String:sDisplay[64], String:sInfo[16], PlayTime, PlayerID, TargetPlaytime;
for(new i = 1; i <= rows; i++)
{
SQL_FetchRow(hndl);
TargetPlaytime = SQL_FetchInt(hndl, 0);
SQL_FetchString(hndl, 1, sName, sizeof(sName));
PlayTime = SQL_FetchInt(hndl, 2);
PlayerID = SQL_FetchInt(hndl, 3);
// Set info
IntToString(PlayerID, sInfo, sizeof(sInfo));
// Set display
FormatPlayerTime(float(PlayTime), sTime, sizeof(sTime), false, 1);
SplitString(sTime, ".", sTime, sizeof(sTime));
FormatEx(sDisplay, sizeof(sDisplay), "#%d: %s: %s", i, sName, sTime);
if((i % 7) == 0 || i == rows)
{
Format(sDisplay, sizeof(sDisplay), "%s\n--------------------------------------", sDisplay);
}
// Add item
AddMenuItem(menu, sInfo, sDisplay);
}
GetNameFromPlayerID(TargetPlayerID, sName, sizeof(sName));
new Float:ConnectionTime, target;
if((target = GetClientFromPlayerID(TargetPlayerID)) != 0)
{
ConnectionTime = GetClientTime(target);
}
FormatPlayerTime(ConnectionTime + float(TargetPlaytime), sTime, sizeof(sTime), false, 1);
SplitString(sTime, ".", sTime, sizeof(sTime));
SetMenuTitle(menu, "Playtimes\n \n%s: %s\n--------------------------------------",
sName,
sTime);
SetMenuExitButton(menu, true);
DisplayMenu(menu, client, MENU_TIME_FOREVER);
}
}
}
else
{
LogError(error);
}
CloseHandle(data);
}
public Menu_ShowPlaytime(Handle:menu, MenuAction:action, param1, param2)
{
if(action == MenuAction_End)
CloseHandle(menu);
}
public Action:SM_Search(client, args)
{
if(args > 0)
{
decl String:sArgString[255], String:sResult[256];
GetCmdArgString(sArgString, sizeof(sArgString));
new iSize = GetArraySize(g_hCommandList);
for(new i=0; i<iSize; i++)
{
GetArrayString(g_hCommandList, i, sResult, sizeof(sResult));
if(StrContains(sResult, sArgString, false) != -1)
{
PrintToConsole(client, sResult);
}
}
}
else
{
CPrintToChat(client, "%s%ssm_search must have a string to search with after it.",
g_msg_start,
g_msg_textcol);
}
return Plugin_Handled;
}
GetClientFromPlayerID(PlayerID)
{
for(new client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client) && !IsFakeClient(client) && g_PlayerID[client] == PlayerID)
{
return client;
}
}
return 0;
}
public Native_IsSpamming(Handle:plugin, numParams)
{
return GetEngineTime() < g_fSpamTime[GetNativeCell(1)];
}
public Native_SetIsSpamming(Handle:plugin, numParams)
{
g_fSpamTime[GetNativeCell(1)] = Float:GetNativeCell(2) + GetEngineTime();
}
public Native_RegisterCommand(Handle:plugin, numParams)
{
if(g_bCommandListLoaded == false)
{
g_hCommandList = CreateArray(ByteCountToCells(256));
g_bCommandListLoaded = true;
}
decl String:sListing[256], String:sCommand[32], String:sDesc[224];
GetNativeString(1, sCommand, sizeof(sCommand));
GetNativeString(2, sDesc, sizeof(sDesc));
FormatEx(sListing, sizeof(sListing), "%s - %s", sCommand, sDesc);
decl String:sIndex[256];
new idx, idxlen, listlen = strlen(sListing), iSize = GetArraySize(g_hCommandList), bool:IdxFound;
for(; idx < iSize; idx++)
{
GetArrayString(g_hCommandList, idx, sIndex, sizeof(sIndex));
idxlen = strlen(sIndex);
for(new cmpidx = 0; cmpidx < listlen && cmpidx < idxlen; cmpidx++)
{
if(sListing[cmpidx] < sIndex[cmpidx])
{
IdxFound = true;
break;
}
else if(sListing[cmpidx] > sIndex[cmpidx])
{
break;
}
}
if(IdxFound == true)
break;
}
if(idx >= iSize)
ResizeArray(g_hCommandList, idx + 1);
else
ShiftArrayUp(g_hCommandList, idx);
SetArrayString(g_hCommandList, idx, sListing);
}
public Native_GetMapNameFromMapId(Handle:plugin, numParams)
{
new Index = FindValueInArray(g_hDbMapIdList, GetNativeCell(1));
if(Index != -1)
{
decl String:sMapName[64];
GetArrayString(g_hDbMapNameList, Index, sMapName, sizeof(sMapName));
SetNativeString(2, sMapName, GetNativeCell(3));
return true;
}
else
{
return false;
}
}
public Native_GetNameFromPlayerID(Handle:plugin, numParams)
{
decl String:sName[MAX_NAME_LENGTH];
GetArrayString(g_hUser, GetNativeCell(1), sName, sizeof(sName));
SetNativeString(2, sName, GetNativeCell(3));
}
public Native_GetSteamIDFromPlayerID(Handle:plugin, numParams)
{
decl String:sAuth[32];
GetArrayString(g_hPlayerID, GetNativeCell(1), sAuth, sizeof(sAuth));
SetNativeString(2, sAuth, GetNativeCell(3));
}
public Native_GetMapIdFromMapName(Handle:plugin, numParams)
{
decl String:sMapName[64];
GetNativeString(1, sMapName, sizeof(sMapName));
new Index = FindStringInArray(g_hDbMapNameList, sMapName);
if(Index != -1)
{
return GetArrayCell(g_hDbMapIdList, Index);
}
else
{
return 0;