This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
sql.sp
8442 lines (7383 loc) · 258 KB
/
sql.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
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
#include "surftimer/db/queries.sp"
/*==================================
= DATABASE SETUP =
==================================*/
public void db_setupDatabase()
{
/*===================================
= INIT CONNECTION TO DATABASE =
===================================*/
char szError[255];
g_hDb = SQL_Connect("surftimer", false, szError, 255);
if (g_hDb == null)
{
SetFailState("[Surftimer] Unable to connect to database (%s)", szError);
return;
}
char szIdent[8];
SQL_ReadDriver(g_hDb, szIdent, 8);
if (strcmp(szIdent, "mysql", false) == 0)
{
// https://github.com/nikooo777/ckSurf/pull/58
SQL_FastQuery(g_hDb, "SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));");
g_DbType = MYSQL;
}
else if (strcmp(szIdent, "sqlite", false) == 0)
{
SetFailState("[Surftimer] Sorry SQLite is not supported.");
return;
}
else
{
SetFailState("[Surftimer] Invalid database type");
return;
}
// If updating from a previous version
SQL_LockDatabase(g_hDb);
SQL_FastQuery(g_hDb, "SET NAMES 'utf8'");
SQL_FastQuery(g_hDb, "SET name 'utf8'");
// Check if tables need to be Created or database needs to be upgraded
g_bRenaming = false;
g_bInTransactionChain = false;
// If tables haven't been created yet.
if (!SQL_FastQuery(g_hDb, "SELECT steamid FROM ck_playerrank LIMIT 1"))
{
SQL_UnlockDatabase(g_hDb);
db_createTables();
return;
}
else
{
// Check for db upgrades
if (!SQL_FastQuery(g_hDb, "SELECT prespeed FROM ck_zones LIMIT 1"))
{
db_upgradeDatabase(0);
return;
}
else if(!SQL_FastQuery(g_hDb, "SELECT ranked FROM ck_maptier LIMIT 1") || !SQL_FastQuery(g_hDb, "SELECT style FROM ck_playerrank LIMIT 1;"))
{
db_upgradeDatabase(1);
return;
}
else if (!SQL_FastQuery(g_hDb, "SELECT wrcppoints FROM ck_playerrank LIMIT 1"))
{
db_upgradeDatabase(2);
}
else if (!SQL_FastQuery(g_hDb, "SELECT teleside FROM ck_playeroptions LIMIT 1"))
{
db_upgradeDatabase(3);
}
}
SQL_UnlockDatabase(g_hDb);
for (int i = 0; i < sizeof(g_failedTransactions); i++)
g_failedTransactions[i] = 0;
}
public void db_createTables()
{
Transaction createTableTnx = SQL_CreateTransaction();
SQL_AddQuery(createTableTnx, sql_createPlayertmp);
SQL_AddQuery(createTableTnx, sql_createPlayertimes);
SQL_AddQuery(createTableTnx, sql_createPlayertimesIndex);
SQL_AddQuery(createTableTnx, sql_createPlayerRank);
SQL_AddQuery(createTableTnx, sql_createPlayerOptions);
SQL_AddQuery(createTableTnx, sql_createLatestRecords);
SQL_AddQuery(createTableTnx, sql_createBonus);
SQL_AddQuery(createTableTnx, sql_createBonusIndex);
SQL_AddQuery(createTableTnx, sql_createCheckpoints);
SQL_AddQuery(createTableTnx, sql_createZones);
SQL_AddQuery(createTableTnx, sql_createMapTier);
SQL_AddQuery(createTableTnx, sql_createSpawnLocations);
SQL_AddQuery(createTableTnx, sql_createAnnouncements);
SQL_AddQuery(createTableTnx, sql_createVipAdmins);
SQL_AddQuery(createTableTnx, sql_createWrcps);
SQL_ExecuteTransaction(g_hDb, createTableTnx, SQLTxn_CreateDatabaseSuccess, SQLTxn_CreateDatabaseFailed);
}
public void SQLTxn_CreateDatabaseSuccess(Handle db, any data, int numQueries, Handle[] results, any[] queryData)
{
PrintToServer("[Surftimer] Database tables succesfully created!");
}
public void SQLTxn_CreateDatabaseFailed(Handle db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
{
SetFailState("[Surftimer] Database tables could not be created! Error: %s", error);
}
public void db_upgradeDatabase(int ver)
{
if (ver == 0)
{
// Surftimer v2.01 -> Surftimer v2.1
char query[128];
for (int i = 1; i < 11; i++)
{
Format(query, sizeof(query), "ALTER TABLE ck_maptier DROP COLUMN btier%i", i);
SQL_FastQuery(g_hDb, query);
}
SQL_FastQuery(g_hDb, "ALTER TABLE ck_maptier ADD COLUMN maxvelocity FLOAT NOT NULL DEFAULT '3500.0';");
SQL_FastQuery(g_hDb, "ALTER TABLE ck_maptier ADD COLUMN announcerecord INT(11) NOT NULL DEFAULT '0';");
SQL_FastQuery(g_hDb, "ALTER TABLE ck_maptier ADD COLUMN gravityfix INT(11) NOT NULL DEFAULT '1';");
SQL_FastQuery(g_hDb, "ALTER TABLE ck_zones ADD COLUMN `prespeed` int(64) NOT NULL DEFAULT '350';");
SQL_FastQuery(g_hDb, "CREATE INDEX tier ON ck_maptier (mapname, tier);");
SQL_FastQuery(g_hDb, "CREATE INDEX mapsettings ON ck_maptier (mapname, maxvelocity, announcerecord, gravityfix);");
SQL_FastQuery(g_hDb, "UPDATE ck_maptier a, ck_mapsettings b SET a.maxvelocity = b.maxvelocity WHERE a.mapname = b.mapname;");
SQL_FastQuery(g_hDb, "UPDATE ck_maptier a, ck_mapsettings b SET a.announcerecord = b.announcerecord WHERE a.mapname = b.mapname;");
SQL_FastQuery(g_hDb, "UPDATE ck_maptier a, ck_mapsettings b SET a.gravityfix = b.gravityfix WHERE a.mapname = b.mapname;");
SQL_FastQuery(g_hDb, "UPDATE ck_zones a, ck_mapsettings b SET a.prespeed = b.startprespeed WHERE a.mapname = b.mapname AND zonetype = 1;");
SQL_FastQuery(g_hDb, "DROP TABLE ck_mapsettings;");
}
else if (ver == 1)
{
// SurfTimer v2.1 -> v2.2
SQL_FastQuery(g_hDb, "ALTER TABLE ck_maptier ADD COLUMN ranked INT(11) NOT NULL DEFAULT '1';");
SQL_FastQuery(g_hDb, "ALTER TABLE ck_playerrank DROP PRIMARY KEY, ADD COLUMN style INT(11) NOT NULL DEFAULT '0', ADD PRIMARY KEY (steamid, style);");
}
else if (ver == 2)
{
SQL_FastQuery(g_hDb, "ALTER TABLE ck_playerrank ADD COLUMN wrcppoints INT(11) NOT NULL DEFAULT 0 AFTER `wrbpoints`;");
}
else if (ver == 3)
{
SQL_FastQuery(g_hDb, "ALTER TABLE ck_playeroptions2 ADD COLUMN teleside INT(11) NOT NULL DEFAULT 0 AFTER centrehud;");
SQL_FastQuery(g_hDb, "ALTER TABLE ck_spawnlocations DROP PRIMARY KEY, ADD COLUMN teleside INT(11) NOT NULL DEFAULT 0 AFTER stage, ADD PRIMARY KEY (mapname, zonegroup, stage, teleside);");
}
SQL_UnlockDatabase(g_hDb);
}
/* Admin Delete Menu */
public void sql_DeleteMenuView(Handle owner, Handle hndl, const char[] error, any data)
{
int client = GetClientFromSerial(data);
Menu editing = new Menu(callback_DeleteRecord);
editing.SetTitle("%s Records Editing Menu - %s\n► Editing %s record\n► Press the menu item to delete the record\n ", g_szMenuPrefix, g_EditingMap[client], g_EditTypes[g_SelectedEditOption[client]]);
char menuFormat[88];
FormatEx(menuFormat, sizeof(menuFormat), "Style: %s\n► Press the menu item to change the style\n ", g_EditStyles[g_SelectedStyle[client]]);
editing.AddItem("0", menuFormat);
if(g_SelectedEditOption[client] > 0)
{
FormatEx(menuFormat, sizeof(menuFormat), "%s: %i\n► Press the menu item to change the %s\n ", g_SelectedEditOption[client] == 1 ? "Stage":"Bonus", g_SelectedType[client], g_SelectedEditOption[client] == 1 ? "stage":"bonus");
editing.AddItem("0", menuFormat);
}
if (hndl == INVALID_HANDLE)
{
PrintToServer("Error %s", error);
}
else if (!SQL_GetRowCount(hndl))
{
editing.AddItem("1", "No records found", ITEMDRAW_DISABLED);
editing.Display(client, MENU_TIME_FOREVER);
}
else
{
char playerName[32], steamID[32];
float runTime;
char menuFormatz[128];
int i = 0;
while (SQL_FetchRow(hndl))
{
i++;
SQL_FetchString(hndl, 0, steamID, 32);
SQL_FetchString(hndl, 1, playerName, 32);
runTime = SQL_FetchFloat(hndl, 2);
char szRunTime[128];
FormatTimeFloat(data, runTime, 3, szRunTime, sizeof(szRunTime));
FormatEx(menuFormat, sizeof(menuFormat), "Rank: %d ► %s - %s", i, playerName, szRunTime);
ReplaceString(playerName, 32, ";;;", ""); // make sure the client dont has this in their name.
FormatEx(menuFormatz, 128, "%s;;;%s;;;%s", playerName, steamID, szRunTime);
editing.AddItem(menuFormatz, menuFormat);
}
editing.Display(client, MENU_TIME_FOREVER);
}
}
public int callback_DeleteRecord(Menu menu, MenuAction action, int client, int key)
{
if(action == MenuAction_Select)
{
if(key == 0)
{
if(g_SelectedStyle[client] < MAX_STYLES - 1)
g_SelectedStyle[client]++;
else
g_SelectedStyle[client] = 0;
char szQuery[512];
switch(g_SelectedEditOption[client])
{
case 0:
{
FormatEx(szQuery, 512, sql_MainEditQuery, "runtimepro", "ck_playertimes", g_EditingMap[client], g_SelectedStyle[client], "", "runtimepro");
}
case 1:
{
char stageQuery[32];
FormatEx(stageQuery, 32, "AND stage='%i' ", g_SelectedType[client]);
FormatEx(szQuery, 512, sql_MainEditQuery, "runtimepro", "ck_wrcps", g_EditingMap[client], g_SelectedStyle[client], stageQuery, "runtimepro");
}
case 2:
{
char stageQuery[32];
FormatEx(stageQuery, 32, "AND zonegroup='%i' ", g_SelectedType[client]);
FormatEx(szQuery, 512, sql_MainEditQuery, "runtime", "ck_bonus", g_EditingMap[client], g_SelectedStyle[client], stageQuery, "runtime");
}
}
PrintToServer(szQuery);
SQL_TQuery(g_hDb, sql_DeleteMenuView, szQuery, GetClientSerial(client));
return 0;
}
if(g_SelectedEditOption[client] > 0 && key == 1)
{
g_iWaitingForResponse[client] = 6;
CPrintToChat(client, "%t", "DeleteRecordsNewValue", g_szChatPrefix);
return 0;
}
char menuItem[128];
menu.GetItem(key, menuItem, 128);
char recordsBreak[3][32];
ExplodeString(menuItem, ";;;", recordsBreak, sizeof(recordsBreak), sizeof(recordsBreak[]));
Menu confirm = new Menu(callback_Confirm);
confirm.SetTitle("%s Records Editing Menu - Confirm Deletion\n► Deleting %s [%s] %s record\n ", g_szMenuPrefix, recordsBreak[0], recordsBreak[1], recordsBreak[2]);
confirm.AddItem("0", "No");
confirm.AddItem(recordsBreak[1], "Yes\n \n► This cannot be undone");
confirm.Display(client, MENU_TIME_FOREVER);
return 0;
}
else if (action == MenuAction_Cancel)
{
if (key == MenuCancel_Exit)
ShowMainDeleteMenu(client);
}
else if(action == MenuAction_End)
delete menu;
return 0;
}
public int callback_Confirm(Menu menu, MenuAction action, int client, int key)
{
if(action == MenuAction_Select)
{
if(key == 1)
{
char steamID[32];
menu.GetItem(key, steamID, 32);
char szQuery[512];
switch(g_SelectedEditOption[client])
{
case 0:
{
FormatEx(szQuery, 512, sql_MainDeleteQeury, "ck_playertimes", g_EditingMap[client], g_SelectedStyle[client], steamID, "");
}
case 1:
{
char stageQuery[32];
FormatEx(stageQuery, 32, "AND stage='%i'", g_SelectedType[client]);
FormatEx(szQuery, 512, sql_MainDeleteQeury, "ck_wrcps", g_EditingMap[client], g_SelectedStyle[client], steamID, stageQuery);
}
case 2:
{
char zoneQuery[32];
FormatEx(zoneQuery, 32, "AND zonegroup='%i'", g_SelectedType[client]);
FormatEx(szQuery, 512, sql_MainDeleteQeury, "ck_bonus", g_EditingMap[client], g_SelectedStyle[client], steamID, zoneQuery);
}
}
SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
// Looking for online player to refresh his record after deleting it.
char player_steamID[32];
for(int i=1; i <= MaxClients; i++)
{
if (!IsValidClient(i) || IsFakeClient(client))
continue;
GetClientAuthId(i, AuthId_Steam2, player_steamID, 32, true);
if(StrEqual(player_steamID,steamID))
{
g_bSettingsLoaded[client] = false;
g_bLoadingSettings[client] = true;
g_iSettingToLoad[client] = 0;
LoadClientSetting(client, g_iSettingToLoad[client]);
break;
}
}
db_GetMapRecord_Pro();
PrintToServer(szQuery);
CPrintToChat(client, "%t", "DeleteRecordsDeletion", g_szChatPrefix);
}
}
else if(action == MenuAction_End)
delete menu;
}
/*==================================
= SPAWN LOCATION =
==================================*/
public void db_deleteSpawnLocations(int zGrp, int teleside)
{
g_bGotSpawnLocation[zGrp][1][teleside] = false;
char szQuery[128];
Format(szQuery, 128, sql_deleteSpawnLocations, g_szMapName, zGrp, teleside);
SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, 1, DBPrio_Low);
}
public void db_updateSpawnLocations(float position[3], float angle[3], float vel[3], int zGrp, int teleside)
{
char szQuery[512];
Format(szQuery, 512, sql_updateSpawnLocations, position[0], position[1], position[2], angle[0], angle[1], angle[2], vel[0], vel[1], vel[2], g_szMapName, zGrp, teleside);
SQL_TQuery(g_hDb, db_editSpawnLocationsCallback, szQuery, zGrp, DBPrio_Low);
}
public void db_insertSpawnLocations(float position[3], float angle[3], float vel[3], int zGrp, int teleside)
{
char szQuery[512];
Format(szQuery, 512, sql_insertSpawnLocations, g_szMapName, position[0], position[1], position[2], angle[0], angle[1], angle[2], vel[0], vel[1], vel[2], zGrp, teleside);
SQL_TQuery(g_hDb, db_editSpawnLocationsCallback, szQuery, zGrp, DBPrio_Low);
}
public void db_editSpawnLocationsCallback(Handle owner, Handle hndl, const char[] error, any data)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (db_editSpawnLocationsCallback): %s ", error);
return;
}
db_selectSpawnLocations();
}
public void db_selectSpawnLocations()
{
for (int s = 0; s < CPLIMIT; s++)
{
for (int i = 0; i < MAXZONEGROUPS; i++)
{
g_bGotSpawnLocation[i][s][0] = false;
g_bGotSpawnLocation[i][s][1] = false;
}
}
char szQuery[254];
Format(szQuery, 254, sql_selectSpawnLocations, g_szMapName);
SQL_TQuery(g_hDb, db_selectSpawnLocationsCallback, szQuery, 1, DBPrio_Low);
}
public void db_selectSpawnLocationsCallback(Handle owner, Handle hndl, const char[] error, any data)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (db_selectSpawnLocationsCallback): %s ", error);
if (!g_bServerDataLoaded)
db_ClearLatestRecords();
return;
}
if (SQL_HasResultSet(hndl))
{
while (SQL_FetchRow(hndl))
{
int zonegroup = SQL_FetchInt(hndl, 10);
int stage = SQL_FetchInt(hndl, 11);
int teleside = SQL_FetchInt(hndl, 12);
g_bGotSpawnLocation[zonegroup][stage][teleside] = true;
g_fSpawnLocation[zonegroup][stage][teleside][0] = SQL_FetchFloat(hndl, 1);
g_fSpawnLocation[zonegroup][stage][teleside][1] = SQL_FetchFloat(hndl, 2);
g_fSpawnLocation[zonegroup][stage][teleside][2] = SQL_FetchFloat(hndl, 3);
g_fSpawnAngle[zonegroup][stage][teleside][0] = SQL_FetchFloat(hndl, 4);
g_fSpawnAngle[zonegroup][stage][teleside][1] = SQL_FetchFloat(hndl, 5);
g_fSpawnAngle[zonegroup][stage][teleside][2] = SQL_FetchFloat(hndl, 6);
g_fSpawnVelocity[zonegroup][stage][teleside][0] = SQL_FetchFloat(hndl, 7);
g_fSpawnVelocity[zonegroup][stage][teleside][1] = SQL_FetchFloat(hndl, 8);
g_fSpawnVelocity[zonegroup][stage][teleside][2] = SQL_FetchFloat(hndl, 9);
}
}
if (!g_bServerDataLoaded)
db_ClearLatestRecords();
}
/*===================================
= PLAYER RANK =
===================================*/
public void db_viewMapProRankCount()
{
g_MapTimesCount = 0;
char szQuery[512];
Format(szQuery, 512, sql_selectPlayerProCount, g_szMapName);
SQL_TQuery(g_hDb, sql_selectPlayerProCountCallback, szQuery, DBPrio_Low);
}
public void sql_selectPlayerProCountCallback(Handle owner, Handle hndl, const char[] error, any data)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (sql_selectPlayerProCountCallback): %s", error);
if (!g_bServerDataLoaded)
{
db_viewFastestBonus();
}
return;
}
int style;
int count;
if (SQL_HasResultSet(hndl))
{
while (SQL_FetchRow(hndl))
{
style = SQL_FetchInt(hndl, 0);
count = SQL_FetchInt(hndl, 1);
if (style == 0)
g_MapTimesCount = count;
else
g_StyleMapTimesCount[style] = count;
}
}
else
{
g_MapTimesCount = 0;
for (int i = 1; i < MAX_STYLES; i++)
g_StyleMapTimesCount[style] = 0;
}
if (!g_bServerDataLoaded)
{
db_viewFastestBonus();
}
return;
}
// Get players rank in current map
public void db_viewMapRankPro(int client)
{
char szQuery[512];
if (!IsValidClient(client))
return;
// "SELECT name,mapname FROM ck_playertimes WHERE runtimepro <= (SELECT runtimepro FROM ck_playertimes WHERE steamid = '%s' AND mapname = '%s' AND runtimepro > -1.0) AND mapname = '%s' AND runtimepro > -1.0 ORDER BY runtimepro;";
Format(szQuery, 512, sql_selectPlayerRankProTime, g_szSteamID[client], g_szMapName, g_szMapName);
SQL_TQuery(g_hDb, db_viewMapRankProCallback, szQuery, client, DBPrio_Low);
}
public void db_viewMapRankProCallback(Handle owner, Handle hndl, const char[] error, any client)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (db_viewMapRankProCallback): %s ", error);
}
if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
{
g_MapRank[client] = SQL_GetRowCount(hndl);
}
// if (!g_bSettingsLoaded[client])
// {
// g_fTick[client][1] = GetGameTime();
// float tick = g_fTick[client][1] - g_fTick[client][0];
// LogToFileEx(g_szLogFile, "[Surftimer] %s: Finished db_viewPersonalRecords in %fs", g_szSteamID[client], tick);
// g_fTick[client][0] = GetGameTime();
// db_viewPersonalBonusRecords(client, g_szSteamID[client]);
// }
}
// Players points have changed in game, make changes in database and recalculate points
public void db_updateStat(int client, int style)
{
Handle pack = CreateDataPack();
WritePackCell(pack, client);
WritePackCell(pack, style);
char szQuery[512];
// "UPDATE ck_playerrank SET finishedmaps ='%i', finishedmapspro='%i', multiplier ='%i' where steamid='%s'";
Format(szQuery, 512, sql_updatePlayerRank, g_pr_finishedmaps[client], g_pr_finishedmaps[client], g_szSteamID[client], style);
SQL_TQuery(g_hDb, SQL_UpdateStatCallback, szQuery, pack, DBPrio_Low);
}
public void SQL_UpdateStatCallback(Handle owner, Handle hndl, const char[] error, any pack)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (SQL_UpdateStatCallback): %s", error);
CloseHandle(pack);
return;
}
ResetPack(pack);
int client = ReadPackCell(pack);
int style = ReadPackCell(pack);
CloseHandle(pack);
// Calculating starts here:
CalculatePlayerRank(client, style);
}
public void RecalcPlayerRank(int client, char steamid[128])
{
int i = 66;
while (g_bProfileRecalc[i] == true)
i++;
if (!g_bProfileRecalc[i])
{
char szQuery[255];
char szsteamid[128 * 2 + 1];
SQL_EscapeString(g_hDb, steamid, szsteamid, 128 * 2 + 1);
Format(g_pr_szSteamID[i], 32, "%s", steamid);
Format(szQuery, 255, sql_selectPlayerName, szsteamid);
Handle pack = CreateDataPack();
WritePackCell(pack, i);
WritePackCell(pack, client);
SQL_TQuery(g_hDb, sql_selectPlayerNameCallback, szQuery, pack);
}
}
//
// 1. Point calculating starts here
// There are two ways:
// - if client > MAXPLAYERS, his rank is being recalculated by an admin
// - else player has increased his rank = recalculate points
//
public void CalculatePlayerRank(int client, int style)
{
char szQuery[255];
char szSteamId[32];
// Take old points into memory, so at the end you can show how much the points changed
g_pr_oldpoints[client][style] = g_pr_points[client][style];
// Initialize point calculatin
g_pr_points[client][style] = 0;
// Start fluffys points
g_Points[client][style][0] = 0; // Map Points
g_Points[client][style][1] = 0; // Bonus Points
g_Points[client][style][2] = 0; // Group Points
g_Points[client][style][3] = 0; // Map WR Points
g_Points[client][style][4] = 0; // Bonus WR Points
g_Points[client][style][5] = 0; // Top 10 Points
g_Points[client][style][6] = 0; // WRCP Points
// g_GroupPoints[client][0] // G1 Points
// g_GroupPoints[client][1] // G2 Points
// g_GroupPoints[client][2] // G3 Points
// g_GroupPoints[client][3] // G4 Points
// g_GroupPoints[client][4] // G5 Points
g_GroupMaps[client][style] = 0; // Group Maps
g_Top10Maps[client][style] = 0; // Top 10 Maps
g_WRs[client][style][0] = 0; // WRs
g_WRs[client][style][1] = 0; // WRBs
g_WRs[client][style][2] = 0; // WRCPs
getSteamIDFromClient(client, szSteamId, 32);
Handle pack = CreateDataPack();
WritePackCell(pack, client);
WritePackCell(pack, style);
Format(szQuery, 255, "SELECT name FROM ck_playerrank WHERE steamid = '%s' AND style = '%i';", szSteamId, style);
SQL_TQuery(g_hDb, sql_CalcuatePlayerRankCallback, szQuery, pack, DBPrio_Low);
}
// 2. See if player exists, insert new player into the database
// Fetched values:
// name
public void sql_CalcuatePlayerRankCallback(Handle owner, Handle hndl, const char[] error, any pack)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (sql_CalcuatePlayerRankCallback): %s", error);
CloseHandle(pack);
return;
}
ResetPack(pack);
int client = ReadPackCell(pack);
int style = ReadPackCell(pack);
char szSteamId[32], szSteamId64[64];
getSteamIDFromClient(client, szSteamId, 32);
if (IsValidClient(client))
GetClientAuthId(client, AuthId_SteamID64, szSteamId64, MAX_NAME_LENGTH, true);
if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
{
if (IsValidClient(client))
{
if (GetClientTime(client) < (GetEngineTime() - g_fMapStartTime))
db_UpdateLastSeen(client); // Update last seen on server
}
if (IsValidClient(client))
g_pr_Calculating[client] = true;
// Next up, calculate bonus points:
char szQuery[512];
Format(szQuery, 512, "SELECT mapname, (SELECT count(1)+1 FROM ck_bonus b WHERE a.mapname=b.mapname AND a.runtime > b.runtime AND a.zonegroup = b.zonegroup AND b.style = %i) AS rank, (SELECT count(1) FROM ck_bonus b WHERE a.mapname = b.mapname AND a.zonegroup = b.zonegroup AND b.style = %i) as total FROM ck_bonus a WHERE steamid = '%s' AND style = %i;", style, style, szSteamId, style);
SQL_TQuery(g_hDb, sql_CountFinishedBonusCallback, szQuery, pack, DBPrio_Low);
}
else
{
// Players first time on server
if (client <= MaxClients)
{
g_pr_Calculating[client] = false;
g_pr_AllPlayers[style]++;
// Insert player to database
char szQuery[512];
char szUName[MAX_NAME_LENGTH];
char szName[MAX_NAME_LENGTH * 2 + 1];
GetClientName(client, szUName, MAX_NAME_LENGTH);
SQL_EscapeString(g_hDb, szUName, szName, MAX_NAME_LENGTH * 2 + 1);
// "INSERT INTO ck_playerrank (steamid, name, country) VALUES('%s', '%s', '%s');";
// No need to continue calculating, as the doesn't have any records.
Format(szQuery, 512, sql_insertPlayerRank, szSteamId, szSteamId64, szName, g_szCountry[client], GetTime(), style);
SQL_TQuery(g_hDb, SQL_InsertPlayerCallBack, szQuery, client, DBPrio_Low);
g_pr_finishedmaps[client][style] = 0;
g_pr_finishedmaps_perc[client][style] = 0.0;
g_pr_finishedbonuses[client][style] = 0;
g_pr_finishedstages[client][style] = 0;
g_GroupMaps[client][style] = 0; // Group Maps
g_Top10Maps[client][style] = 0; // Top 10 Maps
// play time
g_iPlayTimeAlive[client] = 0;
g_iPlayTimeSpec[client] = 0;
CalculatePlayerRank(client, style);
}
}
}
//
// 3. Calculate points gained from bonuses
// Fetched values
// mapname, rank, total
//
public void sql_CountFinishedBonusCallback(Handle owner, Handle hndl, const char[] error, any pack)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (sql_CountFinishedBonusCallback): %s", error);
CloseHandle(pack);
return;
}
ResetPack(pack);
int client = ReadPackCell(pack);
int style = ReadPackCell(pack);
char szMap[128], szSteamId[32], szMapName2[128];
// int totalplayers
int rank;
getSteamIDFromClient(client, szSteamId, 32);
int finishedbonuses = 0;
int wrbs = 0;
if (SQL_HasResultSet(hndl))
{
while (SQL_FetchRow(hndl))
{
finishedbonuses++;
// Total amount of players who have finished the bonus
// totalplayers = SQL_FetchInt(hndl, 2);
rank = SQL_FetchInt(hndl, 1);
SQL_FetchString(hndl, 0, szMap, 128);
for (int i = 0; i < GetArraySize(g_MapList); i++) // Check that the map is in the mapcycle
{
GetArrayString(g_MapList, i, szMapName2, sizeof(szMapName2));
if (StrEqual(szMapName2, szMap, false))
{
/*float percentage = 1.0 + ((1.0 / float(totalplayers)) - (float(rank) / float(totalplayers)));
g_pr_points[client] += RoundToCeil(200.0 * percentage);*/
switch (rank)
{
case 1:
{
g_pr_points[client][style] += 250;
g_Points[client][style][4] += 250;
wrbs++;
}
case 2:
{
g_pr_points[client][style] += 235;
g_Points[client][style][1] += 235;
}
case 3:
{
g_pr_points[client][style] += 220;
g_Points[client][style][1] += 220;
}
case 4:
{
g_pr_points[client][style] += 205;
g_Points[client][style][1] += 205;
}
case 5:
{
g_pr_points[client][style] += 190;
g_Points[client][style][1] += 190;
}
case 6:
{
g_pr_points[client][style] += 175;
g_Points[client][style][1] += 175;
}
case 7:
{
g_pr_points[client][style] += 160;
g_Points[client][style][1] += 160;
}
case 8:
{
g_pr_points[client][style] += 145;
g_Points[client][style][1] += 145;
}
case 9:
{
g_pr_points[client][style] += 130;
g_Points[client][style][1] += 130;
}
case 10:
{
g_pr_points[client][style] += 100;
g_Points[client][style][1] += 100;
}
case 11:
{
g_pr_points[client][style] += 95;
g_Points[client][style][1] += 95;
}
case 12:
{
g_pr_points[client][style] += 90;
g_Points[client][style][1] += 90;
}
case 13:
{
g_pr_points[client][style] += 80;
g_Points[client][style][1] += 80;
}
case 14:
{
g_pr_points[client][style] += 70;
g_Points[client][style][1] += 70;
}
case 15:
{
g_pr_points[client][style] += 60;
g_Points[client][style][1] += 60;
}
case 16:
{
g_pr_points[client][style] += 50;
g_Points[client][style][1] += 50;
}
case 17:
{
g_pr_points[client][style] += 40;
g_Points[client][style][1] += 40;
}
case 18:
{
g_pr_points[client][style] += 30;
g_Points[client][style][1] += 30;
}
case 19:
{
g_pr_points[client][style] += 20;
g_Points[client][style][1] += 20;
}
case 20:
{
g_pr_points[client][style] += 10;
g_Points[client][style][1] += 10;
}
default:
{
g_pr_points[client][style] += 5;
g_Points[client][style][1] += 5;
}
}
break;
}
}
}
}
g_pr_finishedbonuses[client][style] = finishedbonuses;
g_WRs[client][style][1] = wrbs;
// Next up: Points from stages
char szQuery[512];
Format(szQuery, 512, "SELECT mapname, stage, (select count(1)+1 from ck_wrcps b where a.mapname=b.mapname and a.runtimepro > b.runtimepro and a.style = b.style and a.stage = b.stage) AS rank FROM ck_wrcps a where steamid = '%s' AND style = %i;", szSteamId, style);
SQL_TQuery(g_hDb, sql_CountFinishedStagesCallback, szQuery, pack, DBPrio_Low);
}
//
// 4. Calculate points gained from stages
// Fetched values
// mapname, stage, rank, total
//
public void sql_CountFinishedStagesCallback(Handle owner, Handle hndl, const char[] error, any pack)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (sql_CountFinishedStagesCallback): %s", error);
CloseHandle(pack);
return;
}
ResetPack(pack);
int client = ReadPackCell(pack);
int style = ReadPackCell(pack);
char szMap[128], szSteamId[32], szMapName2[128];
// int totalplayers, rank;
getSteamIDFromClient(client, szSteamId, 32);
int finishedstages = 0;
int rank;
int wrcps = 0;
if (SQL_HasResultSet(hndl))
{
while (SQL_FetchRow(hndl))
{
finishedstages++;
// Total amount of players who have finished the bonus
// totalplayers = SQL_FetchInt(hndl, 2);
SQL_FetchString(hndl, 0, szMap, 128);
rank = SQL_FetchInt(hndl, 2);
for (int i = 0; i < GetArraySize(g_MapList); i++) // Check that the map is in the mapcycle
{
GetArrayString(g_MapList, i, szMapName2, sizeof(szMapName2));
if (StrEqual(szMapName2, szMap, false))
{
if (rank == 1)
{
wrcps++;
int wrcpPoints = GetConVarInt(g_hWrcpPoints);
if (wrcpPoints > 0)
{
g_pr_points[client][style] += wrcpPoints;
g_Points[client][style][6] += wrcpPoints;
}
}
break;
}
}
}
}
g_pr_finishedstages[client][style] = finishedstages;
g_WRs[client][style][2] = wrcps;
// Next up: Points from maps
char szQuery[512];
Format(szQuery, 512, "SELECT mapname, (select count(1)+1 from ck_playertimes b where a.mapname=b.mapname and a.runtimepro > b.runtimepro AND b.style = %i) AS rank, (SELECT count(1) FROM ck_playertimes b WHERE a.mapname = b.mapname AND b.style = %i) as total, (SELECT tier FROM `ck_maptier` b WHERE a.mapname = b.mapname) as tier FROM ck_playertimes a where steamid = '%s' AND style = %i;", style, style, szSteamId, style);
SQL_TQuery(g_hDb, sql_CountFinishedMapsCallback, szQuery, pack, DBPrio_Low);
}
// 5. Count the points gained from regular maps
// Fetching:
// mapname, rank, total, tier
public void sql_CountFinishedMapsCallback(Handle owner, Handle hndl, const char[] error, any pack)
{
if (hndl == null)
{
LogError("[Surftimer] SQL Error (sql_CountFinishedMapsCallback): %s", error);
CloseHandle(pack);
return;
}
ResetPack(pack);
int client = ReadPackCell(pack);
int style = ReadPackCell(pack);
CloseHandle(pack);
char szMap[128], szMapName2[128];
int finishedMaps = 0, totalplayers, rank, tier, wrs;
if (SQL_HasResultSet(hndl))
{
while (SQL_FetchRow(hndl))
{
// Total amount of players who have finished the map
totalplayers = SQL_FetchInt(hndl, 2);
// Rank in that map
rank = SQL_FetchInt(hndl, 1);
// Map name
SQL_FetchString(hndl, 0, szMap, 128);
// Map tier
tier = SQL_FetchInt(hndl, 3);
for (int i = 0; i < GetArraySize(g_MapList); i++) // Check that the map is in the mapcycle
{
GetArrayString(g_MapList, i, szMapName2, sizeof(szMapName2));
if (StrEqual(szMapName2, szMap, false))
{
finishedMaps++;
float wrpoints;
int iwrpoints;
float points;
// bool wr;
// bool top10;
float g1points;
float g2points;
float g3points;
float g4points;
float g5points;
// Calculate Group Ranks
// Group 1
float fG1top;
int g1top;
int g1bot = 11;
fG1top = (float(totalplayers) * g_Group1Pc);
fG1top += 11.0; // Rank 11 is always End of Group 1
g1top = RoundToCeil(fG1top);
int g1difference = (g1top - g1bot);
if (g1difference < 4)
g1top = (g1bot + 4);
// Group 2
float fG2top;
int g2top;
int g2bot;
g2bot = g1top + 1;
fG2top = (float(totalplayers) * g_Group2Pc);
fG2top += 11.0;
g2top = RoundToCeil(fG2top);