-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSurfTimerRank.sp
More file actions
1738 lines (1408 loc) · 54.3 KB
/
SurfTimerRank.sp
File metadata and controls
1738 lines (1408 loc) · 54.3 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 <SurfTimerRank>
#undef REQUIRE_PLUGIN
#include <SurfTimer>
#include <SurfTimerMap>
#include <SurfTimerZones>
#include <system2>
new Handle:g_hSQL = INVALID_HANDLE;
new String:g_sCurrentMap[MAPNAME_MAX];
// mysql
new g_iSQLReconnectCounter = 0;
#define MAX_ZONES 40
// map informations
enum Map
{
Id,
Difficulty,
TimesCompleted,
TimesCompletedDistinct,
LatestRecordCount,
WorldRecordCount,
BonusTimesCompleted,
BonusWorldRecordCount,
CompletedStagesCount,
StageCount,
Float:WorldRecordTime,
}
enum Record
{
Id,
float:Time,
UserId,
String:Username[USERNAME_MAX],
String:AuthName[AUTHID_MAX],
}
enum GlobalRank
{
Rank,
String:Username[USERNAME_MAX],
UserPoints
}
enum User
{
Id,
String:AuthName[AUTHID_MAX],
float:PersonalTime,
}
enum Stages
{
Id,
OrderId,
Type,
Float:WorldRecordTime,
bool:Unfinished,
String:Username[USERNAME_MAX],
String:AuthName[AUTHID_MAX],
}
enum PlayerStageRecord
{
StageId,
OrderId,
StageType,
Float:RecordTime,
bool:HasFinished,
CompletedStages,
}
new g_map[Map];
new g_mapRecords[1000][Record];
new g_latestMapRecords[10][Record];
new g_userRank[10000][GlobalRank];
new g_mapStageRecords[MAX_ZONES][Stages]; // identified by map_zone_checkpoint_id - For normal stages only
new g_playerStageRecords[MAXPLAYERS+1][MAX_ZONES][PlayerStageRecord];
new g_user[MAXPLAYERS][User];
new g_userGlobalCount = 0;
new String:g_soundName[] = "surftimer/SurfTimer_WR1.mp3";
new String:g_soundNamePersonalWr[] = "surftimer/SurfTimer_WR2.mp3";
public Plugin:myinfo =
{
name = "[SurfTimer]-Rank",
author = "Fuxx",
description = "The player rank database",
version = PLUGIN_VERSION,
url = "http://www.stefanpopp.de"
};
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
CreateNative("SurfTimerRank_playerDidFinish", Native_SurfTimerRank_playerDidFinish);
CreateNative("SurfTimer_currentWRData", Native_SurfTimer_currentWRData);
CreateNative("SurfTimer_currentStageWRData", Native_SurfTimer_currentStageWRData);
CreateNative("SurfTimer_reloadStats", Native_SurfTimer_reloadStats);
CreateNative("SurfTimerRank_playerDidFinishStage", Native_SurfTimerRank_playerDidFinishStage);
CreateNative("SurfTimerRank_SurfTimer_stageCount", Native_SurfTimerRank_SurfTimer_stageCount);
return APLRes_Success;
}
/**
* On Connect
*/
public OnPluginStart()
{
// load map informations form sql
if (g_hSQL == INVALID_HANDLE) {
ConnectSQL();
}
PrintToServer("[SurfTimer-Rank] %s loaded...", PLUGIN_URL);
RegConsoleCmd("sm_ptop", Global_top, "Show global player top list");
RegConsoleCmd("sm_top10", Map_top, "Show top for the current map");
RegConsoleCmd("sm_top", Map_top, "Show map top list - Optional map name accepted");
RegConsoleCmd("sm_mtop", Map_top, "Show map top list - Optional map name accepted");
RegConsoleCmd("sm_wr", Map_top, "Show map top list - Optional map name accepted");
RegConsoleCmd("sm_dhctop", Map_dhcTop, "Shows your global rank or of a given name");
RegConsoleCmd("sm_rr", Map_latestRecords, "Show latest world records on current map");
RegConsoleCmd("sm_recent", Map_latestRecords, "Show latest world records on current map");
RegConsoleCmd("sm_rrg", Map_latestRecordsGlobal, "Show latest world records on current map");
RegConsoleCmd("sm_recentglobal", Map_latestRecordsGlobal, "Show latest world records on current map");
RegConsoleCmd("sm_grr", Map_latestRecordsGlobal, "Show latest world records on current map");
RegConsoleCmd("sm_globalrecent", Map_latestRecordsGlobal, "Show latest world records on lasts map");
RegConsoleCmd("sm_mr", Map_rank, "Shows map rank for a given map and optional player name");
RegConsoleCmd("sm_mrank", Map_rank, "Shows map rank for a given map and optional player name");
RegConsoleCmd("sm_rank", Map_User_rank, "Shows your rank on current map");
RegConsoleCmd("sm_r", Map_User_rank, "Shows your rank on current map");
RegConsoleCmd("sm_prank", User_rank, "Shows your global rank or of a given name");
RegConsoleCmd("sm_pr", User_rank, "Shows your global rank or of a given name");
RegConsoleCmd("sm_wrcp", Map_stageRecords, "Shows world records for stages on current map");
RegConsoleCmd("sm_cpwr", Map_stageRecords, "Shows world records for stages on current map");
RegConsoleCmd("sm_stagewr", Map_stageRecords, "Shows world records for stages on current map");
RegConsoleCmd("sm_stages", Map_stageRecords, "Shows world records for stages on current map");
RegConsoleCmd("sm_stagetop", Map_stageRecords, "Shows world records for stages on current map");
}
public OnMapStart()
{
GetCurrentMap(g_sCurrentMap, sizeof(g_sCurrentMap));
StringToLower(g_sCurrentMap);
// load map informations form sql
if (g_hSQL == INVALID_HANDLE) {
ConnectSQL();
}
for (new i = 0; i < MAX_ZONES; i++) {
g_mapStageRecords[i][Id] = 0;
g_mapStageRecords[i][OrderId] = 0;
g_mapStageRecords[i][WorldRecordTime] = 0.0;
}
for (new i = 0; i < MAXPLAYERS+1; i++) {
for (new j = 0; j < MAX_ZONES; j++) {
g_playerStageRecords[i][j][StageId] = 0;
g_playerStageRecords[i][j][OrderId] = 0;
g_playerStageRecords[i][j][RecordTime] = 0.0;
}
}
LoadMap();
LoadUserRecords();
LoadMapStageRecords();
}
public OnMapEnd()
{
g_userGlobalCount = 0;
}
public OnClientDisconnect(client)
{
decl String:emptyString[AUTHID_MAX];
Format(emptyString, sizeof(emptyString), "");
strcopy(g_user[client][AuthName], sizeof(emptyString), emptyString);
ResetPlayerStageRecord(client);
}
public OnConfigsExecuted()
{
decl String:buffer[128];
PrecacheSound(g_soundName, true);
Format(buffer, sizeof(buffer), "sound/%s", g_soundName);
AddFileToDownloadsTable(buffer);
PrecacheSound(g_soundNamePersonalWr, true);
Format(buffer, sizeof(buffer), "sound/%s", g_soundNamePersonalWr);
AddFileToDownloadsTable(buffer);
// doppelt hält besser
GetCurrentMap(g_sCurrentMap, sizeof(g_sCurrentMap));
StringToLower(g_sCurrentMap);
LoadMap();
}
public OnClientAuthorized(client, const String:auth[])
{
ResetPlayerStageRecord(client);
LoadUser(client);
}
public OnClientPostAdminCheck(client)
{
GetClientAuthString(client, g_user[client][AuthName], 64);
}
public AdminMenu_IgnoreSelection(Handle:menu, MenuAction:action, param1, param2)
{
if (action == MenuAction_Select) {
} else if (action == MenuAction_Cancel) {
} else if (action == MenuAction_End) {
CloseHandle(menu);
}
}
/**
* MySQL
*/
ConnectSQL()
{
if (g_hSQL != INVALID_HANDLE) {
CloseHandle(g_hSQL);
}
g_hSQL = INVALID_HANDLE;
if (SQL_CheckConfig("default")) {
SQL_TConnect(ConnectSQLCallback, "default");
} else {
SetFailState("PLUGIN STOPPED - Reason: no config entry found for 'default' in databases.cfg - PLUGIN STOPPED");
}
}
public ConnectSQLCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (g_iSQLReconnectCounter >= 5) {
PrintToServer("PLUGIN STOPPED - Reason: reconnect counter reached max - PLUGIN STOPPED");
return;
}
if (hndl == INVALID_HANDLE) {
PrintToServer("Connection to SQL database has failed, Reason: %s", error);
g_iSQLReconnectCounter++;
ConnectSQL();
return;
}
decl String:sDriver[16];
SQL_GetDriverIdent(owner, sDriver, sizeof(sDriver));
if (g_hSQL != INVALID_HANDLE) {
CloseHandle(g_hSQL);
}
g_hSQL = INVALID_HANDLE;
g_hSQL = CloneHandle(hndl);
if (StrEqual(sDriver, "mysql", false)) {
SQL_TQuery(g_hSQL, SetNamesCallback, "SET NAMES 'UTF8'", _, DBPrio_High);
} else {
SetFailState("PLUGIN STOPPED - Reason: MySQL required. Please define valid data source - PLUGIN STOPPED");
}
g_iSQLReconnectCounter = 1;
LoadMap();
}
public SetNamesCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on SetNames: %s", error);
return;
}
if (g_iSQLReconnectCounter) {
g_iSQLReconnectCounter = 0;
}
}
LoadMap()
{
g_map[TimesCompleted] = 0;
g_map[WorldRecordCount] = 0;
g_map[BonusTimesCompleted] = 0;
g_map[BonusWorldRecordCount] = 0;
decl String:sQuery[1024];
FormatEx(sQuery, sizeof(sQuery), "SELECT * FROM maps WHERE map_name = '%s' LIMIT 1", g_sCurrentMap);
if (g_hSQL != INVALID_HANDLE) {
SQL_TQuery(g_hSQL, LoadMapCallback, sQuery, _, DBPrio_High);
} else {
CreateTimer(4.0, TimerLoadMap, _, TIMER_FLAG_NO_MAPCHANGE);
}
}
public Action:TimerLoadMap(Handle:timer, any:serial)
{
LoadMap();
LoadUserRecords();
LoadMapStageRecords();
}
public LoadMapCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on LoadMap: %s", error);
LoadUserRecords();
return;
}
if (! SQL_GetRowCount(hndl)) {
CPrintToChatAll("%s{red}No informations found for map %s", PLUGIN_PREFIX, g_sCurrentMap);
return;
}
SQL_FetchRow(hndl);
g_map[Id] = SQL_FetchInt(hndl, 0);
g_map[Difficulty] = SQL_FetchInt(hndl, 3);
g_map[TimesCompletedDistinct] = SQL_FetchInt(hndl, 7);
g_map[WorldRecordCount] = SQL_FetchInt(hndl, 9);
g_map[BonusTimesCompleted] = SQL_FetchInt(hndl, 8);
g_map[BonusWorldRecordCount] = SQL_FetchInt(hndl, 10);
LoadMapRecords();
}
LoadMapRecords()
{
decl String:sQuery[1024];
FormatEx(sQuery, sizeof(sQuery), "SELECT *, MIN(user_record_time) AS record_time FROM user_records LEFT JOIN user ON user.user_id = user_records.user_user_id WHERE user_record_time > 0.0 AND maps_map_id = %d GROUP BY user_user_id ORDER BY MIN(user_record_time) LIMIT 100", g_map[Id]);
SQL_TQuery(g_hSQL, LoadMapRecordsCallback, sQuery, _, DBPrio_High);
}
LoadLatestMapRecords()
{
g_map[LatestRecordCount] = 0;
decl String:sQuery[1024];
FormatEx(sQuery, sizeof(sQuery), "SELECT *, user_record_time AS record_time FROM user_records LEFT JOIN user ON user.user_id = user_records.user_user_id WHERE user_record_time > 0.0 AND maps_map_id = %d ORDER BY user_records.user_record_created_at DESC LIMIT 10", g_map[Id]);
SQL_TQuery(g_hSQL, LoadLatestMapRecordsCallback, sQuery, _, DBPrio_High);
}
LoadMapStageRecords()
{
g_map[CompletedStagesCount] = 0;
g_map[StageCount] = 0;
decl String:sQuery[2048];
FormatEx(sQuery, sizeof(sQuery), "SELECT a.map_zone_id, a.map_zone_checkpoint_order_id, a.map_id, a.map_zone_type, b.map_enabled, IFNULL(MIN(c.user_stage_records_time), 0.0) as stage_record, d.user_name, d.user_steam_id, d.user_id FROM map_zones as a LEFT JOIN maps AS b ON b.map_id = a.map_id LEFT JOIN user_stage_records AS c on (a.map_zone_id = c.user_stage_records_stage_id AND c.user_stage_records_time = (SELECT MIN(user_stage_records_time) FROM user_stage_records WHERE user_stage_records_stage_id = c.user_stage_records_stage_id)) LEFT JOIN user AS d on c.user_user_id = d.user_id WHERE b.map_id = %d AND (map_zone_type = 2 OR map_zone_type = 3) GROUP BY a.map_zone_id ORDER BY a.map_zone_type, a.map_zone_checkpoint_order_id", g_map[Id]);
SQL_TQuery(g_hSQL, LoadMapStageRecordsCallback, sQuery, _, DBPrio_High);
}
LoadPlayerStageRecords(client)
{
decl String:sQuery[4096];
FormatEx(sQuery, sizeof(sQuery), "SELECT a.map_zone_id, a.map_zone_checkpoint_order_id, a.map_id, a.map_zone_type, IFNULL(MIN(c.user_stage_records_time), 0.0) AS stage_record, d.user_name, d.user_steam_id, d.user_id, COALESCE(d.user_id, %d) as fixed_user_id, IF (COALESCE(MIN(c.user_stage_records_time), 0.0) = 0.0, 0, 1) as hasFinished FROM map_zones as a LEFT JOIN maps AS b ON b.map_id = a.map_id LEFT JOIN user_stage_records AS c on c.user_stage_records_stage_id = a.map_zone_id AND c.user_user_id = %d LEFT JOIN user AS d on c.user_user_id = d.user_id WHERE b.map_id = %d AND (a.map_zone_type = 2 OR a.map_zone_type = 3) GROUP BY a.map_zone_id ORDER BY a.map_zone_type, a.map_zone_checkpoint_order_id", g_user[client][Id], g_user[client][Id], g_map[Id]);
//PrintToServer("%s", sQuery);
new Handle:pack = CreateDataPack();
WritePackCell(pack, client);
SQL_TQuery(g_hSQL, LoadPlayerStageRecordsCallback, sQuery, pack, DBPrio_High);
}
LoadMapRecordsAndShowTop(client, const String:mapName[])
{
decl String:sQuery[1024];
new bufferLen = strlen(mapName) * 2 + 1;
new String:newMapName[bufferLen];
SQL_EscapeString(g_hSQL, mapName, newMapName, bufferLen);
Format(sQuery, sizeof(sQuery), "SELECT *, MIN(user_record_time) AS record_time FROM user_records LEFT JOIN user ON user.user_id = user_records.user_user_id WHERE user_record_time > 0.0 AND maps_map_id = (SELECT map_id FROM maps where map_name = '%s') GROUP BY user_user_id ORDER BY MIN(user_record_time) LIMIT 100", newMapName);
new Handle:pack = CreateDataPack();
WritePackCell(pack, client);
WritePackString(pack, mapName);
SQL_TQuery(g_hSQL, LoadMapRecordsAndShowTopCallback, sQuery, pack, DBPrio_High);
}
public LoadMapRecordsAndShowTopCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
decl String:mapName[MAPNAME_MAX];
ResetPack(data);
new client = ReadPackCell(data);
ReadPackString(data, mapName, sizeof(mapName));
CloseHandle(data);
if (! SQL_GetRowCount(hndl)) {
CPrintToChatAll("%s{lightgreen} No record informations found for map %s", PLUGIN_PREFIX, mapName);
return;
}
new recordCounter = 0;
new Handle:menu = CreateMenu(MapTopMenuHandler, MENU_ACTIONS_ALL);
decl String:menuTitle[128];
Format(menuTitle, sizeof(menuTitle), "Map top 100 - %s", mapName);
SetMenuTitle(menu, menuTitle);
while (SQL_FetchRow(hndl)) {
decl username[USERNAME_MAX];
SQL_FetchString(hndl, 9, username, USERNAME_MAX);
decl String:sTimeString[128];
SurfTimer_secondsToTime(SQL_FetchFloat(hndl, 14), sTimeString, sizeof(sTimeString), true);
decl String:titleString[512];
Format(titleString, sizeof(titleString), "[#%d] %s - %s", (recordCounter+1), username, sTimeString);
AddMenuItem(menu, "", titleString, ITEMDRAW_DISABLED);
recordCounter++;
}
if (recordCounter == 0) {
decl String:titleString[512];
Format(titleString, sizeof(titleString), "No world record found. Good luck!");
AddMenuItem(menu, "", titleString, ITEMDRAW_DISABLED);
}
SetMenuExitButton(menu, true);
DisplayMenu(menu, client, 20);
}
ShowStageRecordMenu(client, stageId)
{
new fixedStageId = stageId+1;
new stageCount = 0;
SurfTimerMap_stageCount(stageCount);
if (fixedStageId == stageCount) {
fixedStageId = 0;
} else if ((stageId+1) > stageCount) {
CPrintToChatAll("%s{lightgreen}{red}There is no stage {white}%d{red}.", PLUGIN_PREFIX, fixedStageId);
return;
}
decl String:sQuery[2048];
Format(sQuery, sizeof(sQuery), "SELECT c.user_name, c.user_id, MIN(a.user_stage_records_time) as recordTime FROM user_stage_records AS a LEFT JOIN map_zones AS b ON b.map_zone_id = a.user_stage_records_stage_id LEFT JOIN user as c on a.user_user_id = c.user_id WHERE b.map_id = %d AND (map_zone_type = 2 OR map_zone_type = 3) AND b.map_zone_checkpoint_order_id = %d GROUP BY a.user_user_id, b.map_zone_checkpoint_order_id ORDER BY recordTime", g_map[Id], fixedStageId);
new Handle:pack = CreateDataPack();
WritePackCell(pack, client);
WritePackCell(pack, (stageId+1));
SQL_TQuery(g_hSQL, ShowStageRecordMenuCallback, sQuery, pack, DBPrio_High);
}
public ShowStageRecordMenuCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
ResetPack(data);
new client = ReadPackCell(data);
new stageId = ReadPackCell(data);
CloseHandle(data);
if (! SQL_GetRowCount(hndl)) {
CPrintToChatAll("%s{lightgreen}{red}No records found for stage {white}%d{red}.", PLUGIN_PREFIX, stageId);
return;
}
new recordCounter = 0;
new Handle:menu = CreateMenu(StageTopMenuHandler, MENU_ACTIONS_ALL);
decl String:menuTitle[64];
Format(menuTitle, 64, "Records for stage %d", stageId);
SetMenuTitle(menu, "Stage records");
while (SQL_FetchRow(hndl)) {
decl username[USERNAME_MAX];
SQL_FetchString(hndl, 0, username, USERNAME_MAX);
new userId = SQL_FetchInt(hndl, 1);
decl String:sTimeString[128];
SurfTimer_secondsToTime(SQL_FetchFloat(hndl, 2), sTimeString, sizeof(sTimeString), true);
decl String:titleString[512];
Format(titleString, sizeof(titleString), "[#%d] %s - %s", (recordCounter+1), username, sTimeString);
AddMenuItem(menu, "", titleString, ITEMDRAW_DISABLED);
recordCounter++;
}
SetMenuExitButton(menu, true);
DisplayMenu(menu, client, 20);
}
LoadLatestGlobalMapRecordsAndShowMenu(client)
{
decl String:sQuery[2048];
Format(sQuery, sizeof(sQuery), "SELECT user_name, map_name, user_record_time AS record_time FROM user_records LEFT JOIN user ON user.user_id = user_records.user_user_id LEFT JOIN maps ON maps.map_id = user_records.maps_map_id WHERE user_record_was_wr = 1 ORDER BY user_records.user_record_created_at DESC LIMIT 100");
new Handle:pack = CreateDataPack();
WritePackCell(pack, client);
SQL_TQuery(g_hSQL, LoadLatestGlobalMapRecordsAndShowMenuCallback, sQuery, pack, DBPrio_High);
}
public LoadLatestGlobalMapRecordsAndShowMenuCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
ResetPack(data);
new client = ReadPackCell(data);
CloseHandle(data);
if (! SQL_GetRowCount(hndl)) {
CPrintToChatAll("%s{lightgreen} No latest global record informations found", PLUGIN_PREFIX);
return;
}
new recordCounter = 0;
new Handle:menu = CreateMenu(MapTopMenuHandler, MENU_ACTIONS_ALL);
SetMenuTitle(menu, "Recent 100 world records");
while (SQL_FetchRow(hndl)) {
decl username[USERNAME_MAX];
SQL_FetchString(hndl, 0, username, USERNAME_MAX);
decl mapname[MAPNAME_MAX];
SQL_FetchString(hndl, 1, mapname, MAPNAME_MAX);
decl String:sTimeString[128];
SurfTimer_secondsToTime(SQL_FetchFloat(hndl, 2), sTimeString, sizeof(sTimeString), true);
decl String:titleString[512];
Format(titleString, sizeof(titleString), "[#%d] %s - %s (%s)", (recordCounter+1), mapname, sTimeString, username);
AddMenuItem(menu, "", titleString, ITEMDRAW_DISABLED);
recordCounter++;
}
if (recordCounter == 0) {
decl String:titleString[512];
Format(titleString, sizeof(titleString), "No recent global world records found.\nTry it again!");
AddMenuItem(menu, "", titleString, ITEMDRAW_DISABLED);
}
SetMenuExitButton(menu, true);
DisplayMenu(menu, client, 20);
}
public LoadMapRecordsCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on LoadMap: %s", error);
LoadUserRecords();
return;
}
if (! SQL_GetRowCount(hndl)) {
g_map[TimesCompleted] = 0;
CPrintToChatAll("%s{lightgreen} No record informations found for map %s", PLUGIN_PREFIX, g_sCurrentMap);
CPrintToChatAll("%s{lightgreen} Have fun hunting the world record!", PLUGIN_PREFIX);
PrintToServer("[SurfTimer-Records] no records found for map %s", g_sCurrentMap);
LoadMapStageRecords();
return;
}
new recordCounter = 0;
while (SQL_FetchRow(hndl)) {
g_mapRecords[recordCounter][Id] = SQL_FetchInt(hndl, 0);
g_mapRecords[recordCounter][Time] = SQL_FetchFloat(hndl, 14);
g_mapRecords[recordCounter][UserId] = SQL_FetchFloat(hndl, 7);
SQL_FetchString(hndl, 8, g_mapRecords[recordCounter][AuthName], USERNAME_MAX);
SQL_FetchString(hndl, 9, g_mapRecords[recordCounter][Username], USERNAME_MAX);
recordCounter++;
}
g_map[TimesCompletedDistinct] = recordCounter;
g_map[WorldRecordTime] = g_mapRecords[0][Time];
PrintToServer("[SurfTimer-Records] loaded %d records from database", recordCounter);
LoadLatestMapRecords();
}
public LoadMapStageRecordsCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on LoadMapStageRecordsCallback: %s", error);
LoadMapStageRecords();
return;
}
if (! SQL_GetRowCount(hndl)) {
CPrintToChatAll("%s{lightgreen} No stage record informations found for map %s", PLUGIN_PREFIX, g_sCurrentMap);
CPrintToChatAll("%s{lightgreen} Have fun hunting the stage records!", PLUGIN_PREFIX);
PrintToServer("[SurfTimer-Records] no stage records found for map %s", g_sCurrentMap);
return;
}
new recordCounter = 0;
new stageCount = 0;
while (SQL_FetchRow(hndl)) {
new orderId = SQL_FetchInt(hndl, 1);
g_mapStageRecords[orderId][Id] = SQL_FetchInt(hndl, 0);
g_mapStageRecords[orderId][OrderId] = SQL_FetchInt(hndl, 1);
g_mapStageRecords[orderId][Type] = SQL_FetchInt(hndl, 3);
g_mapStageRecords[orderId][WorldRecordTime] = SQL_FetchFloat(hndl, 5);
if (0.0 == FloatAbs(SQL_FetchFloat(hndl, 5))) {
g_mapStageRecords[orderId][Unfinished] = true;
} else {
g_mapStageRecords[orderId][Unfinished] = false;
stageCount++;
}
SQL_FetchString(hndl, 6, g_mapStageRecords[orderId][Username], USERNAME_MAX);
SQL_FetchString(hndl, 7, g_mapStageRecords[orderId][AuthName], AUTHID_MAX);
recordCounter++;
}
g_map[CompletedStagesCount] = recordCounter;
g_map[StageCount] = StageCount;
PrintToServer("[SurfTimer-Records] loaded %d stage records (%d/%d completed) from database", recordCounter, stageCount, recordCounter);
}
public LoadLatestMapRecordsCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on LoadMap: %s", error);
return;
}
if (! SQL_GetRowCount(hndl)) {
return;
}
new recordCounter = 0;
while (SQL_FetchRow(hndl)) {
g_latestMapRecords[recordCounter][Id] = SQL_FetchInt(hndl, 0);
g_latestMapRecords[recordCounter][Time] = SQL_FetchFloat(hndl, 14);
g_latestMapRecords[recordCounter][UserId] = SQL_FetchFloat(hndl, 7);
SQL_FetchString(hndl, 8, g_latestMapRecords[recordCounter][AuthName], USERNAME_MAX);
SQL_FetchString(hndl, 9, g_latestMapRecords[recordCounter][Username], USERNAME_MAX);
recordCounter++;
if (recordCounter == 9) {
break;
}
}
g_map[LatestRecordCount] = recordCounter;
}
public Action:TimerLoadUserRecords(Handle:timer, any:serial)
{
LoadUserRecords();
}
public LoadUserRecords()
{
decl String:sQuery[2048];
FormatEx(sQuery, sizeof(sQuery), "SELECT ranking.rank, ranking.user_name, ranking.user_id, ranking.user_points, (SELECT COUNT(*) as userCount FROM user) as userCount FROM (SELECT user_id, user_name, user_points, @rank := @rank+1 AS rank FROM user, (SELECT @rank := 0 AS rank) AS init ORDER BY user_points DESC) AS ranking");
SQL_TQuery(g_hSQL, LoadUserRecordsCallback, sQuery, _, DBPrio_Low);
}
public LoadUserRecordsCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on LoadMap: %s", error);
CreateTimer(3.0, TimerLoadUserRecords, _, TIMER_FLAG_NO_MAPCHANGE);
return;
}
if (! SQL_GetRowCount(hndl)) {
g_map[TimesCompleted] = 0;
CPrintToChatAll("%s{lightgreen} No global record informations", PLUGIN_PREFIX);
PrintToServer("[SurfTimer-Records] No global record informations");
CreateTimer(3.0, TimerLoadUserRecords, _, TIMER_FLAG_NO_MAPCHANGE);
return;
}
new recordCounter = 0;
while (SQL_FetchRow(hndl)) {
g_userRank[recordCounter][Rank] = SQL_FetchInt(hndl, 0);
SQL_FetchString(hndl, 1, g_userRank[recordCounter][Username], USERNAME_MAX);
g_userRank[recordCounter][UserPoints] = SQL_FetchInt(hndl, 3);
recordCounter++;
}
g_userGlobalCount = recordCounter;
PrintToServer("[SurfTimer-Records] loaded %d overall records from database", recordCounter);
}
public LoadPlayerStageRecordsCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
new Handle:pack = data;
ResetPack(pack);
new client = ReadPackCell(pack);
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on LoadPlayerStageRecordsCallback: %s", error);
LoadPlayerStageRecords(client);
return;
}
if (! SQL_GetRowCount(hndl)) {
PrintToServer("[SurfTimer-Records] no stage records found for map %s", g_sCurrentMap);
return;
}
ResetPlayerStageRecord(client);
new recordCounter = 0;
new stageCount = 0;
while (SQL_FetchRow(hndl)) {
g_playerStageRecords[client][recordCounter][StageId] = SQL_FetchInt(hndl, 0);
g_playerStageRecords[client][recordCounter][OrderId] = SQL_FetchInt(hndl, 1);
g_playerStageRecords[client][recordCounter][StageType] = SQL_FetchInt(hndl, 3);
g_playerStageRecords[client][recordCounter][RecordTime] = SQL_FetchFloat(hndl, 4);
g_playerStageRecords[client][recordCounter][HasFinished] = SQL_FetchInt(hndl, 9);
if (g_playerStageRecords[client][recordCounter][HasFinished]) {
stageCount++;
}
recordCounter++;
}
g_playerStageRecords[client][0][CompletedStages] = stageCount;
PrintToServer("[SurfTimer-Records] Player %d completed %d of %d stages (sql query found %d stages)", client, stageCount, recordCounter, g_map[StageCount]);
}
LoadUser(client)
{
if (IsFakeClient(client)) {
return;
}
decl String:sQuery[1024];
new Handle:data = CreateDataPack();
WritePackCell(data, client);
FormatEx(sQuery, sizeof(sQuery), "select user_id from user where user_steam_id = '%s'", g_user[client][AuthName]);
SQL_TQuery(g_hSQL, LoadUserCallback, sQuery, data, DBPrio_High);
}
public LoadUserCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
ResetPack(data);
new client = ReadPackCell(data);
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on LoadUser: %s", error);
return;
}
if (! SQL_GetRowCount(hndl)) {
PrintToServer("Failed to load user id for ranking. (Client-Id %d)", client);
CreateTimer(1.0, TimerLoadUser, data, TIMER_FLAG_NO_MAPCHANGE);
return;
}
SQL_FetchRow(hndl);
g_user[client][Id] = SQL_FetchInt(hndl, 0);
LoadPlayerStageRecords(client);
}
public Action:TimerLoadUser(Handle:timer, any:serial)
{
ResetPack(serial);
new client = ReadPackCell(serial);
LoadUser(client);
}
public AddUserRecord(client, Float:time, bool:isWorldRecord, points)
{
if (g_hSQL == INVALID_HANDLE) {
PrintToServer("[SurfTimer-Rank] Failed to saved player record. SQL unavailable");
return;
}
decl String:sQuery[1024];
new userId = g_user[client][Id];
if (! userId) {
CPrintToChatAll("%s{red}[SurfTimer-Records] Player not found (Client %d)", PLUGIN_PREFIX ,userId, client);
}
Format(sQuery, sizeof(sQuery), "INSERT INTO user_records (user_user_id, maps_map_id, user_record_time, user_record_points, user_record_was_wr, user_record_created_at) VALUES (%d, %d, %f, %d, %d, %d)", userId, g_map[Id], time, points, isWorldRecord, GetTime());
SQL_LockDatabase(g_hSQL);
if (! SQL_FastQuery(g_hSQL, sQuery)) {
new String:uerror[1024];
SQL_GetError(g_hSQL, uerror, sizeof(uerror));
PrintToServer("[SurfTimer-Records] Can not add record for user id %d - %s", userId, uerror);
SQL_UnlockDatabase(g_hSQL);
return;
}
SQL_UnlockDatabase(g_hSQL);
Format(sQuery, sizeof(sQuery), "UPDATE user SET user_points = user_points + %d WHERE user_id = %d", points, userId);
SQL_LockDatabase(g_hSQL);
if (! SQL_FastQuery(g_hSQL, sQuery)) {
PrintToServer("[SurfTimer-Records] Can not update user points for user id %d", userId);
}
SQL_UnlockDatabase(g_hSQL);
// Update map counters and reload map data
FormatEx(sQuery, sizeof(sQuery), "UPDATE maps SET map_total_completitions = map_total_completitions + 1%s WHERE map_name = '%s'", ((isWorldRecord) ? ", map_total_wrs = map_total_wrs + 1" : ""), g_sCurrentMap);
SQL_LockDatabase(g_hSQL);
if (! SQL_FastQuery(g_hSQL, sQuery)) {
new String:uerror[1024];
SQL_GetError(g_hSQL, uerror, sizeof(uerror));
PrintToServer("[SurfTimer-Rank] Failed to update map_total_completitions for map %s (error: %s)", g_sCurrentMap, uerror);
SQL_UnlockDatabase(g_hSQL);
}
SQL_UnlockDatabase(g_hSQL);
LoadMap();
}
public AddUserStageRecord(client, Float:time, bool:isWorldRecord, points, zoneId)
{
if (g_hSQL == INVALID_HANDLE) {
PrintToServer("[SurfTimer-Rank] Failed to saved player record. SQL unavailable");
return;
}
decl String:sQuery[1024];
new userId = g_user[client][Id];
if (! userId) {
CPrintToChatAll("%s{red}[SurfTimer-Records] Player not found (Client %d)", PLUGIN_PREFIX ,userId, client);
}
Format(sQuery, sizeof(sQuery), "INSERT INTO user_stage_records (user_stage_record_is_wr, user_stage_records_time, user_user_id, user_stage_records_stage_id, user_stage_records_created_at, maps_map_id, user_stage_record_points) VALUES ( %d, %f, %d, %d, %d, %d, %d)", isWorldRecord, time, userId, zoneId, GetTime(), g_map[Id], points);
SQL_LockDatabase(g_hSQL);
if (! SQL_FastQuery(g_hSQL, sQuery)) {
new String:uerror[1024];
SQL_GetError(g_hSQL, uerror, sizeof(uerror));
PrintToServer("[SurfTimer-Records] Can not add stage record for user id %d - %s", userId, uerror);
SQL_UnlockDatabase(g_hSQL);
return;
}
SQL_UnlockDatabase(g_hSQL);
Format(sQuery, sizeof(sQuery), "UPDATE user SET user_points = user_points + %d WHERE user_id = %d", points, userId);
SQL_LockDatabase(g_hSQL);
if (! SQL_FastQuery(g_hSQL, sQuery)) {
PrintToServer("[SurfTimer-Records] Can not update user points for user id %d", userId);
}
SQL_UnlockDatabase(g_hSQL);
LoadMapStageRecords();
}
public AddUserRecordCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
decl authName[AUTHID_MAX];
new client = 0;
new Float:time = 0;
new bool:isWorldRecord = false;
new points = 0;
ResetPack(data);
ReadPackString(data, authName, sizeof(authName));
client = ReadPackCell(data);
time = ReadPackFloat(data);
isWorldRecord = ReadPackCell(data);
points = ReadPackCell(data);
CloseHandle(data);
if (hndl == INVALID_HANDLE) {
PrintToServer("[SurfTimer-Rank] SQL Error on AddUserRecordCallback: %s", error);
return;
}
if (! SQL_GetRowCount(hndl)) {
PrintToServer("[SurfTimer-Records] Player not found for Steam ID %s", authName);
return;
}
SQL_FetchRow(hndl);
new userId = SQL_FetchInt(hndl, 0);
if (! userId) {
PrintToServer("[SurfTimer-Records] Player not found for Steam ID %s", authName);
return;
}
// Add user record
}
ShowMapPlayerRank(client, args)
{
decl String:sQuery[2048];
FormatEx(sQuery, sizeof(sQuery), "SELECT ranking.rank, ranking.record_time, (SELECT COUNT(*) FROM (SELECT COUNT(user_user_id) FROM user_records AS user_records LEFT JOIN user ON user.user_id = user_records.user_user_id WHERE maps_map_id = %i GROUP BY user_user_id) as total) as total FROM (SELECT @rank := @rank+1 AS rank, ranking.* FROM (SELECT @rank := 0 AS rank) AS init, (SELECT *, MIN(user_record_time) AS record_time FROM user_records AS user_records LEFT JOIN user ON user.user_id = user_records.user_user_id WHERE maps_map_id = %i GROUP BY user_user_id ORDER BY MIN(user_record_time)) as ranking) as ranking where user_steam_id = '%s' LIMIT 1", g_map[Id], g_map[Id], g_user[client][AuthName]);
new Handle:user = CreateDataPack();
if (user == INVALID_HANDLE) {
CPrintToChatAll("%s{red}Ups, failed look up rank database. Please try it later again :(", PLUGIN_PREFIX);
}
WritePackCell(user, client);
SQL_LockDatabase(g_hSQL);
SQL_FastQuery(g_hSQL, "SET NAMES 'UTF8'");
SQL_UnlockDatabase(g_hSQL);
SQL_TQuery(g_hSQL, ShowMapPlayerRankCallback, sQuery, user, DBPrio_Low);
}
public ShowMapPlayerRankCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
ResetPack(data);
new client = ReadPackCell(data);
decl String:name[USERNAME_MAX];
GetClientName(client, name, sizeof(name));
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error on selecting play rank: %s", error);
return;
}
if (! SQL_GetRowCount(hndl)) {
CPrintToChatAll("%s{red}No ranking informations found for %s", PLUGIN_PREFIX, name);
return;
}
SQL_FetchRow(hndl);
new userRank = SQL_FetchInt(hndl, 0);
decl String:userMax[11];
SQL_FetchString(hndl, 2, userMax, sizeof(userMax));
new float:userTime = SQL_FetchFloat(hndl, 1);
decl String:sTimeFormatted[128];
SurfTimer_secondsToTime(userTime, sTimeFormatted, sizeof(sTimeFormatted));
CPrintToChatAll("%s{white}%s {lightgreen}ranks {white}%i{lightgreen}/{white}%s {lightgreen}with {white}%s {lightgreen}on {white}%s", PLUGIN_PREFIX, name, userRank, userMax, sTimeFormatted, g_sCurrentMap);
}
ShowMapPlayerRankWithParameters(client, const String:mapName[], const String:userName[])
{
new String:newMapName[MAPNAME_MAX*2];
new String:newUserName[USERNAME_MAX*2];
SQL_EscapeString(g_hSQL, mapName, newMapName, MAPNAME_MAX*2);
SQL_EscapeString(g_hSQL, userName, newUserName, USERNAME_MAX*2);
decl String:sQuery[2048];
FormatEx(sQuery, sizeof(sQuery), "SELECT ranking.rank, ranking.record_time, (SELECT COUNT(*) FROM (SELECT COUNT(user_user_id) FROM user_records AS user_records LEFT JOIN user ON user.user_id = user_records.user_user_id WHERE maps_map_id = (SELECT map_id FROM maps WHERE map_name = '%s' LIMIT 1) GROUP BY user_user_id) as total) as total FROM (SELECT @rank := @rank+1 AS rank, ranking.* FROM (SELECT @rank := 0 AS rank) AS init, (SELECT *, MIN(user_record_time) AS record_time FROM user_records AS user_records LEFT JOIN user ON user.user_id = user_records.user_user_id WHERE maps_map_id = (SELECT map_id FROM maps WHERE map_name = '%s' LIMIT 1) GROUP BY user_user_id ORDER BY MIN(user_record_time)) as ranking) as ranking where user_name = '%s' LIMIT 1", newMapName, newMapName, newUserName);
new Handle:user = CreateDataPack();
if (user == INVALID_HANDLE) {
CPrintToChatAll("%s{red}Ups, failed look up rank database. Please try it later again :(", PLUGIN_PREFIX);
}
WritePackCell(user, client);
WritePackString(user, mapName);
WritePackString(user, newUserName);
SQL_TQuery(g_hSQL, ShowMapPlayerRankWithParametersCallback, sQuery, user, DBPrio_Low);
}
public ShowMapPlayerRankWithParametersCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
new String:newMapName[MAPNAME_MAX*2];
new String:newUserName[USERNAME_MAX*2];
ResetPack(data);
new client = ReadPackCell(data);
ReadPackString(data, newMapName, MAPNAME_MAX*2);
ReadPackString(data, newUserName, USERNAME_MAX*2);
decl String:name[USERNAME_MAX];
GetClientName(client, name, sizeof(name));
if (hndl == INVALID_HANDLE) {
PrintToServer("SQL Error selecting map / player rank: %s", error);
return;
}
if (! SQL_GetRowCount(hndl)) {
CPrintToChatAll("%s{red}No ranking informations found for map %s %s", PLUGIN_PREFIX, newMapName, (strlen(newUserName) ? newUserName : ""));