· 8 years ago · Jul 02, 2018, 04:20 PM
1 /////////////////////////
2// PREPARED STATEMENTS //
3////////////////////////
4
5//TABLE CK_SPAWNLOCATIONS
6char sql_createSpawnLocations[] = "CREATE TABLE IF NOT EXISTS ck_spawnlocations (mapname VARCHAR(54) NOT NULL, pos_x FLOAT NOT NULL, pos_y FLOAT NOT NULL, pos_z FLOAT NOT NULL, ang_x FLOAT NOT NULL, ang_y FLOAT NOT NULL, ang_z FLOAT NOT NULL, zonegroup INT(12) DEFAULT 0, stage INT(12) DEFAULT 0, PRIMARY KEY(mapname, zonegroup));";
7char sql_insertSpawnLocations[] = "INSERT INTO ck_spawnlocations (mapname, pos_x, pos_y, pos_z, ang_x, ang_y, ang_z, zonegroup) VALUES ('%s', '%f', '%f', '%f', '%f', '%f', '%f', %i);";
8char sql_updateSpawnLocations[] = "UPDATE ck_spawnlocations SET pos_x = '%f', pos_y = '%f', pos_z = '%f', ang_x = '%f', ang_y = '%f', ang_z = '%f' WHERE mapname = '%s' AND zonegroup = %i";
9char sql_selectSpawnLocations[] = "SELECT mapname, pos_x, pos_y, pos_z, ang_x, ang_y, ang_z, zonegroup FROM ck_spawnlocations WHERE mapname ='%s';";
10char sql_deleteSpawnLocations[] = "DELETE FROM ck_spawnlocations WHERE mapname = '%s' AND zonegroup = %i";
11
12
13//TABLE PLAYERTITLES
14char sql_createPlayerFlags[] = "CREATE TABLE IF NOT EXISTS ck_playertitles (steamid VARCHAR(32), vip INT(12) DEFAULT 0, mapper INT(12) DEFAULT 0, teacher INT(12) DEFAULT 0, custom1 INT(12) DEFAULT 0, custom2 INT(12) DEFAULT 0, custom3 INT(12) DEFAULT 0, custom4 INT(12) DEFAULT 0, custom5 INT(12) DEFAULT 0, custom6 INT(12) DEFAULT 0, custom7 INT(12) DEFAULT 0, custom8 INT(12) DEFAULT 0, custom9 INT(12) DEFAULT 0, custom10 INT(12) DEFAULT 0, custom11 INT(12) DEFAULT 0, custom12 INT(12) DEFAULT 0, custom13 INT(12) DEFAULT 0, custom14 INT(12) DEFAULT 0, custom15 INT(12) DEFAULT 0, custom16 INT(12) DEFAULT 0, custom17 INT(12) DEFAULT 0, custom18 INT(12) DEFAULT 0, custom19 INT(12) DEFAULT 0, custom20 INT(12) DEFAULT 0, inuse INT(12) DEFAULT 0, PRIMARY KEY(steamid));";
15char sql_insertPlayerFlags[] = "INSERT INTO ck_playertitles (steamid, vip, mapper, teacher, custom1, custom2, custom3, custom4, custom5, custom6, custom7, custom8, custom9, custom10, custom11, custom12, custom13, custom14, custom15, custom16, custom17, custom18, custom19, custom20, inuse) VALUES ('%s', %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i);";
16char sql_updatePlayerFlags[] = "UPDATE ck_playertitles SET vip = %i, mapper = %i, teacher = %i, custom1 = %i, custom2 = %i, custom3 = %i, custom4 = %i, custom5 = %i, custom6 = %i, custom7 = %i, custom8 = %i, custom9 = %i, custom10 = %i, custom11 = %i, custom12 = %i, custom13 = %i, custom14 = %i, custom15 = %i, custom16 = %i, custom17 = %i, custom18 = %i, custom19 = %i, custom20 = %i, inuse = %i WHERE steamid = '%s'";
17char sql_updatePlayerFlagsInUse[] = "UPDATE ck_playertitles SET inuse = %i WHERE steamid = '%s'";
18char sql_deletePlayerFlags[] = "DELETE FROM ck_playertitles WHERE steamid = '%s'";
19char sql_selectPlayerFlags[] = "SELECT vip, mapper, teacher, custom1, custom2, custom3, custom4, custom5, custom6, custom7, custom8, custom9, custom10, custom11, custom12, custom13, custom14, custom15, custom16, custom17, custom18, custom19, custom20, inuse FROM ck_playertitles WHERE steamid = '%s'";
20
21//TABLE CHALLENGE
22char sql_createChallenges[] = "CREATE TABLE IF NOT EXISTS ck_challenges (steamid VARCHAR(32), steamid2 VARCHAR(32), bet INT(12) unsigned, map VARCHAR(32), date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(steamid,steamid2,date));";
23char sql_insertChallenges[] = "INSERT INTO ck_challenges (steamid, steamid2, bet, map) VALUES('%s','%s','%i','%s');";
24char sql_selectChallenges2[] = "SELECT steamid, steamid2, bet, map, date FROM ck_challenges where steamid = '%s' OR steamid2 ='%s' ORDER BY date DESC";
25char sql_selectChallenges[] = "SELECT steamid, steamid2, bet, map FROM ck_challenges where steamid = '%s' OR steamid2 ='%s'";
26char sql_selectChallengesCompare[] = "SELECT steamid, steamid2, bet FROM ck_challenges where (steamid = '%s' AND steamid2 ='%s') OR (steamid = '%s' AND steamid2 ='%s')";
27char sql_deleteChallenges[] = "DELETE from ck_challenges where steamid = '%s'";
28
29//TABLE ZONES
30char sql_createZones[] = "CREATE TABLE IF NOT EXISTS ck_zones (mapname VARCHAR(54) NOT NULL, zoneid INT(12) DEFAULT '-1', zonetype INT(12) DEFAULT '-1', zonetypeid INT(12) DEFAULT '-1', pointa_x FLOAT DEFAULT '-1.0', pointa_y FLOAT DEFAULT '-1.0', pointa_z FLOAT DEFAULT '-1.0', pointb_x FLOAT DEFAULT '-1.0', pointb_y FLOAT DEFAULT '-1.0', pointb_z FLOAT DEFAULT '-1.0', vis INT(12) DEFAULT '0', team INT(12) DEFAULT '0', zonegroup INT(12) DEFAULT 0, zonename VARCHAR(128), PRIMARY KEY(mapname, zoneid));";
31char sql_insertZones[] = "INSERT INTO ck_zones (mapname, zoneid, zonetype, zonetypeid, pointa_x, pointa_y, pointa_z, pointb_x, pointb_y, pointb_z, vis, team, zonegroup, zonename) VALUES ('%s', '%i', '%i', '%i', '%f', '%f', '%f', '%f', '%f', '%f', '%i', '%i', '%i','%s')";
32char sql_updateZone[] = "UPDATE ck_zones SET zonetype = '%i', zonetypeid = '%i', pointa_x = '%f', pointa_y ='%f', pointa_z = '%f', pointb_x = '%f', pointb_y = '%f', pointb_z = '%f', vis = '%i', team = '%i', zonegroup = '%i' WHERE zoneid = '%i' AND mapname = '%s'";
33char sql_selectzoneTypeIds[] = "SELECT zonetypeid FROM ck_zones WHERE mapname='%s' AND zonetype='%i' AND zonegroup = '%i';";
34char sql_selectMapZones[] = "SELECT zoneid, zonetype, zonetypeid, pointa_x, pointa_y, pointa_z, pointb_x, pointb_y, pointb_z, vis, team, zonegroup, zonename FROM ck_zones WHERE mapname = '%s' ORDER BY zonetypeid ASC";
35char sql_selectTotalBonusCount[] = "SELECT mapname, zoneid, zonetype, zonetypeid, pointa_x, pointa_y, pointa_z, pointb_x, pointb_y, pointb_z, vis, team, zonegroup, zonename FROM ck_zones WHERE zonetype = 3 GROUP BY mapname, zonegroup;";
36char sql_selectZoneIds[] = "SELECT mapname, zoneid, zonetype, zonetypeid, pointa_x, pointa_y, pointa_z, pointb_x, pointb_y, pointb_z, vis, team, zonegroup, zonename FROM ck_zones WHERE mapname = '%s' ORDER BY zoneid ASC";
37char sql_selectBonusesInMap[] = "SELECT mapname, zonegroup, zonename FROM `ck_zones` WHERE mapname LIKE '%s%%' AND zonegroup > 0 GROUP BY zonegroup;";
38char sql_deleteMapZones[] = "DELETE FROM ck_zones WHERE mapname = '%s'";
39char sql_deleteZone[] = "DELETE FROM ck_zones WHERE mapname = '%s' AND zoneid = '%i'";
40char sql_deleteAllZones[] = "DELETE FROM ck_zones";
41char sql_deleteZonesInGroup[] = "DELETE FROM ck_zones WHERE mapname = '%s' AND zonegroup = '%i'";
42char sql_setZoneNames[] = "UPDATE ck_zones SET zonename = '%s' WHERE mapname = '%s' AND zonegroup = '%i';";
43
44//TABLE MAPTIER
45char sql_createMapTier[] = "CREATE TABLE IF NOT EXISTS ck_maptier (mapname VARCHAR(54) NOT NULL, tier INT(12), btier1 INT(12), btier2 INT(12), btier3 INT(12), btier4 INT(12), btier5 INT(12), btier6 INT(12), btier7 INT(12), btier8 INT(12), btier9 INT(12), btier10 INT(12), PRIMARY KEY(mapname));";
46char sql_selectMapTier[] = "SELECT tier, btier1, btier2, btier3, btier4, btier5, btier6, btier7, btier8, btier9, btier10 FROM ck_maptier WHERE mapname = '%s'";
47char sql_deleteAllMapTiers[] = "DELETE FROM ck_maptier";
48char sql_insertmaptier[] = "INSERT INTO ck_maptier (mapname, tier) VALUES ('%s', '%i');";
49char sql_updatemaptier[] = "UPDATE ck_maptier SET tier = %i WHERE mapname ='%s'";
50char sql_updateBonusTier[] = "UPDATE ck_maptier SET btier%i = %i WHERE mapname ='%s'";
51char sql_insertBonusTier[] = "INSERT INTO ck_maptier (mapname, btier%i) VALUES ('%s', '%i');";
52
53//TABLE BONUS
54char sql_createBonus[] = "CREATE TABLE IF NOT EXISTS ck_bonus (steamid VARCHAR(32), name VARCHAR(64), mapname VARCHAR(32), runtime FLOAT NOT NULL DEFAULT '-1.0', zonegroup INT(12) NOT NULL DEFAULT 1, PRIMARY KEY(steamid, mapname, zonegroup));";
55char sql_createBonusIndex[] = "CREATE INDEX bonusrank ON ck_bonus (mapname,runtime,zonegroup);";
56char sql_insertBonus[] = "INSERT INTO ck_bonus (steamid, name, mapname, runtime, zonegroup) VALUES ('%s', '%s', '%s', '%f', '%i')";
57char sql_updateBonus[] = "UPDATE ck_bonus SET runtime = '%f', name = '%s' WHERE steamid = '%s' AND mapname = '%s' AND zonegroup = %i";
58char sql_selectBonusCount[] = "SELECT zonegroup, count(1) FROM ck_bonus WHERE mapname = '%s' GROUP BY zonegroup";
59char sql_selectPersonalBonusRecords[] = "SELECT runtime, zonegroup FROM ck_bonus WHERE steamid = '%s' AND mapname = '%s' AND runtime > '0.0'";
60char sql_selectPlayerRankBonus[] = "SELECT name FROM ck_bonus WHERE runtime <= (SELECT runtime FROM ck_bonus WHERE steamid = '%s' AND mapname= '%s' AND runtime > 0.0 AND zonegroup = %i) AND mapname = '%s' AND zonegroup = %i;";
61char sql_selectFastestBonus[] = "SELECT name, MIN(runtime), zonegroup FROM ck_bonus WHERE mapname = '%s' GROUP BY zonegroup;";
62char sql_deleteBonus[] = "DELETE FROM ck_bonus WHERE mapname = '%s'";
63char sql_selectAllBonusTimesinMap[] = "SELECT zonegroup, runtime from ck_bonus WHERE mapname = '%s';";
64char sql_selectTopBonusSurfers[] = "SELECT db2.steamid, db1.name, db2.runtime as overall, db1.steamid, db2.mapname FROM ck_bonus as db2 INNER JOIN ck_playerrank as db1 on db2.steamid = db1.steamid WHERE db2.mapname LIKE '%s' AND db2.runtime > -1.0 AND zonegroup = %i ORDER BY db2.mapname,overall ASC LIMIT 100;";
65
66//TABLE CHECKPOINTS
67char sql_createCheckpoints[] = "CREATE TABLE IF NOT EXISTS ck_checkpoints (steamid VARCHAR(32), mapname VARCHAR(32), cp1 FLOAT DEFAULT '0.0', cp2 FLOAT DEFAULT '0.0', cp3 FLOAT DEFAULT '0.0', cp4 FLOAT DEFAULT '0.0', cp5 FLOAT DEFAULT '0.0', cp6 FLOAT DEFAULT '0.0', cp7 FLOAT DEFAULT '0.0', cp8 FLOAT DEFAULT '0.0', cp9 FLOAT DEFAULT '0.0', cp10 FLOAT DEFAULT '0.0', cp11 FLOAT DEFAULT '0.0', cp12 FLOAT DEFAULT '0.0', cp13 FLOAT DEFAULT '0.0', cp14 FLOAT DEFAULT '0.0', cp15 FLOAT DEFAULT '0.0', cp16 FLOAT DEFAULT '0.0', cp17 FLOAT DEFAULT '0.0', cp18 FLOAT DEFAULT '0.0', cp19 FLOAT DEFAULT '0.0', cp20 FLOAT DEFAULT '0.0', cp21 FLOAT DEFAULT '0.0', cp22 FLOAT DEFAULT '0.0', cp23 FLOAT DEFAULT '0.0', cp24 FLOAT DEFAULT '0.0', cp25 FLOAT DEFAULT '0.0', cp26 FLOAT DEFAULT '0.0', cp27 FLOAT DEFAULT '0.0', cp28 FLOAT DEFAULT '0.0', cp29 FLOAT DEFAULT '0.0', cp30 FLOAT DEFAULT '0.0', cp31 FLOAT DEFAULT '0.0', cp32 FLOAT DEFAULT '0.0', cp33 FLOAT DEFAULT '0.0', cp34 FLOAT DEFAULT '0.0', cp35 FLOAT DEFAULT '0.0', zonegroup INT(12) NOT NULL DEFAULT 0, PRIMARY KEY(steamid, mapname, zonegroup));";
68//char sql_updateCheckpoints[] = "UPDATE ck_checkpoints SET cp1='%f', cp2='%f', cp3='%f', cp4='%f', cp5='%f', cp6='%f', cp7='%f', cp8='%f', cp9='%f', cp10='%f', cp11='%f', cp12='%f', cp13='%f', cp14='%f', cp15='%f', cp16='%f', cp17='%f', cp18='%f', cp19='%f', cp20='%f', cp21='%f', cp22='%f', cp23='%f', cp24='%f', cp25='%f', cp26='%f', cp27='%f', cp28='%f', cp29='%f', cp30='%f', cp31='%f', cp32='%f', cp33='%f', cp34='%f', cp35='%f' WHERE steamid='%s' AND mapname='%s' AND zonegroup='%i'";
69//char sql_insertCheckpoints[] = "INSERT INTO ck_checkpoints VALUES ('%s', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%i')";
70char sql_selectCheckpoints[] = "SELECT zonegroup, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, cp26, cp27, cp28, cp29, cp30, cp31, cp32, cp33, cp34, cp35 FROM ck_checkpoints WHERE mapname='%s' AND steamid = '%s';";
71char sql_selectCheckpointsinZoneGroup[] = "SELECT cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, cp26, cp27, cp28, cp29, cp30, cp31, cp32, cp33, cp34, cp35 FROM ck_checkpoints WHERE mapname='%s' AND steamid = '%s' AND zonegroup = %i;";
72char sql_selectRecordCheckpoints[] = "SELECT zonegroup, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, cp26, cp27, cp28, cp29, cp30, cp31, cp32, cp33, cp34, cp35 FROM ck_checkpoints WHERE steamid = '%s' AND mapname='%s' UNION SELECT a.zonegroup, b.cp1, b.cp2, b.cp3, b.cp4, b.cp5, b.cp6, b.cp7, b.cp8, b.cp9, b.cp10, b.cp11, b.cp12, b.cp13, b.cp14, b.cp15, b.cp16, b.cp17, b.cp18, b.cp19, b.cp20, b.cp21, b.cp22, b.cp23, b.cp24, b.cp25, b.cp26, b.cp27, b.cp28, b.cp29, b.cp30, b.cp31, b.cp32, b.cp33, b.cp34, b.cp35 FROM ck_bonus a LEFT JOIN ck_checkpoints b ON a.steamid = b.steamid AND a.zonegroup = b.zonegroup WHERE a.mapname = '%s' GROUP BY a.zonegroup";
73//char sql_selectRecordCheckpoints[] = "SELECT zonegroup, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, cp26, cp27, cp28, cp29, cp30, cp31, cp32, cp33, cp34, cp35 FROM ck_checkpoints WHERE steamid = '%s' AND mapname='%s' UNION SELECT a.zonegroup, b.cp1, b.cp2, b.cp3, b.cp4, b.cp5, b.cp6, b.cp7, b.cp8, b.cp9, b.cp10, b.cp11, b.cp12, b.cp13, b.cp14, b.cp15, b.cp16, b.cp17, b.cp18, b.cp19, b.cp20, b.cp21, b.cp22, b.cp23, b.cp24, b.cp25, b.cp26, b.cp27, b.cp28, b.cp29, b.cp30, b.cp31, b.cp32, b.cp33, b.cp34, b.cp35 FROM ck_bonus a LEFT JOIN ck_checkpoints b USE INDEX(steamid) ON a.steamid = b.steamid AND a.zonegroup = b.zonegroup WHERE a.mapname = '%s' GROUP BY a.zonegroup;";
74char sql_deleteCheckpoints[] = "DELETE FROM ck_checkpoints WHERE mapname = '%s'";
75
76//TABLE LATEST 15 LOCAL RECORDS
77char sql_createLatestRecords[] = "CREATE TABLE IF NOT EXISTS ck_latestrecords (steamid VARCHAR(32), name VARCHAR(64), runtime FLOAT NOT NULL DEFAULT '-1.0', map VARCHAR(32), date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(steamid,map,date));";
78char sql_insertLatestRecords[] = "INSERT INTO ck_latestrecords (steamid, name, runtime, map) VALUES('%s','%s','%f','%s');";
79char sql_selectLatestRecords[] = "SELECT name, runtime, map, date FROM ck_latestrecords ORDER BY date DESC LIMIT 50";
80
81//TABLE PLAYEROPTIONS
82char sql_createPlayerOptions[] = "CREATE TABLE IF NOT EXISTS ck_playeroptions (steamid VARCHAR(32), speedmeter INT(12) DEFAULT '0', quake_sounds INT(12) DEFAULT '1', autobhop INT(12) DEFAULT '0', shownames INT(12) DEFAULT '1', goto INT(12) DEFAULT '1', showtime INT(12) DEFAULT '1', hideplayers INT(12) DEFAULT '0', showspecs INT(12) DEFAULT '1', knife VARCHAR(32) DEFAULT 'weapon_knife', new1 INT(12) DEFAULT '0', new2 INT(12) DEFAULT '0', new3 INT(12) DEFAULT '0', checkpoints INT(12) DEFAULT '1', PRIMARY KEY(steamid));";
83char sql_insertPlayerOptions[] = "INSERT INTO ck_playeroptions (steamid, speedmeter, quake_sounds, autobhop, shownames, goto, showtime, hideplayers, showspecs, knife, new1, new2, new3, checkpoints) VALUES('%s', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%s', '%i', '%i', '%i', '%i');";
84char sql_selectPlayerOptions[] = "SELECT speedmeter, quake_sounds, autobhop, shownames, goto, showtime, hideplayers, showspecs, knife, new1, new2, new3, checkpoints FROM ck_playeroptions where steamid = '%s'";
85char sql_updatePlayerOptions[] = "UPDATE ck_playeroptions SET speedmeter ='%i', quake_sounds ='%i', autobhop ='%i', shownames ='%i', goto ='%i', showtime ='%i', hideplayers ='%i', showspecs ='%i', knife ='%s', new1 = '%i', new2 = '%i', new3 = '%i', checkpoints = '%i' where steamid = '%s'";
86
87//TABLE PLAYERRANK
88char sql_createPlayerRank[] = "CREATE TABLE IF NOT EXISTS ck_playerrank (steamid VARCHAR(32), name VARCHAR(64), country VARCHAR(32), points INT(12) unsigned DEFAULT '0', winratio INT(12) DEFAULT '0', pointsratio INT(12) DEFAULT '0',finishedmaps INT(12) DEFAULT '0', multiplier INT(12) unsigned DEFAULT '0', finishedmapspro INT(12) DEFAULT '0', lastseen DATE, PRIMARY KEY(steamid));";
89char sql_insertPlayerRank[] = "INSERT INTO ck_playerrank (steamid, name, country) VALUES('%s', '%s', '%s');";
90char sql_updatePlayerRankPoints[] = "UPDATE ck_playerrank SET name ='%s', points ='%i', finishedmapspro='%i',winratio = '%i',pointsratio = '%i' where steamid='%s'";
91char sql_updatePlayerRankPoints2[] = "UPDATE ck_playerrank SET name ='%s', points ='%i', finishedmapspro='%i',winratio = '%i',pointsratio = '%i', country ='%s' where steamid='%s'";
92char sql_updatePlayerRank[] = "UPDATE ck_playerrank SET finishedmaps ='%i', finishedmapspro='%i', multiplier ='%i' where steamid='%s'";
93char sql_selectPlayerRankAll[] = "SELECT name, steamid FROM ck_playerrank where name like '%s'";
94char sql_selectPlayerRankAll2[] = "SELECT name, steamid FROM ck_playerrank where name = '%s'";
95char sql_selectPlayerName[] = "SELECT name FROM ck_playerrank where steamid = '%s'";
96char sql_UpdateLastSeenMySQL[] = "UPDATE ck_playerrank SET lastseen = NOW() where steamid = '%s';";
97char sql_UpdateLastSeenSQLite[] = "UPDATE ck_playerrank SET lastseen = date('now') where steamid = '%s';";
98char sql_selectTopPlayers[] = "SELECT name, points, finishedmapspro, steamid FROM ck_playerrank ORDER BY points DESC LIMIT 100";
99char sql_selectTopChallengers[] = "SELECT name, winratio, pointsratio, steamid FROM ck_playerrank ORDER BY pointsratio DESC LIMIT 5";
100char sql_selectRankedPlayer[] = "SELECT steamid, name, points, finishedmapspro, multiplier, country, lastseen from ck_playerrank where steamid='%s'";
101char sql_selectRankedPlayersRank[] = "SELECT name FROM ck_playerrank WHERE points >= (SELECT points FROM ck_playerrank WHERE steamid = '%s') ORDER BY points";
102char sql_selectRankedPlayers[] = "SELECT steamid, name from ck_playerrank where points > 0 ORDER BY points DESC";
103char sql_CountRankedPlayers[] = "SELECT COUNT(steamid) FROM ck_playerrank";
104char sql_CountRankedPlayers2[] = "SELECT COUNT(steamid) FROM ck_playerrank where points > 0";
105
106//TABLE PLAYERTIMES
107char sql_createPlayertimes[] = "CREATE TABLE IF NOT EXISTS ck_playertimes (steamid VARCHAR(32), mapname VARCHAR(32), name VARCHAR(64), runtimepro FLOAT NOT NULL DEFAULT '-1.0', PRIMARY KEY(steamid,mapname));";
108char sql_createPlayertimesIndex[] = "CREATE INDEX maprank ON ck_playertimes (mapname, runtimepro);";
109char sql_insertPlayer[] = "INSERT INTO ck_playertimes (steamid, mapname, name) VALUES('%s', '%s', '%s');";
110char sql_insertPlayerTime[] = "INSERT INTO ck_playertimes (steamid, mapname, name,runtimepro) VALUES('%s', '%s', '%s', '%f');";
111char sql_updateRecordPro[] = "UPDATE ck_playertimes SET name = '%s', runtimepro = '%f' WHERE steamid = '%s' AND mapname = '%s';";
112char sql_selectPlayer[] = "SELECT steamid FROM ck_playertimes WHERE steamid = '%s' AND mapname = '%s';";
113//char sql_selectMapRecord[] = "SELECT MIN(runtimepro), name, steamid FROM ck_playertimes WHERE mapname = '%s' AND runtimepro > -1.0";
114char sql_selectMapRecord[] = "SELECT runtimepro, name, steamid FROM ck_playertimes WHERE mapname = '%s' AND runtimepro = (SELECT MIN(runtimepro) FROM ck_playertimes WHERE mapname = '%s' ) AND runtimepro > -1.0;";
115char sql_selectPersonalRecords[] = "SELECT runtimepro, name FROM ck_playertimes WHERE mapname = '%s' AND steamid = '%s' AND runtimepro > 0.0";
116char sql_selectPersonalAllRecords[] = "SELECT db1.name, db2.steamid, db2.mapname, db2.runtimepro as overall, db1.steamid FROM ck_playertimes as db2 INNER JOIN ck_playerrank as db1 on db2.steamid = db1.steamid WHERE db2.steamid = '%s' AND db2.runtimepro > -1.0 ORDER BY mapname ASC;";
117char sql_selectProSurfers[] = "SELECT db1.name, db2.runtimepro, db2.steamid, db1.steamid FROM ck_playertimes as db2 INNER JOIN ck_playerrank as db1 on db2.steamid = db1.steamid WHERE db2.mapname = '%s' AND db2.runtimepro > -1.0 ORDER BY db2.runtimepro ASC LIMIT 20";
118char sql_selectTopSurfers2[] = "SELECT db2.steamid, db1.name, db2.runtimepro as overall, db1.steamid, db2.mapname FROM ck_playertimes as db2 INNER JOIN ck_playerrank as db1 on db2.steamid = db1.steamid WHERE db2.mapname LIKE '%s%%%%' AND db2.runtimepro > -1.0 ORDER BY db2.mapname, overall ASC LIMIT 100;";
119char sql_selectTopSurfers[] = "SELECT db2.steamid, db1.name, db2.runtimepro as overall, db1.steamid, db2.mapname FROM ck_playertimes as db2 INNER JOIN ck_playerrank as db1 on db2.steamid = db1.steamid WHERE db2.mapname = '%s' AND db2.runtimepro > -1.0 ORDER BY overall ASC LIMIT 100;";
120char sql_selectPlayerProCount[] = "SELECT name FROM ck_playertimes WHERE mapname = '%s' AND runtimepro > -1.0;";
121char sql_selectPlayerRankProTime[] = "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;";
122char sql_selectMapRecordHolders[] = "SELECT y.steamid, COUNT(*) AS rekorde FROM (SELECT s.steamid FROM ck_playertimes s INNER JOIN (SELECT mapname, MIN(runtimepro) AS runtimepro FROM ck_playertimes where runtimepro > -1.0 GROUP BY mapname) x ON s.mapname = x.mapname AND s.runtimepro = x.runtimepro) y GROUP BY y.steamid ORDER BY rekorde DESC , y.steamid LIMIT 5;";
123char sql_selectMapRecordCount[] = "SELECT y.steamid, COUNT(*) AS rekorde FROM (SELECT s.steamid FROM ck_playertimes s INNER JOIN (SELECT mapname, MIN(runtimepro) AS runtimepro FROM ck_playertimes where runtimepro > -1.0 GROUP BY mapname) x ON s.mapname = x.mapname AND s.runtimepro = x.runtimepro) y where y.steamid = '%s' GROUP BY y.steamid ORDER BY rekorde DESC , y.steamid;";
124char sql_selectAllMapTimesinMap[] = "SELECT runtimepro from ck_playertimes WHERE mapname = '%s';";
125
126//TABLE PLAYERTEMP
127char sql_createPlayertmp[] = "CREATE TABLE IF NOT EXISTS ck_playertemp (steamid VARCHAR(32), mapname VARCHAR(32), cords1 FLOAT NOT NULL DEFAULT '-1.0', cords2 FLOAT NOT NULL DEFAULT '-1.0', cords3 FLOAT NOT NULL DEFAULT '-1.0', angle1 FLOAT NOT NULL DEFAULT '-1.0',angle2 FLOAT NOT NULL DEFAULT '-1.0',angle3 FLOAT NOT NULL DEFAULT '-1.0', EncTickrate INT(12) DEFAULT '-1.0', runtimeTmp FLOAT NOT NULL DEFAULT '-1.0', Stage INT, zonegroup INT NOT NULL DEFAULT 0, PRIMARY KEY(steamid,mapname));";
128char sql_insertPlayerTmp[] = "INSERT INTO ck_playertemp (cords1, cords2, cords3, angle1,angle2,angle3,runtimeTmp,steamid,mapname,EncTickrate,Stage,zonegroup) VALUES ('%f','%f','%f','%f','%f','%f','%f','%s', '%s', '%i', %i, %i);";
129char sql_updatePlayerTmp[] = "UPDATE ck_playertemp SET cords1 = '%f', cords2 = '%f', cords3 = '%f', angle1 = '%f', angle2 = '%f', angle3 = '%f', runtimeTmp = '%f', mapname ='%s', EncTickrate='%i', Stage = %i, zonegroup = %i WHERE steamid = '%s';";
130char sql_deletePlayerTmp[] = "DELETE FROM ck_playertemp where steamid = '%s';";
131char sql_selectPlayerTmp[] = "SELECT cords1,cords2,cords3, angle1, angle2, angle3,runtimeTmp, EncTickrate, Stage, zonegroup FROM ck_playertemp WHERE steamid = '%s' AND mapname = '%s';";
132
133// ADMIN
134char sqlite_dropChallenges[] = "DROP TABLE ck_challenges; VACCUM";
135char sql_dropChallenges[] = "DROP TABLE ck_challenges;";
136char sqlite_dropPlayer[] = "DROP TABLE ck_playertimes; VACCUM";
137char sql_dropPlayer[] = "DROP TABLE ck_playertimes;";
138char sql_dropPlayerRank[] = "DROP TABLE ck_playerrank;";
139char sqlite_dropPlayerRank[] = "DROP TABLE ck_playerrank; VACCUM";
140char sql_resetRecords[] = "DELETE FROM ck_playertimes WHERE steamid = '%s'";
141char sql_resetRecords2[] = "DELETE FROM ck_playertimes WHERE steamid = '%s' AND mapname LIKE '%s';";
142char sql_resetRecordPro[] = "UPDATE ck_playertimes SET runtimepro = '-1.0' WHERE steamid = '%s' AND mapname LIKE '%s';";
143char sql_resetCheckpoints[] = "DELETE FROM ck_checkpoints WHERE steamid = '%s' AND mapname LIKE '%s';";
144char sql_resetMapRecords[] = "DELETE FROM ck_playertimes WHERE mapname = '%s'";
145
146////////////////////////
147//// DATABASE SETUP/////
148////////////////////////
149
150public void db_setupDatabase()
151{
152 if(!SQL_CheckConfig("cksurf"))
153 SetFailState("Invalid or couldn't find database configuration for \"cksurf\" inside \"addons/sourcemod/configs/databases.cfg\"");
154
155 Database.Connect(Callback_ConnectDB, "cksurf");
156}
157
158public void Callback_ConnectDB(Database db, const char[] error, int data)
159{
160 if (db == null)
161 {
162 SetFailState("(ConnectDB-Ranking) - %s", error);
163 return;
164 }
165
166 g_hDb = db;
167 g_hDb.SetCharset("utf8mb4");
168
169 char sDriver[8];
170 SQL_ReadDriver(g_hDb, sDriver, 8);
171
172 if (strcmp(sDriver, "mysql", false) == 0)
173 g_DbType = MYSQL;
174 else if (strcmp(sDriver, "sqlite", false) == 0)
175 g_DbType = SQLITE;
176 else
177 {
178 LogError("[ckSurf] Invalid Database-Type");
179 return;
180 }
181
182 db_setupDatabase2();
183}
184
185void db_setupDatabase2()
186{
187 // If updating from a previous version
188 SQL_LockDatabase(g_hDb);
189 //SQL_FastQuery(g_hDb, "SET NAMES 'utf8mb4'");
190 g_hDb.SetCharset("utf8mb4");
191
192 ////////////////////////////////
193 // CHECK WHICH CHANGES ARE //
194 // TO BE DONE TO THE DATABASE //
195 ////////////////////////////////
196 g_bRenaming = false;
197 g_bInTransactionChain = false;
198
199 // If coming from KZTimer or a really old version, rename and edit tables to new format
200 if (SQL_FastQuery(g_hDb, "SELECT steamid FROM playerrank LIMIT 1") && !SQL_FastQuery(g_hDb, "SELECT steamid FROM ck_playerrank LIMIT 1"))
201 {
202 SQL_UnlockDatabase(g_hDb);
203 db_renameTables();
204 return;
205 }
206 else // If startring for the first time and tables haven't been created yet.
207 if (!SQL_FastQuery(g_hDb, "SELECT steamid FROM playerrank LIMIT 1") && !SQL_FastQuery(g_hDb, "SELECT steamid FROM ck_playerrank LIMIT 1"))
208 {
209 SQL_UnlockDatabase(g_hDb);
210 db_createTables();
211 return;
212 }
213
214
215 // 1.17 Command to disable checkpoint messages
216 SQL_FastQuery(g_hDb, "ALTER TABLE ck_playeroptions ADD checkpoints INT DEFAULT 1;");
217
218
219 ////////////////////////////
220 // 1.18 A bunch of changes //
221 // - Zone Groups //
222 // - Zone Names //
223 // - Bonus Tiers //
224 // - Titles //
225 // - More checkpoints //
226 ////////////////////////////
227
228 SQL_FastQuery(g_hDb, "ALTER TABLE ck_zones ADD zonegroup INT NOT NULL DEFAULT 0;");
229 SQL_FastQuery(g_hDb, "ALTER TABLE ck_zones ADD zonename VARCHAR(128);");
230 SQL_FastQuery(g_hDb, "ALTER TABLE ck_playertemp ADD zonegroup INT NOT NULL DEFAULT 0;");
231 SQL_FastQuery(g_hDb, sql_createPlayerFlags);
232
233 SQL_FastQuery(g_hDb, "CREATE INDEX maprank ON ck_playertimes (mapname, runtimepro)");
234 SQL_FastQuery(g_hDb, "CREATE INDEX bonusrank ON ck_bonus (mapname,runtime,zonegroup)");
235
236 SQL_UnlockDatabase(g_hDb);
237
238 for (int i = 0; i < sizeof(g_failedTransactions); i++)
239 g_failedTransactions[i] = 0;
240
241 txn_addExtraCheckpoints();
242 return;
243}
244
245void txn_addExtraCheckpoints()
246{
247 // Add extra checkpoints to Checkpoints and add new primary key:
248 if (!SQL_FastQuery(g_hDb, "SELECT cp35 FROM ck_checkpoints;"))
249 {
250 PrintToServer("---------------------------------------------------------------------------");
251 disableServerHibernate();
252 PrintToServer("[ckSurf] Started to make changes to database. Updating from 1.17 -> 1.18.");
253 PrintToServer("[ckSurf] WARNING: DO NOT CONNECT TO THE SERVER, OR CHANGE MAP!");
254 PrintToServer("[ckSurf] Adding extra checkpoints... (1 / 6)");
255
256 g_bInTransactionChain = true;
257 Transaction h_checkpoint = SQL_CreateTransaction();
258
259 SQL_AddQuery(h_checkpoint, "ALTER TABLE ck_checkpoints RENAME TO ck_checkpoints_temp;");
260 SQL_AddQuery(h_checkpoint, sql_createCheckpoints);
261 SQL_AddQuery(h_checkpoint, "INSERT INTO ck_checkpoints(steamid, mapname, zonegroup, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20) SELECT steamid, mapname, 0, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20 FROM ck_checkpoints_temp GROUP BY mapname, steamid;");
262 SQL_AddQuery(h_checkpoint, "DROP TABLE ck_checkpoints_temp;");
263
264 SQL_ExecuteTransaction(g_hDb, h_checkpoint, SQLTxn_Success, SQLTxn_TXNFailed, 1);
265 }
266 else
267 {
268 PrintToServer("[ckSurf] No database update needed!");
269 return;
270 }
271}
272
273void txn_addZoneGroups()
274{
275 // Add zonegroups to ck_bonus and make it a primary key
276 if (!SQL_FastQuery(g_hDb, "SELECT zonegroup FROM ck_bonus;"))
277 {
278 Transaction h_bonus = SQL_CreateTransaction();
279
280 SQL_AddQuery(h_bonus, "ALTER TABLE ck_bonus RENAME TO ck_bonus_temp;");
281 SQL_AddQuery(h_bonus, sql_createBonus);
282 SQL_AddQuery(h_bonus, sql_createBonusIndex);
283 SQL_AddQuery(h_bonus, "INSERT INTO ck_bonus(steamid, name, mapname, runtime) SELECT steamid, name, mapname, runtime FROM ck_bonus_temp;");
284 SQL_AddQuery(h_bonus, "DROP TABLE ck_bonus_temp;");
285
286 SQL_ExecuteTransaction(g_hDb, h_bonus, SQLTxn_Success, SQLTxn_TXNFailed, 2);
287 }
288 else
289 {
290 PrintToServer("[ckSurf] Zonegroup changes were already done! Skipping to recreating playertemp!");
291 txn_recreatePlayerTemp();
292 }
293}
294
295void txn_recreatePlayerTemp()
296{
297 // Recreate playertemp without BonusTimer
298 if (SQL_FastQuery(g_hDb, "SELECT BonusTimer FROM ck_playertemp;"))
299 {
300 // No need to preserve temp data, just drop table
301 Transaction h_playertemp = SQL_CreateTransaction();
302 SQL_AddQuery(h_playertemp, "DROP TABLE IF EXISTS ck_playertemp");
303 SQL_AddQuery(h_playertemp, sql_createPlayertmp);
304 SQL_ExecuteTransaction(g_hDb, h_playertemp, SQLTxn_Success, SQLTxn_TXNFailed, 3);
305 }
306 else
307 {
308 PrintToServer("[ckSurf] Playertemp was already recreated! Skipping to bonus tiers");
309 txn_addBonusTiers();
310 }
311}
312
313void txn_addBonusTiers()
314{
315 // Add bonus tiers
316 if (SQL_FastQuery(g_hDb, "ALTER TABLE ck_maptier ADD btier1 INT;"))
317 {
318 Transaction h_maptiers = SQL_CreateTransaction();
319 char sql[258];
320 for (int x = 2; x < 11; x++)
321 {
322 Format(sql, 258, "ALTER TABLE ck_maptier ADD btier%i INT;", x);
323 SQL_AddQuery(h_maptiers, sql);
324 }
325 SQL_ExecuteTransaction(g_hDb, h_maptiers, SQLTxn_Success, SQLTxn_TXNFailed, 4);
326 }
327 else
328 {
329 PrintToServer("[ckSurf] Bonus tiers were already added. Skipping to spawn points");
330 txn_addSpawnPoints();
331 }
332}
333void txn_addSpawnPoints()
334{
335 if (!SQL_FastQuery(g_hDb, "SELECT zonegroup FROM ck_spawnlocations;"))
336 {
337 Transaction h_spawnPoints = SQL_CreateTransaction();
338 SQL_AddQuery(h_spawnPoints, "ALTER TABLE ck_spawnlocations RENAME TO ck_spawnlocations_temp;");
339 SQL_AddQuery(h_spawnPoints, sql_createSpawnLocations);
340 SQL_AddQuery(h_spawnPoints, "INSERT INTO ck_spawnlocations (mapname, pos_x, pos_y, pos_z, ang_x, ang_y, ang_z) SELECT mapname, pos_x, pos_y, pos_z, ang_x, ang_y, ang_z FROM ck_spawnlocations_temp;");
341 SQL_AddQuery(h_spawnPoints, "DROP TABLE ck_spawnlocations_temp");
342 SQL_ExecuteTransaction(g_hDb, h_spawnPoints, SQLTxn_Success, SQLTxn_TXNFailed, 5);
343 }
344 else
345 {
346 PrintToServer("[ckSurf] Spawnpoints were already added! Skipping to changes in zones");
347 txn_changesToZones();
348 }
349}
350
351void txn_changesToZones()
352{
353 Transaction h_changesToZones = SQL_CreateTransaction();
354 // Set right zonegroups
355 SQL_AddQuery(h_changesToZones, "UPDATE ck_zones SET zonegroup = 1 WHERE zonetype = 3 OR zonetype = 4;");
356 SQL_AddQuery(h_changesToZones, "UPDATE ck_zones SET zonetypeid = 0 WHERE zonetype = 3 OR zonetype = 4;");
357
358 // Remove ZoneTypes 3 & 4
359 SQL_AddQuery(h_changesToZones, "UPDATE ck_zones SET zonetype = 1 WHERE zonetype = 3;");
360 SQL_AddQuery(h_changesToZones, "UPDATE ck_zones SET zonetype = 2 WHERE zonetype = 4;");
361
362 // Adjust bigger zonetype numbers to match the changes
363 SQL_AddQuery(h_changesToZones, "UPDATE ck_zones SET zonetype = zonetype-2 WHERE zonetype > 4;");
364 SQL_ExecuteTransaction(g_hDb, h_changesToZones, SQLTxn_Success, SQLTxn_TXNFailed, 6);
365}
366
367
368public void SQLTxn_Success(Handle db, any data, int numQueries, Handle[] results, any[] queryData)
369{
370 switch (data)
371 {
372 case 1: {
373 PrintToServer("[ckSurf] Checkpoints added succesfully! Next up: adding zonegroups to ck_bonus (2 / 6)");
374 txn_addZoneGroups();
375 }
376 case 2: {
377 PrintToServer("[ckSurf] Bonus zonegroups succesfully added! Next up: recreating playertemp (3 / 6)");
378 txn_recreatePlayerTemp();
379 }
380 case 3: {
381 PrintToServer("[ckSurf] Playertemp succesfully recreated! Next up: adding bonus tiers (4 / 6)");
382 txn_addBonusTiers();
383 }
384 case 4: {
385 PrintToServer("[ckSurf] Bonus tiers added succesfully! Next up: adding spawn points (5 / 6)");
386 txn_addSpawnPoints();
387 }
388 case 5: {
389 PrintToServer("[ckSurf] Spawnpoints added succesfully! Next up: making changes to zones, to make them match the new database (6 / 6)");
390 txn_changesToZones();
391 }
392 case 6: {
393 g_bInTransactionChain = false;
394
395 revertServerHibernateSettings();
396 PrintToServer("[ckSurf] All changes succesfully done! Changing map!");
397 ForceChangeLevel(g_szMapName, "[ckSurf] Changing level after changes to the database have been done");
398 }
399 }
400}
401
402public void SQLTxn_TXNFailed(Handle db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
403{
404 if (g_failedTransactions[data] == 0)
405 {
406 switch (data)
407 {
408 case 1: {
409 PrintToServer("[ckSurf] Error in adding extra checkpoints! Retrying.. (%s)", error);
410 txn_addExtraCheckpoints();
411 }
412 case 2: {
413 PrintToServer("[ckSurf] Error in addin zonegroups! Retrying... (%s)", error);
414 txn_addZoneGroups();
415 }
416 case 3: {
417 PrintToServer("[ckSurf] Error in recreating playertemp! Retrying... (%s)", error);
418 txn_recreatePlayerTemp();
419 }
420 case 4: {
421 PrintToServer("[ckSurf] Error in adding bonus tiers! Retrying... (%s)", error);
422 txn_addBonusTiers();
423 }
424 case 5: {
425 PrintToServer("[ckSurf] Error in adding spawn points! Retrying... (%s)", error);
426 txn_addSpawnPoints();
427 }
428 case 6: {
429 PrintToServer("[ckSurf] Error in making changes to zones! Retrying... (%s)", error);
430 txn_changesToZones();
431 }
432 }
433 }
434 else
435 {
436 revertServerHibernateSettings();
437 PrintToServer("[ckSurf]: Couldn't make changes into the database. Transaction: %i, error: %s", data, error);
438 PrintToServer("[ckSurf]: Revert back to database backup.");
439 LogError("[ckSurf]: Couldn't make changes into the database. Transaction: %i, error: %s", data, error);
440 return;
441 }
442 g_failedTransactions[data]++;
443}
444
445
446public void db_createTables()
447{
448 Transaction createTableTnx = SQL_CreateTransaction();
449
450 SQL_AddQuery(createTableTnx, sql_createPlayertmp);
451 SQL_AddQuery(createTableTnx, sql_createPlayertimes);
452 SQL_AddQuery(createTableTnx, sql_createPlayertimesIndex);
453 SQL_AddQuery(createTableTnx, sql_createPlayerRank);
454 SQL_AddQuery(createTableTnx, sql_createChallenges);
455 SQL_AddQuery(createTableTnx, sql_createPlayerOptions);
456 SQL_AddQuery(createTableTnx, sql_createLatestRecords);
457 SQL_AddQuery(createTableTnx, sql_createBonus);
458 SQL_AddQuery(createTableTnx, sql_createBonusIndex);
459 SQL_AddQuery(createTableTnx, sql_createCheckpoints);
460 SQL_AddQuery(createTableTnx, sql_createZones);
461 SQL_AddQuery(createTableTnx, sql_createMapTier);
462 SQL_AddQuery(createTableTnx, sql_createSpawnLocations);
463 SQL_AddQuery(createTableTnx, sql_createPlayerFlags);
464
465 SQL_ExecuteTransaction(g_hDb, createTableTnx, SQLTxn_CreateDatabaseSuccess, SQLTxn_CreateDatabaseFailed);
466
467}
468
469public void SQLTxn_CreateDatabaseSuccess(Handle db, any data, int numQueries, Handle[] results, any[] queryData)
470{
471 PrintToServer("[ckSurf] Database tables succesfully created!");
472}
473public void SQLTxn_CreateDatabaseFailed(Handle db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
474{
475 SetFailState("[ckSurf] Database tables could not be created! Error: %s", error);
476}
477
478
479public void db_renameTables()
480{
481 disableServerHibernate();
482
483 g_bRenaming = true;
484 Transaction hndl = SQL_CreateTransaction();
485
486 SQL_AddQuery(hndl, sql_createSpawnLocations);
487
488 if (g_DbType == MYSQL)
489 {
490 // Remove unused columns, if coming from KZTimer
491 SQL_AddQuery(hndl, "ALTER TABLE challenges DROP COLUMN cp_allowed");
492 SQL_AddQuery(hndl, "ALTER TABLE latestrecords DROP COLUMN teleports");
493 SQL_AddQuery(hndl, "ALTER TABLE playeroptions2 DROP COLUMN colorchat");
494 SQL_AddQuery(hndl, "ALTER TABLE playeroptions2 DROP COLUMN Surfersmenu_sounds");
495 SQL_AddQuery(hndl, "ALTER TABLE playeroptions2 DROP COLUMN strafesync");
496 SQL_AddQuery(hndl, "ALTER TABLE playeroptions2 DROP COLUMN cpmessage");
497 SQL_AddQuery(hndl, "ALTER TABLE playeroptions2 DROP COLUMN adv_menu");
498 SQL_AddQuery(hndl, "ALTER TABLE playeroptions2 DROP COLUMN jumppenalty");
499 SQL_AddQuery(hndl, "ALTER TABLE playerrank DROP COLUMN finishedmapstp");
500 SQL_AddQuery(hndl, "ALTER TABLE playertimes DROP COLUMN teleports");
501 SQL_AddQuery(hndl, "ALTER TABLE playertimes DROP COLUMN runtime");
502 SQL_AddQuery(hndl, "ALTER TABLE playertimes DROP COLUMN teleports_pro");
503 SQL_AddQuery(hndl, "ALTER TABLE playertmp DROP COLUMN teleports");
504 SQL_AddQuery(hndl, "ALTER TABLE playertmp DROP COLUMN checkpoints");
505 SQL_AddQuery(hndl, "ALTER TABLE LatestRecords DROP COLUMN teleports");
506
507 SQL_AddQuery(hndl, "ALTER TABLE playeroptions2 RENAME TO ck_playeroptions;");
508 SQL_AddQuery(hndl, "ALTER TABLE playertimes RENAME TO ck_playertimes;");
509 SQL_AddQuery(hndl, "ALTER TABLE challenges RENAME TO ck_challenges;");
510 SQL_AddQuery(hndl, "ALTER TABLE playerrank RENAME TO ck_playerrank;");
511
512 }
513 else if (g_DbType == SQLITE)
514 {
515 // player options
516 SQL_AddQuery(hndl, sql_createPlayerOptions);
517 SQL_AddQuery(hndl, "INSERT INTO ck_playeroptions(steamid, speedmeter, quake_sounds, autobhop, shownames, goto, showtime, hideplayers, showspecs, new1, new2, new3) SELECT steamid, speedmeter, quake_sounds, autobhop, shownames, goto, showtime, hideplayers, showspecs, new1, new2, new3 FROM playeroptions2;");
518 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS playeroptions2");
519
520 // player times
521 SQL_AddQuery(hndl, sql_createPlayertimes);
522 SQL_AddQuery(hndl, sql_createPlayertimesIndex);
523 SQL_AddQuery(hndl, "INSERT INTO ck_playertimes(steamid, mapname, name, runtimepro) SELECT steamid, mapname, name, runtimepro FROM playertimes;");
524 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS playertimes");
525
526 // challenges
527 SQL_AddQuery(hndl, sql_createChallenges);
528 SQL_AddQuery(hndl, "INSERT INTO ck_challenges(steamid, steamid2, bet, map, date) SELECT steamid, steamid2, bet, map, date FROM challenges;");
529 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS challenges");
530
531 // playerrank
532 SQL_AddQuery(hndl, sql_createPlayerRank);
533 SQL_AddQuery(hndl, "INSERT INTO ck_playerrank(steamid, name, country, points, winratio, pointsratio, finishedmaps, multiplier, finishedmapspro, lastseen) SELECT steamid, name, country, points, winratio, pointsratio, finishedmaps, multiplier, finishedmapspro, lastseen FROM playerrank;");
534 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS playerrank");
535 }
536
537 SQL_AddQuery(hndl, "ALTER TABLE bonus RENAME TO ck_bonus;");
538 SQL_AddQuery(hndl, "ALTER TABLE checkpoints RENAME TO ck_checkpoints;");
539 SQL_AddQuery(hndl, "ALTER TABLE maptier RENAME TO ck_maptier;");
540 SQL_AddQuery(hndl, "ALTER TABLE zones RENAME TO ck_zones;");
541
542 SQL_AddQuery(hndl, sql_createPlayertmp);
543 SQL_AddQuery(hndl, sql_createLatestRecords);
544
545 // Drop useless tables from KZTimer
546 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS playertmp");
547 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS LatestRecords");
548 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS ck_mapbuttons");
549 SQL_AddQuery(hndl, "DROP TABLE IF EXISTS playerjumpstats3");
550
551 SQL_ExecuteTransaction(g_hDb, hndl, SQLTxn_RenameSuccess, SQLTxn_RenameFailed);
552}
553
554public void SQLTxn_RenameSuccess(Handle db, any data, int numQueries, Handle[] results, any[] queryData)
555{
556 g_bRenaming = false;
557 revertServerHibernateSettings();
558 CPrintToChatAll("[CK] Database changes done succesfully, reloading the map...");
559 ForceChangeLevel(g_szMapName, "Database Renaming Done. Restarting Map.");
560}
561
562public void SQLTxn_RenameFailed(Handle db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
563{
564 g_bRenaming = false;
565 revertServerHibernateSettings();
566 SetFailState("[ckSurf] Database changes failed! (Renaming) Error: %s", error);
567}
568
569
570///////////////////////
571//// PLAYER TITLES ////
572///////////////////////
573
574public void db_checkPlayersTitles(int client)
575{
576 for (int i = 0; i < TITLE_COUNT; i++)
577 g_bAdminFlagTitlesTemp[client][i] = false;
578
579 char szQuery[512];
580 Format(szQuery, 512, sql_selectPlayerFlags, g_szAdminSelectedSteamID[client]);
581
582 switch (g_iAdminEditingType[client])
583 {
584 case 1:SQL_TQuery(g_hDb, SQL_checkPlayerFlagsCallback, szQuery, client, DBPrio_Low);
585 case 3:SQL_TQuery(g_hDb, SQL_checkPlayerFlagsCallback2, szQuery, client, DBPrio_Low);
586 }
587}
588
589public void SQL_checkPlayerFlagsCallback(Handle owner, Handle hndl, const char[] error, any data)
590{
591 if (hndl == null)
592 {
593 LogError("[ckSurf] SQL Error (SQL_checkPlayerFlagsCallback): %s ", error);
594 return;
595 }
596
597 Menu titleMenu = CreateMenu(Handler_TitleMenu);
598 char id[8], menuItem[152];
599
600 if (IsValidClient(g_iAdminSelectedClient[data]))
601 {
602 char szName[MAX_NAME_LENGTH];
603 GetClientName(g_iAdminSelectedClient[data], szName, sizeof(szName));
604 SetMenuTitle(titleMenu, "Select title to give to %s:", szName);
605 }
606 else
607 {
608 SetMenuTitle(titleMenu, "Select which title to give:");
609 }
610
611
612 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
613 {
614 g_bAdminSelectedHasFlag[data] = true;
615 for (int i = 0; i < TITLE_COUNT; i++)
616 {
617 if (!StrEqual(g_szflagTitle[i], ""))
618 {
619 Format(id, sizeof(id), "%i", i);
620 if (SQL_FetchInt(hndl, i) > 0)
621 {
622 g_bAdminFlagTitlesTemp[data][i] = true;
623 Format(menuItem, sizeof(menuItem), "[ON] %s", g_szflagTitle[i]);
624 AddMenuItem(titleMenu, id, menuItem);
625 }
626 else
627 {
628 Format(menuItem, sizeof(menuItem), "[OFF] %s", g_szflagTitle[i]);
629 AddMenuItem(titleMenu, id, menuItem);
630 }
631 }
632 }
633 }
634 else
635 {
636 g_bAdminSelectedHasFlag[data] = false;
637 for (int i = 0; i < TITLE_COUNT; i++)
638 {
639 if (!StrEqual(g_szflagTitle[i], ""))
640 {
641 Format(id, 8, "%i", i);
642 Format(menuItem, sizeof(menuItem), "[OFF] %s", g_szflagTitle[i]);
643 AddMenuItem(titleMenu, id, menuItem);
644 }
645 }
646 }
647
648 SetMenuExitButton(titleMenu, true);
649 DisplayMenu(titleMenu, data, MENU_TIME_FOREVER);
650}
651
652public void SQL_checkPlayerFlagsCallback2(Handle owner, Handle hndl, const char[] error, any data)
653{
654
655 if (hndl == null)
656 {
657 LogError("[ckSurf] SQL Error (SQL_checkPlayerFlagsCallback2): %s ", error);
658 return;
659 }
660
661 Menu titleMenu = CreateMenu(Handler_TitleMenu);
662 char id[2], menuItem[152];
663
664 if (IsValidClient(g_iAdminSelectedClient[data]))
665 {
666 char szName[MAX_NAME_LENGTH];
667 GetClientName(g_iAdminSelectedClient[data], szName, sizeof(szName));
668 SetMenuTitle(titleMenu, "Which title do you want to remove from %s:", szName);
669 }
670 else
671 {
672 SetMenuTitle(titleMenu, "Which title do you wan to remove? :");
673 }
674
675
676 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
677 {
678 g_bAdminSelectedHasFlag[data] = true;
679 for (int i = 0; i < TITLE_COUNT; i++)
680 {
681 if (!StrEqual(g_szflagTitle_Colored[i], ""))
682 {
683 Format(id, sizeof(id), "%i", i);
684 if (SQL_FetchInt(hndl, i) > 0)
685 {
686 g_bAdminFlagTitlesTemp[data][i] = true;
687 Format(menuItem, sizeof(menuItem), "[ON] %s", g_szflagTitle_Colored[i]);
688 AddMenuItem(titleMenu, id, menuItem);
689 }
690 }
691 }
692 }
693 else
694 {
695 AddMenuItem(titleMenu, "-1", "The chosen player doesn't have any titles.");
696 }
697
698 SetMenuExitButton(titleMenu, true);
699 DisplayMenu(titleMenu, data, MENU_TIME_FOREVER);
700}
701
702public void sql_disableTitleFromAllbyIndex(int index)
703{
704 // Do a transaction
705 Transaction h_disableUnusedTitles = SQL_CreateTransaction();
706 char query[248];
707 for (int i = index; i < TITLE_COUNT; i++)
708 {
709 switch (i)
710 {
711 case 0:
712 {
713 SQL_AddQuery(h_disableUnusedTitles, "UPDATE ck_playertitles SET vip = 0;");
714 SQL_AddQuery(h_disableUnusedTitles, "UPDATE ck_playertitles SET inuse = -1 WHERE inuse = 0;");
715 }
716 case 1:
717 {
718 SQL_AddQuery(h_disableUnusedTitles, "UPDATE ck_playertitles SET mapper = 0;");
719 SQL_AddQuery(h_disableUnusedTitles, "UPDATE ck_playertitles SET inuse = -1 WHERE inuse = 1;");
720 }
721 case 2:
722 {
723 SQL_AddQuery(h_disableUnusedTitles, "UPDATE ck_playertitles SET teacher = 0;");
724 SQL_AddQuery(h_disableUnusedTitles, "UPDATE ck_playertitles SET inuse = -1 WHERE inuse = 2;");
725 }
726 default:
727 {
728 if (i > 2 && i < TITLE_COUNT)
729 {
730 Format(query, sizeof(query), "UPDATE ck_playertitles SET custom%i = 0;", (i - 2));
731 SQL_AddQuery(h_disableUnusedTitles, query);
732 Format(query, sizeof(query), "UPDATE ck_playertitles SET inuse = -1 WHERE inuse = %i;", i);
733 SQL_AddQuery(h_disableUnusedTitles, query);
734 }
735 }
736 }
737 }
738 SQL_ExecuteTransaction(g_hDb, h_disableUnusedTitles, _, _);
739}
740
741
742public void db_viewPersonalFlags(int client, char SteamID[32])
743{
744 char szQuery[728];
745 Handle pack = CreateDataPack();
746 WritePackCell(pack, client);
747 WritePackString(pack, SteamID);
748 Format(szQuery, sizeof(szQuery), sql_selectPlayerFlags, SteamID);
749 SQL_TQuery(g_hDb, SQL_PersonalFlagCallback, szQuery, pack, DBPrio_Low);
750}
751
752public void SQL_PersonalFlagCallback(Handle owner, Handle hndl, const char[] error, any pack)
753{
754 ResetPack(pack);
755 int client = ReadPackCell(pack);
756 char szSteamID[32];
757 ReadPackString(pack, szSteamID, sizeof(szSteamID));
758 CloseHandle(pack);
759
760 if (hndl == null)
761 {
762 LogError("[ckSurf] SQL Error (SQL_PersonalFlagCallback): %s ", error);
763 if (!g_bSettingsLoaded[client])
764 db_viewCheckpoints(client, szSteamID, g_szMapName);
765 return;
766 }
767
768 for (int i = 0; i < TITLE_COUNT; i++)
769 g_bflagTitles[client][i] = false;
770
771 g_bHasTitle[client] = false;
772 bool hasTitleRow;
773
774 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
775 {
776 hasTitleRow = true;
777 for (int i = 0; i < TITLE_COUNT; i++)
778 {
779 if (SQL_FetchInt(hndl, i) > 0)
780 {
781 g_bHasTitle[client] = true;
782 g_bflagTitles[client][i] = true;
783 }
784 }
785 g_iTitleInUse[client] = SQL_FetchInt(hndl, 23);
786 }
787
788 if (IsValidClient(client) && g_bAutoVIPFlag)
789 {
790 if ((GetUserFlagBits(client) & g_AutoVIPFlag))
791 {
792 if (!g_bHasTitle[client])
793 db_updateAdminVIP(client, szSteamID, hasTitleRow);
794 else
795 if (!g_bflagTitles[client][0])
796 db_updateAdminVIP(client, szSteamID, hasTitleRow);
797 }
798 }
799
800 Array_Copy(g_bflagTitles[client], g_bflagTitles_orig[client], TITLE_COUNT);
801
802 if (!g_bSettingsLoaded[client])
803 db_viewCheckpoints(client, szSteamID, g_szMapName);
804
805}
806
807public void db_updateAdminVIP(int client, char SteamID[32], bool hasTitleRow)
808{
809 Handle pack = CreateDataPack();
810 WritePackCell(pack, client);
811 WritePackString(pack, SteamID);
812
813 char szQuery[420];
814 if (!hasTitleRow)
815 Format(szQuery, sizeof(szQuery), "INSERT INTO `ck_playertitles`(`steamid`, `vip`, `mapper`, `teacher`, `custom1`, `custom2`, `custom3`, `custom4`, `custom5`, `custom6`, `custom7`, `custom8`, `custom9`, `custom10`, `custom11`, `custom12`, `custom13`, `custom14`, `custom15`, `custom16`, `custom17`, `custom18`, `custom19`, `custom20`, `inuse`) VALUES ('%s',1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);", SteamID);
816 else
817 Format(szQuery, sizeof(szQuery), "UPDATE `ck_playertitles` SET `vip`= 1, `inuse`= 0 WHERE `steamid` = '%s'", SteamID);
818 SQL_TQuery(g_hDb, db_updateVIPAdminCallback, szQuery, pack, DBPrio_Low);
819}
820
821public void db_updateVIPAdminCallback(Handle owner, Handle hndl, const char[] error, any pack)
822{
823 if (hndl == null)
824 {
825 LogError("[ckSurf] SQL Error (db_updateVIPAdminCallback): %s ", error);
826 return;
827 }
828
829 ResetPack(pack);
830 int client = ReadPackCell(pack);
831 char szSteamID[32];
832 ReadPackString(pack, szSteamID, sizeof(szSteamID));
833 CloseHandle(pack);
834
835 db_checkChangesInTitle(client, szSteamID);
836}
837
838
839
840
841public void db_checkChangesInTitle(int client, char SteamID[32])
842{
843 char szQuery[728];
844 Format(szQuery, sizeof(szQuery), sql_selectPlayerFlags, SteamID);
845
846 Handle pack = CreateDataPack();
847 WritePackCell(pack, client);
848 WritePackString(pack, SteamID);
849
850 SQL_TQuery(g_hDb, db_checkChangesInTitleCallback, szQuery, pack, DBPrio_Low);
851}
852
853public void db_checkChangesInTitleCallback(Handle owner, Handle hndl, const char[] error, any data)
854{
855 if (hndl == null)
856 {
857 LogError("[ckSurf] SQL Error (db_checkChangesInTitleCallback): %s ", error);
858 return;
859 }
860
861
862 ResetPack(data);
863 int client = ReadPackCell(data);
864 char steamid[32];
865 ReadPackString(data, steamid, sizeof(steamid));
866 CloseHandle(data);
867
868 if (IsValidClient(client))
869 {
870 for (int i = 0; i < TITLE_COUNT; i++)
871 g_bflagTitles[client][i] = false;
872
873 g_bHasTitle[client] = false;
874
875 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
876 {
877 for (int i = 0; i < TITLE_COUNT; i++)
878 if (SQL_FetchInt(hndl, i) > 0)
879 {
880 g_bHasTitle[client] = true;
881 g_bflagTitles[client][i] = true;
882 }
883
884 //g_iTitleInUse[client] = SQL_FetchInt(hndl, 23);
885 }
886 }
887 else
888 {
889 db_updatePlayerTitleInUse(-1, steamid);
890 return;
891 }
892
893 for (int i = 0; i < TITLE_COUNT; i++)
894 {
895 if (g_bflagTitles[client][i] != g_bflagTitles_orig[client][i])
896 {
897 if (g_bflagTitles[client][i])
898 ClientCommand(client, "play commander\\commander_comment_0%i", GetRandomInt(1, 9));
899 else ClientCommand(client, "play commander\\commander_comment_%i", GetRandomInt(20, 23));
900 switch (i)
901 {
902 case 0:
903 {
904 g_bflagTitles_orig[client][i] = g_bflagTitles[client][i];
905 if (g_bflagTitles[client][i])
906 CPrintToChat(client, PREFIX_T, "Title_VIP_Gained", client);
907 else
908 {
909 if (g_iTitleInUse[client] == i)
910 {
911 g_iTitleInUse[client] = -1;
912 db_updatePlayerTitleInUse(-1, steamid);
913 }
914
915 g_bTrailOn[client] = false;
916 CPrintToChat(client, PREFIX_T, "Title_VIP_Lost", client);
917 }
918 break;
919 }
920 default:
921 {
922
923 g_bflagTitles_orig[client][i] = g_bflagTitles[client][i];
924 if (g_bflagTitles[client][i])
925 CPrintToChat(client, PREFIX_T, "Title_Custom_Gained", client, g_szflagTitle_Colored[i]);
926 else
927 {
928 if (g_iTitleInUse[client] == i)
929 {
930 g_iTitleInUse[client] = -1;
931 db_updatePlayerTitleInUse(-1, steamid);
932 }
933
934 CPrintToChat(client, PREFIX_T, "Title_Custom_Lost", client, g_szflagTitle_Colored[i]);
935 }
936 break;
937 }
938 }
939 }
940 }
941}
942
943public void db_insertPlayerTitles(int client, int titleID)
944{
945 char szQuery[1024];
946 Format(szQuery, sizeof(szQuery), sql_insertPlayerFlags, g_szAdminSelectedSteamID[client], BooltoInt(g_bAdminFlagTitlesTemp[client][0]), BooltoInt(g_bAdminFlagTitlesTemp[client][1]), BooltoInt(g_bAdminFlagTitlesTemp[client][2]), BooltoInt(g_bAdminFlagTitlesTemp[client][3]), BooltoInt(g_bAdminFlagTitlesTemp[client][4]), BooltoInt(g_bAdminFlagTitlesTemp[client][5]), BooltoInt(g_bAdminFlagTitlesTemp[client][6]), BooltoInt(g_bAdminFlagTitlesTemp[client][7]), BooltoInt(g_bAdminFlagTitlesTemp[client][8]), BooltoInt(g_bAdminFlagTitlesTemp[client][9]), BooltoInt(g_bAdminFlagTitlesTemp[client][10]), BooltoInt(g_bAdminFlagTitlesTemp[client][11]), BooltoInt(g_bAdminFlagTitlesTemp[client][12]), BooltoInt(g_bAdminFlagTitlesTemp[client][13]), BooltoInt(g_bAdminFlagTitlesTemp[client][14]), BooltoInt(g_bAdminFlagTitlesTemp[client][15]), BooltoInt(g_bAdminFlagTitlesTemp[client][16]), BooltoInt(g_bAdminFlagTitlesTemp[client][17]), BooltoInt(g_bAdminFlagTitlesTemp[client][18]), BooltoInt(g_bAdminFlagTitlesTemp[client][19]), BooltoInt(g_bAdminFlagTitlesTemp[client][20]), BooltoInt(g_bAdminFlagTitlesTemp[client][21]), BooltoInt(g_bAdminFlagTitlesTemp[client][22]), titleID);
947 SQL_TQuery(g_hDb, SQL_insertFlagCallback, szQuery, client, DBPrio_Low);
948}
949
950public void SQL_insertFlagCallback(Handle owner, Handle hndl, const char[] error, any data)
951{
952 if (hndl == null)
953 {
954 LogError("[ckSurf] SQL Error (SQL_insertFlagCallback): %s ", error);
955 return;
956 }
957
958 if (IsValidClient(data))
959 CPrintToChat(data, PREFIX_t, "granted_title_success");
960
961 db_checkChangesInTitle(g_iAdminSelectedClient[data], g_szAdminSelectedSteamID[data]);
962}
963
964public void db_updatePlayerTitles(int client, int titleID)
965{
966 char szQuery[1024];
967 Format(szQuery, 1024, sql_updatePlayerFlags, g_bAdminFlagTitlesTemp[client][0], g_bAdminFlagTitlesTemp[client][1], g_bAdminFlagTitlesTemp[client][2], g_bAdminFlagTitlesTemp[client][3], g_bAdminFlagTitlesTemp[client][4], g_bAdminFlagTitlesTemp[client][5], g_bAdminFlagTitlesTemp[client][6], g_bAdminFlagTitlesTemp[client][7], g_bAdminFlagTitlesTemp[client][8], g_bAdminFlagTitlesTemp[client][9], g_bAdminFlagTitlesTemp[client][10], g_bAdminFlagTitlesTemp[client][11], g_bAdminFlagTitlesTemp[client][12], g_bAdminFlagTitlesTemp[client][13], g_bAdminFlagTitlesTemp[client][14], g_bAdminFlagTitlesTemp[client][15], g_bAdminFlagTitlesTemp[client][16], g_bAdminFlagTitlesTemp[client][17], g_bAdminFlagTitlesTemp[client][18], g_bAdminFlagTitlesTemp[client][19], g_bAdminFlagTitlesTemp[client][20], g_bAdminFlagTitlesTemp[client][21], g_bAdminFlagTitlesTemp[client][22], titleID, g_szAdminSelectedSteamID[client]);
968 SQL_TQuery(g_hDb, SQL_updatePlayerFlagsCallback, szQuery, client, DBPrio_Low);
969}
970
971public void SQL_updatePlayerFlagsCallback(Handle owner, Handle hndl, const char[] error, any data)
972{
973 if (hndl == null)
974 {
975 LogError("[ckSurf] SQL Error (SQL_updatePlayerFlagsCallback): %s ", error);
976 return;
977 }
978
979 if (IsValidClient(data))
980 CPrintToChat(data, PREFIX_t, "update_titles_success");
981
982 if (g_iAdminSelectedClient[data] != -1)
983 db_checkChangesInTitle(g_iAdminSelectedClient[data], g_szAdminSelectedSteamID[data]);
984}
985
986public void db_updatePlayerTitleInUse(int inUse, char szSteamId[32])
987{
988 char szQuery[512];
989 Format(szQuery, 512, sql_updatePlayerFlagsInUse, inUse, szSteamId);
990 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, -1, DBPrio_Low);
991}
992
993public void db_deletePlayerTitles(int client)
994{
995 if (IsValidClient(g_iAdminSelectedClient[client]))
996 {
997 GetClientAuthId(g_iAdminSelectedClient[client], AuthId_Steam2, g_szAdminSelectedSteamID[client], MAX_NAME_LENGTH, true);
998 }
999 else if (StrEqual(g_szAdminSelectedSteamID[client], ""))
1000 return;
1001
1002 char szQuery[258];
1003 Format(szQuery, 258, sql_deletePlayerFlags, g_szAdminSelectedSteamID[client]);
1004 SQL_TQuery(g_hDb, SQL_deletePlayerTitlesCallback, szQuery, client, DBPrio_Low);
1005}
1006
1007public void SQL_deletePlayerTitlesCallback(Handle owner, Handle hndl, const char[] error, any data)
1008{
1009 if (hndl == null)
1010 {
1011 LogError("[ckSurf] SQL Error (SQL_deletePlayerTitlesCallback): %s ", error);
1012 return;
1013 }
1014
1015 CPrintToChat(data, PREFIX_t, "delete_titles_success");
1016 db_checkChangesInTitle(g_iAdminSelectedClient[data], g_szAdminSelectedSteamID[data]);
1017}
1018
1019/////////////////////////
1020//// SPAWN LOCATIONS ////
1021/////////////////////////
1022
1023public void db_deleteSpawnLocations(int zGrp)
1024{
1025 g_bGotSpawnLocation[zGrp] = false;
1026 char szQuery[128];
1027 Format(szQuery, sizeof(szQuery), sql_deleteSpawnLocations, g_szMapName, zGrp);
1028 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, 1, DBPrio_Low);
1029}
1030
1031public void db_updateSpawnLocations(float position[3], float angle[3], int zGrp)
1032{
1033 char szQuery[512];
1034 Format(szQuery, sizeof(szQuery), sql_updateSpawnLocations, position[0], position[1], position[2], angle[0], angle[1], angle[2], g_szMapName, zGrp);
1035 SQL_TQuery(g_hDb, db_editSpawnLocationsCallback, szQuery, zGrp, DBPrio_Low);
1036}
1037
1038public void db_insertSpawnLocations(float position[3], float angle[3], int zGrp)
1039{
1040 char szQuery[512];
1041 Format(szQuery, sizeof(szQuery), sql_insertSpawnLocations, g_szMapName, position[0], position[1], position[2], angle[0], angle[1], angle[2], zGrp);
1042 SQL_TQuery(g_hDb, db_editSpawnLocationsCallback, szQuery, zGrp, DBPrio_Low);
1043}
1044
1045public void db_editSpawnLocationsCallback(Handle owner, Handle hndl, const char[] error, any data)
1046{
1047 if (hndl == null)
1048 {
1049 LogError("[ckSurf] SQL Error (db_editSpawnLocationsCallback): %s ", error);
1050 return;
1051 }
1052 db_selectSpawnLocations();
1053}
1054
1055public void db_selectSpawnLocations()
1056{
1057 for (int i = 0; i < MAXZONEGROUPS; i++)
1058 g_bGotSpawnLocation[i] = false;
1059
1060 char szQuery[254];
1061 Format(szQuery, 254, sql_selectSpawnLocations, g_szMapName);
1062 SQL_TQuery(g_hDb, db_selectSpawnLocationsCallback, szQuery, 1, DBPrio_Low);
1063}
1064
1065public void db_selectSpawnLocationsCallback(Handle owner, Handle hndl, const char[] error, any data)
1066{
1067 if (hndl == null)
1068 {
1069 LogError("[ckSurf] SQL Error (db_selectSpawnLocationsCallback): %s ", error);
1070 if (!g_bServerDataLoaded)
1071 db_ClearLatestRecords();
1072 return;
1073 }
1074
1075 if (SQL_HasResultSet(hndl))
1076 {
1077 while (SQL_FetchRow(hndl))
1078 {
1079 g_bGotSpawnLocation[SQL_FetchInt(hndl, 7)] = true;
1080 g_fSpawnLocation[SQL_FetchInt(hndl, 7)][0] = SQL_FetchFloat(hndl, 1);
1081 g_fSpawnLocation[SQL_FetchInt(hndl, 7)][1] = SQL_FetchFloat(hndl, 2);
1082 g_fSpawnLocation[SQL_FetchInt(hndl, 7)][2] = SQL_FetchFloat(hndl, 3);
1083 g_fSpawnAngle[SQL_FetchInt(hndl, 7)][0] = SQL_FetchFloat(hndl, 4);
1084 g_fSpawnAngle[SQL_FetchInt(hndl, 7)][1] = SQL_FetchFloat(hndl, 5);
1085 g_fSpawnAngle[SQL_FetchInt(hndl, 7)][2] = SQL_FetchFloat(hndl, 6);
1086 }
1087 }
1088 if (!g_bServerDataLoaded)
1089 db_ClearLatestRecords();
1090 return;
1091}
1092
1093
1094
1095/////////////////////
1096//// PLAYER RANK ////
1097/////////////////////
1098
1099public void db_viewMapProRankCount()
1100{
1101 g_MapTimesCount = 0;
1102 char szQuery[512];
1103 Format(szQuery, 512, sql_selectPlayerProCount, g_szMapName);
1104 SQL_TQuery(g_hDb, sql_selectPlayerProCountCallback, szQuery, DBPrio_Low);
1105}
1106
1107public void sql_selectPlayerProCountCallback(Handle owner, Handle hndl, const char[] error, any data)
1108{
1109 if (hndl == null)
1110 {
1111 LogError("[ckSurf] SQL Error (sql_selectPlayerProCountCallback): %s", error);
1112 if (!g_bServerDataLoaded)
1113 db_viewFastestBonus();
1114 return;
1115 }
1116
1117 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1118 g_MapTimesCount = SQL_GetRowCount(hndl);
1119 else
1120 g_MapTimesCount = 0;
1121
1122 if (!g_bServerDataLoaded)
1123 db_viewFastestBonus();
1124
1125 return;
1126}
1127
1128//
1129// Get players rank in current map
1130//
1131public void db_viewMapRankPro(int client)
1132{
1133 char szQuery[512];
1134 if (!IsValidClient(client))
1135 return;
1136
1137 //"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;";
1138 Format(szQuery, 512, sql_selectPlayerRankProTime, g_szSteamID[client], g_szMapName, g_szMapName);
1139 SQL_TQuery(g_hDb, db_viewMapRankProCallback, szQuery, client, DBPrio_Low);
1140}
1141
1142public void db_viewMapRankProCallback(Handle owner, Handle hndl, const char[] error, any client)
1143{
1144 if (hndl == null)
1145 {
1146 LogError("[ckSurf] SQL Error (db_viewMapRankProCallback): %s ", error);
1147 }
1148
1149 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1150 {
1151 g_MapRank[client] = SQL_GetRowCount(hndl);
1152 }
1153 if (!g_bSettingsLoaded[client])
1154 db_viewPersonalBonusRecords(client, g_szSteamID[client]);
1155}
1156
1157//
1158// Players points have changed in game, make changes in database and recalculate points
1159//
1160public void db_updateStat(int client)
1161{
1162 char szQuery[512];
1163 //"UPDATE ck_playerrank SET finishedmaps ='%i', finishedmapspro='%i', multiplier ='%i' where steamid='%s'";
1164 Format(szQuery, 512, sql_updatePlayerRank, g_pr_finishedmaps[client], g_pr_finishedmaps[client], g_pr_multiplier[client], g_szSteamID[client]);
1165
1166 SQL_TQuery(g_hDb, SQL_UpdateStatCallback, szQuery, client, DBPrio_Low);
1167
1168}
1169
1170public void SQL_UpdateStatCallback(Handle owner, Handle hndl, const char[] error, any data)
1171{
1172 if (hndl == null)
1173 {
1174 LogError("[ckSurf] SQL Error (SQL_UpdateStatCallback): %s", error);
1175 return;
1176 }
1177
1178 // Calculating starts here:
1179 CalculatePlayerRank(data);
1180}
1181
1182public void RecalcPlayerRank(int client, char steamid[128])
1183{
1184 int i = 66;
1185 while (g_bProfileRecalc[i] == true)
1186 i++;
1187 if (!g_bProfileRecalc[i])
1188 {
1189 char szQuery[255];
1190 char szsteamid[128 * 2 + 1];
1191 SQL_EscapeString(g_hDb, steamid, szsteamid, sizeof(szsteamid));
1192 Format(g_pr_szSteamID[i], 32, "%s", steamid);
1193 Format(szQuery, sizeof(szQuery), sql_selectPlayerName, szsteamid);
1194 Handle pack = CreateDataPack();
1195 WritePackCell(pack, i);
1196 WritePackCell(pack, client);
1197 SQL_TQuery(g_hDb, sql_selectPlayerNameCallback, szQuery, pack);
1198 }
1199}
1200
1201//
1202// 1. Point calculating starts here
1203// There are two ways:
1204// - if client > MAXPLAYERS, his rank is being recalculated by an admin
1205// - else player has increased his rank = recalculate points
1206//
1207public void CalculatePlayerRank(int client)
1208{
1209
1210 char szQuery[255];
1211 char szSteamId[32];
1212 // Take old points into memory, so at the end you can show how much the points changed
1213 g_pr_oldpoints[client] = g_pr_points[client];
1214 // Initialize point calculatin
1215 g_pr_points[client] = 0;
1216
1217 getSteamIDFromClient(client, szSteamId, sizeof(szSteamId));
1218
1219 Format(szQuery, sizeof(szQuery), "SELECT multiplier FROM ck_playerrank WHERE steamid = '%s'", szSteamId);
1220 SQL_TQuery(g_hDb, sql_selectRankedPlayerCallback, szQuery, client, DBPrio_Low);
1221}
1222
1223//
1224// 2. Count points from improvements, or insert new player into the database
1225// Fetched values:
1226// multiplier
1227//
1228public void sql_selectRankedPlayerCallback(Handle owner, Handle hndl, const char[] error, any client)
1229{
1230 if (hndl == null)
1231 {
1232 LogError("[ckSurf] SQL Error (sql_selectRankedPlayerCallback): %s", error);
1233 return;
1234 }
1235
1236 char szSteamId[32];
1237
1238 getSteamIDFromClient(client, szSteamId, sizeof(szSteamId));
1239
1240 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1241 {
1242 if (IsValidClient(client))
1243 {
1244 g_pr_Calculating[client] = true;
1245 if (GetClientTime(client) < (GetEngineTime() - g_fMapStartTime))
1246 db_UpdateLastSeen(client); // Update last seen on server
1247 }
1248 // Multiplier = The amount of times a player has improved on his time
1249 g_pr_multiplier[client] = SQL_FetchInt(hndl, 0);
1250 if (g_pr_multiplier[client] < 0)
1251 g_pr_multiplier[client] *= -1;
1252 /**
1253 * The following printtoconsole are for debugging! remove them when done
1254 */
1255 if (IsValidClient(client))
1256 PrintToConsole(client, "-----\nBefore calculation:\ng_pr_multiplier: %d\ng_pr_points: %d\n------", g_pr_multiplier[client],g_pr_points[client]);
1257
1258 // Multiplier increases players points by the set amount in ck_ranking_extra_points_improvements
1259 g_pr_points[client] += GetConVarInt(g_hExtraPoints) * g_pr_multiplier[client];
1260
1261 if (IsValidClient(client))
1262 PrintToConsole(client, "-----\nafter calculation:\ng_hExtraPoints: %d\ng_pr_points: %d\n------", GetConVarInt(g_hExtraPoints),g_pr_points[client]);
1263 // Next up, challenge points
1264 char szQuery[512];
1265
1266 Format(szQuery, 512, "SELECT steamid, bet FROM ck_challenges WHERE steamid = '%s' OR steamid2 ='%s'", szSteamId, szSteamId);
1267 SQL_TQuery(g_hDb, sql_selectChallengesCallbackCalc, szQuery, client, DBPrio_Low);
1268 }
1269 else
1270 {
1271 // Players first time on server
1272 if (client <= MaxClients)
1273 {
1274 g_pr_Calculating[client] = false;
1275 g_pr_AllPlayers++;
1276
1277 // Insert player to database
1278 char szQuery[255];
1279 char szUName[MAX_NAME_LENGTH];
1280 char szName[MAX_NAME_LENGTH * 2 + 1];
1281
1282 GetClientName(client, szUName, sizeof(szUName));
1283 SQL_EscapeString(g_hDb, szUName, szName, sizeof(szName));
1284
1285 //"INSERT INTO ck_playerrank (steamid, name, country) VALUES('%s', '%s', '%s');";
1286 // No need to continue calculating, as the doesn't have any records.
1287 Format(szQuery, sizeof(szQuery), sql_insertPlayerRank, szSteamId, szName, g_szCountry[client]);
1288 SQL_TQuery(g_hDb, SQL_InsertPlayerCallBack, szQuery, client, DBPrio_Low);
1289
1290 g_pr_multiplier[client] = 0;
1291 g_pr_finishedmaps[client] = 0;
1292 g_pr_finishedmaps_perc[client] = 0.0;
1293 }
1294 }
1295}
1296
1297//
1298// 3. Counting points gained from challenges
1299// Fetched values:
1300// steamid, bet
1301public void sql_selectChallengesCallbackCalc(Handle owner, Handle hndl, const char[] error, any client)
1302{
1303 if (hndl == null)
1304 {
1305 LogError("[ckSurf] SQL Error (sql_selectChallengesCallbackCalc): %s", error);
1306 return;
1307 }
1308
1309 char szQuery[512];
1310 char szSteamId[32];
1311 char szSteamIdChallenge[32];
1312
1313 getSteamIDFromClient(client, szSteamId, sizeof(szSteamId));
1314
1315 int bet;
1316
1317 if (SQL_HasResultSet(hndl))
1318 {
1319 g_Challenge_WinRatio[client] = 0;
1320 g_Challenge_PointsRatio[client] = 0;
1321 while (SQL_FetchRow(hndl))
1322 {
1323 SQL_FetchString(hndl, 0, szSteamIdChallenge, sizeof(szSteamIdChallenge));
1324 bet = SQL_FetchInt(hndl, 1);
1325 if (StrEqual(szSteamIdChallenge, szSteamId)) // Won the challenge
1326 {
1327 //jesus christ. this is not a ratio. a ratio would be wins/losses -> always positive
1328 g_Challenge_WinRatio[client]++;
1329 g_Challenge_PointsRatio[client] += bet;
1330 }
1331 else // Lost the challenge
1332 {
1333 g_Challenge_WinRatio[client]--;
1334 g_Challenge_PointsRatio[client] -= bet;
1335 }
1336 }
1337 }
1338 if (GetConVarBool(g_hChallengePoints)) // If challenge points are enabled: add them to players points
1339 g_pr_points[client] += g_Challenge_PointsRatio[client];
1340
1341 // Next up, calculate bonus points:
1342 Format(szQuery, sizeof(szQuery), "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) AS rank, (SELECT count(1) FROM ck_bonus b WHERE a.mapname = b.mapname AND a.zonegroup = b.zonegroup) as total FROM ck_bonus a WHERE steamid = '%s';", szSteamId);
1343 SQL_TQuery(g_hDb, sql_CountFinishedBonusCallback, szQuery, client, DBPrio_Low);
1344}
1345
1346//
1347// 4. Calculate points gained from bonuses
1348// Fetched values
1349// mapname, rank, total
1350//
1351public void sql_CountFinishedBonusCallback(Handle owner, Handle hndl, const char[] error, any client)
1352{
1353 if (hndl == null)
1354 {
1355 LogError("[ckSurf] SQL Error (sql_CountFinishedBonusCallback): %s", error);
1356 return;
1357 }
1358
1359
1360 char szMap[128], szSteamId[32], szMapName2[128];
1361 int totalplayers, rank;
1362
1363 getSteamIDFromClient(client, szSteamId, sizeof(szSteamId));
1364
1365 if (SQL_HasResultSet(hndl))
1366 {
1367 while (SQL_FetchRow(hndl))
1368 {
1369 // Total amount of players who have finished the bonus
1370 totalplayers = SQL_FetchInt(hndl, 2);
1371 rank = SQL_FetchInt(hndl, 1);
1372 SQL_FetchString(hndl, 0, szMap, sizeof(szMap));
1373 for (int i = 0; i < GetArraySize(g_MapList); i++) // Check that the map is in the mapcycle
1374 {
1375 GetArrayString(g_MapList, i, szMapName2, sizeof(szMapName2));
1376 if (StrEqual(szMapName2, szMap, false))
1377 {
1378 float percentage = 1.0 + ((1.0 / float(totalplayers)) - (float(rank) / float(totalplayers)));
1379 g_pr_points[client] += RoundToCeil(200.0 * percentage);
1380 g_pr_points[client] += RoundToCeil(200.0/float(rank));
1381 break;
1382 }
1383 }
1384 }
1385 }
1386
1387 // Next up: Points from maps
1388 char szQuery[512];
1389 Format(szQuery, sizeof(szQuery), "SELECT mapname, (select count(1)+1 from ck_playertimes b where a.mapname=b.mapname and a.runtimepro > b.runtimepro) AS rank, (SELECT count(1) FROM ck_playertimes b WHERE a.mapname = b.mapname) as total FROM ck_playertimes a where steamid = '%s';", szSteamId);
1390 SQL_TQuery(g_hDb, sql_CountFinishedMapsCallback, szQuery, client, DBPrio_Low);
1391}
1392
1393//
1394// 5. Count the points gained from regular maps
1395// Fetching:
1396// mapname, rank, total
1397//
1398public void sql_CountFinishedMapsCallback(Handle owner, Handle hndl, const char[] error, any client)
1399{
1400 if (hndl == null)
1401 {
1402 LogError("[ckSurf] SQL Error (sql_CountFinishedMapsCallback): %s", error);
1403 return;
1404 }
1405
1406 char szMap[128], szMapName2[128];
1407 int finishedMaps = 0, totalplayers, rank;
1408
1409 if (SQL_HasResultSet(hndl))
1410 {
1411 while (SQL_FetchRow(hndl))
1412 {
1413 // Total amount of players who have finished the map
1414 totalplayers = SQL_FetchInt(hndl, 2);
1415 // Rank in that map
1416 rank = SQL_FetchInt(hndl, 1);
1417 // Map name
1418 SQL_FetchString(hndl, 0, szMap, 128);
1419
1420 for (int i = 0; i < GetArraySize(g_MapList); i++) // Check that the map is in the mapcycle
1421 {
1422 GetArrayString(g_MapList, i, szMapName2, sizeof(szMapName2));
1423 if (StrEqual(szMapName2, szMap, false))
1424 {
1425 finishedMaps++;
1426 float percentage = 1.0 + ((1.0 / float(totalplayers)) - (float(rank) / float(totalplayers)));
1427 g_pr_points[client] += RoundToCeil(200.0 * percentage);
1428 g_pr_points[client] += RoundToCeil(500.0/float(rank));
1429 break;
1430 }
1431 }
1432 }
1433 }
1434 // Finished maps amount is stored in memory
1435 g_pr_finishedmaps[client] = finishedMaps;
1436 // Percentage of maps finished
1437 g_pr_finishedmaps_perc[client] = (float(finishedMaps) / float(g_pr_MapCount)) * 100.0;
1438 // Points gained from finishing maps for the first time
1439 g_pr_points[client] += (finishedMaps * GetConVarInt(g_hExtraPoints2));
1440
1441 // Done checking, update points
1442 db_updatePoints(client);
1443
1444}
1445
1446//
1447// 6. Updating points to database
1448//
1449public void db_updatePoints(int client)
1450{
1451 char szQuery[512];
1452 char szName[MAX_NAME_LENGTH * 2 + 1];
1453 char szSteamId[32];
1454 if (client > MAXPLAYERS && g_pr_RankingRecalc_InProgress || client > MAXPLAYERS && g_bProfileRecalc[client])
1455 {
1456 SQL_EscapeString(g_hDb, g_pr_szName[client], szName, MAX_NAME_LENGTH * 2 + 1);
1457 Format(szQuery, 512, sql_updatePlayerRankPoints, szName, g_pr_points[client], g_pr_finishedmaps[client], g_Challenge_WinRatio[client], g_Challenge_PointsRatio[client], g_pr_szSteamID[client]);
1458 SQL_TQuery(g_hDb, sql_updatePlayerRankPointsCallback, szQuery, client, DBPrio_Low);
1459 }
1460 else
1461 {
1462 if (IsValidClient(client))
1463 {
1464 GetClientName(client, szName, MAX_NAME_LENGTH);
1465 GetClientAuthId(client, AuthId_Steam2, szSteamId, MAX_NAME_LENGTH, true);
1466 //GetClientAuthString(client, szSteamId, MAX_NAME_LENGTH);
1467 Format(szQuery, 512, sql_updatePlayerRankPoints2, szName, g_pr_points[client], g_pr_finishedmaps[client], g_Challenge_WinRatio[client], g_Challenge_PointsRatio[client], g_szCountry[client], szSteamId);
1468 SQL_TQuery(g_hDb, sql_updatePlayerRankPointsCallback, szQuery, client, DBPrio_Low);
1469 }
1470 }
1471}
1472
1473//
1474// 7. Calculations done, if calculating all, move forward, if not announce changes.
1475//
1476public void sql_updatePlayerRankPointsCallback(Handle owner, Handle hndl, const char[] error, any data)
1477{
1478 if (hndl == null)
1479 {
1480 LogError("[ckSurf] SQL Error (sql_updatePlayerRankPointsCallback): %s", error);
1481 return;
1482 }
1483
1484 // If was recalculating points, go to the next player, announce or end calculating
1485 if (data > MAXPLAYERS && g_pr_RankingRecalc_InProgress || data > MAXPLAYERS && g_bProfileRecalc[data])
1486 {
1487 if (g_bProfileRecalc[data] && !g_pr_RankingRecalc_InProgress)
1488 {
1489 for (int i = 1; i <= MaxClients; i++)
1490 {
1491 if (IsValidClient(i))
1492 {
1493 if (StrEqual(g_szSteamID[i], g_pr_szSteamID[data]))
1494 CalculatePlayerRank(i);
1495 }
1496 }
1497 }
1498 g_bProfileRecalc[data] = false;
1499 if (g_pr_RankingRecalc_InProgress)
1500 {
1501 //console info
1502 if (IsValidClient(g_pr_Recalc_AdminID) && g_bManualRecalc)
1503 PrintToConsole(g_pr_Recalc_AdminID, "%i/%i", g_pr_Recalc_ClientID, g_pr_TableRowCount);
1504 int x = 66 + g_pr_Recalc_ClientID;
1505 if (StrContains(g_pr_szSteamID[x], "STEAM", false) != -1)
1506 {
1507 ContinueRecalc(x);
1508 }
1509 else
1510 {
1511 for (int i = 1; i <= MaxClients; i++)
1512 if (1 <= i <= MaxClients && IsValidEntity(i) && IsValidClient(i))
1513 {
1514 if (g_bManualRecalc)
1515 CPrintToChat(i, PREFIX_t, "PrUpdateFinished");
1516 }
1517 g_bManualRecalc = false;
1518 g_pr_RankingRecalc_InProgress = false;
1519
1520 if (IsValidClient(g_pr_Recalc_AdminID))
1521 CreateTimer(0.1, RefreshAdminMenu, g_pr_Recalc_AdminID, TIMER_FLAG_NO_MAPCHANGE);
1522 }
1523 g_pr_Recalc_ClientID++;
1524 }
1525 }
1526 else // Gaining points normally
1527 {
1528 // Player recalculated own points in !profile
1529 if (g_bRecalcRankInProgess[data] && data <= MAXPLAYERS)
1530 {
1531 ProfileMenu(data, -1);
1532 if (IsValidClient(data))
1533 CPrintToChat(data, PREFIX_t, "Rc_PlayerRankFinished", g_pr_points[data]);
1534 g_bRecalcRankInProgess[data] = false;
1535 }
1536 if (IsValidClient(data) && g_pr_showmsg[data]) // Player gained points
1537 {
1538 char szName[MAX_NAME_LENGTH];
1539 GetClientName(data, szName, MAX_NAME_LENGTH);
1540 int earnedPoints = g_pr_points[data] - g_pr_oldpoints[data];
1541 if (earnedPoints > 0) // if player earned points -> Announce
1542 {
1543 for (int i = 1; i <= MaxClients; i++)
1544 if (IsValidClient(i))
1545 CPrintToChat(i, PREFIX_t, "EarnedPoints", szName, earnedPoints, g_pr_points[data]);
1546 }
1547 g_pr_showmsg[data] = false;
1548 db_CalculatePlayersCountGreater0();
1549 }
1550 g_pr_Calculating[data] = false;
1551 db_GetPlayerRank(data);
1552 CreateTimer(1.0, SetClanTag, data, TIMER_FLAG_NO_MAPCHANGE);
1553 }
1554}
1555
1556//
1557// Called when player joins server
1558//
1559public void db_viewPlayerPoints(int client)
1560{
1561 g_pr_multiplier[client] = 0;
1562 g_pr_finishedmaps[client] = 0;
1563 g_pr_finishedmaps_perc[client] = 0.0;
1564 g_pr_points[client] = 0;
1565 char szQuery[255];
1566 if (!IsValidClient(client))
1567 return;
1568
1569 //"SELECT steamid, name, points, finishedmapspro, multiplier, country, lastseen from ck_playerrank where steamid='%s'";
1570 Format(szQuery, sizeof(szQuery), sql_selectRankedPlayer, g_szSteamID[client]);
1571 SQL_TQuery(g_hDb, db_viewPlayerPointsCallback, szQuery, client, DBPrio_Low);
1572}
1573
1574public void db_viewPlayerPointsCallback(Handle owner, Handle hndl, const char[] error, any client)
1575{
1576 if (hndl == null)
1577 {
1578 LogError("[ckSurf] SQL Error (db_viewPlayerPointsCallback): %s", error);
1579 if (!g_bSettingsLoaded[client])
1580 db_viewPlayerOptions(client, g_szSteamID[client]);
1581 return;
1582 }
1583
1584 // Old player - get points
1585 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1586 {
1587 g_pr_points[client] = SQL_FetchInt(hndl, 2);
1588 g_pr_finishedmaps[client] = SQL_FetchInt(hndl, 3);
1589 g_pr_multiplier[client] = SQL_FetchInt(hndl, 4);
1590 if (g_pr_multiplier[client] < 0)
1591 g_pr_multiplier[client] *= -1;
1592 g_pr_finishedmaps_perc[client] = (float(g_pr_finishedmaps[client]) / float(g_pr_MapCount)) * 100.0;
1593 if (IsValidClient(client)) // Count players rank
1594 db_GetPlayerRank(client);
1595 }
1596 else
1597 { // New player - insert
1598 if (IsValidClient(client))
1599 {
1600 //insert
1601 char szQuery[512];
1602 char szUName[MAX_NAME_LENGTH];
1603
1604 if (IsValidClient(client))
1605 GetClientName(client, szUName, sizeof(szUName));
1606 else
1607 return;
1608
1609 // SQL injection protection
1610 char szName[MAX_NAME_LENGTH * 2 + 1];
1611 SQL_EscapeString(g_hDb, szUName, szName, sizeof(szName));
1612
1613 Format(szQuery, 512, sql_insertPlayerRank, g_szSteamID[client], szName, g_szCountry[client]);
1614 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
1615 db_GetPlayerRank(client); // Count players rank
1616 }
1617 }
1618}
1619
1620//
1621// Get the amount of palyers, who have more points
1622//
1623public void db_GetPlayerRank(int client)
1624{
1625 char szQuery[512];
1626 //"SELECT name FROM ck_playerrank WHERE points >= (SELECT points FROM ck_playerrank WHERE steamid = '%s') ORDER BY points";
1627 Format(szQuery, sizeof(szQuery), sql_selectRankedPlayersRank, g_szSteamID[client]);
1628 SQL_TQuery(g_hDb, sql_selectRankedPlayersRankCallback, szQuery, client, DBPrio_Low);
1629}
1630
1631public void sql_selectRankedPlayersRankCallback(Handle owner, Handle hndl, const char[] error, any client)
1632{
1633 if (hndl == null)
1634 {
1635 LogError("[ckSurf] SQL Error (sql_selectRankedPlayersRankCallback): %s", error);
1636 if (!g_bSettingsLoaded[client])
1637 db_viewPlayerOptions(client, g_szSteamID[client]);
1638 return;
1639 }
1640
1641 if (!IsValidClient(client))
1642 return;
1643
1644 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1645 {
1646 g_PlayerRank[client] = SQL_GetRowCount(hndl);
1647 // Sort players by rank in scoreboard
1648 if (g_pr_AllPlayers < g_PlayerRank[client])
1649 CS_SetClientContributionScore(client, 0);
1650 else
1651 CS_SetClientContributionScore(client, (g_pr_AllPlayers - SQL_GetRowCount(hndl)));
1652 }
1653
1654 if (!g_bSettingsLoaded[client])
1655 db_viewPlayerOptions(client, g_szSteamID[client]);
1656}
1657
1658public void db_resetPlayerRecords(int client, char steamid[128])
1659{
1660 char szQuery[255];
1661 char szsteamid[128 * 2 + 1];
1662 SQL_EscapeString(g_hDb, steamid, szsteamid, sizeof(szsteamid));
1663 Format(szQuery, sizeof(szQuery), sql_resetRecords, szsteamid);
1664 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery);
1665 PrintToConsole(client, "map times of %s cleared.", szsteamid);
1666 Handle pack = CreateDataPack();
1667 WritePackCell(pack, client);
1668 WritePackString(pack, steamid);
1669 SQL_TQuery(g_hDb, SQL_CheckCallback3, "UPDATE ck_playerrank SET multiplier ='0'", pack);
1670 for (int i = 1; i <= MaxClients; i++)
1671 {
1672 if (IsValidClient(i))
1673 {
1674 if (StrEqual(g_szSteamID[i], szsteamid))
1675 {
1676 Format(g_szPersonalRecord[i], 64, "NONE");
1677 g_fPersonalRecord[i] = 0.0;
1678 g_MapRank[i] = 99999;
1679 }
1680 }
1681 }
1682}
1683
1684public void db_dropPlayerRanks(int client)
1685{
1686 SQL_LockDatabase(g_hDb);
1687 if (g_DbType == MYSQL)
1688 SQL_FastQuery(g_hDb, sql_dropPlayerRank);
1689 else
1690 SQL_FastQuery(g_hDb, sqlite_dropPlayerRank);
1691
1692 SQL_FastQuery(g_hDb, sql_createPlayerRank);
1693 SQL_UnlockDatabase(g_hDb);
1694 PrintToConsole(client, "playerranks table dropped. Please restart your server!");
1695}
1696
1697public void db_dropPlayer(int client)
1698{
1699 SQL_TQuery(g_hDb, sql_selectMutliplierCallback, "UPDATE ck_playerrank SET multiplier ='0'", client);
1700 SQL_LockDatabase(g_hDb);
1701 if (g_DbType == MYSQL)
1702 SQL_FastQuery(g_hDb, sql_dropPlayer);
1703 else
1704 SQL_FastQuery(g_hDb, sqlite_dropPlayer);
1705 SQL_FastQuery(g_hDb, sql_createPlayertimes);
1706 SQL_FastQuery(g_hDb, sql_createPlayertimesIndex);
1707 SQL_UnlockDatabase(g_hDb);
1708 PrintToConsole(client, "playertimes table dropped. Please restart your server!");
1709}
1710
1711public void db_viewPlayerRank(int client, char szSteamId[32])
1712{
1713 char szQuery[512];
1714 Format(g_pr_szrank[client], 512, "");
1715 Format(szQuery, 512, sql_selectRankedPlayer, szSteamId);
1716 SQL_TQuery(g_hDb, SQL_ViewRankedPlayerCallback, szQuery, client, DBPrio_Low);
1717}
1718
1719public void SQL_ViewRankedPlayerCallback(Handle owner, Handle hndl, const char[] error, any data)
1720{
1721 if (hndl == null)
1722 {
1723 LogError("[ckSurf] SQL Error (SQL_ViewRankedPlayerCallback): %s", error);
1724 return;
1725 }
1726
1727 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1728 {
1729 char szQuery[512];
1730 char szName[MAX_NAME_LENGTH];
1731 char szCountry[100];
1732 char szLastSeen[100];
1733 char szSteamId[32];
1734 int finishedmapspro;
1735 int points;
1736 g_MapRecordCount[data] = 0;
1737
1738 //get the result
1739 SQL_FetchString(hndl, 0, szSteamId, 32);
1740 SQL_FetchString(hndl, 1, szName, MAX_NAME_LENGTH);
1741 points = SQL_FetchInt(hndl, 2);
1742 finishedmapspro = SQL_FetchInt(hndl, 3);
1743 SQL_FetchString(hndl, 5, szCountry, 100);
1744 SQL_FetchString(hndl, 6, szLastSeen, 100);
1745 Handle pack_pr = CreateDataPack();
1746 WritePackString(pack_pr, szName);
1747 WritePackString(pack_pr, szSteamId);
1748 WritePackCell(pack_pr, data);
1749 WritePackCell(pack_pr, points);
1750 WritePackCell(pack_pr, finishedmapspro);
1751 WritePackString(pack_pr, szCountry);
1752 WritePackString(pack_pr, szLastSeen);
1753 Format(szQuery, 512, sql_selectRankedPlayersRank, szSteamId);
1754 SQL_TQuery(g_hDb, SQL_ViewRankedPlayerCallback2, szQuery, pack_pr, DBPrio_Low);
1755 }
1756}
1757
1758
1759
1760
1761public void SQL_ViewRankedPlayerCallback2(Handle owner, Handle hndl, const char[] error, any data)
1762{
1763 if (hndl == null)
1764 {
1765 LogError("[ckSurf] SQL Error (SQL_ViewRankedPlayerCallback2): %s", error);
1766 return;
1767 }
1768
1769 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1770 {
1771 char szQuery[512];
1772 char szSteamId[32];
1773 char szName[MAX_NAME_LENGTH];
1774 int rank = SQL_GetRowCount(hndl);
1775
1776 WritePackCell(data, rank);
1777 ResetPack(data);
1778 ReadPackString(data, szName, MAX_NAME_LENGTH);
1779 ReadPackString(data, szSteamId, 32);
1780 Format(szQuery, 512, sql_selectMapRecordCount, szSteamId);
1781 SQL_TQuery(g_hDb, SQL_ViewRankedPlayerCallback4, szQuery, data, DBPrio_Low);
1782 }
1783}
1784
1785public void SQL_ViewRankedPlayerCallback4(Handle owner, Handle hndl, const char[] error, any data)
1786{
1787 if (hndl == null)
1788 {
1789 LogError("[ckSurf] SQL Error (SQL_ViewRankedPlayerCallback4): %s", error);
1790 return;
1791 }
1792
1793 char szQuery[512];
1794 char szSteamId[32];
1795 char szName[MAX_NAME_LENGTH];
1796
1797 ResetPack(data);
1798 ReadPackString(data, szName, MAX_NAME_LENGTH);
1799 ReadPackString(data, szSteamId, 32);
1800 int client = ReadPackCell(data);
1801 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1802 g_MapRecordCount[client] = SQL_FetchInt(hndl, 1); //pack full?
1803 Format(szQuery, 512, sql_selectChallenges, szSteamId, szSteamId);
1804 SQL_TQuery(g_hDb, SQL_ViewRankedPlayerCallback5, szQuery, data, DBPrio_Low);
1805}
1806
1807public void SQL_ViewRankedPlayerCallback5(Handle owner, Handle hndl, const char[] error, any data)
1808{
1809 if (hndl == null)
1810 {
1811 LogError("[ckSurf] SQL Error (SQL_ViewRankedPlayerCallback5): %s", error);
1812 return;
1813 }
1814
1815 char szChallengesPoints[32];
1816 Format(szChallengesPoints, 32, "0p");
1817 char szChallengesWinRatio[32];
1818 Format(szChallengesWinRatio, 32, "0");
1819
1820 char szName[MAX_NAME_LENGTH];
1821 char szSteamId[32];
1822 char szSteamIdChallenge[32];
1823 char szCountry[100];
1824 char szLastSeen[100];
1825 char szNextRank[32];
1826 char szSkillGroup[32];
1827
1828 ResetPack(data);
1829 ReadPackString(data, szName, MAX_NAME_LENGTH);
1830 ReadPackString(data, szSteamId, 32);
1831 int client = ReadPackCell(data);
1832 int points = ReadPackCell(data);
1833 int finishedmapspro = ReadPackCell(data);
1834 ReadPackString(data, szCountry, 100);
1835 ReadPackString(data, szLastSeen, 100);
1836 if (StrEqual(szLastSeen, ""))
1837 Format(szLastSeen, 100, "Unknown");
1838 int rank = ReadPackCell(data);
1839 int prorecords = g_MapRecordCount[client];
1840 Format(g_szProfileSteamId[client], 32, "%s", szSteamId);
1841 Format(g_szProfileName[client], MAX_NAME_LENGTH, "%s", szName);
1842 bool master = false;
1843 int RankDifference;
1844 CloseHandle(data);
1845
1846 int bet;
1847
1848 if (StrEqual(szSteamId, g_szSteamID[client]))
1849 g_PlayerRank[client] = rank;
1850
1851 //get challenge results
1852 int challenges = 0;
1853 int challengeswon = 0;
1854 int challengespoints = 0;
1855 if (SQL_HasResultSet(hndl))
1856 {
1857 challenges = SQL_GetRowCount(hndl);
1858 while (SQL_FetchRow(hndl))
1859 {
1860 SQL_FetchString(hndl, 0, szSteamIdChallenge, 32);
1861 bet = SQL_FetchInt(hndl, 2);
1862 if (StrEqual(szSteamIdChallenge, szSteamId))
1863 {
1864 challengespoints += bet;
1865 challengeswon++;
1866 }
1867 else
1868 {
1869 challengespoints -= bet;
1870 challengeswon--;
1871 }
1872 }
1873 }
1874
1875 if (!GetConVarBool(g_hChallengePoints))
1876 challengespoints = 0;
1877
1878 if (challengespoints > 0)
1879 Format(szChallengesPoints, 32, "+%ip", challengespoints);
1880 else
1881 if (challengespoints <= 0 && GetConVarBool(g_hChallengePoints))
1882 Format(szChallengesPoints, 32, "%ip", challengespoints);
1883 else
1884 if (challengespoints <= 0 && !GetConVarBool(g_hChallengePoints))
1885 Format(szChallengesPoints, 32, "0p (disabled)");
1886
1887
1888 if (challengeswon > 0)
1889 Format(szChallengesWinRatio, 32, "+%i", challengeswon);
1890 else
1891 if (challengeswon < 0)
1892 Format(szChallengesWinRatio, 32, "%i", challengeswon);
1893
1894
1895 if (finishedmapspro > g_pr_MapCount)
1896 finishedmapspro = g_pr_MapCount;
1897
1898
1899 int index = GetSkillgroupFromPoints(points), RankValue[SkillGroup];
1900 GetArrayArray(g_hSkillGroups, index, RankValue[0]);
1901
1902 Format(szSkillGroup, 32, "%s", RankValue[RankName]);
1903
1904 if (index == (GetArraySize(g_hSkillGroups)-1))
1905 {
1906 RankDifference = 0;
1907 Format(szNextRank, 32, " ");
1908 master = true;
1909 }
1910 else
1911 {
1912 GetArrayArray(g_hSkillGroups, (index+1), RankValue[0]);
1913 RankDifference = RankValue[PointReq] - points;
1914 Format(szNextRank, 32, " (%s)", RankValue[RankName]);
1915 }
1916
1917 char szRank[32];
1918 if (rank > g_pr_RankedPlayers || points == 0)
1919 Format(szRank, 32, "-");
1920 else
1921 Format(szRank, 32, "%i", rank);
1922
1923 char szRanking[255];
1924 Format(szRanking, 255, "");
1925 if (master == false)
1926 {
1927 if (GetConVarBool(g_hPointSystem))
1928 Format(szRanking, 255, "Rank: %s/%i (%i)\nPoints: %ip (%s)\nNext skill group in: %ip%s\n", szRank, g_pr_RankedPlayers, g_pr_AllPlayers, points, szSkillGroup, RankDifference, szNextRank);
1929 Format(g_pr_szrank[client], 512, "Rank: %s/%i (%i)\nPoints: %ip (%s)\nNext skill group in: %ip%s\nMaps completed: %i/%i (records: %i)\nPlayed challenges: %i\n鈺榃/L Ratio: %s\n鈺榃/L Points ratio: %s\n ", szRank, g_pr_RankedPlayers, g_pr_AllPlayers, points, szSkillGroup, RankDifference, szNextRank, finishedmapspro, g_pr_MapCount, prorecords, challenges, szChallengesWinRatio, szChallengesPoints);
1930 }
1931 else
1932 {
1933 if (GetConVarBool(g_hPointSystem))
1934 Format(szRanking, 255, "Rank: %s/%i (%i)\nPoints: %ip (%s)\n", szRank, g_pr_RankedPlayers, g_pr_AllPlayers, points, szSkillGroup);
1935 Format(g_pr_szrank[client], 512, "Rank: %s/%i (%i)\nPoints: %ip (%s)\nMaps completed: %i/%i (records: %i)\nPlayed challenges: %i\n鈺?W/L Ratio: %s\n鈺?W/L points ratio: %s\n ", szRank, g_pr_RankedPlayers, g_pr_AllPlayers, points, szSkillGroup, finishedmapspro, g_pr_MapCount, prorecords, challenges, szChallengesWinRatio, szChallengesPoints);
1936
1937 }
1938 char szID[32][2];
1939 ExplodeString(szSteamId, "_", szID, 2, 32);
1940 char szTitle[1024];
1941 if (GetConVarBool(g_hCountry))
1942 Format(szTitle, sizeof(szTitle), "Player: %s\nSteamID: %s\nNationality: %s \nLast seen: %s\n \n%s\n", szName, szID[1], szCountry, szLastSeen, g_pr_szrank[client]);
1943 else
1944 Format(szTitle, sizeof(szTitle), "Player: %s\nSteamID: %s\nLast seen: %s\n \n%s\n", szName, szID[1], szLastSeen, g_pr_szrank[client]);
1945
1946 Menu profileMenu = new Menu(ProfileMenuHandler);
1947 profileMenu.SetTitle(szTitle);
1948
1949 char sBuffer[128];
1950
1951 Format(sBuffer, sizeof(sBuffer), "%T", "current_map_time", client);
1952 profileMenu.AddItem("0", sBuffer);
1953
1954 Format(sBuffer, sizeof(sBuffer), "%T", "challenge_history", client);
1955 profileMenu.AddItem("1", sBuffer);
1956
1957 Format(sBuffer, sizeof(sBuffer), "%T", "finished_maps", client);
1958 profileMenu.AddItem("2", sBuffer);
1959
1960 if (IsValidClient(client))
1961 {
1962 if (StrEqual(szSteamId, g_szSteamID[client]))
1963 {
1964 Format(sBuffer, sizeof(sBuffer), "%T", "unfinished_maps", client);
1965 profileMenu.AddItem("3", sBuffer);
1966 if (GetConVarBool(g_hPointSystem))
1967 {
1968 Format(sBuffer, sizeof(sBuffer), "%T", "refresh_my_profile", client);
1969 profileMenu.AddItem("4", sBuffer);
1970 }
1971 }
1972 }
1973 profileMenu.ExitButton = true;
1974 profileMenu.Display(client, MENU_TIME_FOREVER);
1975}
1976
1977public void db_viewPlayerRank2(int client, char szSteamId[32])
1978{
1979 char szQuery[512];
1980 Format(g_pr_szrank[client], 512, "");
1981 Format(szQuery, 512, sql_selectRankedPlayer, szSteamId);
1982 SQL_TQuery(g_hDb, SQL_ViewRankedPlayer2Callback, szQuery, client, DBPrio_Low);
1983}
1984
1985public void SQL_ViewRankedPlayer2Callback(Handle owner, Handle hndl, const char[] error, any data)
1986{
1987 if (hndl == null)
1988 {
1989 LogError("[ckSurf] SQL Error (SQL_ViewRankedPlayer2Callback): %s", error);
1990 return;
1991 }
1992
1993 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
1994 {
1995 if (!IsValidClient(data))
1996 return;
1997
1998 char szQuery[512];
1999 char szName[MAX_NAME_LENGTH];
2000 char szSteamIdTarget[32];
2001 SQL_FetchString(hndl, 0, szSteamIdTarget, 32);
2002 SQL_FetchString(hndl, 1, szName, MAX_NAME_LENGTH);
2003
2004 Handle pack = CreateDataPack();
2005 WritePackCell(pack, data);
2006 WritePackString(pack, szName);
2007 Format(szQuery, 512, sql_selectChallengesCompare, g_szSteamID[data], szSteamIdTarget, szSteamIdTarget, g_szSteamID[data]);
2008 SQL_TQuery(g_hDb, sql_selectChallengesCompareCallback, szQuery, pack, DBPrio_Low);
2009 }
2010}
2011
2012public void db_viewPlayerAll2(int client, char szPlayerName[MAX_NAME_LENGTH])
2013{
2014 char szQuery[512];
2015 char szName[MAX_NAME_LENGTH * 2 + 1];
2016 SQL_EscapeString(g_hDb, szPlayerName, szName, MAX_NAME_LENGTH * 2 + 1);
2017 Format(szQuery, 512, sql_selectPlayerRankAll, PERCENT, szName, PERCENT);
2018 Handle pack = CreateDataPack();
2019 WritePackCell(pack, client);
2020 WritePackString(pack, szPlayerName);
2021 SQL_TQuery(g_hDb, SQL_ViewPlayerAll2Callback, szQuery, pack, DBPrio_Low);
2022}
2023
2024public void SQL_ViewPlayerAll2Callback(Handle owner, Handle hndl, const char[] error, any data)
2025{
2026
2027 if (hndl == null)
2028 {
2029 LogError("[ckSurf] SQL Error (SQL_ViewPlayerAll2Callback): %s", error);
2030 return;
2031 }
2032
2033 char szName[MAX_NAME_LENGTH];
2034 char szSteamId2[32];
2035
2036 ResetPack(data);
2037 int client = ReadPackCell(data);
2038 ReadPackString(data, szName, MAX_NAME_LENGTH);
2039 if (!IsValidClient(client))
2040 {
2041 CloseHandle(data);
2042 return;
2043 }
2044 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
2045 {
2046 SQL_FetchString(hndl, 1, szSteamId2, 32);
2047 if (!StrEqual(szSteamId2, g_szSteamID[client]))
2048 db_viewPlayerRank2(client, szSteamId2);
2049 }
2050 else CPrintToChat(client, PREFIX_T, client, "PlayerNotFound", szName);
2051 CloseHandle(data);
2052}
2053
2054
2055
2056public void db_viewPlayerAll(int client, char szPlayerName[MAX_NAME_LENGTH])
2057{
2058 char szQuery[512];
2059 char szName[MAX_NAME_LENGTH * 2 + 1];
2060 SQL_EscapeString(g_hDb, szPlayerName, szName, MAX_NAME_LENGTH * 2 + 1);
2061 Format(szQuery, 512, sql_selectPlayerRankAll, PERCENT, szName, PERCENT);
2062 SQL_TQuery(g_hDb, SQL_ViewPlayerAllCallback, szQuery, client, DBPrio_Low);
2063}
2064
2065
2066public void SQL_ViewPlayerAllCallback(Handle owner, Handle hndl, const char[] error, any data)
2067{
2068 if (hndl == null)
2069 {
2070 LogError("[ckSurf] SQL Error (SQL_ViewPlayerAllCallback): %s", error);
2071 return;
2072 }
2073
2074 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
2075 {
2076 SQL_FetchString(hndl, 1, g_szProfileSteamId[data], 32);
2077 db_viewPlayerRank(data, g_szProfileSteamId[data]);
2078 }
2079 else
2080 if (IsClientInGame(data))
2081 CPrintToChat(data, PREFIX_t, "PlayerNotFound", g_szProfileName[data]);
2082}
2083
2084public void ContinueRecalc(int client)
2085{
2086 //ON RECALC ALL
2087 if (client > MAXPLAYERS)
2088 CalculatePlayerRank(client);
2089 else
2090 {
2091 //ON CONNECT
2092 if (!IsValidClient(client) || IsFakeClient(client))
2093 return;
2094 float diff = GetGameTime() - g_fMapStartTime + 1.5;
2095 if (GetClientTime(client) < diff)
2096 {
2097 CalculatePlayerRank(client);
2098 }
2099 else
2100 {
2101 db_viewPlayerPoints(client);
2102 }
2103 }
2104}
2105
2106///////////////////////////
2107//// CHALLENGES ///////////
2108///////////////////////////
2109
2110public void db_selectTopChallengers(int client)
2111{
2112 char szQuery[128];
2113 Format(szQuery, 128, sql_selectTopChallengers);
2114 SQL_TQuery(g_hDb, sql_selectTopChallengersCallback, szQuery, client, DBPrio_Low);
2115}
2116
2117public void sql_selectTopChallengersCallback(Handle owner, Handle hndl, const char[] error, any data)
2118{
2119 if (hndl == null)
2120 {
2121 LogError("[ckSurf] SQL Error (sql_selectTopChallengersCallback): %s", error);
2122 return;
2123 }
2124 char szValue[128];
2125 char szName[MAX_NAME_LENGTH];
2126 char szWinRatio[32];
2127 char szSteamID[32];
2128 char szPointsRatio[32];
2129 char sBuffer[128];
2130 int winratio;
2131 int pointsratio;
2132 Menu topChallengersMenu = new Menu(TopChallengeHandler1);
2133 SetMenuPagination(topChallengersMenu, 5);
2134
2135 Format(sBuffer, sizeof(sBuffer), "%t", "menu_rank_no_colors_keep_spaces");
2136 topChallengersMenu.SetTitle(sBuffer);
2137 if (SQL_HasResultSet(hndl))
2138 {
2139 int i = 1;
2140 while (SQL_FetchRow(hndl))
2141 {
2142 SQL_FetchString(hndl, 0, szName, MAX_NAME_LENGTH);
2143 winratio = SQL_FetchInt(hndl, 1);
2144 if (!GetConVarBool(g_hChallengePoints))
2145 pointsratio = 0;
2146 else
2147 pointsratio = SQL_FetchInt(hndl, 2);
2148 SQL_FetchString(hndl, 3, szSteamID, 32);
2149 if (winratio >= 0)
2150 Format(szWinRatio, 32, "+%i", winratio);
2151 else
2152 Format(szWinRatio, 32, "%i", winratio);
2153
2154 if (pointsratio >= 0)
2155 Format(szPointsRatio, 32, "+%ip", pointsratio);
2156 else
2157 Format(szPointsRatio, 32, "%ip", pointsratio);
2158
2159
2160
2161
2162 if (pointsratio < 10)
2163 Format(szValue, 128, " %s 禄 %s (%s)", szPointsRatio, szName, szWinRatio);
2164 else
2165 if (pointsratio < 100)
2166 Format(szValue, 128, " %s 禄 %s (%s)", szPointsRatio, szName, szWinRatio);
2167 else
2168 if (pointsratio < 1000)
2169 Format(szValue, 128, " %s 禄 %s (%s)", szPointsRatio, szName, szWinRatio);
2170 else
2171 if (pointsratio < 10000)
2172 Format(szValue, 128, " %s 禄 %s (%s)", szPointsRatio, szName, szWinRatio);
2173 else
2174 Format(szValue, 128, " %s 禄 %s (%s)", szPointsRatio, szName, szWinRatio);
2175
2176 topChallengersMenu.AddItem(szSteamID, szValue, ITEMDRAW_DEFAULT);
2177 i++;
2178 }
2179 if (i == 1)
2180 {
2181 CPrintToChat(data, PREFIX_t, "NoPlayerTop");
2182 ckTopMenu(data);
2183 }
2184 else
2185 {
2186 SetMenuOptionFlags(topChallengersMenu, MENUFLAG_BUTTON_EXIT);
2187 topChallengersMenu.Display(data, MENU_TIME_FOREVER);
2188 }
2189 }
2190 else
2191 {
2192 CPrintToChat(data, PREFIX_t, "NoPlayerTop");
2193 ckTopMenu(data);
2194 }
2195}
2196
2197public void db_resetPlayerResetChallenges(int client, char steamid[128])
2198{
2199 char szQuery[255];
2200 char szsteamid[128 * 2 + 1];
2201 SQL_EscapeString(g_hDb, steamid, szsteamid, 128 * 2 + 1);
2202 Format(szQuery, 255, sql_deleteChallenges, szsteamid);
2203 Handle pack = CreateDataPack();
2204 WritePackCell(pack, client);
2205 WritePackString(pack, steamid);
2206 SQL_TQuery(g_hDb, SQL_CheckCallback4, szQuery, pack);
2207 PrintToConsole(client, "won challenges cleared (%s)", szsteamid);
2208}
2209
2210public void db_dropChallenges(int client)
2211{
2212 SQL_TQuery(g_hDb, SQL_CheckCallback, "UPDATE ck_playerrank SET winratio = '0',pointsratio = '0'", client);
2213 SQL_LockDatabase(g_hDb);
2214 if (g_DbType == MYSQL)
2215 SQL_FastQuery(g_hDb, sql_dropChallenges);
2216 else
2217 SQL_FastQuery(g_hDb, sqlite_dropChallenges);
2218 SQL_FastQuery(g_hDb, sql_createChallenges);
2219 SQL_UnlockDatabase(g_hDb);
2220 PrintToConsole(client, "challenge table dropped. Please restart your server!");
2221}
2222
2223public int TopChallengeHandler1(Handle menu, MenuAction action, int client, int item)
2224{
2225
2226 if (action == MenuAction_Select)
2227 {
2228 char info[32];
2229 GetMenuItem(menu, item, info, sizeof(info));
2230 g_MenuLevel[client] = 3;
2231 db_viewPlayerRank(client, info);
2232 }
2233
2234 if (action == MenuAction_Cancel)
2235 {
2236 ckTopMenu(client);
2237 }
2238 else if (action == MenuAction_End)
2239 {
2240 CloseHandle(menu);
2241 }
2242}
2243
2244public void TopTpHoldersHandler1(Handle menu, MenuAction action, int iClient, int iInfo)
2245{
2246
2247 if (action == MenuAction_Select)
2248 {
2249 char info[32];
2250 GetMenuItem(menu, iInfo, info, sizeof(info));
2251 g_MenuLevel[iClient] = 10;
2252 db_viewPlayerRank(iClient, info);
2253 }
2254
2255 if (action == MenuAction_Cancel)
2256 {
2257 ckTopMenu(iClient);
2258 }
2259 else if (action == MenuAction_End)
2260 {
2261 CloseHandle(menu);
2262 }
2263}
2264
2265public int TopProHoldersHandler1(Handle menu, MenuAction action, int client, int item)
2266{
2267
2268 if (action == MenuAction_Select)
2269 {
2270 char info[32];
2271 GetMenuItem(menu, item, info, sizeof(info));
2272 g_MenuLevel[client] = 11;
2273 db_viewPlayerRank(client, info);
2274 }
2275
2276 if (action == MenuAction_Cancel)
2277 {
2278 ckTopMenu(client);
2279 }
2280 else if (action == MenuAction_End)
2281 {
2282 CloseHandle(menu);
2283 }
2284}
2285
2286public void db_viewChallengeHistory(int client, char szSteamId[32])
2287{
2288 char szQuery[1024];
2289 Format(szQuery, 1024, sql_selectChallenges2, szSteamId, szSteamId);
2290 if ((StrContains(szSteamId, "STEAM_") != -1) && IsClientInGame(client))
2291 {
2292 Handle pack = CreateDataPack();
2293 WritePackString(pack, szSteamId);
2294 WritePackString(pack, g_szProfileName[client]);
2295 WritePackCell(pack, client);
2296 SQL_TQuery(g_hDb, sql_selectChallengesCallback, szQuery, pack, DBPrio_Low);
2297 }
2298 else if (IsClientInGame(client))
2299 CPrintToChat(client, PREFIX_T, client, "InvalidSteamID.");
2300 ProfileMenu(client, -1);
2301}
2302
2303public void sql_selectChallengesCallback(Handle owner, Handle hndl, const char[] error, any data)
2304{
2305 if (hndl == null)
2306 {
2307 LogError("[ckSurf] SQL Error (sql_selectChallengesCallback): %s", error);
2308 return;
2309 }
2310
2311 //decl.
2312 int bet, cp_allowed = 0, client;
2313 int bHeader = false;
2314 char szMapName[32];
2315 char szSteamId[32];
2316 char szSteamId2[32];
2317 char szSteamIdTarget[32];
2318 char szNameTarget[32];
2319 char szDate[64];
2320
2321 //get pack data
2322 ResetPack(data);
2323 ReadPackString(data, szSteamIdTarget, 32);
2324 ReadPackString(data, szNameTarget, 32);
2325 client = ReadPackCell(data);
2326 CloseHandle(data);
2327
2328 if (SQL_HasResultSet(hndl))
2329 {
2330 //fetch rows
2331 while (SQL_FetchRow(hndl))
2332 {
2333 //get row data
2334 SQL_FetchString(hndl, 0, szSteamId, 32);
2335 SQL_FetchString(hndl, 1, szSteamId2, 32);
2336 bet = SQL_FetchInt(hndl, 2);
2337 SQL_FetchString(hndl, 3, szMapName, 32);
2338 SQL_FetchString(hndl, 4, szDate, 64);
2339
2340 //header
2341 if (!bHeader)
2342 {
2343 PrintToConsole(client, " ");
2344 PrintToConsole(client, "-------------");
2345 PrintToConsole(client, "Challenge history");
2346 PrintToConsole(client, "Player: %s", szNameTarget);
2347 PrintToConsole(client, "SteamID: %s", szSteamIdTarget);
2348 PrintToConsole(client, "-------------");
2349 PrintToConsole(client, " ");
2350 bHeader = true;
2351 CPrintToChat(client, PREFIX_T, client, "CheckConsole_Output");
2352 }
2353
2354 //won/loss?
2355 int WinnerTarget = 0;
2356 if (StrEqual(szSteamId, szSteamIdTarget))
2357 WinnerTarget = 1;
2358
2359 //create pack
2360 Handle pack2 = CreateDataPack();
2361 WritePackCell(pack2, client);
2362 WritePackCell(pack2, WinnerTarget);
2363 WritePackString(pack2, szNameTarget);
2364 WritePackString(pack2, szSteamId);
2365 WritePackString(pack2, szSteamId2);
2366 WritePackString(pack2, szMapName);
2367 WritePackString(pack2, szDate);
2368 WritePackCell(pack2, bet);
2369 WritePackCell(pack2, cp_allowed);
2370
2371 //Query
2372 char szQuery[512];
2373 if (WinnerTarget == 1)
2374 Format(szQuery, 512, "select name from ck_playerrank where steamid = '%s'", szSteamId2);
2375 else Format(szQuery, 512, "select name from ck_playerrank where steamid = '%s'", szSteamId);
2376 SQL_TQuery(g_hDb, sql_selectChallengesCallback2, szQuery, pack2, DBPrio_Low);
2377 }
2378 }
2379 if (!bHeader)
2380 {
2381 ProfileMenu(client, -1);
2382 CPrintToChat(client, PREFIX_T, "no_challenges_found", client);
2383 }
2384}
2385
2386public void sql_selectChallengesCallback2(Handle owner, Handle hndl, const char[] error, any data)
2387{
2388 if (hndl == null)
2389 {
2390 LogError("[ckSurf] SQL Error (sql_selectChallengesCallback2): %s", error);
2391 return;
2392 }
2393
2394 //decl.
2395 char szNameTarget[32];
2396 char szNameOpponent[32];
2397 char szSteamId[32];
2398 char szCps[32];
2399 char szResult[32];
2400 char szSteamId2[32];
2401 char szMapName[32];
2402 char szDate[64];
2403 int client, bet, WinnerTarget, cp_allowed;
2404
2405 //get pack data
2406 ResetPack(data);
2407 client = ReadPackCell(data);
2408 WinnerTarget = ReadPackCell(data);
2409 ReadPackString(data, szNameTarget, 32);
2410 ReadPackString(data, szSteamId, 32);
2411 ReadPackString(data, szSteamId2, 32);
2412 ReadPackString(data, szMapName, 32);
2413 ReadPackString(data, szDate, 64);
2414 bet = ReadPackCell(data);
2415 cp_allowed = ReadPackCell(data);
2416 CloseHandle(data);
2417
2418 //default name=steamid
2419 if (WinnerTarget == 1)
2420 Format(szNameOpponent, 32, "%s", szSteamId2);
2421 else Format(szNameOpponent, 32, "%s", szSteamId);
2422
2423 //query result
2424 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
2425 SQL_FetchString(hndl, 0, szNameOpponent, 32);
2426
2427 //format..
2428 if (WinnerTarget == 1)
2429 Format(szResult, 32, "WIN");
2430 else Format(szResult, 32, "LOSS");
2431
2432 if (cp_allowed == 1)
2433 Format(szCps, 32, "yes");
2434 else Format(szCps, 32, "no");
2435
2436 //console msg
2437 if (IsClientInGame(client))
2438 PrintToConsole(client, "(%s) %s vs. %s, map: %s, bet: %i, result: %s", szDate, szNameTarget, szNameOpponent, szMapName, bet, szCps, szResult);
2439}
2440
2441public void sql_selectChallengesCompareCallback(Handle owner, Handle hndl, const char[] error, any data)
2442{
2443 if (hndl == null)
2444 {
2445 LogError("[ckSurf] SQL Error (sql_selectChallengesCompareCallback): %s", error);
2446 return;
2447 }
2448
2449 int winratio = 0;
2450 int challenges = SQL_GetRowCount(hndl);
2451 int pointratio = 0;
2452 char szWinRatio[32];
2453 char szPointsRatio[32];
2454 char szName[MAX_NAME_LENGTH];
2455
2456 ResetPack(data);
2457 int client = ReadPackCell(data);
2458 ReadPackString(data, szName, sizeof(szName));
2459 CloseHandle(data);
2460
2461 if (!IsValidClient(client))
2462 return;
2463
2464 if (SQL_HasResultSet(hndl))
2465 {
2466 while (SQL_FetchRow(hndl))
2467 {
2468 char szID[32];
2469 int bet;
2470 SQL_FetchString(hndl, 0, szID, sizeof(szID));
2471 bet = SQL_FetchInt(hndl, 2);
2472 if (StrEqual(szID, g_szSteamID[client]))
2473 {
2474 winratio++;
2475 pointratio += bet;
2476 }
2477 else
2478 {
2479 winratio--;
2480 pointratio -= bet;
2481 }
2482 }
2483 if (winratio > 0)
2484 Format(szWinRatio, sizeof(szWinRatio), "+%i", winratio);
2485 else Format(szWinRatio, sizeof(szWinRatio), "%i", winratio);
2486
2487 if (pointratio > 0)
2488 Format(szPointsRatio, sizeof(szPointsRatio), "+%ip", pointratio);
2489 else Format(szPointsRatio, sizeof(szPointsRatio), "%ip", pointratio);
2490
2491 if (winratio > 0)
2492 {
2493 if (pointratio > 0)
2494 CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2495 else if (pointratio < 0)
2496 CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2497 else CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2498 }
2499 else
2500 {
2501 if (winratio < 0)
2502 {
2503 if (pointratio > 0)
2504 CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2505 else if (pointratio < 0)
2506 CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2507 else CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2508
2509 }
2510 else
2511 {
2512 if (pointratio > 0)
2513 CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2514 else if (pointratio < 0)
2515 CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2516 else CPrintToChat(client, PREFIX_T, "challenges_played", client, challenges, szName, szWinRatio, szPointsRatio);
2517 }
2518 }
2519 }
2520 else CPrintToChat(client, PREFIX_t, "no_challenges_found", szName);
2521}
2522
2523public void db_insertPlayerChallenge(int client)
2524{
2525 if (!IsValidClient(client))
2526 return;
2527 char szQuery[255];
2528 int points;
2529 points = g_Challenge_Bet[client] * g_pr_PointUnit;
2530
2531 Format(szQuery, sizeof(szQuery), sql_insertChallenges, g_szSteamID[client], g_szChallenge_OpponentID[client], points, g_szMapName);
2532 SQL_TQuery(g_hDb, sql_insertChallengesCallback, szQuery, client, DBPrio_Low);
2533}
2534
2535public void sql_insertChallengesCallback(Handle owner, Handle hndl, const char[] error, any data)
2536{
2537 if (hndl == null)
2538 {
2539 LogError("[ckSurf] SQL Error (sql_insertChallengesCallback): %s", error);
2540 return;
2541 }
2542}
2543
2544///////////////////
2545// PLAYERTIMES ////
2546///////////////////
2547
2548public void db_resetPlayerMapRecord(int client, char steamid[128], char szMapName[128])
2549{
2550 char szQuery[255];
2551 char szQuery2[255];
2552 char szsteamid[128 * 2 + 1];
2553
2554 SQL_EscapeString(g_hDb, steamid, szsteamid, sizeof(szsteamid));
2555 Format(szQuery, sizeof(szQuery), sql_resetRecordPro, szsteamid, szMapName);
2556 Format(szQuery2, sizeof(szQuery2), sql_resetCheckpoints, szsteamid, szMapName);
2557
2558 Handle pack = CreateDataPack();
2559 WritePackCell(pack, client);
2560 WritePackString(pack, steamid);
2561
2562 SQL_TQuery(g_hDb, SQL_CheckCallback3, szQuery, pack);
2563 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery2, 1);
2564 PrintToConsole(client, "map time of %s on %s cleared.", steamid, szMapName);
2565
2566 if (StrEqual(szMapName, g_szMapName))
2567 {
2568 for (int i = 1; i <= MaxClients; i++)
2569 {
2570 if (IsValidClient(i))
2571 {
2572 if (StrEqual(g_szSteamID[i], szsteamid))
2573 {
2574 Format(g_szPersonalRecord[i], 64, "NONE");
2575 g_fPersonalRecord[i] = 0.0;
2576 g_MapRank[i] = 99999;
2577 }
2578 }
2579 }
2580 }
2581}
2582
2583public void db_resetPlayerRecords2(int client, char steamid[128], char szMapName[128])
2584{
2585 char szQuery[255];
2586 char szsteamid[128 * 2 + 1];
2587
2588 SQL_EscapeString(g_hDb, steamid, szsteamid, sizeof(szsteamid));
2589 Format(szQuery, sizeof(szQuery), sql_resetRecords2, szsteamid, szMapName);
2590 Handle pack = CreateDataPack();
2591 WritePackCell(pack, client);
2592 WritePackString(pack, steamid);
2593 SQL_TQuery(g_hDb, SQL_CheckCallback3, szQuery, pack);
2594 PrintToConsole(client, "map times of %s on %s cleared.", steamid, szMapName);
2595
2596 if (StrEqual(szMapName, g_szMapName))
2597 {
2598 for (int i = 1; i <= MaxClients; i++)
2599 {
2600 if (IsValidClient(i))
2601 {
2602 if (StrEqual(g_szSteamID[i], szsteamid))
2603 {
2604 Format(g_szPersonalRecord[i], sizeof(g_szPersonalRecord[]), "NONE");
2605 g_fPersonalRecord[i] = 0.0;
2606 g_MapRank[i] = 99999;
2607 }
2608 }
2609 }
2610 }
2611}
2612
2613public void db_GetMapRecord_Pro()
2614{
2615 g_fRecordMapTime = 9999999.0;
2616 char szQuery[512];
2617 // SELECT MIN(runtimepro), name, steamid FROM ck_playertimes WHERE mapname = '%s' AND runtimepro > -1.0
2618 Format(szQuery, 512, sql_selectMapRecord, g_szMapName, g_szMapName);
2619 SQL_TQuery(g_hDb, sql_selectMapRecordCallback, szQuery, DBPrio_Low);
2620}
2621
2622public void sql_selectMapRecordCallback(Handle owner, Handle hndl, const char[] error, any data)
2623{
2624 if (hndl == null)
2625 {
2626 LogError("[ckSurf] SQL Error (sql_selectMapRecordCallback): %s", error);
2627 if (!g_bServerDataLoaded)
2628 db_viewMapProRankCount();
2629 return;
2630 }
2631
2632 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
2633 {
2634 g_fRecordMapTime = SQL_FetchFloat(hndl, 0);
2635 if (g_fRecordMapTime > -1.0 && !SQL_IsFieldNull(hndl, 0))
2636 {
2637 g_fRecordMapTime = SQL_FetchFloat(hndl, 0);
2638 FormatTimeFloat(0, g_fRecordMapTime, 3, g_szRecordMapTime, 64);
2639 SQL_FetchString(hndl, 1, g_szRecordPlayer, MAX_NAME_LENGTH);
2640 SQL_FetchString(hndl, 2, g_szRecordMapSteamID, MAX_NAME_LENGTH);
2641 }
2642 else
2643 {
2644 Format(g_szRecordMapTime, 64, "N/A");
2645 g_fRecordMapTime = 9999999.0;
2646 }
2647 }
2648 else
2649 {
2650 Format(g_szRecordMapTime, 64, "N/A");
2651 g_fRecordMapTime = 9999999.0;
2652 }
2653 if (!g_bServerDataLoaded)
2654 db_viewMapProRankCount();
2655 return;
2656}
2657
2658
2659public void sql_selectProSurfersCallback(Handle owner, Handle hndl, const char[] error, any data)
2660{
2661 if (hndl == null)
2662 {
2663 LogError("[ckSurf] SQL Error (sql_selectProSurfersCallback): %s", error);
2664 return;
2665 }
2666
2667 char szValue[128];
2668 char szSteamID[32];
2669 char szName[64];
2670 char szTime[32];
2671 float time;
2672
2673 Menu topSurfersMenu = new Menu(MapMenuHandler3);
2674 topSurfersMenu.Pagination = 5;
2675 topSurfersMenu.SetTitle("Top 20 Map Times (local)\n Rank Time Player");
2676 if (SQL_HasResultSet(hndl))
2677
2678 {
2679 int i = 1;
2680 while (SQL_FetchRow(hndl))
2681 {
2682 SQL_FetchString(hndl, 0, szName, sizeof(szName));
2683 time = SQL_FetchFloat(hndl, 1);
2684 SQL_FetchString(hndl, 2, szSteamID, sizeof(szSteamID));
2685 FormatTimeFloat(data, time, 3, szTime, sizeof(szTime));
2686 if (time < 3600.0)
2687 Format(szTime, sizeof(szTime), " %s", szTime);
2688 if (i < 10)
2689 Format(szValue, sizeof(szValue), "[0%i.] %s 禄 %s", i, szTime, szName);
2690 else
2691 Format(szValue, sizeof(szValue), "[%i.] %s 禄 %s", i, szTime, szName);
2692 AddMenuItem(topSurfersMenu, szSteamID, szValue, ITEMDRAW_DEFAULT);
2693 i++;
2694 }
2695 if (i == 1)
2696 {
2697 CPrintToChat(data, PREFIX_t, "NoMapRecords", g_szMapName);
2698 }
2699 }
2700 topSurfersMenu.OptionFlags = MENUFLAG_BUTTON_EXIT;
2701 topSurfersMenu.Display(data, MENU_TIME_FOREVER);
2702}
2703
2704public void db_selectTopSurfers(int client, char mapname[128])
2705{
2706 char szQuery[1024];
2707 Format(szQuery, 1024, sql_selectTopSurfers, mapname);
2708 Handle pack = CreateDataPack();
2709 WritePackCell(pack, client);
2710 WritePackString(pack, mapname);
2711 SQL_TQuery(g_hDb, sql_selectTopSurfersCallback, szQuery, pack, DBPrio_Low);
2712}
2713
2714public void db_selectMapTopSurfers(int client, char mapname[128])
2715{
2716 char szQuery[1024];
2717
2718 char sMap[257];
2719 SQL_EscapeString(g_hDb, mapname, sMap, sizeof(sMap));
2720
2721 Handle pack = CreateDataPack();
2722 WritePackCell(pack, client);
2723 WritePackString(pack, sMap);
2724
2725 Format(szQuery, sizeof(szQuery), sql_selectTopSurfers2, sMap);
2726 SQL_TQuery(g_hDb, sql_selectTopSurfersCallback, szQuery, pack, DBPrio_Low);
2727}
2728
2729
2730//// BONUS //////////'
2731
2732public void db_selectBonusesInMap(int client, char mapname[128])
2733{
2734 // SELECT mapname, zonegroup, zonename FROM `ck_zones` WHERE mapname LIKE '%s%%' AND zonegroup > 0 GROUP BY zonegroup;
2735 char szQuery[1024];
2736 Format(szQuery, sizeof(szQuery), sql_selectBonusesInMap, mapname);
2737 SQL_TQuery(g_hDb, db_selectBonusesInMapCallback, szQuery, client, DBPrio_Low);
2738}
2739
2740public void db_selectBonusesInMapCallback(Handle owner, Handle hndl, const char[] error, any client)
2741{
2742 if (hndl == null)
2743 {
2744 LogError("[ckSurf] SQL Error (db_selectBonusesInMapCallback): %s", error);
2745 return;
2746 }
2747 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
2748 {
2749 char mapname[128], MenuTitle[248], BonusName[128], MenuID[248];
2750 int zGrp;
2751
2752 if (SQL_GetRowCount(hndl) == 1)
2753 {
2754 SQL_FetchString(hndl, 0, mapname, sizeof(mapname));
2755 db_selectBonusTopSurfers(client, mapname, SQL_FetchInt(hndl, 1), false);
2756 return;
2757 }
2758
2759 Menu listBonusesinMapMenu = new Menu(MenuHandler_SelectBonusinMap);
2760
2761 SQL_FetchString(hndl, 0, mapname, sizeof(mapname));
2762 zGrp = SQL_FetchInt(hndl, 1);
2763 Format(MenuTitle, sizeof(MenuTitle), "Choose a Bonus in %s", mapname);
2764 listBonusesinMapMenu.SetTitle(MenuTitle);
2765
2766 SQL_FetchString(hndl, 2, BonusName, sizeof(BonusName));
2767
2768 if (!BonusName[0])
2769 Format(BonusName, sizeof(BonusName), "BONUS %i", zGrp);
2770
2771 Format(MenuID, sizeof(MenuID), "%s-%i", mapname, zGrp);
2772
2773 listBonusesinMapMenu.AddItem(MenuID, BonusName);
2774
2775
2776 while (SQL_FetchRow(hndl))
2777 {
2778 SQL_FetchString(hndl, 2, BonusName, 128);
2779 zGrp = SQL_FetchInt(hndl, 1);
2780
2781 if (StrEqual(BonusName, "NULL", false))
2782 Format(BonusName, 128, "BONUS %i", zGrp);
2783
2784 Format(MenuID, 248, "%s-%i", mapname, zGrp);
2785
2786 listBonusesinMapMenu.AddItem(MenuID, BonusName);
2787 }
2788
2789 listBonusesinMapMenu.ExitButton = true;
2790 listBonusesinMapMenu.Display(client, 60);
2791 }
2792 else
2793 {
2794 CPrintToChat(client, PREFIX_T, "no_bonuses_found", client);
2795 return;
2796 }
2797}
2798
2799public int MenuHandler_SelectBonusinMap(Handle sMenu, MenuAction action, int client, int item)
2800{
2801 switch (action)
2802 {
2803 case MenuAction_Select:
2804 {
2805 char aID[248];
2806 char splits[2][128];
2807 GetMenuItem(sMenu, item, aID, sizeof(aID));
2808 ExplodeString(aID, "-", splits, sizeof(splits), sizeof(splits[]));
2809
2810 db_selectBonusTopSurfers(client, splits[0], StringToInt(splits[1]), false);
2811 }
2812 case MenuAction_End:
2813 {
2814 delete sMenu;
2815 }
2816 }
2817}
2818
2819
2820
2821public void db_selectBonusTopSurfers(int client, char mapname[128], int zGrp, bool search)
2822{
2823 char sMap[257];
2824 SQL_EscapeString(g_hDb, mapname, sMap, sizeof(sMap));
2825
2826 Handle pack = CreateDataPack();
2827 WritePackCell(pack, client);
2828 WritePackString(pack, sMap);
2829 WritePackCell(pack, zGrp);
2830
2831 char szQuery[1024];
2832
2833 if(search)
2834 Format(sMap, sizeof(sMap), "%s%%", sMap);
2835
2836 Format(szQuery, sizeof(szQuery), sql_selectTopBonusSurfers, sMap, zGrp);
2837 SQL_TQuery(g_hDb, sql_selectTopBonusSurfersCallback, szQuery, pack, DBPrio_Low);
2838}
2839
2840public void sql_selectTopBonusSurfersCallback(Handle owner, Handle hndl, const char[] error, any data)
2841{
2842 if (hndl == null)
2843 {
2844 LogError("[ckSurf] SQL Error (sql_selectTopBonusSurfersCallback): %s", error);
2845 return;
2846 }
2847
2848 ResetPack(data);
2849 int client = ReadPackCell(data);
2850 char szMap[128];
2851 ReadPackString(data, szMap, sizeof(szMap));
2852 int zGrp = ReadPackCell(data);
2853 CloseHandle(data);
2854
2855 char szFirstMap[128], szValue[128], szName[64], szSteamID[32], title[256];
2856 float time;
2857 bool bduplicat = false;
2858 Handle stringArray = CreateArray(100);
2859 Menu topMenu;
2860
2861 if (StrEqual(szMap, g_szMapName))
2862 topMenu = new Menu(MapMenuHandler1);
2863 else
2864 topMenu = new Menu(MapTopMenuHandler2);
2865
2866 topMenu.Pagination = 5;
2867
2868 if (SQL_HasResultSet(hndl))
2869 {
2870 int i = 1;
2871 while (SQL_FetchRow(hndl))
2872 {
2873 bduplicat = false;
2874 SQL_FetchString(hndl, 0, szSteamID, 32);
2875 SQL_FetchString(hndl, 1, szName, 64);
2876 time = SQL_FetchFloat(hndl, 2);
2877 SQL_FetchString(hndl, 4, szMap, sizeof(szMap));
2878 if (i == 1 || (i > 1 && StrEqual(szFirstMap, szMap)))
2879 {
2880 /*
2881 char lineBuf[256];
2882 for (int x = 0; x < GetArraySize(stringArray); x++)
2883 {
2884 GetArrayString(stringArray, x, lineBuf, sizeof(lineBuf));
2885 if (StrEqual(lineBuf, szName, false))
2886 bduplicat = true;
2887 }
2888 */
2889
2890 if (bduplicat == false && i < 51)
2891 {
2892 char szTime[32];
2893 FormatTimeFloat(client, time, 3, szTime, sizeof(szTime));
2894 if (time < 3600.0)
2895 Format(szTime, sizeof(szTime), " %s", szTime);
2896 if (i == 100)
2897 Format(szValue, sizeof(szValue), "[%i.] %s | 禄 %s", i, szTime, szName);
2898 if (i >= 10)
2899 Format(szValue, sizeof(szValue), "[%i.] %s | 禄 %s", i, szTime, szName);
2900 else Format(szValue, sizeof(szValue), "[0%i.] %s | 禄 %s", i, szTime, szName);
2901 topMenu.AddItem(szSteamID, szValue, ITEMDRAW_DEFAULT);
2902 PushArrayString(stringArray, szName);
2903 if (i == 1)
2904 Format(szFirstMap, sizeof(szFirstMap), "%s", szMap);
2905 i++;
2906 }
2907 }
2908 }
2909 if (i == 1)
2910 {
2911 CPrintToChat(client, PREFIX_T, "NoTopRecords", client, szMap);
2912 }
2913 }
2914 else CPrintToChat(client, PREFIX_T, "NoTopRecords", client, szMap);
2915 Format(title, sizeof(title), "Top 50 Times on %s (B %i) \n Rank Time Player", szFirstMap, zGrp);
2916 topMenu.SetTitle(title);
2917 topMenu.OptionFlags = MENUFLAG_BUTTON_EXIT;
2918 topMenu.Display(client, MENU_TIME_FOREVER);
2919 CloseHandle(stringArray);
2920}
2921
2922public void sql_selectTopSurfersCallback(Handle owner, Handle hndl, const char[] error, any data)
2923{
2924 if (hndl == null)
2925 {
2926 LogError("[ckSurf] SQL Error (sql_selectTopSurfersCallback): %s", error);
2927 return;
2928 }
2929
2930 DataPack pack = view_as<DataPack>(data);
2931
2932 ResetPack(pack);
2933 int client = pack.ReadCell();
2934 char szMap[128];
2935 pack.ReadString(szMap, sizeof(szMap));
2936 delete pack;
2937
2938 char szFirstMap[128];
2939 char szValue[128];
2940 char szName[64];
2941 float time;
2942 char szSteamID[32];
2943 Handle menu;
2944
2945 if (StrEqual(szMap, g_szMapName))
2946 menu = CreateMenu(MapMenuHandler1);
2947 else menu = CreateMenu(MapTopMenuHandler2);
2948
2949 SetMenuPagination(menu, 5);
2950 bool bduplicat = false;
2951 char title[256];
2952
2953 if (!SQL_HasResultSet(hndl))
2954 {
2955 CPrintToChat(client, PREFIX_T, "NoTopRecords", client, szMap);
2956 return;
2957 }
2958
2959 Handle stringArray = CreateArray(256);
2960
2961 int iCount = 1;
2962 while (SQL_FetchRow(hndl))
2963 {
2964 bduplicat = false;
2965 SQL_FetchString(hndl, 0, szSteamID, 32);
2966 SQL_FetchString(hndl, 1, szName, 64);
2967 time = SQL_FetchFloat(hndl, 2);
2968 SQL_FetchString(hndl, 4, szMap, 128);
2969
2970 if (iCount == 1 || (iCount > 1 && StrEqual(szFirstMap, szMap)))
2971 {
2972 /*
2973 char lineBuf[256];
2974 for (int x = 0; x < GetArraySize(stringArray); x++)
2975 {
2976 GetArrayString(stringArray, x, lineBuf, sizeof(lineBuf));
2977 if (StrEqual(lineBuf, szName, false))
2978 bduplicat = true;
2979 }
2980 */
2981
2982 if (bduplicat == false && iCount < 51)
2983 {
2984 char szTime[32];
2985 FormatTimeFloat(client, time, 3, szTime, sizeof(szTime));
2986 if (time < 3600.0)
2987 Format(szTime, sizeof(szTime), " %s", szTime);
2988 if (iCount == 100)
2989 Format(szValue, sizeof(szValue), "[%i.] %s | 禄 %s", iCount, szTime, szName);
2990 if (iCount >= 10)
2991 Format(szValue, sizeof(szValue), "[%i.] %s | 禄 %s", iCount, szTime, szName);
2992 else Format(szValue, sizeof(szValue), "[0%i.] %s | 禄 %s", iCount, szTime, szName);
2993 AddMenuItem(menu, szSteamID, szValue, ITEMDRAW_DEFAULT);
2994 PushArrayString(stringArray, szName);
2995 if (iCount == 1)
2996 Format(szFirstMap, sizeof(szFirstMap), "%s", szMap);
2997 iCount++;
2998 }
2999 }
3000 }
3001
3002 delete stringArray;
3003
3004 if (iCount == 1)
3005 CPrintToChat(client, PREFIX_T, "NoTopRecords", client, szMap);
3006
3007 Format(title, sizeof(title), "Top 50 Times on %s \n Rank Time Player", szFirstMap);
3008 SetMenuTitle(menu, title);
3009 SetMenuOptionFlags(menu, MENUFLAG_BUTTON_EXIT);
3010 DisplayMenu(menu, client, MENU_TIME_FOREVER);
3011}
3012
3013public void db_selectProSurfers(int client)
3014{
3015 char szQuery[1024];
3016 Format(szQuery, sizeof(szQuery), sql_selectProSurfers, g_szMapName);
3017 SQL_TQuery(g_hDb, sql_selectProSurfersCallback, szQuery, client, DBPrio_Low);
3018}
3019
3020public void db_currentRunRank(int client)
3021{
3022 if (!IsValidClient(client))
3023 return;
3024
3025 char szQuery[512];
3026 Format(szQuery, sizeof(szQuery), "SELECT count(runtimepro)+1 FROM `ck_playertimes` WHERE `mapname` = '%s' AND `runtimepro` < %f;", g_szMapName, g_fFinalTime[client]);
3027 SQL_TQuery(g_hDb, SQL_CurrentRunRankCallback, szQuery, client, DBPrio_Low);
3028}
3029
3030public void SQL_CurrentRunRankCallback(Handle owner, Handle hndl, const char[] error, any client)
3031{
3032 if (hndl == null)
3033 {
3034 LogError("[ckSurf] SQL Error (SQL_CurrentRunRankCallback): %s", error);
3035 return;
3036 }
3037 // Get players rank, 9999999 = error
3038 int rank;
3039 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3040 {
3041 rank = SQL_FetchInt(hndl, 0);
3042 }
3043
3044 MapFinishedMsgs(client, rank);
3045}
3046
3047//
3048// Get clients record from database
3049// Called when a player finishes a map
3050//
3051public void db_selectRecord(int client)
3052{
3053 if (!IsValidClient(client))
3054 return;
3055
3056 char szQuery[255];
3057 Format(szQuery, 255, "SELECT runtimepro FROM ck_playertimes WHERE steamid = '%s' AND mapname = '%s' AND runtimepro > -1.0", g_szSteamID[client], g_szMapName);
3058 SQL_TQuery(g_hDb, sql_selectRecordCallback, szQuery, client, DBPrio_Low);
3059}
3060
3061public void sql_selectRecordCallback(Handle owner, Handle hndl, const char[] error, any client)
3062{
3063 if (hndl == null)
3064 {
3065 LogError("[ckSurf] SQL Error (sql_selectRecordCallback): %s", error);
3066 return;
3067 }
3068
3069 if (!IsValidClient(client))
3070 return;
3071
3072 char szQuery[512];
3073
3074 // Found old time from database
3075 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3076 {
3077 float time = SQL_FetchFloat(hndl, 0);
3078
3079 // If new time was faster than the old one, update it
3080 if ((g_fFinalTime[client] <= time || time <= 0.1))
3081 db_updateRecordPro(client);
3082 }
3083 else
3084 { // No record found from database - Let's insert
3085
3086 // Escape name for SQL injection protection
3087 char szName[MAX_NAME_LENGTH * 2 + 1], szUName[MAX_NAME_LENGTH];
3088 GetClientName(client, szUName, sizeof(szUName));
3089 SQL_EscapeString(g_hDb, szUName, szName, sizeof(szName));
3090
3091 // Move required information in datapack
3092 Handle pack = CreateDataPack();
3093 WritePackFloat(pack, g_fFinalTime[client]);
3094 WritePackCell(pack, GetClientUserId(client));
3095
3096 //"INSERT INTO ck_playertimes (steamid, mapname, name,runtimepro) VALUES('%s', '%s', '%s', '%f');";
3097 Format(szQuery, sizeof(szQuery), sql_insertPlayerTime, g_szSteamID[client], g_szMapName, szName, g_fFinalTime[client]);
3098 SQL_TQuery(g_hDb, SQL_UpdateRecordProCallback, szQuery, pack, DBPrio_Low);
3099 }
3100}
3101
3102//
3103// If latest record was faster than old - Update time
3104//
3105public void db_updateRecordPro(int client)
3106{
3107 if (!IsValidClient(client))
3108 return;
3109
3110 char szUName[MAX_NAME_LENGTH];
3111 GetClientName(client, szUName, sizeof(szUName));
3112
3113 // Also updating name in database, escape string
3114 char szName[MAX_NAME_LENGTH * 2 + 1];
3115 SQL_EscapeString(g_hDb, szUName, szName, sizeof(szName));
3116
3117 // Packing required information for later
3118 Handle pack = CreateDataPack();
3119 WritePackFloat(pack, g_fFinalTime[client]);
3120 WritePackCell(pack, GetClientUserId(client));
3121
3122 char szQuery[1024];
3123 //"UPDATE ck_playertimes SET name = '%s', runtimepro = '%f' WHERE steamid = '%s' AND mapname = '%s';";
3124 Format(szQuery, sizeof(szQuery), sql_updateRecordPro, szName, g_fFinalTime[client], g_szSteamID[client], g_szMapName);
3125 SQL_TQuery(g_hDb, SQL_UpdateRecordProCallback, szQuery, pack, DBPrio_Low);
3126}
3127
3128public void SQL_UpdateRecordProCallback(Handle owner, Handle hndl, const char[] error, any data)
3129{
3130 if (hndl == null)
3131 {
3132 LogError("[ckSurf] SQL Error (SQL_UpdateRecordProCallback): %s", error);
3133 return;
3134 }
3135
3136 if (data != INVALID_HANDLE)
3137 {
3138 ResetPack(data);
3139 float time = ReadPackFloat(data);
3140 int client = GetClientOfUserId(ReadPackCell(data));
3141 CloseHandle(data);
3142
3143 if(!IsValidClient(client))
3144 return;
3145
3146 // Find out how many times are are faster than the players time
3147 char szQuery[512];
3148 Format(szQuery, sizeof(szQuery), "SELECT count(runtimepro) FROM `ck_playertimes` WHERE `mapname` = '%s' AND `runtimepro` < %f;", g_szMapName, time);
3149 SQL_TQuery(g_hDb, SQL_UpdateRecordProCallback2, szQuery, GetClientUserId(client), DBPrio_Low);
3150 }
3151}
3152
3153public void SQL_UpdateRecordProCallback2(Handle owner, Handle hndl, const char[] error, any userid)
3154{
3155 if (hndl == null)
3156 {
3157 LogError("[ckSurf] SQL Error (SQL_UpdateRecordProCallback2): %s", error);
3158 return;
3159 }
3160
3161 int client = GetClientOfUserId(userid);
3162
3163 if(!IsValidClient(client))
3164 return;
3165
3166 // Get players rank, 9999999 = error
3167 int rank = 9999999;
3168
3169 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3170 rank = (SQL_FetchInt(hndl, 0)+1);
3171
3172 g_MapRank[client] = rank;
3173 MapFinishedMsgs(client);
3174}
3175
3176public void db_viewRecord(int client, char szSteamId[32], char szMapName[128])
3177{
3178 char szQuery[512];
3179 // SELECT runtimepro, name FROM ck_playertimes WHERE mapname = '%s' AND steamid = '%s' AND runtimepro > 0.0
3180 Handle pack = CreateDataPack();
3181 WritePackString(pack, szMapName);
3182 WritePackString(pack, szSteamId);
3183 WritePackCell(pack, GetClientUserId(client));
3184
3185 Format(szQuery, 512, sql_selectPersonalRecords, szSteamId, szMapName);
3186 SQL_TQuery(g_hDb, SQL_ViewRecordCallback, szQuery, pack, DBPrio_Low);
3187}
3188
3189public void SQL_ViewRecordCallback(Handle owner, Handle hndl, const char[] error, any pack)
3190{
3191 if (hndl == null)
3192 {
3193 LogError("[ckSurf] SQL Error (SQL_ViewRecordCallback): %s", error);
3194 return;
3195 }
3196
3197 char szSteamId[32];
3198 char szMapName[128];
3199
3200 ResetPack(pack);
3201 ReadPackString(pack, szMapName, sizeof(szMapName));
3202 ReadPackString(pack, szSteamId, sizeof(szSteamId));
3203 int client = GetClientOfUserId(ReadPackCell(pack));
3204 CloseHandle(pack);
3205
3206 if(!IsValidClient(client))
3207 return;
3208
3209 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3210 {
3211 char szName[MAX_NAME_LENGTH];
3212
3213 SQL_FetchString(hndl, 1, szName, sizeof(szName));
3214 float runtime = SQL_FetchFloat(hndl, 0);
3215
3216 Handle pack1 = CreateDataPack();
3217 WritePackString(pack1, szMapName);
3218 WritePackString(pack1, szSteamId);
3219 WritePackString(pack1, szName);
3220 WritePackCell(pack1, GetClientUserId(client));
3221 WritePackFloat(pack1, runtime);
3222
3223 char szQuery[512];
3224 Format(szQuery, sizeof(szQuery), sql_selectPlayerRankProTime, szSteamId, szMapName, szMapName);
3225 SQL_TQuery(g_hDb, SQL_ViewRecordCallback2, szQuery, pack1, DBPrio_Low);
3226 }
3227 else
3228 {
3229 char sBuffer[128];
3230 Panel panel = new Panel();
3231 Format(sBuffer, sizeof(sBuffer), "%T", "current_map_time", client);
3232 panel.DrawText(sBuffer);
3233
3234 panel.DrawText(" ");
3235
3236 Format(sBuffer, sizeof(sBuffer), "%T", "no_record_map", client);
3237 panel.DrawText(sBuffer);
3238
3239 Format(sBuffer, sizeof(sBuffer), "%T", "exit_menu", client);
3240 panel.DrawItem(sBuffer);
3241 panel.Send(client, MenuHandler2, 300);
3242 delete panel;
3243 }
3244}
3245
3246public void SQL_ViewRecordCallback2(Handle owner, Handle hndl, const char[] error, any pack)
3247{
3248 if (hndl == null)
3249 {
3250 LogError("[ckSurf] SQL Error (SQL_ViewRecordCallback2): %s", error);
3251 return;
3252 }
3253
3254 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3255 {
3256 char szQuery[512];
3257 int rank = SQL_GetRowCount(hndl);
3258 char szMapName[128];
3259 char szSteamId[32];
3260 char szName[MAX_NAME_LENGTH];
3261
3262 WritePackCell(pack, rank);
3263 ResetPack(pack);
3264 ReadPackString(pack, szMapName, sizeof(szMapName));
3265 ReadPackString(pack, szSteamId, sizeof(szSteamId));
3266 ReadPackString(pack, szName, sizeof(szName));
3267 int client = GetClientOfUserId(ReadPackCell(pack));
3268
3269 if(!IsValidClient(client))
3270 {
3271 CloseHandle(pack);
3272 return;
3273 }
3274
3275 Format(szQuery, 512, sql_selectPlayerProCount, szMapName);
3276 SQL_TQuery(g_hDb, SQL_ViewRecordCallback3, szQuery, pack, DBPrio_Low);
3277 }
3278}
3279
3280
3281public void SQL_ViewRecordCallback3(Handle owner, Handle hndl, const char[] error, any pack)
3282{
3283 if (hndl == null)
3284 {
3285 LogError("[ckSurf] SQL Error (SQL_ViewRecordCallback3): %s", error);
3286 return;
3287 }
3288
3289 //if there is a player record
3290 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3291 {
3292 int count1 = SQL_GetRowCount(hndl);
3293 char szMapName[128];
3294 char szSteamId[32];
3295 char szName[MAX_NAME_LENGTH];
3296 float runtime = ReadPackFloat(pack);
3297
3298 ResetPack(pack);
3299 ReadPackString(pack, szMapName, sizeof(szMapName));
3300 ReadPackString(pack, szSteamId, sizeof(szSteamId));
3301 ReadPackString(pack, szName, sizeof(szName));
3302
3303 int client = GetClientOfUserId(ReadPackCell(pack));
3304
3305 if(!IsValidClient(client))
3306 {
3307 CloseHandle(pack);
3308 return;
3309 }
3310
3311 int rank = ReadPackCell(pack);
3312
3313 if (runtime != -1.0)
3314 {
3315 CloseHandle(pack);
3316
3317 Panel panel = new Panel();
3318 char szVrItem[256];
3319 Format(szVrItem, sizeof(szVrItem), "Map time of %s", szName);
3320 panel.DrawText(szVrItem);
3321 panel.DrawText(" ");
3322
3323 FormatTimeFloat(client, runtime, 3, szVrItem, sizeof(szVrItem));
3324 Format(szVrItem, sizeof(szVrItem), "Time: %s", szVrItem);
3325 panel.DrawText(szVrItem);
3326
3327 panel.DrawText("Map time:");
3328 Format(szVrItem, sizeof(szVrItem), "Rank: %i of %i", rank, count1);
3329 panel.DrawText(szVrItem);
3330 panel.DrawText(" ");
3331
3332 panel.DrawItem("Exit");
3333 panel.Send(client, RecordPanelHandler, 300);
3334 CloseHandle(panel);
3335 }
3336 else if (runtime != 0.0)
3337 {
3338 WritePackCell(pack, count1);
3339 char szQuery[512];
3340 Format(szQuery, 512, sql_selectPlayerRankProTime, szSteamId, szMapName, szMapName);
3341 SQL_TQuery(g_hDb, SQL_ViewRecordCallback4, szQuery, pack, DBPrio_Low);
3342 }
3343 }
3344}
3345
3346public void SQL_ViewRecordCallback4(Handle owner, Handle hndl, const char[] error, any pack)
3347{
3348 if (hndl == null)
3349 {
3350 LogError("[ckSurf] SQL Error (SQL_ViewRecordCallback4): %s", error);
3351 return;
3352 }
3353
3354 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3355 {
3356 char szQuery[512];
3357 int rankPro = SQL_GetRowCount(hndl);
3358 char szMapName[128];
3359
3360 WritePackCell(pack, rankPro);
3361 ResetPack(pack);
3362 ReadPackString(pack, szMapName, 128);
3363
3364 Format(szQuery, 512, sql_selectPlayerProCount, szMapName);
3365 SQL_TQuery(g_hDb, SQL_ViewRecordCallback5, szQuery, pack, DBPrio_Low);
3366 }
3367}
3368
3369public void SQL_ViewRecordCallback5(Handle owner, Handle hndl, const char[] error, any pack)
3370{
3371 if (hndl == null)
3372 {
3373 LogError("[ckSurf] SQL Error (SQL_ViewRecordCallback5): %s", error);
3374 return;
3375 }
3376
3377 //if there is a player record
3378 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3379 {
3380 int countPro = SQL_GetRowCount(hndl);
3381 //retrieve all values
3382 ResetPack(pack);
3383 char szMapName[128];
3384 ReadPackString(pack, szMapName, 128);
3385 char szSteamId[32];
3386 ReadPackString(pack, szSteamId, 32);
3387 char szName[MAX_NAME_LENGTH];
3388 ReadPackString(pack, szName, MAX_NAME_LENGTH);
3389
3390 int client = GetClientOfUserId(ReadPackCell(pack));
3391
3392 if(!IsValidClient(client))
3393 {
3394 CloseHandle(pack);
3395 return;
3396 }
3397
3398 float runtime = ReadPackFloat(pack);
3399 int rank = ReadPackCell(pack);
3400 int count1 = ReadPackCell(pack);
3401 int rankPro = ReadPackCell(pack);
3402
3403 if (runtime != -1.0)
3404 {
3405 Handle panel = CreatePanel();
3406 char szVrName[256];
3407 Format(szVrName, 256, "Map time of %s", szName);
3408 DrawPanelText(panel, szVrName);
3409 Format(szVrName, 256, "on %s", g_szMapName);
3410 DrawPanelText(panel, " ");
3411
3412 char szVrRank[32];
3413 char szVrRankPro[32];
3414 char szVrTimePro[256];
3415 FormatTimeFloat(client, runtime, 3, szVrTimePro, sizeof(szVrTimePro));
3416 Format(szVrTimePro, 256, "Time: %s", szVrTimePro);
3417
3418 Format(szVrRank, 32, "Rank: %i of %i", rank, count1);
3419 Format(szVrRankPro, 32, "Rank: %i of %i", rankPro, countPro);
3420
3421 DrawPanelText(panel, szVrRank);
3422 DrawPanelText(panel, " ");
3423 DrawPanelText(panel, "Time:");
3424 DrawPanelText(panel, szVrTimePro);
3425 DrawPanelText(panel, szVrRankPro);
3426 DrawPanelText(panel, " ");
3427 DrawPanelItem(panel, "exit");
3428 SendPanelToClient(panel, client, RecordPanelHandler, 300);
3429 CloseHandle(panel);
3430 }
3431 }
3432 CloseHandle(pack);
3433}
3434
3435public void db_viewAllRecords(int client, char szSteamId[32])
3436{
3437 //"SELECT db1.name, db2.steamid, db2.mapname, db2.runtimepro as overall, db1.steamid FROM ck_playertimes as db2 INNER JOIN ck_playerrank as db1 on db2.steamid = db1.steamid WHERE db2.steamid = '%s' AND db2.runtimepro > -1.0 ORDER BY mapname ASC;";
3438
3439 char szQuery[1024];
3440 Format(szQuery, 1024, sql_selectPersonalAllRecords, szSteamId, szSteamId);
3441 if ((StrContains(szSteamId, "STEAM_") != -1))
3442 SQL_TQuery(g_hDb, SQL_ViewAllRecordsCallback, szQuery, client, DBPrio_Low);
3443 else if (IsClientInGame(client))
3444 CPrintToChat(client, PREFIX_T, "InvalidSteamID", client);
3445 ProfileMenu(client, -1);
3446}
3447
3448
3449public void SQL_ViewAllRecordsCallback(Handle owner, Handle hndl, const char[] error, any data)
3450{
3451 if (hndl == null)
3452 {
3453 LogError("[ckSurf] SQL Error (SQL_ViewAllRecordsCallback): %s", error);
3454 return;
3455 }
3456
3457 int bHeader = false;
3458 char szUncMaps[1024];
3459 int mapcount = 0;
3460 char szName[MAX_NAME_LENGTH];
3461 char szSteamId[32];
3462 if (SQL_HasResultSet(hndl))
3463 {
3464 float time;
3465 char szMapName[128];
3466 char szMapName2[128];
3467 char szQuery[1024];
3468 Format(szUncMaps, sizeof(szUncMaps), "");
3469 while (SQL_FetchRow(hndl))
3470 {
3471 SQL_FetchString(hndl, 0, szName, MAX_NAME_LENGTH);
3472 SQL_FetchString(hndl, 1, szSteamId, MAX_NAME_LENGTH);
3473 SQL_FetchString(hndl, 2, szMapName, 128);
3474
3475 time = SQL_FetchFloat(hndl, 3);
3476
3477 int mapfound = false;
3478
3479 //map in rotation?
3480 for (int i = 0; i < GetArraySize(g_MapList); i++)
3481 {
3482 GetArrayString(g_MapList, i, szMapName2, sizeof(szMapName2));
3483 if (StrEqual(szMapName2, szMapName, false))
3484 {
3485 if (!bHeader)
3486 {
3487 PrintToConsole(data, " ");
3488 PrintToConsole(data, "-------------");
3489 PrintToConsole(data, "Finished Maps");
3490 PrintToConsole(data, "Player: %s", szName);
3491 PrintToConsole(data, "SteamID: %s", szSteamId);
3492 PrintToConsole(data, "-------------");
3493 PrintToConsole(data, " ");
3494 bHeader = true;
3495 CPrintToChat(data, PREFIX_t, "CheckConsole_Output");
3496 }
3497 Handle pack = CreateDataPack();
3498 WritePackString(pack, szName);
3499 WritePackString(pack, szSteamId);
3500 WritePackString(pack, szMapName);
3501 WritePackFloat(pack, time);
3502 WritePackCell(pack, data);
3503
3504 Format(szQuery, 1024, sql_selectPlayerRankProTime, szSteamId, szMapName, szMapName);
3505 SQL_TQuery(g_hDb, SQL_ViewAllRecordsCallback2, szQuery, pack, DBPrio_Low);
3506 mapfound = true;
3507 continue;
3508 }
3509 }
3510 if (!mapfound)
3511 {
3512 mapcount++;
3513 if (!mapfound && mapcount == 1)
3514 {
3515 Format(szUncMaps, sizeof(szUncMaps), "%s", szMapName);
3516 }
3517 else
3518 {
3519 if (!mapfound && mapcount > 1)
3520 {
3521 Format(szUncMaps, sizeof(szUncMaps), "%s, %s", szUncMaps, szMapName);
3522 }
3523 }
3524 }
3525 }
3526 }
3527 if (!StrEqual(szUncMaps, ""))
3528 {
3529 if (!bHeader)
3530 {
3531 CPrintToChat(data, PREFIX_t, "CheckConsole_Output");
3532 PrintToConsole(data, " ");
3533 PrintToConsole(data, "-------------");
3534 PrintToConsole(data, "Finished Maps");
3535 PrintToConsole(data, "Player: %s", szName);
3536 PrintToConsole(data, "SteamID: %s", szSteamId);
3537 PrintToConsole(data, "-------------");
3538 PrintToConsole(data, " ");
3539 }
3540 PrintToConsole(data, "Times on maps which are not in the mapcycle.txt (Records still count but you don't get points): %s", szUncMaps);
3541 }
3542 if (!bHeader && StrEqual(szUncMaps, ""))
3543 {
3544 ProfileMenu(data, -1);
3545 CPrintToChat(data, PREFIX_t, "PlayerHasNoMapRecords", g_szProfileName[data]);
3546 }
3547}
3548
3549public void SQL_ViewAllRecordsCallback2(Handle owner, Handle hndl, const char[] error, any data)
3550{
3551 if (hndl == null)
3552 {
3553 LogError("[ckSurf] SQL Error (SQL_ViewAllRecordsCallback2): %s", error);
3554 return;
3555 }
3556
3557 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3558 {
3559 char szQuery[512];
3560 char szName[MAX_NAME_LENGTH];
3561 char szSteamId[32];
3562 char szMapName[128];
3563
3564 int rank = SQL_GetRowCount(hndl);
3565 WritePackCell(data, rank);
3566 ResetPack(data);
3567 ReadPackString(data, szName, sizeof(szName));
3568 ReadPackString(data, szSteamId, sizeof(szSteamId));
3569 ReadPackString(data, szMapName, sizeof(szMapName));
3570
3571 Format(szQuery, 512, sql_selectPlayerProCount, szMapName);
3572 SQL_TQuery(g_hDb, SQL_ViewAllRecordsCallback3, szQuery, data, DBPrio_Low);
3573 }
3574}
3575
3576public void SQL_ViewAllRecordsCallback3(Handle owner, Handle hndl, const char[] error, any data)
3577{
3578 if (hndl == null)
3579 {
3580 LogError("[ckSurf] SQL Error (SQL_ViewAllRecordsCallback3): %s", error);
3581 return;
3582 }
3583
3584 //if there is a player record
3585 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3586 {
3587 int count = SQL_GetRowCount(hndl);
3588 char szTime[32];
3589 char szMapName[128];
3590 char szSteamId[32];
3591 char szName[MAX_NAME_LENGTH];
3592
3593 ResetPack(data);
3594 ReadPackString(data, szName, sizeof(szName));
3595 ReadPackString(data, szSteamId, sizeof(szSteamId));
3596 ReadPackString(data, szMapName, sizeof(szMapName));
3597 float time = ReadPackFloat(data);
3598 int client = ReadPackCell(data);
3599 int rank = ReadPackCell(data);
3600 CloseHandle(data);
3601
3602 FormatTimeFloat(client, time, 3, szTime, sizeof(szTime));
3603 if (IsValidClient(client))
3604 PrintToConsole(client, "%s, Time: %s, Rank: %i/%i", szMapName, szTime, rank, count);
3605 }
3606}
3607
3608
3609public void db_selectPlayer(int client)
3610{
3611 char szQuery[255];
3612 if (!IsValidClient(client))
3613 return;
3614 Format(szQuery, sizeof(szQuery), sql_selectPlayer, g_szSteamID[client], g_szMapName);
3615 SQL_TQuery(g_hDb, SQL_SelectPlayerCallback, szQuery, client, DBPrio_Low);
3616}
3617
3618public void SQL_SelectPlayerCallback(Handle owner, Handle hndl, const char[] error, any data)
3619{
3620 if (hndl == null)
3621 {
3622 LogError("[ckSurf] SQL Error (SQL_SelectPlayerCallback): %s", error);
3623 return;
3624 }
3625
3626 if (!SQL_HasResultSet(hndl) && !SQL_FetchRow(hndl) && !IsValidClient(data))
3627 db_insertPlayer(data);
3628}
3629
3630public void db_insertPlayer(int client)
3631{
3632 char szQuery[255];
3633 char szUName[MAX_NAME_LENGTH];
3634 if (IsValidClient(client))
3635 {
3636 GetClientName(client, szUName, MAX_NAME_LENGTH);
3637 }
3638 else
3639 return;
3640 char szName[MAX_NAME_LENGTH * 2 + 1];
3641 SQL_EscapeString(g_hDb, szUName, szName, MAX_NAME_LENGTH * 2 + 1);
3642 Format(szQuery, 255, sql_insertPlayer, g_szSteamID[client], g_szMapName, szName);
3643 SQL_TQuery(g_hDb, SQL_InsertPlayerCallBack, szQuery, client, DBPrio_Low);
3644}
3645
3646//
3647// Getting player settings starts here
3648//
3649public void db_viewPersonalRecords(int client, char szSteamId[32], char szMapName[128])
3650{
3651 char szQuery[1024];
3652 Format(szQuery, 1024, "SELECT runtimepro FROM ck_playertimes WHERE steamid = '%s' AND mapname ='%s' AND runtimepro > 0.0;", szSteamId, szMapName);
3653 SQL_TQuery(g_hDb, SQL_selectPersonalRecordsCallback, szQuery, client, DBPrio_Low);
3654}
3655
3656
3657public void SQL_selectPersonalRecordsCallback(Handle owner, Handle hndl, const char[] error, any client)
3658{
3659 if (hndl == null)
3660 {
3661 LogError("[ckSurf] SQL Error (SQL_selectPersonalRecordsCallback): %s", error);
3662 if (!g_bSettingsLoaded[client])
3663 db_viewPersonalBonusRecords(client, g_szSteamID[client]);
3664 return;
3665 }
3666
3667 g_fPersonalRecord[client] = 0.0;
3668 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3669 {
3670 g_fPersonalRecord[client] = SQL_FetchFloat(hndl, 0);
3671
3672 if (g_fPersonalRecord[client] > 0.0)
3673 {
3674 FormatTimeFloat(client, g_fPersonalRecord[client], 3, g_szPersonalRecord[client], 64);
3675 // Time found, get rank in current map
3676 db_viewMapRankPro(client);
3677 return;
3678 }
3679 else
3680 {
3681 Format(g_szPersonalRecord[client], 64, "NONE");
3682 g_fPersonalRecord[client] = 0.0;
3683 }
3684 }
3685 if (!g_bSettingsLoaded[client])
3686 db_viewPersonalBonusRecords(client, g_szSteamID[client]);
3687 return;
3688}
3689
3690///////////////////////
3691//// PLAYER TEMP //////
3692///////////////////////
3693
3694public void db_deleteTmp(int client)
3695{
3696 char szQuery[256];
3697 if (!IsValidClient(client))
3698 return;
3699 Format(szQuery, 256, sql_deletePlayerTmp, g_szSteamID[client]);
3700 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, client, DBPrio_Low);
3701}
3702
3703public void db_selectLastRun(int client)
3704{
3705 char szQuery[512];
3706 if (!IsValidClient(client))
3707 return;
3708 Format(szQuery, 512, sql_selectPlayerTmp, g_szSteamID[client], g_szMapName);
3709 SQL_TQuery(g_hDb, SQL_LastRunCallback, szQuery, client, DBPrio_Low);
3710}
3711
3712public void SQL_LastRunCallback(Handle owner, Handle hndl, const char[] error, any data)
3713{
3714 if (hndl == null)
3715 {
3716 LogError("[ckSurf] SQL Error (SQL_LastRunCallback): %s", error);
3717 return;
3718 }
3719
3720 g_bTimeractivated[data] = false;
3721 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl) && IsValidClient(data))
3722 {
3723
3724 //"SELECT cords1,cords2,cords3, angle1, angle2, angle3,runtimeTmp, EncTickrate, Stage, zonegroup FROM ck_playertemp WHERE steamid = '%s' AND mapname = '%s';";
3725
3726 //Get last psition
3727 g_fPlayerCordsRestore[data][0] = SQL_FetchFloat(hndl, 0);
3728 g_fPlayerCordsRestore[data][1] = SQL_FetchFloat(hndl, 1);
3729 g_fPlayerCordsRestore[data][2] = SQL_FetchFloat(hndl, 2);
3730 g_fPlayerAnglesRestore[data][0] = SQL_FetchFloat(hndl, 3);
3731 g_fPlayerAnglesRestore[data][1] = SQL_FetchFloat(hndl, 4);
3732 g_fPlayerAnglesRestore[data][2] = SQL_FetchFloat(hndl, 5);
3733
3734
3735 int zGroup;
3736 zGroup = SQL_FetchInt(hndl, 9);
3737
3738 g_iClientInZone[data][2] = zGroup;
3739
3740 g_Stage[zGroup][data] = SQL_FetchInt(hndl, 8);
3741
3742 //Set new start time
3743 float fl_time = SQL_FetchFloat(hndl, 6);
3744 int tickrate = RoundFloat(float(SQL_FetchInt(hndl, 7)) / 5.0 / 11.0);
3745 if (tickrate == g_Server_Tickrate)
3746 {
3747 if (fl_time > 0.0)
3748 {
3749 g_fStartTime[data] = GetGameTime() - fl_time;
3750 g_bTimeractivated[data] = true;
3751 }
3752
3753 if (SQL_FetchFloat(hndl, 0) == -1.0 && SQL_FetchFloat(hndl, 1) == -1.0 && SQL_FetchFloat(hndl, 2) == -1.0)
3754 {
3755 g_bRestorePosition[data] = false;
3756 g_bRestorePositionMsg[data] = false;
3757 }
3758 else
3759 {
3760 if (g_bLateLoaded && IsPlayerAlive(data) && !g_specToStage[data])
3761 {
3762 g_bPositionRestored[data] = true;
3763 TeleportEntity(data, g_fPlayerCordsRestore[data], g_fPlayerAnglesRestore[data], NULL_VECTOR);
3764 g_bRestorePosition[data] = false;
3765 }
3766 else
3767 {
3768 g_bRestorePosition[data] = true;
3769 g_bRestorePositionMsg[data] = true;
3770 }
3771
3772 }
3773 }
3774 }
3775 else
3776 {
3777
3778 g_bTimeractivated[data] = false;
3779 }
3780}
3781
3782///////////////////////
3783//// Checkpoints //////
3784///////////////////////
3785
3786public void db_viewRecordCheckpointInMap()
3787{
3788 for (int k = 0; k < MAXZONEGROUPS; k++)
3789 {
3790 g_bCheckpointRecordFound[k] = false;
3791 for (int i = 0; i < CPLIMIT; i++)
3792 g_fCheckpointServerRecord[k][i] = 0.0;
3793 }
3794
3795 //"SELECT c.zonegroup, c.cp1, c.cp2, c.cp3, c.cp4, c.cp5, c.cp6, c.cp7, c.cp8, c.cp9, c.cp10, c.cp11, c.cp12, c.cp13, c.cp14, c.cp15, c.cp16, c.cp17, c.cp18, c.cp19, c.cp20, c.cp21, c.cp22, c.cp23, c.cp24, c.cp25, c.cp26, c.cp27, c.cp28, c.cp29, c.cp30, c.cp31, c.cp32, c.cp33, c.cp34, c.cp35 FROM ck_checkpoints c WHERE steamid = '%s' AND mapname='%s' UNION SELECT a.zonegroup, b.cp1, b.cp2, b.cp3, b.cp4, b.cp5, b.cp6, b.cp7, b.cp8, b.cp9, b.cp10, b.cp11, b.cp12, b.cp13, b.cp14, b.cp15, b.cp16, b.cp17, b.cp18, b.cp19, b.cp20, b.cp21, b.cp22, b.cp23, b.cp24, b.cp25, b.cp26, b.cp27, b.cp28, b.cp29, b.cp30, b.cp31, b.cp32, b.cp33, b.cp34, b.cp35 FROM ck_bonus a LEFT JOIN ck_checkpoints b ON a.steamid = b.steamid AND a.zonegroup = b.zonegroup WHERE a.mapname = '%s' GROUP BY a.zonegroup";
3796 char szQuery[1028];
3797 Format(szQuery, sizeof(szQuery), sql_selectRecordCheckpoints, g_szRecordMapSteamID, g_szMapName, g_szMapName);
3798 SQL_TQuery(g_hDb, sql_selectRecordCheckpointsCallback, szQuery, 1, DBPrio_Low);
3799}
3800
3801public void sql_selectRecordCheckpointsCallback(Handle owner, Handle hndl, const char[] error, any data)
3802{
3803 if (hndl == null)
3804 {
3805 LogError("[ckSurf] SQL Error (sql_selectRecordCheckpointsCallback): %s", error);
3806 if (!g_bServerDataLoaded)
3807 db_CalcAvgRunTime();
3808 return;
3809 }
3810 if (SQL_HasResultSet(hndl))
3811 {
3812 int zonegroup;
3813 while (SQL_FetchRow(hndl))
3814 {
3815 zonegroup = SQL_FetchInt(hndl, 0);
3816 for (int i = 0; i < CPLIMIT; i++)
3817 {
3818 g_fCheckpointServerRecord[zonegroup][i] = SQL_FetchFloat(hndl, (i + 1));
3819 if (!g_bCheckpointRecordFound[zonegroup] && g_fCheckpointServerRecord[zonegroup][i] > 0.0)
3820 g_bCheckpointRecordFound[zonegroup] = true;
3821 }
3822 }
3823 }
3824 if (!g_bServerDataLoaded)
3825 db_CalcAvgRunTime();
3826
3827 return;
3828}
3829
3830public void db_viewCheckpoints(int client, char szSteamID[32], char szMapName[128])
3831{
3832 char szQuery[1024];
3833 //"SELECT zonegroup, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, cp26, cp27, cp28, cp29, cp30, cp31, cp32, cp33, cp34, cp35 FROM ck_checkpoints WHERE mapname='%s' AND steamid = '%s';";
3834 Format(szQuery, 1024, sql_selectCheckpoints, szMapName, szSteamID);
3835 SQL_TQuery(g_hDb, SQL_selectCheckpointsCallback, szQuery, GetClientUserId(client), DBPrio_Low);
3836}
3837
3838public void SQL_selectCheckpointsCallback(Handle owner, Handle hndl, const char[] error, any userid)
3839{
3840 if (hndl == null)
3841 {
3842 LogError("[ckSurf] SQL Error (SQL_selectCheckpointsCallback): %s", error);
3843 return;
3844 }
3845
3846 int client = GetClientOfUserId(userid);
3847
3848 if (!IsValidClient(client))
3849 return;
3850
3851 int zoneGrp;
3852
3853 if (SQL_HasResultSet(hndl))
3854 {
3855 while (SQL_FetchRow(hndl))
3856 {
3857 zoneGrp = SQL_FetchInt(hndl, 0);
3858 g_bCheckpointsFound[zoneGrp][client] = true;
3859 int k = 1;
3860 for (int i = 0; i < CPLIMIT; i++)
3861 {
3862 g_fCheckpointTimesRecord[zoneGrp][client][i] = SQL_FetchFloat(hndl, k);
3863 k++;
3864 }
3865 }
3866 }
3867
3868 if (!g_bSettingsLoaded[client])
3869 {
3870 g_bSettingsLoaded[client] = true;
3871 g_bLoadingSettings[client] = false;
3872 if (GetConVarBool(g_hTeleToStartWhenSettingsLoaded) && GetClientTeam(client) > 1)
3873 Command_Restart(client, 1);
3874
3875 // Seach for next client to load
3876 for (int i = 1; i < MAXPLAYERS + 1; i++)
3877 {
3878 if (IsValidClient(i) && !IsFakeClient(i) && !g_bSettingsLoaded[i] && !g_bLoadingSettings[i])
3879 {
3880 char szSteamID[32];
3881 GetClientAuthId(i, AuthId_Steam2, szSteamID, sizeof(szSteamID), true);
3882 db_viewPersonalRecords(i, szSteamID, g_szMapName);
3883 g_bLoadingSettings[i] = true;
3884 break;
3885 }
3886 }
3887 }
3888}
3889
3890public void db_viewCheckpointsinZoneGroup(int client, char szSteamID[32], char szMapName[128], int zonegroup)
3891{
3892 char szQuery[1024];
3893 //"SELECT cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, cp26, cp27, cp28, cp29, cp30, cp31, cp32, cp33, cp34, cp35 FROM ck_checkpoints WHERE mapname='%s' AND steamid = '%s' AND zonegroup = %i;";
3894 Handle pack = CreateDataPack();
3895 WritePackCell(pack, client);
3896 WritePackCell(pack, zonegroup);
3897
3898 Format(szQuery, sizeof(szQuery), sql_selectCheckpointsinZoneGroup, szMapName, szSteamID, zonegroup);
3899 SQL_TQuery(g_hDb, db_viewCheckpointsinZoneGroupCallback, szQuery, pack, DBPrio_Low);
3900}
3901
3902public void db_viewCheckpointsinZoneGroupCallback(Handle owner, Handle hndl, const char[] error, any pack)
3903{
3904 if (hndl == null)
3905 {
3906 LogError("[ckSurf] SQL Error (SQL_selectCheckpointsCallback): %s", error);
3907 return;
3908 }
3909
3910 ResetPack(pack);
3911 int client = ReadPackCell(pack);
3912 int zonegrp = ReadPackCell(pack);
3913 CloseHandle(pack);
3914
3915 if (!IsValidClient(client))
3916 return;
3917
3918 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
3919 {
3920 g_bCheckpointsFound[zonegrp][client] = true;
3921 for (int i = 0; i < CPLIMIT; i++)
3922 {
3923 g_fCheckpointTimesRecord[zonegrp][client][i] = SQL_FetchFloat(hndl, i);
3924 }
3925 }
3926 else
3927 {
3928 g_bCheckpointsFound[zonegrp][client] = false;
3929 }
3930}
3931
3932char sql_updateCheckpointsSplit[] = "UPDATE ck_checkpoints SET %s WHERE steamid='%s' AND mapname='%s' AND zonegroup='%i'";
3933char sql_insertCheckpointsSplit[] = "INSERT INTO ck_checkpoints VALUES ('%s', '%s', %s ,'%i')";
3934
3935public void db_UpdateCheckpoints(int client, char szSteamID[32], int zGroup)
3936{
3937 Handle pack = CreateDataPack();
3938 WritePackCell(pack, client);
3939 WritePackCell(pack, zGroup);
3940 if (g_bCheckpointsFound[zGroup][client])
3941 {
3942 char sBuffer[1024];
3943 for (int i = 0; i < 35; i++)
3944 {
3945 Format(sBuffer, sizeof(sBuffer), "%s%scp%i='%f'", sBuffer, i == 0 ? "" : ",", i+1, g_fCheckpointTimesNew[zGroup][client][i]);
3946 }
3947
3948 char szQuery[1024];
3949 Format(szQuery, sizeof(szQuery), sql_updateCheckpointsSplit, sBuffer, szSteamID, g_szMapName, zGroup);
3950 SQL_TQuery(g_hDb, SQL_updateCheckpointsCallback, szQuery, pack, DBPrio_Low);
3951 }
3952 else
3953 {
3954 char sBuffer[1024];
3955 for (int i = 0; i < 35; i++)
3956 {
3957 Format(sBuffer, sizeof(sBuffer), "%s%s'%f'", sBuffer, i == 0 ? "" : ",", g_fCheckpointTimesNew[zGroup][client][i]);
3958 }
3959 char szQuery[1024];
3960 Format(szQuery, sizeof(szQuery), sql_insertCheckpointsSplit, szSteamID, g_szMapName, sBuffer, zGroup);
3961 SQL_TQuery(g_hDb, SQL_updateCheckpointsCallback, szQuery, pack, DBPrio_Low);
3962 }
3963}
3964
3965public void SQL_updateCheckpointsCallback(Handle owner, Handle hndl, const char[] error, any data)
3966{
3967 if (hndl == null)
3968 {
3969 LogError("[ckSurf] SQL Error (SQL_updateCheckpointsCallback): %s", error);
3970 return;
3971 }
3972 ResetPack(data);
3973 int client = ReadPackCell(data);
3974 int zonegrp = ReadPackCell(data);
3975 CloseHandle(data);
3976
3977 db_viewCheckpointsinZoneGroup(client, g_szSteamID[client], g_szMapName, zonegrp);
3978}
3979
3980public void db_deleteCheckpoints()
3981{
3982 char szQuery[258];
3983 Format(szQuery, 258, sql_deleteCheckpoints, g_szMapName);
3984 SQL_TQuery(g_hDb, SQL_deleteCheckpointsCallback, szQuery, 1, DBPrio_Low);
3985}
3986
3987public void SQL_deleteCheckpointsCallback(Handle owner, Handle hndl, const char[] error, any data)
3988{
3989 if (hndl == null)
3990 {
3991 LogError("[ckSurf] SQL Error (SQL_deleteCheckpointsCallback): %s", error);
3992 return;
3993 }
3994}
3995
3996//////////////////////
3997//// MapTier /////////
3998//////////////////////
3999
4000public void db_insertMapTier(int tier, int zGrp)
4001{
4002 char szQuery[256];
4003 if (g_bTierEntryFound)
4004 {
4005 if (zGrp > 0)
4006 {
4007 Format(szQuery, 256, sql_updateBonusTier, zGrp, tier, g_szMapName);
4008 }
4009 else
4010 {
4011 Format(szQuery, 256, sql_updatemaptier, tier, g_szMapName);
4012 }
4013 SQL_TQuery(g_hDb, db_insertMapTierCallback, szQuery, 1, DBPrio_Low);
4014 }
4015 else
4016 {
4017 if (zGrp > 0)
4018 {
4019 Format(szQuery, 256, sql_insertBonusTier, zGrp, tier, g_szMapName);
4020 }
4021 else
4022 {
4023 Format(szQuery, 256, sql_insertmaptier, g_szMapName, tier);
4024 }
4025 SQL_TQuery(g_hDb, db_insertMapTierCallback, szQuery, 1, DBPrio_Low);
4026 }
4027}
4028
4029public void db_insertMapTierCallback(Handle owner, Handle hndl, const char[] error, any data)
4030{
4031 if (hndl == null)
4032 {
4033 LogError("[ckSurf] SQL Error (db_insertMapTierCallback): %s", error);
4034 return;
4035 }
4036
4037 db_selectMapTier();
4038}
4039
4040public void db_selectMapTier()
4041{
4042 g_bTierEntryFound = false;
4043
4044 char szQuery[1024];
4045 Format(szQuery, sizeof(szQuery), sql_selectMapTier, g_szMapName);
4046 SQL_TQuery(g_hDb, SQL_selectMapTierCallback, szQuery, 1, DBPrio_Low);
4047}
4048
4049public void SQL_selectMapTierCallback(Handle owner, Handle hndl, const char[] error, any data)
4050{
4051 if (hndl == null)
4052 {
4053 LogError("[ckSurf] SQL Error (SQL_selectMapTierCallback): %s", error);
4054 if (!g_bServerDataLoaded)
4055 db_viewRecordCheckpointInMap();
4056 return;
4057 }
4058
4059 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
4060 {
4061 g_bTierEntryFound = true;
4062 int tier;
4063
4064 // Format tier string for all
4065 for (int i = 0; i < 11; i++)
4066 {
4067 tier = SQL_FetchInt(hndl, i);
4068 if (0 < tier < 7)
4069 {
4070 g_bTierFound[i] = true;
4071 if (i == 0)
4072 {
4073 Format(g_sTierString[0], sizeof(g_sTierString[]), PREFIX_t, "map_name", g_szMapName);
4074 switch (tier)
4075 {
4076 case 1:Format(g_sTierString[0], sizeof(g_sTierString[]), "%sTier %i {grey}| {red}", g_sTierString[0], tier);
4077 case 2:Format(g_sTierString[0], sizeof(g_sTierString[]), "%sTier %i {grey}| {red}", g_sTierString[0], tier);
4078 case 3:Format(g_sTierString[0], sizeof(g_sTierString[]), "%sTier %i {grey}| {red}", g_sTierString[0], tier);
4079 case 4:Format(g_sTierString[0], sizeof(g_sTierString[]), "%sTier %i {grey}| {red}", g_sTierString[0], tier);
4080 case 5:Format(g_sTierString[0], sizeof(g_sTierString[]), "%sTier %i {grey}| {red}", g_sTierString[0], tier);
4081 case 6:Format(g_sTierString[0], sizeof(g_sTierString[]), "%sTier %i {grey}| {red}", g_sTierString[0], tier);
4082 default:Format(g_sTierString[0], sizeof(g_sTierString[]), "%sTier %i {grey}| {red}", g_sTierString[0], tier);
4083 }
4084 if (g_bhasStages)
4085 Format(g_sTierString[0], sizeof(g_sTierString[]), "%s%i Stages", g_sTierString[0], (g_mapZonesTypeCount[0][3] + 1));
4086 else Format(g_sTierString[0], sizeof(g_sTierString[]), "%sLinear", g_sTierString[0]);
4087
4088 if (g_bhasBonus)
4089 if (g_mapZoneGroupCount > 2)
4090 Format(g_sTierString[0], sizeof(g_sTierString[]), "%s {grey}| {red}%i Bonuses", g_sTierString[0], (g_mapZoneGroupCount - 1));
4091 else Format(g_sTierString[0], sizeof(g_sTierString[]), "%s {grey}| {red}Bonus", g_sTierString[0], (g_mapZoneGroupCount - 1));
4092 }
4093 else
4094 {
4095 switch (tier)
4096 {
4097 case 1:Format(g_sTierString[i], sizeof(g_sTierString[]), "&c%s Tier: %i", g_szZoneGroupName[i], tier);
4098 case 2:Format(g_sTierString[i], sizeof(g_sTierString[]), "&c%s Tier: %i", g_szZoneGroupName[i], tier);
4099 case 3:Format(g_sTierString[i], sizeof(g_sTierString[]), "&c%s Tier: %i", g_szZoneGroupName[i], tier);
4100 case 4:Format(g_sTierString[i], sizeof(g_sTierString[]), "&c%s Tier: %i", g_szZoneGroupName[i], tier);
4101 case 5:Format(g_sTierString[i], sizeof(g_sTierString[]), "&c%s Tier: %i", g_szZoneGroupName[i], tier);
4102 case 6:Format(g_sTierString[i], sizeof(g_sTierString[]), "&c%s Tier: %i", g_szZoneGroupName[i], tier);
4103 }
4104 }
4105 }
4106 }
4107 }
4108 else g_bTierEntryFound = false;
4109
4110 if (!g_bServerDataLoaded)
4111 db_viewRecordCheckpointInMap();
4112 return;
4113}
4114
4115
4116public void db_deleteAllMaptiers(int client)
4117{
4118 char szQuery[128];
4119 Format(szQuery, sizeof(szQuery), sql_deleteAllMapTiers);
4120 SQL_TQuery(g_hDb, SQL_deleteAllMapTiersCallback, szQuery, client, DBPrio_Low);
4121}
4122
4123public void SQL_deleteAllMapTiersCallback(Handle owner, Handle hndl, const char[] error, any data)
4124{
4125 if (hndl == null)
4126 {
4127 LogError("[ckSurf] SQL Error (SQL_deleteAllMapTiersCallback): %s", error);
4128 return;
4129 }
4130 Admin_InsertMapTierstoDatabase(data);
4131}
4132
4133/////////////////////
4134//// SQL Bonus //////
4135/////////////////////
4136
4137public void db_currentBonusRunRank(int client, int zGroup)
4138{
4139 char szQuery[512];
4140 Handle pack = CreateDataPack();
4141 WritePackCell(pack, client);
4142 WritePackCell(pack, zGroup);
4143 Format(szQuery, sizeof(szQuery), "SELECT count(runtime)+1 FROM ck_bonus WHERE mapname = '%s' AND zonegroup = '%i' AND runtime < %f", g_szMapName, zGroup, g_fFinalTime[client]);
4144 SQL_TQuery(g_hDb, db_viewBonusRunRank, szQuery, pack, DBPrio_Low);
4145}
4146
4147public void db_viewBonusRunRank(Handle owner, Handle hndl, const char[] error, any pack)
4148{
4149 if (hndl == null)
4150 {
4151 LogError("[ckSurf] SQL Error (db_viewBonusRunRank): %s", error);
4152 return;
4153 }
4154
4155 ResetPack(pack);
4156 int client = ReadPackCell(pack);
4157 int zGroup = ReadPackCell(pack);
4158 CloseHandle(pack);
4159 int rank;
4160 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
4161 {
4162 rank = SQL_FetchInt(hndl, 0);
4163 }
4164
4165 PrintChatBonus(client, zGroup, rank);
4166}
4167
4168public void db_viewMapRankBonus(int client, int zgroup, int type)
4169{
4170 char szQuery[1024];
4171 Handle pack = CreateDataPack();
4172 WritePackCell(pack, client);
4173 WritePackCell(pack, zgroup);
4174 WritePackCell(pack, type);
4175
4176 Format(szQuery, sizeof(szQuery), sql_selectPlayerRankBonus, g_szSteamID[client], g_szMapName, zgroup, g_szMapName, zgroup);
4177 SQL_TQuery(g_hDb, db_viewMapRankBonusCallback, szQuery, pack, DBPrio_Low);
4178}
4179
4180public void db_viewMapRankBonusCallback(Handle owner, Handle hndl, const char[] error, any data)
4181{
4182 if (hndl == null)
4183 {
4184 LogError("[ckSurf] SQL Error (db_viewMapRankBonusCallback): %s", error);
4185 return;
4186 }
4187
4188 ResetPack(data);
4189 int client = ReadPackCell(data);
4190 int zgroup = ReadPackCell(data);
4191 int type = ReadPackCell(data);
4192 CloseHandle(data);
4193
4194 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
4195 {
4196 g_MapRankBonus[zgroup][client] = SQL_GetRowCount(hndl);
4197 }
4198 else
4199 {
4200 g_MapRankBonus[zgroup][client] = 9999999;
4201 }
4202
4203 switch (type)
4204 {
4205 case 1: {
4206 g_iBonusCount[zgroup]++;
4207 PrintChatBonus(client, zgroup);
4208 }
4209 case 2: {
4210 PrintChatBonus(client, zgroup);
4211 }
4212 }
4213}
4214
4215//
4216// Get player rank in bonus - current map
4217//
4218public void db_viewPersonalBonusRecords(int client, char szSteamId[32])
4219{
4220 char szQuery[1024];
4221 //"SELECT runtime, zonegroup FROM ck_bonus WHERE steamid = '%s' AND mapname = '%s' AND runtime > '0.0'";
4222 Format(szQuery, sizeof(szQuery), sql_selectPersonalBonusRecords, szSteamId, g_szMapName);
4223 SQL_TQuery(g_hDb, SQL_selectPersonalBonusRecordsCallback, szQuery, client, DBPrio_Low);
4224}
4225
4226public void SQL_selectPersonalBonusRecordsCallback(Handle owner, Handle hndl, const char[] error, any client)
4227{
4228 if (hndl == null)
4229 {
4230 LogError("[ckSurf] SQL Error (SQL_selectPersonalBonusRecordsCallback): %s", error);
4231 if (!g_bSettingsLoaded[client])
4232 db_viewPlayerPoints(client);
4233 return;
4234 }
4235
4236 int zgroup;
4237
4238 for (int i = 0; i < MAXZONEGROUPS; i++)
4239 {
4240 g_fPersonalRecordBonus[i][client] = 0.0;
4241 Format(g_szPersonalRecordBonus[i][client], 64, "N/A");
4242 }
4243
4244 if (SQL_HasResultSet(hndl))
4245 {
4246 while (SQL_FetchRow(hndl))
4247 {
4248 zgroup = SQL_FetchInt(hndl, 1);
4249 g_fPersonalRecordBonus[zgroup][client] = SQL_FetchFloat(hndl, 0);
4250
4251 if (g_fPersonalRecordBonus[zgroup][client] > 0.0)
4252 {
4253 FormatTimeFloat(client, g_fPersonalRecordBonus[zgroup][client], 3, g_szPersonalRecordBonus[zgroup][client], 64);
4254 db_viewMapRankBonus(client, zgroup, 0); // get rank
4255 }
4256 else
4257 {
4258 Format(g_szPersonalRecordBonus[zgroup][client], 64, "N/A");
4259 g_fPersonalRecordBonus[zgroup][client] = 0.0;
4260 }
4261 }
4262 }
4263 if (!g_bSettingsLoaded[client])
4264 db_viewPlayerPoints(client);
4265 return;
4266}
4267
4268public void db_viewFastestBonus()
4269{
4270 char szQuery[1024];
4271 //SELECT name, MIN(runtime), zonegroup FROM ck_bonus WHERE mapname = '%s' GROUP BY zonegroup;
4272 Format(szQuery, 1024, sql_selectFastestBonus, g_szMapName);
4273 SQL_TQuery(g_hDb, SQL_selectFastestBonusCallback, szQuery, 1, DBPrio_High);
4274}
4275
4276public void SQL_selectFastestBonusCallback(Handle owner, Handle hndl, const char[] error, any data)
4277{
4278 if (hndl == null)
4279 {
4280 LogError("[ckSurf] SQL Error (SQL_selectFastestBonusCallback): %s", error);
4281
4282 if (!g_bServerDataLoaded)
4283 db_viewBonusTotalCount();
4284 return;
4285 }
4286
4287 for (int i = 0; i < MAXZONEGROUPS; i++)
4288 {
4289 Format(g_szBonusFastestTime[i], 64, "N/A");
4290 g_fBonusFastest[i] = 9999999.0;
4291 }
4292
4293 if (SQL_HasResultSet(hndl))
4294 {
4295 int zonegroup;
4296 while (SQL_FetchRow(hndl))
4297 {
4298 zonegroup = SQL_FetchInt(hndl, 2);
4299 SQL_FetchString(hndl, 0, g_szBonusFastest[zonegroup], MAX_NAME_LENGTH);
4300 g_fBonusFastest[zonegroup] = SQL_FetchFloat(hndl, 1);
4301
4302 FormatTimeFloat(1, g_fBonusFastest[zonegroup], 3, g_szBonusFastestTime[zonegroup], 64);
4303 }
4304 }
4305
4306 for (int i = 0; i < MAXZONEGROUPS; i++)
4307 {
4308 if (g_fBonusFastest[i] == 0.0)
4309 g_fBonusFastest[i] = 9999999.0;
4310 }
4311
4312 if (!g_bServerDataLoaded)
4313 db_viewBonusTotalCount();
4314
4315 return;
4316}
4317
4318public void db_deleteBonus()
4319{
4320 char szQuery[1024];
4321 Format(szQuery, sizeof(szQuery), sql_deleteBonus, g_szMapName);
4322 SQL_TQuery(g_hDb, SQL_deleteBonusCallback, szQuery, 1, DBPrio_Low);
4323}
4324public void db_viewBonusTotalCount()
4325{
4326 char szQuery[1024];
4327 //"SELECT zonegroup, count(1) FROM ck_bonus WHERE mapname = '%s' GROUP BY zonegroup";
4328 Format(szQuery, sizeof(szQuery), sql_selectBonusCount, g_szMapName);
4329 SQL_TQuery(g_hDb, SQL_selectBonusTotalCountCallback, szQuery, 1, DBPrio_Low);
4330}
4331
4332public void SQL_selectBonusTotalCountCallback(Handle owner, Handle hndl, const char[] error, any data)
4333{
4334 if (hndl == null)
4335 {
4336 LogError("[ckSurf] SQL Error (SQL_selectBonusTotalCountCallback): %s", error);
4337 if (!g_bServerDataLoaded)
4338 db_selectMapTier();
4339 return;
4340 }
4341
4342 for (int i = 1; i < MAXZONEGROUPS; i++)
4343 g_iBonusCount[i] = 0;
4344
4345 if (SQL_HasResultSet(hndl))
4346 {
4347 int zonegroup;
4348 while (SQL_FetchRow(hndl))
4349 {
4350 zonegroup = SQL_FetchInt(hndl, 0);
4351 g_iBonusCount[zonegroup] = SQL_FetchInt(hndl, 1);
4352 }
4353 }
4354
4355 if (!g_bServerDataLoaded)
4356 db_selectMapTier();
4357
4358 return;
4359}
4360
4361
4362public void db_insertBonus(int client, char szSteamId[32], char szUName[32], float FinalTime, int zoneGrp)
4363{
4364 char szQuery[1024];
4365 char szName[MAX_NAME_LENGTH * 2 + 1];
4366 SQL_EscapeString(g_hDb, szUName, szName, sizeof(szName));
4367 Handle pack = CreateDataPack();
4368 WritePackCell(pack, client);
4369 WritePackCell(pack, zoneGrp);
4370 Format(szQuery, sizeof(szQuery), sql_insertBonus, szSteamId, szName, g_szMapName, FinalTime, zoneGrp);
4371 SQL_TQuery(g_hDb, SQL_insertBonusCallback, szQuery, pack, DBPrio_Low);
4372}
4373
4374public void SQL_insertBonusCallback(Handle owner, Handle hndl, const char[] error, any data)
4375{
4376 if (hndl == null)
4377 {
4378 LogError("[ckSurf] SQL Error (SQL_insertBonusCallback): %s", error);
4379 return;
4380 }
4381
4382 ResetPack(data);
4383 int client = ReadPackCell(data);
4384 int zgroup = ReadPackCell(data);
4385 CloseHandle(data);
4386
4387 db_viewMapRankBonus(client, zgroup, 1);
4388 // Change to update profile timer, if giving multiplier count or extra points for bonuses
4389 CalculatePlayerRank(client);
4390}
4391
4392public void db_updateBonus(int client, char szSteamId[32], char szUName[32], float FinalTime, int zoneGrp)
4393{
4394 char szQuery[1024];
4395 char szName[MAX_NAME_LENGTH * 2 + 1];
4396 Handle datapack = CreateDataPack();
4397 WritePackCell(datapack, client);
4398 WritePackCell(datapack, zoneGrp);
4399 SQL_EscapeString(g_hDb, szUName, szName, sizeof(szName));
4400 Format(szQuery, sizeof(szQuery), sql_updateBonus, FinalTime, szName, szSteamId, g_szMapName, zoneGrp);
4401 SQL_TQuery(g_hDb, SQL_updateBonusCallback, szQuery, datapack, DBPrio_Low);
4402}
4403
4404
4405public void SQL_updateBonusCallback(Handle owner, Handle hndl, const char[] error, any data)
4406{
4407 if (hndl == null)
4408 {
4409 LogError("[ckSurf] SQL Error (SQL_updateBonusCallback): %s", error);
4410 return;
4411 }
4412
4413 ResetPack(data);
4414 int client = ReadPackCell(data);
4415 int zgroup = ReadPackCell(data);
4416 CloseHandle(data);
4417
4418 db_viewMapRankBonus(client, zgroup, 2);
4419
4420 CalculatePlayerRank(client);
4421}
4422
4423public void SQL_deleteBonusCallback(Handle owner, Handle hndl, const char[] error, any data)
4424{
4425 if (hndl == null)
4426 {
4427 LogError("[ckSurf] SQL Error (SQL_deleteBonusCallback): %s", error);
4428 return;
4429 }
4430}
4431
4432public void db_selectBonusCount()
4433{
4434 char szQuery[258];
4435 Format(szQuery, sizeof(szQuery), sql_selectTotalBonusCount);
4436 SQL_TQuery(g_hDb, SQL_selectBonusCountCallback, szQuery, 1, DBPrio_Low);
4437}
4438
4439public void SQL_selectBonusCountCallback(Handle owner, Handle hndl, const char[] error, any data)
4440{
4441 if (hndl == null)
4442 {
4443 LogError("[ckSurf] SQL Error (SQL_selectBonusCountCallback): %s", error);
4444 return;
4445 }
4446
4447 if (SQL_HasResultSet(hndl))
4448 {
4449 char mapName[128];
4450 char mapName2[128];
4451 g_totalBonusCount = 0;
4452 while (SQL_FetchRow(hndl))
4453 {
4454 SQL_FetchString(hndl, 0, mapName2, 128);
4455 for (int i = 0; i < GetArraySize(g_MapList); i++)
4456 {
4457 GetArrayString(g_MapList, i, mapName, 128);
4458 if (StrEqual(mapName, mapName2, false))
4459 g_totalBonusCount++;
4460 }
4461 }
4462 }
4463 else
4464 {
4465 g_totalBonusCount = 0;
4466 }
4467 SetSkillGroups();
4468}
4469
4470////////////////////////////
4471//// SQL Zones /////////////
4472////////////////////////////
4473
4474public void db_setZoneNames(int client, char szName[128])
4475{
4476 char szQuery[512], szEscapedName[128 * 2 + 1];
4477 SQL_EscapeString(g_hDb, szName, szEscapedName, sizeof(szEscapedName));
4478 Handle pack = CreateDataPack();
4479 WritePackCell(pack, client);
4480 WritePackCell(pack, g_CurrentSelectedZoneGroup[client]);
4481 WritePackString(pack, szEscapedName);
4482 // UPDATE ck_zones SET zonename = '%s' WHERE mapname = '%s' AND zonegroup = '%i';
4483 Format(szQuery, sizeof(szQuery), sql_setZoneNames, szEscapedName, g_szMapName, g_CurrentSelectedZoneGroup[client]);
4484 SQL_TQuery(g_hDb, sql_setZoneNamesCallback, szQuery, pack, DBPrio_Low);
4485}
4486
4487public void sql_setZoneNamesCallback(Handle owner, Handle hndl, const char[] error, any data)
4488{
4489 if (hndl == null)
4490 {
4491 LogError("[ckSurf] SQL Error (sql_setZoneNamesCallback): %s", error);
4492 CloseHandle(data);
4493 return;
4494 }
4495
4496 char szName[64];
4497 ResetPack(data);
4498 int client = ReadPackCell(data);
4499 int zonegrp = ReadPackCell(data);
4500 ReadPackString(data, szName, 64);
4501 CloseHandle(data);
4502
4503 for (int i = 0; i < g_mapZonesCount; i++)
4504 {
4505 if (g_mapZones[i][zoneGroup] == zonegrp)
4506 Format(g_mapZones[i][zoneName], 64, szName);
4507 }
4508
4509 if (IsValidClient(client))
4510 {
4511 CPrintToChat(client, PREFIX_T, "bonus_name_changed", client);
4512 ListBonusSettings(client);
4513 }
4514 db_selectMapZones();
4515}
4516
4517public void db_checkAndFixZoneIds()
4518{
4519 char szQuery[512];
4520 //"SELECT mapname, zoneid, zonetype, zonetypeid, pointa_x, pointa_y, pointa_z, pointb_x, pointb_y, pointb_z, vis, team, zonegroup, zonename FROM ck_zones WHERE mapname = '%s' ORDER BY zoneid ASC";
4521 if (!g_szMapName[0])
4522 GetCurrentMap(g_szMapName, 128);
4523
4524 Format(szQuery, 512, sql_selectZoneIds, g_szMapName);
4525 SQL_TQuery(g_hDb, db_checkAndFixZoneIdsCallback, szQuery, 1, DBPrio_Low);
4526}
4527
4528public void db_checkAndFixZoneIdsCallback(Handle owner, Handle hndl, const char[] error, any data)
4529{
4530 if (hndl == null)
4531 {
4532 LogError("[ckSurf] SQL Error (db_checkAndFixZoneIdsCallback): %s", error);
4533 return;
4534 }
4535
4536 if (SQL_HasResultSet(hndl))
4537 {
4538 bool IDError = false;
4539 float x1[128], y1[128], z1[128], x2[128], y2[128], z2[128];
4540 int checker = 0, i, zonetype[128], zonetypeid[128], vis[128], team[128], zoneGrp[128];
4541 char zName[128][128];
4542
4543 while (SQL_FetchRow(hndl))
4544 {
4545 i = SQL_FetchInt(hndl, 1);
4546 zonetype[checker] = SQL_FetchInt(hndl, 2);
4547 zonetypeid[checker] = SQL_FetchInt(hndl, 3);
4548 x1[checker] = SQL_FetchFloat(hndl, 4);
4549 y1[checker] = SQL_FetchFloat(hndl, 5);
4550 z1[checker] = SQL_FetchFloat(hndl, 6);
4551 x2[checker] = SQL_FetchFloat(hndl, 7);
4552 y2[checker] = SQL_FetchFloat(hndl, 8);
4553 z2[checker] = SQL_FetchFloat(hndl, 9);
4554 vis[checker] = SQL_FetchInt(hndl, 10);
4555 team[checker] = SQL_FetchInt(hndl, 11);
4556 zoneGrp[checker] = SQL_FetchInt(hndl, 12);
4557 SQL_FetchString(hndl, 13, zName[checker], 128);
4558
4559 if (i != checker)
4560 IDError = true;
4561
4562 checker++;
4563 }
4564
4565 if (IDError)
4566 {
4567 char szQuery[256];
4568 Format(szQuery, sizeof(szQuery), sql_deleteMapZones, g_szMapName);
4569 SQL_FastQuery(g_hDb, szQuery);
4570
4571 for (int k = 0; k < checker; k++)
4572 {
4573 db_insertZoneCheap(k, zonetype[k], zonetypeid[k], x1[k], y1[k], z1[k], x2[k], y2[k], z2[k], vis[k], team[k], zoneGrp[k], zName[k], -10);
4574 }
4575 }
4576 }
4577 db_selectMapZones();
4578}
4579
4580public void db_deleteAllZones(int client)
4581{
4582 char szQuery[128];
4583 Format(szQuery, sizeof(szQuery), sql_deleteAllZones);
4584 SQL_TQuery(g_hDb, SQL_deleteAllZonesCallback, szQuery, client, DBPrio_Low);
4585}
4586
4587public void SQL_deleteAllZonesCallback(Handle owner, Handle hndl, const char[] error, any data)
4588{
4589 if (hndl == null)
4590 {
4591 LogError("[ckSurf] SQL Error (SQL_deleteAllZonesCallback): %s", error);
4592 return;
4593 }
4594
4595 Admin_InsertZonestoDatabase(data);
4596}
4597
4598public void ZoneDefaultName(int zonetype, int zonegroup, char zName[128])
4599{
4600 if (zonegroup > 0)
4601 {
4602 Format(zName, sizeof(zName), "BONUS %i", zonegroup);
4603 }
4604 else
4605 if (-1 < zonetype < ZONEAMOUNT)
4606 Format(zName, sizeof(zName), "%s %i", g_szZoneDefaultNames[zonetype], zonegroup);
4607 else Format(zName, sizeof(zName), "Unknown");
4608}
4609
4610public void db_insertZoneCheap(int zoneid, int zonetype, int zonetypeid, float pointax, float pointay, float pointaz, float pointbx, float pointby, float pointbz, int vis, int team, int zGrp, char zName[128], int query)
4611{
4612 char szQuery[1024];
4613 //"INSERT INTO ck_zones (mapname, zoneid, zonetype, zonetypeid, pointa_x, pointa_y, pointa_z, pointb_x, pointb_y, pointb_z, vis, team, zonegroup, zonename) VALUES ('%s', '%i', '%i', '%i', '%f', '%f', '%f', '%f', '%f', '%f', '%i', '%i', '%i', '%s')";
4614 Format(szQuery, sizeof(szQuery), sql_insertZones, g_szMapName, zoneid, zonetype, zonetypeid, pointax, pointay, pointaz, pointbx, pointby, pointbz, vis, team, zGrp, zName);
4615 SQL_TQuery(g_hDb, SQL_insertZonesCheapCallback, szQuery, query, DBPrio_Low);
4616}
4617
4618public void SQL_insertZonesCheapCallback(Handle owner, Handle hndl, const char[] error, any query)
4619{
4620 if (hndl == null)
4621 {
4622 CPrintToChatAll("[CK] Failed to create a zone, attempting a fix... Recreate the zone, please.");
4623 db_checkAndFixZoneIds();
4624 return;
4625 }
4626 if (query == (g_mapZonesCount - 1))
4627 db_selectMapZones();
4628}
4629
4630public void db_insertZone(int zoneid, int zonetype, int zonetypeid, float pointax, float pointay, float pointaz, float pointbx, float pointby, float pointbz, int vis, int team, int zonegroup)
4631{
4632 char szQuery[1024];
4633 char zName[128];
4634
4635 if (zonegroup == g_mapZoneGroupCount)
4636 ZoneDefaultName(zonetype, zonegroup, zName);
4637 else
4638 Format(zName, 128, g_szZoneGroupName[zonegroup]);
4639
4640 //"INSERT INTO ck_zones (mapname, zoneid, zonetype, zonetypeid, pointa_x, pointa_y, pointa_z, pointb_x, pointb_y, pointb_z, vis, team, zonegroup, zonename) VALUES ('%s', '%i', '%i', '%i', '%f', '%f', '%f', '%f', '%f', '%f', '%i', '%i', '%i', '%s')";
4641 Format(szQuery, sizeof(szQuery), sql_insertZones, g_szMapName, zoneid, zonetype, zonetypeid, pointax, pointay, pointaz, pointbx, pointby, pointbz, vis, team, zonegroup, zName);
4642 SQL_TQuery(g_hDb, SQL_insertZonesCallback, szQuery, 1, DBPrio_Low);
4643}
4644
4645public void SQL_insertZonesCallback(Handle owner, Handle hndl, const char[] error, any data)
4646{
4647 if (hndl == null)
4648 {
4649
4650 CPrintToChatAll("[CK] Failed to create a zone, attempting a fix... Recreate the zone, please.");
4651 db_checkAndFixZoneIds();
4652 return;
4653 }
4654
4655 db_selectMapZones();
4656}
4657
4658public void db_saveZones()
4659{
4660 char szQuery[258];
4661 Format(szQuery, sizeof(szQuery), sql_deleteMapZones, g_szMapName);
4662 SQL_TQuery(g_hDb, SQL_saveZonesCallBack, szQuery, 1, DBPrio_Low);
4663}
4664
4665public void SQL_saveZonesCallBack(Handle owner, Handle hndl, const char[] error, any data)
4666{
4667 if (hndl == null)
4668 {
4669 LogError("[ckSurf] SQL Error (SQL_saveZonesCallBack): %s", error);
4670 return;
4671 }
4672 char szzone[128];
4673 for (int i = 0; i < g_mapZonesCount; i++)
4674 {
4675 Format(szzone, 128, "%s", g_szZoneGroupName[g_mapZones[i][zoneGroup]]);
4676 if (g_mapZones[i][PointA][0] != -1.0 && g_mapZones[i][PointA][1] != -1.0 && g_mapZones[i][PointA][2] != -1.0)
4677 db_insertZoneCheap(g_mapZones[i][zoneId], g_mapZones[i][zoneType], g_mapZones[i][zoneTypeId], g_mapZones[i][PointA][0], g_mapZones[i][PointA][1], g_mapZones[i][PointA][2], g_mapZones[i][PointB][0], g_mapZones[i][PointB][1], g_mapZones[i][PointB][2], g_mapZones[i][Vis], g_mapZones[i][Team], g_mapZones[i][zoneGroup], szzone, i);
4678 }
4679}
4680
4681public void db_updateZone(int zoneid, int zonetype, int zonetypeid, float[] Point1, float[] Point2, int vis, int team, int zonegroup)
4682{
4683 char szQuery[1024];
4684 Format(szQuery, sizeof(szQuery), sql_updateZone, zonetype, zonetypeid, Point1[0], Point1[1], Point1[2], Point2[0], Point2[1], Point2[2], vis, team, zonegroup, zoneid, g_szMapName);
4685 SQL_TQuery(g_hDb, SQL_updateZoneCallback, szQuery, 1, DBPrio_Low);
4686}
4687
4688public void SQL_updateZoneCallback(Handle owner, Handle hndl, const char[] error, any data)
4689{
4690 if (hndl == null)
4691 {
4692 LogError("[ckSurf] SQL Error (SQL_updateZoneCallback): %s", error);
4693 return;
4694 }
4695
4696 db_selectMapZones();
4697}
4698
4699public int db_deleteZonesInGroup(int client)
4700{
4701 char szQuery[258];
4702
4703 if (g_CurrentSelectedZoneGroup[client] < 1)
4704 {
4705 if(IsValidClient(client))
4706 CPrintToChat(client, "[CK] Invalid zonegroup index selected, aborting. (%i)", g_CurrentSelectedZoneGroup[client]);
4707
4708 PrintToServer("[CK] Invalid zonegroup index selected, aborting. (%i)", g_CurrentSelectedZoneGroup[client]);
4709 }
4710
4711 Transaction h_DeleteZoneGroup = SQL_CreateTransaction();
4712
4713 Format(szQuery, 258, sql_deleteZonesInGroup, g_szMapName, g_CurrentSelectedZoneGroup[client]);
4714 SQL_AddQuery(h_DeleteZoneGroup, szQuery);
4715
4716 Format(szQuery, 258, "UPDATE ck_zones SET zonegroup = zonegroup-1 WHERE zonegroup > %i AND mapname = '%s';", g_CurrentSelectedZoneGroup[client], g_szMapName);
4717 SQL_AddQuery(h_DeleteZoneGroup, szQuery);
4718
4719 Format(szQuery, 258, "DELETE FROM ck_bonus WHERE zonegroup = %i AND mapname = '%s';", g_CurrentSelectedZoneGroup[client], g_szMapName);
4720 SQL_AddQuery(h_DeleteZoneGroup, szQuery);
4721
4722 Format(szQuery, 258, "UPDATE ck_bonus SET zonegroup = zonegroup-1 WHERE zonegroup > %i AND mapname = '%s';", g_CurrentSelectedZoneGroup[client], g_szMapName);
4723 SQL_AddQuery(h_DeleteZoneGroup, szQuery);
4724
4725 SQL_ExecuteTransaction(g_hDb, h_DeleteZoneGroup, SQLTxn_ZoneGroupRemovalSuccess, SQLTxn_ZoneGroupRemovalFailed, client);
4726
4727}
4728
4729public void SQLTxn_ZoneGroupRemovalSuccess(Handle db, any client, int numQueries, Handle[] results, any[] queryData)
4730{
4731 PrintToServer("[CK] Zonegroup removal was successful");
4732
4733 db_selectMapZones();
4734 db_viewFastestBonus();
4735 db_viewBonusTotalCount();
4736 db_viewRecordCheckpointInMap();
4737
4738 if (IsValidClient(client))
4739 {
4740 ZoneMenu(client);
4741 CPrintToChat(client, PREFIX_T, "zonegroup_deleted", client);
4742 }
4743 return;
4744}
4745
4746public void SQLTxn_ZoneGroupRemovalFailed(Handle db, any client, int numQueries, const char[] error, int failIndex, any[] queryData)
4747{
4748 if(IsValidClient(client))
4749 CPrintToChat(client, PREFIX_T, client, "zonegroup_deleted_failed", error);
4750
4751 PrintToServer("[CK] Zonegroup removal failed (Error: %s)", error);
4752 return;
4753}
4754
4755public void db_selectzoneTypeIds(int zonetype, int client, int zonegrp)
4756{
4757 char szQuery[258];
4758 Format(szQuery, 258, sql_selectzoneTypeIds, g_szMapName, zonetype, zonegrp);
4759 SQL_TQuery(g_hDb, SQL_selectzoneTypeIdsCallback, szQuery, client, DBPrio_Low);
4760}
4761
4762public void SQL_selectzoneTypeIdsCallback(Handle owner, Handle hndl, const char[] error, any data)
4763{
4764 if (hndl == null)
4765 {
4766 LogError("[ckSurf] SQL Error (SQL_selectzoneTypeIdsCallback): %s", error);
4767 return;
4768 }
4769
4770 if (SQL_HasResultSet(hndl))
4771 {
4772 int availableids[MAXZONES] = { 0, ... }, i;
4773 while (SQL_FetchRow(hndl))
4774 {
4775 i = SQL_FetchInt(hndl, 0);
4776 if (i < MAXZONES)
4777 availableids[i] = 1;
4778 }
4779 Menu TypeMenu = new Menu(Handle_EditZoneTypeId);
4780 char MenuNum[24], MenuInfo[6], MenuItemName[24];
4781 int x = 0;
4782 // Types: Start(1), End(2), Stage(3), Checkpoint(4), Speed(5), TeleToStart(6), Validator(7), Chekcer(8), Stop(0)
4783 switch (g_CurrentZoneType[data]) {
4784 case 0:Format(MenuItemName, 24, "Stop");
4785 case 1:Format(MenuItemName, 24, "Start");
4786 case 2:Format(MenuItemName, 24, "End");
4787 case 3: {
4788 Format(MenuItemName, 24, "Stage");
4789 x = 2;
4790 }
4791 case 4:Format(MenuItemName, 24, "Checkpoint");
4792 case 5:Format(MenuItemName, 24, "Speed");
4793 case 6:Format(MenuItemName, 24, "TeleToStart");
4794 case 7:Format(MenuItemName, 24, "Validator");
4795 case 8:Format(MenuItemName, 24, "Checker");
4796 default:Format(MenuItemName, 24, "Unknown");
4797 }
4798
4799 for (int k = 0; k < 35; k++)
4800 {
4801 if (availableids[k] == 0)
4802 {
4803 Format(MenuNum, sizeof(MenuNum), "%s-%i", MenuItemName, (k + x));
4804 Format(MenuInfo, sizeof(MenuInfo), "%i", k);
4805 TypeMenu.AddItem(MenuInfo, MenuNum);
4806 }
4807 }
4808 TypeMenu.ExitButton = true;
4809 TypeMenu.Display(data, MENU_TIME_FOREVER);
4810 }
4811}
4812/*
4813public checkZoneTypeIds()
4814{
4815 InitZoneVariables();
4816
4817 char szQuery[258];
4818 Format(szQuery, 258, "SELECT `zonegroup` ,`zonetype`, `zonetypeid` FROM `ck_zones` WHERE `mapname` = '%s';", g_szMapName);
4819 SQL_TQuery(g_hDb, checkZoneTypeIdsCallback, szQuery, 1, DBPrio_High);
4820}
4821
4822public checkZoneTypeIdsCallback(Handle owner, Handle hndl, const char[] error, any:data)
4823{
4824 if(hndl == null)
4825 {
4826 LogError("[ckSurf] SQL Error (checkZoneTypeIds): %s", error);
4827 return;
4828 }
4829 if(SQL_HasResultSet(hndl))
4830 {
4831 int idChecker[MAXZONEGROUPS][ZONEAMOUNT][MAXZONES], idCount[MAXZONEGROUPS][ZONEAMOUNT];
4832 char szQuery[258];
4833 // Fill array with id's
4834 // idChecker = map zones in
4835 while (SQL_FetchRow(hndl))
4836 {
4837 idChecker[SQL_FetchInt(hndl, 0)][SQL_FetchInt(hndl, 1)][SQL_FetchInt(hndl, 2)] = 1;
4838 idCount[SQL_FetchInt(hndl, 0)][SQL_FetchInt(hndl, 1)]++;
4839 }
4840 for (int i = 0; i < MAXZONEGROUPS; i++)
4841 {
4842 for (int j = 0; j < ZONEAMOUNT; j++)
4843 {
4844 for (int k = 0; k < idCount[i][j]; k++)
4845 {
4846 if (idChecker[i][j][k] == 1)
4847 continue;
4848 else
4849 {
4850 PrintToServer("[ckSurf] Error on zonetype: %i, zonetypeid: %i", i, idChecker[i][k]);
4851 Format(szQuery, 258, "UPDATE `ck_zones` SET zonetypeid = zonetypeid-1 WHERE mapname = '%s' AND zonetype = %i AND zonetypeid > %i AND zonegroup = %i;", g_szMapName, j, k, i);
4852 SQL_LockDatabase(g_hDb);
4853 SQL_FastQuery(g_hDb, szQuery);
4854 SQL_UnlockDatabase(g_hDb);
4855 }
4856 }
4857 }
4858 }
4859
4860 Format(szQuery, 258, "SELECT `zoneid` FROM `ck_zones` WHERE mapname = '%s' ORDER BY zoneid ASC;", g_szMapName);
4861 SQL_TQuery(g_hDb, checkZoneIdsCallback, szQuery, 1, DBPrio_High);
4862 }
4863}
4864
4865public checkZoneIdsCallback(Handle owner, Handle hndl, const char[] error, any:data)
4866{
4867 if(hndl == null)
4868 {
4869 LogError("[ckSurf] SQL Error (checkZoneIdsCallback): %s", error);
4870 return;
4871 }
4872
4873 if(SQL_HasResultSet(hndl))
4874 {
4875 int i = 0;
4876 char szQuery[258];
4877 while (SQL_FetchRow(hndl))
4878 {
4879 if (SQL_FetchInt(hndl, 0) == i)
4880 {
4881 i++;
4882 continue;
4883 }
4884 else
4885 {
4886 PrintToServer("[ckSurf] Found an error in ZoneID's. Fixing...");
4887 Format(szQuery, 258, "UPDATE `ck_zones` SET zoneid = %i WHERE mapname = '%s' AND zoneid = %i", i, g_szMapName, SQL_FetchInt(hndl, 0));
4888 SQL_LockDatabase(g_hDb);
4889 SQL_FastQuery(g_hDb, szQuery);
4890 SQL_UnlockDatabase(g_hDb);
4891 i++;
4892 }
4893 }
4894
4895 char szQuery2[258];
4896 Format(szQuery2, 258, "SELECT `zonegroup` FROM `ck_zones` WHERE `mapname` = '%s' ORDER BY `zonegroup` ASC;", g_szMapName);
4897 SQL_TQuery(g_hDb, checkZoneGroupIds, szQuery2, 1, DBPrio_Low);
4898 }
4899}
4900
4901public checkZoneGroupIds(Handle owner, Handle hndl, const char[] error, any:data)
4902{
4903 if(hndl == null)
4904 {
4905 LogError("[ckSurf] SQL Error (checkZoneGroupIds): %s", error);
4906 return;
4907 }
4908
4909 if(SQL_HasResultSet(hndl))
4910 {
4911 int i = 0;
4912 char szQuery[258];
4913 while (SQL_FetchRow(hndl))
4914 {
4915 if (SQL_FetchInt(hndl, 0) == i)
4916 continue;
4917 else if (SQL_FetchInt(hndl, 0) == (i+1))
4918 i++;
4919 else
4920 {
4921 i++;
4922 PrintToServer("[ckSurf] Found an error in zoneGroupID's. Fixing...");
4923 Format(szQuery, 258, "UPDATE `ck_zones` SET `zonegroup` = %i WHERE `mapname` = '%s' AND `zonegroup` = %i", i, g_szMapName, SQL_FetchInt(hndl, 0));
4924 SQL_LockDatabase(g_hDb);
4925 SQL_FastQuery(g_hDb, szQuery);
4926 SQL_UnlockDatabase(g_hDb);
4927 }
4928 }
4929 db_selectMapZones();
4930 }
4931}
4932*/
4933public void db_selectMapZones()
4934{
4935 char szQuery[258];
4936 Format(szQuery, 258, sql_selectMapZones, g_szMapName);
4937 SQL_TQuery(g_hDb, SQL_selectMapZonesCallback, szQuery, 1, DBPrio_High);
4938}
4939
4940public void SQL_selectMapZonesCallback(Handle owner, Handle hndl, const char[] error, any data)
4941{
4942 if (hndl == null)
4943 {
4944 LogError("[ckSurf] SQL Error (SQL_selectMapZonesCallback): %s", error);
4945 if (!g_bServerDataLoaded)
4946 db_GetMapRecord_Pro();
4947 return;
4948 }
4949
4950 RemoveZones();
4951
4952 if (SQL_HasResultSet(hndl))
4953 {
4954 g_mapZonesCount = 0;
4955 g_bhasStages = false;
4956 g_bhasBonus = false;
4957 g_mapZoneGroupCount = 0; // 1 = No Bonus, 2 = Bonus, >2 = Multiple bonuses
4958
4959 for (int i = 0; i < MAXZONES; i++)
4960 {
4961 resetZone(i);
4962 }
4963
4964 for (int x = 0; x < MAXZONEGROUPS; x++)
4965 {
4966 g_mapZoneCountinGroup[x] = 0;
4967 for (int k = 0; k < ZONEAMOUNT; k++)
4968 g_mapZonesTypeCount[x][k] = 0;
4969 }
4970
4971 int zoneIdChecker[MAXZONES], zoneTypeIdChecker[MAXZONEGROUPS][ZONEAMOUNT][MAXZONES], zoneTypeIdCheckerCount[MAXZONEGROUPS][ZONEAMOUNT], zoneGroupChecker[MAXZONEGROUPS];
4972
4973 // Types: Start(1), End(2), Stage(3), Checkpoint(4), Speed(5), TeleToStart(6), Validator(7), Chekcer(8), Stop(0)
4974 while (SQL_FetchRow(hndl))
4975 {
4976 g_mapZones[g_mapZonesCount][zoneId] = SQL_FetchInt(hndl, 0);
4977 g_mapZones[g_mapZonesCount][zoneType] = SQL_FetchInt(hndl, 1);
4978 g_mapZones[g_mapZonesCount][zoneTypeId] = SQL_FetchInt(hndl, 2);
4979 g_mapZones[g_mapZonesCount][PointA][0] = SQL_FetchFloat(hndl, 3);
4980 g_mapZones[g_mapZonesCount][PointA][1] = SQL_FetchFloat(hndl, 4);
4981 g_mapZones[g_mapZonesCount][PointA][2] = SQL_FetchFloat(hndl, 5);
4982 g_mapZones[g_mapZonesCount][PointB][0] = SQL_FetchFloat(hndl, 6);
4983 g_mapZones[g_mapZonesCount][PointB][1] = SQL_FetchFloat(hndl, 7);
4984 g_mapZones[g_mapZonesCount][PointB][2] = SQL_FetchFloat(hndl, 8);
4985 g_mapZones[g_mapZonesCount][Vis] = SQL_FetchInt(hndl, 9);
4986 g_mapZones[g_mapZonesCount][Team] = SQL_FetchInt(hndl, 10);
4987 g_mapZones[g_mapZonesCount][zoneGroup] = SQL_FetchInt(hndl, 11);
4988
4989
4990 /**
4991 * Initialize error checking
4992 * 0 = zone not found
4993 * 1 = zone found
4994 *
4995 * IDs must be in order 0, 1, 2.... n
4996 * Duplicate zoneids not possible due to primary key
4997 */
4998 zoneIdChecker[g_mapZones[g_mapZonesCount][zoneId]]++;
4999 if (zoneGroupChecker[g_mapZones[g_mapZonesCount][zoneGroup]] != 1)
5000 {
5001 // 1 = No Bonus, 2 = Bonus, >2 = Multiple bonuses
5002 g_mapZoneGroupCount++;
5003 zoneGroupChecker[g_mapZones[g_mapZonesCount][zoneGroup]] = 1;
5004 }
5005
5006 // You can have the same zonetype and zonetypeid values in different zonegroups
5007 zoneTypeIdChecker[g_mapZones[g_mapZonesCount][zoneGroup]][g_mapZones[g_mapZonesCount][zoneType]][g_mapZones[g_mapZonesCount][zoneTypeId]]++;
5008 zoneTypeIdCheckerCount[g_mapZones[g_mapZonesCount][zoneGroup]][g_mapZones[g_mapZonesCount][zoneType]]++;
5009
5010 SQL_FetchString(hndl, 12, g_mapZones[g_mapZonesCount][zoneName], 128);
5011
5012 if (!g_mapZones[g_mapZonesCount][zoneName][0])
5013 {
5014 switch (g_mapZones[g_mapZonesCount][zoneType])
5015 {
5016 case 0: {
5017 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "Stop-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5018 }
5019 case 1: {
5020 if (g_mapZones[g_mapZonesCount][zoneGroup] > 0)
5021 {
5022 g_bhasBonus = true;
5023 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "BonusStart-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5024 Format(g_szZoneGroupName[g_mapZones[g_mapZonesCount][zoneGroup]], 128, "BONUS %i", g_mapZones[g_mapZonesCount][zoneGroup]);
5025 }
5026 else
5027 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "Start-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5028 }
5029 case 2: {
5030 if (g_mapZones[g_mapZonesCount][zoneGroup] > 0)
5031 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "BonusEnd-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5032 else
5033 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "End-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5034 }
5035 case 3: {
5036 g_bhasStages = true;
5037 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "Stage-%i", (g_mapZones[g_mapZonesCount][zoneTypeId] + 2));
5038 }
5039 case 4: {
5040 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "Checkpoint-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5041 }
5042 case 5: {
5043 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "Speed-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5044 }
5045 case 6: {
5046 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "TeleToStart-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5047 }
5048 case 7: {
5049 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "Validator-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5050 }
5051 case 8: {
5052 Format(g_mapZones[g_mapZonesCount][zoneName], 128, "Checker-%i", g_mapZones[g_mapZonesCount][zoneTypeId]);
5053 }
5054 }
5055 }
5056 else
5057 {
5058 switch (g_mapZones[g_mapZonesCount][zoneType])
5059 {
5060 case 1:
5061 {
5062 if (g_mapZones[g_mapZonesCount][zoneGroup] > 0)
5063 g_bhasBonus = true;
5064 Format(g_szZoneGroupName[g_mapZones[g_mapZonesCount][zoneGroup]], 128, "%s", g_mapZones[g_mapZonesCount][zoneName]);
5065 }
5066 case 3:
5067 g_bhasStages = true;
5068
5069 }
5070 }
5071
5072 /**
5073 * Count zone center
5074 **/
5075 // Center
5076 float posA[3], posB[3], result[3];
5077 Array_Copy(g_mapZones[g_mapZonesCount][PointA], posA, 3);
5078 Array_Copy(g_mapZones[g_mapZonesCount][PointB], posB, 3);
5079 AddVectors(posA, posB, result);
5080 g_mapZones[g_mapZonesCount][CenterPoint][0] = FloatDiv(result[0], 2.0);
5081 g_mapZones[g_mapZonesCount][CenterPoint][1] = FloatDiv(result[1], 2.0);
5082 g_mapZones[g_mapZonesCount][CenterPoint][2] = FloatDiv(result[2], 2.0);
5083
5084 for (int i = 0; i < 3; i++)
5085 {
5086 g_fZoneCorners[g_mapZonesCount][0][i] = g_mapZones[g_mapZonesCount][PointA][i];
5087 g_fZoneCorners[g_mapZonesCount][7][i] = g_mapZones[g_mapZonesCount][PointB][i];
5088 }
5089
5090 // Zone counts:
5091 g_mapZonesTypeCount[g_mapZones[g_mapZonesCount][zoneGroup]][g_mapZones[g_mapZonesCount][zoneType]]++;
5092 g_mapZonesCount++;
5093 }
5094 // Count zone corners
5095 // https://forums.alliedmods.net/showpost.php?p=2006539&postcount=8
5096 for (int x = 0; x < g_mapZonesCount; x++)
5097 {
5098 for(int i = 1; i < 7; i++)
5099 {
5100 for(int j = 0; j < 3; j++)
5101 {
5102 g_fZoneCorners[x][i][j] = g_fZoneCorners[x][((i >> (2-j)) & 1) * 7][j];
5103 }
5104 }
5105 }
5106
5107 /**
5108 * Check for errors
5109 *
5110 * 1. ZoneId
5111 */
5112 char szQuery[258];
5113 for (int i = 0; i < g_mapZonesCount; i++)
5114 if (zoneIdChecker[i] == 0)
5115 {
5116 PrintToServer("[ckSurf] Found an error in zoneid : %i", i);
5117 Format(szQuery, sizeof(szQuery), "UPDATE `ck_zones` SET zoneid = zoneid-1 WHERE mapname = '%s' AND zoneid > %i", g_szMapName, i);
5118 PrintToServer("Query: %s", szQuery);
5119 SQL_TQuery(g_hDb, sql_zoneFixCallback, szQuery, -1, DBPrio_Low);
5120 return;
5121 }
5122
5123 // 2nd ZoneGroup
5124 for (int i = 0; i < g_mapZoneGroupCount; i++)
5125 if (zoneGroupChecker[i] == 0)
5126 {
5127 PrintToServer("[ckSurf] Found an error in zonegroup %i (ZoneGroups total: %i)", i, g_mapZoneGroupCount);
5128 Format(szQuery, sizeof(szQuery), "UPDATE `ck_zones` SET `zonegroup` = zonegroup-1 WHERE `mapname` = '%s' AND `zonegroup` > %i", g_szMapName, i);
5129 SQL_TQuery(g_hDb, sql_zoneFixCallback, szQuery, zoneGroupChecker[i], DBPrio_Low);
5130 return;
5131 }
5132
5133 // 3rd ZoneTypeId
5134 for (int i = 0; i < g_mapZoneGroupCount; i++)
5135 for (int k = 0; k < ZONEAMOUNT; k++)
5136 for (int x = 0; x < zoneTypeIdCheckerCount[i][k]; x++)
5137 if (zoneTypeIdChecker[i][k][x] != 1 && (k == 3) || (k == 4))
5138 {
5139 if (zoneTypeIdChecker[i][k][x] == 0)
5140 {
5141 PrintToServer("[ckSurf] ZoneTypeID missing! [ZoneGroup: %i ZoneType: %i, ZonetypeId: %i]", i, k, x);
5142 Format(szQuery, 258, "UPDATE `ck_zones` SET zonetypeid = zonetypeid-1 WHERE mapname = '%s' AND zonetype = %i AND zonetypeid > %i AND zonegroup = %i;", g_szMapName, k, x, i);
5143 SQL_TQuery(g_hDb, sql_zoneFixCallback, szQuery, -1, DBPrio_Low);
5144 return;
5145 }
5146 else if (zoneTypeIdChecker[i][k][x] > 1)
5147 {
5148 char szerror[258];
5149 Format(szerror, 258, "[ckSurf] Duplicate Stage Zone ID's on %s [ZoneGroup: %i, ZoneType: 3, ZoneTypeId: %i]", g_szMapName, k, x);
5150 LogError(szerror);
5151 }
5152 }
5153
5154 RefreshZones();
5155
5156 // Set mapzone count in group
5157 for (int x = 0; x < g_mapZoneGroupCount; x++)
5158 for (int k = 0; k < ZONEAMOUNT; k++)
5159 if (g_mapZonesTypeCount[x][k] > 0)
5160 g_mapZoneCountinGroup[x]++;
5161
5162 if (!g_bServerDataLoaded)
5163 db_GetMapRecord_Pro();
5164
5165 return;
5166 }
5167}
5168
5169public void sql_zoneFixCallback(Handle owner, Handle hndl, const char[] error, any zongeroup)
5170{
5171 if (hndl == null)
5172 {
5173 LogError("[ckSurf] SQL Error (sql_zoneFixCallback): %s", error);
5174 return;
5175 }
5176 if (zongeroup == -1)
5177 {
5178 db_selectMapZones();
5179 }
5180 else
5181 {
5182 char szQuery[258];
5183 Format(szQuery, sizeof(szQuery), "DELETE FROM `ck_bonus` WHERE `mapname` = '%s' AND `zonegroup` = %i;", g_szMapName, zongeroup);
5184 SQL_TQuery(g_hDb, sql_zoneFixCallback2, szQuery, zongeroup, DBPrio_Low);
5185 }
5186}
5187
5188public void sql_zoneFixCallback2(Handle owner, Handle hndl, const char[] error, any zongeroup)
5189{
5190 if (hndl == null)
5191 {
5192 LogError("[ckSurf] SQL Error (sql_zoneFixCallback2): %s", error);
5193 return;
5194 }
5195
5196 char szQuery[258];
5197 Format(szQuery, sizeof(szQuery), "UPDATE ck_bonus SET zonegroup = zonegroup-1 WHERE `mapname` = '%s' AND `zonegroup` = %i;", g_szMapName, zongeroup);
5198 SQL_TQuery(g_hDb, sql_zoneFixCallback, szQuery, -1, DBPrio_Low);
5199}
5200
5201public void db_deleteMapZones()
5202{
5203 char szQuery[258];
5204 Format(szQuery, sizeof(szQuery), sql_deleteMapZones, g_szMapName);
5205 SQL_TQuery(g_hDb, SQL_deleteMapZonesCallback, szQuery, 1, DBPrio_Low);
5206}
5207
5208public void SQL_deleteMapZonesCallback(Handle owner, Handle hndl, const char[] error, any data)
5209{
5210 if (hndl == null)
5211 {
5212 LogError("[ckSurf] SQL Error (SQL_deleteMapZonesCallback): %s", error);
5213 return;
5214 }
5215}
5216
5217public void db_deleteZone(int client, int zoneid)
5218{
5219 char szQuery[258];
5220 Transaction h_deleteZone = SQL_CreateTransaction();
5221
5222 Format(szQuery, sizeof(szQuery), sql_deleteZone, g_szMapName, zoneid);
5223 SQL_AddQuery(h_deleteZone, szQuery);
5224
5225 Format(szQuery, sizeof(szQuery), "UPDATE ck_zones SET zoneid = zoneid-1 WHERE mapname = '%s' AND zoneid > %i", g_szMapName, zoneid);
5226 SQL_AddQuery(h_deleteZone, szQuery);
5227
5228 SQL_ExecuteTransaction(g_hDb, h_deleteZone, SQLTxn_ZoneRemovalSuccess, SQLTxn_ZoneRemovalFailed, client);
5229}
5230
5231public void SQLTxn_ZoneRemovalSuccess(Handle db, any client, int numQueries, Handle[] results, any[] queryData)
5232{
5233 if (IsValidClient(client))
5234 CPrintToChat(client, PREFIX_T, "zone_deleted", client);
5235 PrintToServer("[ckSurf] Zone Removed Succesfully");
5236}
5237
5238public void SQLTxn_ZoneRemovalFailed(Handle db, any client, int numQueries, const char[] error, int failIndex, any[] queryData)
5239{
5240 if (IsValidClient(client))
5241 CPrintToChat(client, PREFIX_T, "zone_deleted_failed", client, error);
5242 PrintToServer("[ckSurf] Zone Removal Failed. Error: %s", error);
5243 return;
5244}
5245
5246///////////////////////
5247//// MISC /////////////
5248///////////////////////
5249
5250public void db_insertLastPosition(int client, char szMapName[128], int stage, int zgroup)
5251{
5252 if (GetConVarBool(g_hcvarRestore) && !g_bRoundEnd && (StrContains(g_szSteamID[client], "STEAM_") != -1) && g_bTimeractivated[client])
5253 {
5254 Handle pack = CreateDataPack();
5255 WritePackCell(pack, client);
5256 WritePackString(pack, szMapName);
5257 WritePackString(pack, g_szSteamID[client]);
5258 WritePackCell(pack, stage);
5259 WritePackCell(pack, zgroup);
5260 char szQuery[512];
5261 Format(szQuery, sizeof(szQuery), "SELECT * FROM ck_playertemp WHERE steamid = '%s'", g_szSteamID[client]);
5262 SQL_TQuery(g_hDb, db_insertLastPositionCallback, szQuery, pack, DBPrio_Low);
5263 }
5264}
5265
5266public void db_insertLastPositionCallback(Handle owner, Handle hndl, const char[] error, any data)
5267{
5268 if (hndl == null)
5269 {
5270 LogError("[ckSurf] SQL Error (db_insertLastPositionCallback): %s", error);
5271 return;
5272 }
5273
5274 char szQuery[1024];
5275 char szMapName[128];
5276 char szSteamID[32];
5277
5278 ResetPack(data);
5279 int client = ReadPackCell(data);
5280 ReadPackString(data, szMapName, 128);
5281 ReadPackString(data, szSteamID, 32);
5282 int stage = ReadPackCell(data);
5283 int zgroup = ReadPackCell(data);
5284 CloseHandle(data);
5285
5286 if (1 <= client <= MaxClients)
5287 {
5288 if (!g_bTimeractivated[client])
5289 g_fPlayerLastTime[client] = -1.0;
5290 int tickrate = g_Server_Tickrate * 5 * 11;
5291 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
5292 {
5293 Format(szQuery, sizeof(szQuery), sql_updatePlayerTmp, g_fPlayerCordsLastPosition[client][0], g_fPlayerCordsLastPosition[client][1], g_fPlayerCordsLastPosition[client][2], g_fPlayerAnglesLastPosition[client][0], g_fPlayerAnglesLastPosition[client][1], g_fPlayerAnglesLastPosition[client][2], g_fPlayerLastTime[client], szMapName, tickrate, stage, zgroup, szSteamID);
5294 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
5295 }
5296 else
5297 {
5298 Format(szQuery, sizeof(szQuery), sql_insertPlayerTmp, g_fPlayerCordsLastPosition[client][0], g_fPlayerCordsLastPosition[client][1], g_fPlayerCordsLastPosition[client][2], g_fPlayerAnglesLastPosition[client][0], g_fPlayerAnglesLastPosition[client][1], g_fPlayerAnglesLastPosition[client][2], g_fPlayerLastTime[client], szSteamID, szMapName, tickrate, stage, zgroup);
5299 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
5300 }
5301 }
5302}
5303
5304public void db_deletePlayerTmps()
5305{
5306 char szQuery[64];
5307 Format(szQuery, sizeof(szQuery), "delete FROM ck_playertemp");
5308 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
5309}
5310
5311public void db_ViewLatestRecords(int client)
5312{
5313 SQL_TQuery(g_hDb, sql_selectLatestRecordsCallback, sql_selectLatestRecords, client, DBPrio_Low);
5314}
5315
5316public void sql_selectLatestRecordsCallback(Handle owner, Handle hndl, const char[] error, any data)
5317{
5318 if (hndl == null)
5319 {
5320 LogError("[ckSurf] SQL Error (sql_selectLatestRecordsCallback): %s", error);
5321 return;
5322 }
5323
5324 char szName[64];
5325 char szMapName[64];
5326 char szDate[64];
5327 char szTime[32];
5328 float ftime;
5329 PrintToConsole(data, "----------------------------------------------------------------------------------------------------");
5330 PrintToConsole(data, "Last map records:");
5331 if (SQL_HasResultSet(hndl))
5332 {
5333 int i = 1;
5334 while (SQL_FetchRow(hndl))
5335 {
5336 SQL_FetchString(hndl, 0, szName, sizeof(szName));
5337 ftime = SQL_FetchFloat(hndl, 1);
5338 FormatTimeFloat(data, ftime, 3, szTime, sizeof(szTime));
5339 SQL_FetchString(hndl, 2, szMapName, 64);
5340 SQL_FetchString(hndl, 3, szDate, 64);
5341 PrintToConsole(data, "%s: %s on %s - Time %s", szDate, szName, szMapName, szTime);
5342 i++;
5343 }
5344 if (i == 1)
5345 PrintToConsole(data, "No records found.");
5346 }
5347 else PrintToConsole(data, "No records found.");
5348 PrintToConsole(data, "----------------------------------------------------------------------------------------------------");
5349 CPrintToChat(data, PREFIX_t, "see_console");
5350}
5351
5352
5353public void db_InsertLatestRecords(char szSteamID[32], char szName[32], float FinalTime)
5354{
5355 char szQuery[512];
5356 Format(szQuery, 512, sql_insertLatestRecords, szSteamID, szName, FinalTime, g_szMapName);
5357 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
5358}
5359
5360public void GetDBName(int client, char szSteamId[32])
5361{
5362 char szQuery[512];
5363 Format(szQuery, 512, sql_selectRankedPlayer, szSteamId);
5364 SQL_TQuery(g_hDb, GetDBNameCallback, szQuery, client, DBPrio_Low);
5365}
5366
5367public void GetDBNameCallback(Handle owner, Handle hndl, const char[] error, any data)
5368{
5369 if (hndl == null)
5370 {
5371 LogError("[ckSurf] SQL Error (GetDBNameCallback): %s", error);
5372 return;
5373 }
5374
5375 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
5376 {
5377 SQL_FetchString(hndl, 1, g_szProfileName[data], MAX_NAME_LENGTH);
5378 db_viewPlayerAll(data, g_szProfileName[data]);
5379 }
5380}
5381
5382public void db_CalcAvgRunTime()
5383{
5384 char szQuery[256];
5385 Format(szQuery, 256, sql_selectAllMapTimesinMap, g_szMapName);
5386 SQL_TQuery(g_hDb, SQL_db_CalcAvgRunTimeCallback, szQuery, DBPrio_Low);
5387}
5388
5389public void SQL_db_CalcAvgRunTimeCallback(Handle owner, Handle hndl, const char[] error, any data)
5390{
5391 if (hndl == null)
5392 {
5393 LogError("[ckSurf] SQL Error (SQL_db_CalcAvgRunTimeCallback): %s", error);
5394
5395 if (!g_bServerDataLoaded && g_bhasBonus)
5396 db_CalcAvgRunTimeBonus();
5397 else if (!g_bServerDataLoaded)
5398 db_CalculatePlayerCount();
5399
5400 return;
5401 }
5402
5403 g_favg_maptime = 0.0;
5404 if (SQL_HasResultSet(hndl))
5405 {
5406 int rowcount = SQL_GetRowCount(hndl);
5407 int i, protimes;
5408 float ProTime;
5409 while (SQL_FetchRow(hndl))
5410 {
5411 float pro = SQL_FetchFloat(hndl, 0);
5412 if (pro > 0.0)
5413 {
5414 ProTime += pro;
5415 protimes++;
5416 }
5417 i++;
5418 if (rowcount == i)
5419 {
5420 g_favg_maptime = ProTime / protimes;
5421 }
5422 }
5423 }
5424
5425 if (g_bhasBonus)
5426 db_CalcAvgRunTimeBonus();
5427 else
5428 db_CalculatePlayerCount();
5429}
5430public void db_CalcAvgRunTimeBonus()
5431{
5432 char szQuery[256];
5433 Format(szQuery, 256, sql_selectAllBonusTimesinMap, g_szMapName);
5434 SQL_TQuery(g_hDb, SQL_db_CalcAvgRunBonusTimeCallback, szQuery, 1, DBPrio_Low);
5435}
5436
5437public void SQL_db_CalcAvgRunBonusTimeCallback(Handle owner, Handle hndl, const char[] error, any data)
5438{
5439 if (hndl == null)
5440 {
5441 LogError("[ckSurf] SQL Error (SQL_db_CalcAvgRunTimeCallback): %s", error);
5442 if (!g_bServerDataLoaded)
5443 db_CalculatePlayerCount();
5444 return;
5445 }
5446
5447 for (int i = 1; i < MAXZONEGROUPS; i++)
5448 g_fAvg_BonusTime[i] = 0.0;
5449
5450 if (SQL_HasResultSet(hndl))
5451 {
5452 int zonegroup, runtimes[MAXZONEGROUPS];
5453 float runtime[MAXZONEGROUPS], time;
5454 while (SQL_FetchRow(hndl))
5455 {
5456 zonegroup = SQL_FetchInt(hndl, 0);
5457 time = SQL_FetchFloat(hndl, 1);
5458 if (time > 0.0)
5459 {
5460 runtime[zonegroup] += time;
5461 runtimes[zonegroup]++;
5462 }
5463 }
5464
5465 for (int i = 1; i < MAXZONEGROUPS; i++)
5466 g_fAvg_BonusTime[i] = runtime[i] / runtimes[i];
5467 }
5468
5469 if (!g_bServerDataLoaded)
5470 db_CalculatePlayerCount();
5471
5472 return;
5473}
5474
5475public void db_GetDynamicTimelimit()
5476{
5477 if (!GetConVarBool(g_hDynamicTimelimit))
5478 {
5479 if (!g_bServerDataLoaded)
5480 loadAllClientSettings();
5481 return;
5482 }
5483 char szQuery[256];
5484 Format(szQuery, 256, sql_selectAllMapTimesinMap, g_szMapName);
5485 SQL_TQuery(g_hDb, SQL_db_GetDynamicTimelimitCallback, szQuery, DBPrio_Low);
5486}
5487
5488
5489public void SQL_db_GetDynamicTimelimitCallback(Handle owner, Handle hndl, const char[] error, any data)
5490{
5491 if (hndl == null)
5492 {
5493 LogError("[ckSurf] SQL Error (SQL_db_GetDynamicTimelimitCallback): %s", error);
5494 loadAllClientSettings();
5495 return;
5496 }
5497
5498 if (SQL_HasResultSet(hndl))
5499 {
5500 int maptimes = 0;
5501 float total = 0.0, time = 0.0;
5502 while (SQL_FetchRow(hndl))
5503 {
5504 time = SQL_FetchFloat(hndl, 0);
5505 if (time > 0.0)
5506 {
5507 total += time;
5508 maptimes++;
5509 }
5510 }
5511 //requires min. 5 map times
5512 if (maptimes > 5)
5513 {
5514 int scale_factor = 3;
5515 int avg = RoundToNearest((total) / 60.0 / float(maptimes));
5516
5517 //scale factor
5518 if (avg <= 10)
5519 scale_factor = 5;
5520 if (avg <= 5)
5521 scale_factor = 8;
5522 if (avg <= 3)
5523 scale_factor = 10;
5524 if (avg <= 2)
5525 scale_factor = 12;
5526 if (avg <= 1)
5527 scale_factor = 14;
5528
5529 avg = avg * scale_factor;
5530
5531 //timelimit: min 20min, max 120min
5532 if (avg < 20)
5533 avg = 20;
5534 if (avg > 120)
5535 avg = 120;
5536
5537 //set timelimit
5538 char szTimelimit[32];
5539 Format(szTimelimit, 32, "mp_timelimit %i;mp_roundtime %i", avg, avg);
5540 ServerCommand(szTimelimit);
5541 ServerCommand("mp_restartgame 1");
5542 }
5543 else
5544 ServerCommand("mp_timelimit 50");
5545 }
5546
5547 if (!g_bServerDataLoaded)
5548 loadAllClientSettings();
5549
5550 return;
5551}
5552
5553
5554public void db_CalculatePlayerCount()
5555{
5556 char szQuery[255];
5557 Format(szQuery, 255, sql_CountRankedPlayers);
5558 SQL_TQuery(g_hDb, sql_CountRankedPlayersCallback, szQuery, DBPrio_Low);
5559}
5560
5561public void db_CalculatePlayersCountGreater0()
5562{
5563 char szQuery[255];
5564 Format(szQuery, 255, sql_CountRankedPlayers2);
5565 SQL_TQuery(g_hDb, sql_CountRankedPlayers2Callback, szQuery, DBPrio_Low);
5566}
5567
5568
5569
5570public void sql_CountRankedPlayersCallback(Handle owner, Handle hndl, const char[] error, any data)
5571{
5572 if (hndl == null)
5573 {
5574 LogError("[ckSurf] SQL Error (sql_CountRankedPlayersCallback): %s", error);
5575 db_CalculatePlayersCountGreater0();
5576 return;
5577 }
5578
5579 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
5580 {
5581 g_pr_AllPlayers = SQL_FetchInt(hndl, 0);
5582 }
5583 else
5584 g_pr_AllPlayers = 1;
5585
5586 //get amount of players with actual player points
5587 db_CalculatePlayersCountGreater0();
5588 return;
5589}
5590
5591public void sql_CountRankedPlayers2Callback(Handle owner, Handle hndl, const char[] error, any data)
5592{
5593 if (hndl == null)
5594 {
5595 LogError("[ckSurf] SQL Error (sql_CountRankedPlayers2Callback): %s", error);
5596 if (!g_bServerDataLoaded)
5597 db_selectSpawnLocations();
5598 return;
5599 }
5600
5601 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
5602 {
5603 g_pr_RankedPlayers = SQL_FetchInt(hndl, 0);
5604 }
5605 else
5606 g_pr_RankedPlayers = 0;
5607
5608 if (!g_bServerDataLoaded)
5609 db_selectSpawnLocations();
5610
5611 return;
5612}
5613
5614
5615public void db_ClearLatestRecords()
5616{
5617 if (g_DbType == MYSQL)
5618 SQL_TQuery(g_hDb, SQL_CheckCallback, "DELETE FROM ck_latestrecords WHERE date < NOW() - INTERVAL 1 WEEK", DBPrio_Low);
5619 else
5620 SQL_TQuery(g_hDb, SQL_CheckCallback, "DELETE FROM ck_latestrecords WHERE date <= date('now','-7 day')", DBPrio_Low);
5621
5622 if (!g_bServerDataLoaded)
5623 db_GetDynamicTimelimit();
5624}
5625
5626public void db_viewUnfinishedMaps(int client, char szSteamId[32])
5627{
5628 if (IsValidClient(client))
5629 {
5630 CPrintToChat(client, PREFIX_t, "CheckConsole_Output");
5631 ProfileMenu(client, -1);
5632 }
5633 else
5634 return;
5635
5636 char szQuery[720];
5637 // Gets all players unfinished maps and bonuses from the database
5638 Format(szQuery, sizeof(szQuery), "SELECT mapname, zonegroup, zonename FROM ck_zones a WHERE (zonetype = 1 OR zonetype = 5) AND (SELECT runtimepro FROM ck_playertimes b WHERE b.mapname = a.mapname AND a.zonegroup = 0 AND steamid = '%s' UNION SELECT runtime FROM ck_bonus c WHERE c.mapname = a.mapname AND c.zonegroup = a.zonegroup AND steamid = '%s') IS NULL GROUP BY mapname, zonegroup ORDER BY mapname, zonegroup ASC;", szSteamId, szSteamId);
5639 SQL_TQuery(g_hDb, db_viewUnfinishedMapsCallback, szQuery, client, DBPrio_Low);
5640}
5641
5642public void db_viewUnfinishedMapsCallback(Handle owner, Handle hndl, const char[] error, any client)
5643{
5644 if (hndl == null)
5645 {
5646 LogError("[ckSurf] SQL Error (db_viewUnfinishedMapsCallback): %s", error);
5647 return;
5648 }
5649
5650 if (SQL_HasResultSet(hndl))
5651 {
5652 char szMap[128], szMap2[128], tmpMap[128], consoleString[1024], unfinishedBonusBuffer[772], zName[128];
5653 bool mapUnfinished, bonusUnfinished;
5654 int zGrp, count, mapCount, bonusCount, mapListSize = GetArraySize(g_MapList), digits;
5655 float time = 0.5;
5656 while (SQL_FetchRow(hndl))
5657 {
5658 // Get the map and check that it is in the mapcycle
5659 SQL_FetchString(hndl, 0, szMap, sizeof(szMap));
5660 for (int i = 0; i < mapListSize; i++)
5661 {
5662 GetArrayString(g_MapList, i, szMap2, sizeof(szMap2));
5663 if (StrEqual(szMap, szMap2, false))
5664 {
5665 // Map is in the mapcycle, and is unfinished
5666
5667 // Initialize the name
5668 if (!tmpMap[0])
5669 strcopy(tmpMap, sizeof(tmpMap), szMap);
5670
5671 // Check if the map changed, if so announce to client's console
5672 if (!StrEqual(szMap, tmpMap, false))
5673 {
5674 if (count < 10)
5675 digits = 1;
5676 else
5677 if (count < 100)
5678 digits = 2;
5679 else
5680 digits = 3;
5681
5682 if (strlen(tmpMap) < (13-digits)) // <- 11
5683 Format(tmpMap, sizeof(tmpMap), "%s:\t\t\t\t", tmpMap);
5684 else
5685 if ((12-digits) < strlen(tmpMap) < (21-digits)) // 12 - 19
5686 Format(tmpMap, sizeof(tmpMap), "%s:\t\t\t", tmpMap);
5687 else
5688 if ((20-digits) < strlen(tmpMap) < (28-digits)) // 20 - 25
5689 Format(tmpMap, sizeof(tmpMap), "%s:\t\t", tmpMap);
5690 else
5691 Format(tmpMap, sizeof(tmpMap), "%s:\t", tmpMap);
5692
5693 count++;
5694 if (!mapUnfinished) // Only bonus is unfinished
5695 Format(consoleString, sizeof(consoleString), "%i. %s\t\t| %s", count, tmpMap, unfinishedBonusBuffer);
5696 else
5697 if (!bonusUnfinished) // Only map is unfinished
5698 Format(consoleString, sizeof(consoleString), "%i. %sMap unfinished\t|", count, tmpMap);
5699 else // Both unfinished
5700 Format(consoleString, sizeof(consoleString), "%i. %sMap unfinished\t| %s", count, tmpMap, unfinishedBonusBuffer);
5701
5702 // Throttle messages to not cause errors on huge mapcycles
5703 time = time + 0.1;
5704 Handle pack = CreateDataPack();
5705 WritePackCell(pack, client);
5706 WritePackString(pack, consoleString);
5707 CreateTimer(time, PrintUnfinishedLine, pack);
5708
5709 mapUnfinished = false;
5710 bonusUnfinished = false;
5711 consoleString[0] = '\0';
5712 unfinishedBonusBuffer[0] = '\0';
5713 strcopy(tmpMap, sizeof(tmpMap), szMap);
5714 }
5715
5716 zGrp = SQL_FetchInt(hndl, 1);
5717 if (zGrp < 1)
5718 {
5719 mapUnfinished = true;
5720 mapCount++;
5721 }
5722 else
5723 {
5724 SQL_FetchString(hndl, 2, zName, sizeof(zName));
5725
5726 if (!zName[0])
5727 Format(zName, 128, "BONUS %i", zGrp);
5728
5729 if (bonusUnfinished)
5730 Format(unfinishedBonusBuffer, sizeof(unfinishedBonusBuffer), "%s, %s", unfinishedBonusBuffer, zName);
5731 else
5732 {
5733 bonusUnfinished = true;
5734 Format(unfinishedBonusBuffer, sizeof(unfinishedBonusBuffer), "Bonus: %s", zName);
5735 }
5736 bonusCount++;
5737 }
5738 break;
5739 }
5740 }
5741 }
5742 if (IsValidClient(client))
5743 {
5744 PrintToConsole(client, " ");
5745 PrintToConsole(client, "------- User Stats -------");
5746 PrintToConsole(client, "%i unfinished maps of total %i maps", mapCount, g_pr_MapCount);
5747 PrintToConsole(client, "%i unfinished bonuses", bonusCount);
5748 PrintToConsole(client, "SteamID: %s", g_szProfileSteamId[client]);
5749 PrintToConsole(client, "--------------------------");
5750 PrintToConsole(client, " ");
5751 PrintToConsole(client, "------------------------------ Map Details -----------------------------");
5752 }
5753 }
5754 return;
5755}
5756public Action PrintUnfinishedLine(Handle timer, any pack)
5757{
5758 ResetPack(pack);
5759 int client = ReadPackCell(pack);
5760 char teksti[1024];
5761 ReadPackString(pack, teksti, sizeof(teksti));
5762 CloseHandle(pack);
5763 PrintToConsole(client, teksti);
5764
5765}
5766
5767/*
5768void PrintUnfinishedLine(Handle pack)
5769{
5770 ResetPack(pack);
5771 int client = ReadPackCell(pack);
5772 char teksti[1024];
5773 ReadPackString(pack, teksti, 1024);
5774 CloseHandle(pack);
5775 PrintToConsole(client, teksti);
5776}
5777*/
5778public void db_viewPlayerProfile1(int client, char szPlayerName[MAX_NAME_LENGTH])
5779{
5780 char szQuery[512];
5781 char szName[MAX_NAME_LENGTH * 2 + 1];
5782 SQL_EscapeString(g_hDb, szPlayerName, szName, sizeof(szName));
5783 Format(szQuery, sizeof(szQuery), sql_selectPlayerRankAll2, szName);
5784 Handle pack = CreateDataPack();
5785 WritePackCell(pack, client);
5786 WritePackString(pack, szPlayerName);
5787 SQL_TQuery(g_hDb, SQL_ViewPlayerProfile1Callback, szQuery, pack, DBPrio_Low);
5788}
5789
5790public void SQL_ViewPlayerProfile1Callback(Handle owner, Handle hndl, const char[] error, any data)
5791{
5792 if (hndl == null)
5793 {
5794 LogError("[ckSurf] SQL Error (SQL_ViewPlayerProfile1Callback): %s", error);
5795 return;
5796 }
5797 char szPlayerName[MAX_NAME_LENGTH];
5798
5799 ResetPack(data);
5800 int client = ReadPackCell(data);
5801 ReadPackString(data, szPlayerName, sizeof(szPlayerName));
5802 CloseHandle(data);
5803
5804 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
5805 {
5806 SQL_FetchString(hndl, 1, g_szProfileSteamId[client], sizeof(g_szProfileSteamId[]));
5807 db_viewPlayerRank(client, g_szProfileSteamId[client]);
5808 }
5809 else
5810 {
5811 char szQuery[512];
5812 char szName[MAX_NAME_LENGTH * 2 + 1];
5813 SQL_EscapeString(g_hDb, szPlayerName, szName, sizeof(szName));
5814 Format(szQuery, sizeof(szQuery), sql_selectPlayerRankAll, PERCENT, szName, PERCENT);
5815 SQL_TQuery(g_hDb, SQL_ViewPlayerProfile2Callback, szQuery, client, DBPrio_Low);
5816 }
5817}
5818
5819
5820public void sql_selectPlayerNameCallback(Handle owner, Handle hndl, const char[] error, any data)
5821{
5822 if (hndl == null)
5823 {
5824 LogError("[ckSurf] SQL Error (sql_selectPlayerNameCallback): %s", error);
5825 return;
5826 }
5827
5828 ResetPack(data);
5829 int clientid = ReadPackCell(data);
5830 int client = ReadPackCell(data);
5831 CloseHandle(data);
5832
5833 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
5834 {
5835 SQL_FetchString(hndl, 0, g_pr_szName[clientid], sizeof(g_pr_szName[]));
5836 g_bProfileRecalc[clientid] = true;
5837 if (IsValidClient(client))
5838 PrintToConsole(client, "Profile refreshed (%s).", g_pr_szSteamID[clientid]);
5839 }
5840 else
5841 if (IsValidClient(client))
5842 PrintToConsole(client, "SteamID %s not found.", g_pr_szSteamID[clientid]);
5843}
5844
5845//
5846// 0. Admins counting players points starts here
5847//
5848public void RefreshPlayerRankTable(int max)
5849{
5850 g_pr_Recalc_ClientID = 1;
5851 g_pr_RankingRecalc_InProgress = true;
5852 char szQuery[255];
5853
5854 //SELECT steamid, name from ck_playerrank where points > 0 ORDER BY points DESC";
5855 Format(szQuery, sizeof(szQuery), sql_selectRankedPlayers);
5856 SQL_TQuery(g_hDb, sql_selectRankedPlayersCallback, szQuery, max, DBPrio_Low);
5857}
5858
5859public void sql_selectRankedPlayersCallback(Handle owner, Handle hndl, const char[] error, any data)
5860{
5861 if (hndl == null)
5862 {
5863 LogError("[ckSurf] SQL Error (sql_selectRankedPlayersCallback): %s", error);
5864 return;
5865 }
5866
5867 if (SQL_HasResultSet(hndl))
5868 {
5869 int i = 66;
5870 int x;
5871 g_pr_TableRowCount = SQL_GetRowCount(hndl);
5872 if (g_pr_TableRowCount == 0)
5873 {
5874 for (int c = 1; c <= MaxClients; c++)
5875 if (1 <= c <= MaxClients && IsValidEntity(c) && IsValidClient(c))
5876 {
5877 if (g_bManualRecalc)
5878 CPrintToChat(c, PREFIX_t, "PrUpdateFinished");
5879 }
5880 g_bManualRecalc = false;
5881 g_pr_RankingRecalc_InProgress = false;
5882
5883 if (IsValidClient(g_pr_Recalc_AdminID))
5884 {
5885 PrintToConsole(g_pr_Recalc_AdminID, ">> Recalculation finished");
5886 CreateTimer(0.1, RefreshAdminMenu, g_pr_Recalc_AdminID, TIMER_FLAG_NO_MAPCHANGE);
5887 }
5888 }
5889 if (MAX_PR_PLAYERS != data && g_pr_TableRowCount > data)
5890 x = 66 + data;
5891 else
5892 x = 66 + g_pr_TableRowCount;
5893
5894 if (g_pr_TableRowCount > MAX_PR_PLAYERS)
5895 g_pr_TableRowCount = MAX_PR_PLAYERS;
5896
5897 if (x > MAX_PR_PLAYERS)
5898 x = MAX_PR_PLAYERS - 1;
5899 if (IsValidClient(g_pr_Recalc_AdminID) && g_bManualRecalc)
5900 {
5901 int max = MAX_PR_PLAYERS - 66;
5902 PrintToConsole(g_pr_Recalc_AdminID, " \n>> Recalculation started! (Only Top %i because of performance reasons)", max);
5903 }
5904 while (SQL_FetchRow(hndl))
5905 {
5906 if (i <= x)
5907 {
5908 g_pr_points[i] = 0;
5909 SQL_FetchString(hndl, 0, g_pr_szSteamID[i], sizeof(g_pr_szSteamID[]));
5910 SQL_FetchString(hndl, 1, g_pr_szName[i], sizeof(g_pr_szName[]));
5911
5912 g_bProfileRecalc[i] = true;
5913 i++;
5914 }
5915 if (i == x)
5916 {
5917 CalculatePlayerRank(66);
5918 }
5919 }
5920 }
5921 else PrintToConsole(g_pr_Recalc_AdminID, " \n>> No valid players found!");
5922}
5923
5924public void db_Cleanup()
5925{
5926 char szQuery[255];
5927
5928 //tmps
5929 Format(szQuery, sizeof(szQuery), "DELETE FROM ck_playertemp where mapname != '%s'", g_szMapName);
5930 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery);
5931
5932 //times
5933 SQL_TQuery(g_hDb, SQL_CheckCallback, "DELETE FROM ck_playertimes where runtimepro = -1.0");
5934}
5935
5936public void db_resetMapRecords(int client, char szMapName[128])
5937{
5938 char szQuery[255];
5939 Format(szQuery, sizeof(szQuery), sql_resetMapRecords, szMapName);
5940 SQL_TQuery(g_hDb, SQL_CheckCallback2, szQuery, DBPrio_Low);
5941 PrintToConsole(client, "player times on %s cleared.", szMapName);
5942 if (StrEqual(szMapName, g_szMapName))
5943 {
5944 for (int i = 1; i <= MaxClients; i++)
5945 {
5946 if (IsValidClient(i))
5947 {
5948 Format(g_szPersonalRecord[i], sizeof(g_szPersonalRecord[]), "NONE");
5949 g_fPersonalRecord[i] = 0.0;
5950 g_MapRank[i] = 99999;
5951 }
5952 }
5953 }
5954}
5955
5956public void SQL_InsertPlayerCallBack(Handle owner, Handle hndl, const char[] error, any data)
5957{
5958 if (hndl == null)
5959 {
5960 LogError("[ckSurf] SQL Error (SQL_InsertPlayerCallBack): %s", error);
5961 return;
5962 }
5963 if (IsClientInGame(data))
5964 db_UpdateLastSeen(data);
5965}
5966
5967public void db_UpdateLastSeen(int client)
5968{
5969 if ((StrContains(g_szSteamID[client], "STEAM_") != -1) && !IsFakeClient(client))
5970 {
5971 char szQuery[512];
5972 if (g_DbType == MYSQL)
5973 Format(szQuery, 512, sql_UpdateLastSeenMySQL, g_szSteamID[client]);
5974 else
5975 if (g_DbType == SQLITE)
5976 Format(szQuery, 512, sql_UpdateLastSeenSQLite, g_szSteamID[client]);
5977 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
5978 }
5979}
5980
5981
5982/////////////////////////////
5983///// DEFAULT CALLBACKS /////
5984/////////////////////////////
5985
5986public void SQL_CheckCallback(Handle owner, Handle hndl, const char[] error, any data)
5987{
5988 if (hndl == null)
5989 {
5990 LogError("[ckSurf] SQL Error (SQL_CheckCallback): %s", error);
5991 return;
5992 }
5993}
5994
5995
5996public void SQL_CheckCallback2(Handle owner, Handle hndl, const char[] error, any data)
5997{
5998 if (hndl == null)
5999 {
6000 LogError("[ckSurf] SQL Error (SQL_CheckCallback2): %s", error);
6001 return;
6002 }
6003
6004 db_viewMapProRankCount();
6005 db_GetMapRecord_Pro();
6006}
6007
6008public void SQL_CheckCallback3(Handle owner, Handle hndl, const char[] error, any data)
6009{
6010 if (hndl == null)
6011 {
6012 LogError("[ckSurf] SQL Error (SQL_CheckCallback3): %s", error);
6013 return;
6014 }
6015
6016 char steamid[128];
6017
6018 ResetPack(data);
6019 int client = ReadPackCell(data);
6020 ReadPackString(data, steamid, 128);
6021 CloseHandle(data);
6022
6023 RecalcPlayerRank(client, steamid);
6024 db_viewMapProRankCount();
6025 db_GetMapRecord_Pro();
6026}
6027
6028public void SQL_CheckCallback4(Handle owner, Handle hndl, const char[] error, any data)
6029{
6030 if (hndl == null)
6031 {
6032 LogError("[ckSurf] SQL Error (SQL_CheckCallback4): %s", error);
6033 return;
6034 }
6035 char steamid[128];
6036
6037 ResetPack(data);
6038 int client = ReadPackCell(data);
6039 ReadPackString(data, steamid, 128);
6040 CloseHandle(data);
6041
6042 RecalcPlayerRank(client, steamid);
6043}
6044
6045///////////////////////////
6046///// PLAYER OPTIONS //////
6047///////////////////////////
6048
6049public void db_viewPlayerOptions(int client, char szSteamId[32])
6050{
6051 char szQuery[512];
6052 Format(szQuery, 512, sql_selectPlayerOptions, szSteamId);
6053 SQL_TQuery(g_hDb, db_viewPlayerOptionsCallback, szQuery, client, DBPrio_Low);
6054}
6055
6056public void db_viewPlayerOptionsCallback(Handle owner, Handle hndl, const char[] error, any client)
6057{
6058 if (hndl == null)
6059 {
6060 LogError("[ckSurf] SQL Error (db_viewPlayerOptionsCallback): %s", error);
6061 if (!g_bSettingsLoaded[client])
6062 db_viewPersonalFlags(client, g_szSteamID[client]);
6063 return;
6064 }
6065
6066 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
6067 {
6068 //"SELECT speedmeter, quake_sounds, autobhop, shownames, goto, showtime, hideplayers, showspecs, knife, new1, new2, new3, checkpoints FROM ck_playeroptions where steamid = '%s'";
6069
6070 g_bInfoPanel[client] = view_as<bool>(SQL_FetchInt(hndl, 0));
6071 g_bEnableQuakeSounds[client] = view_as<bool>(SQL_FetchInt(hndl, 1));
6072 g_bAutoBhopClient[client] = view_as<bool>(SQL_FetchInt(hndl, 2));
6073 g_bShowNames[client] = view_as<bool>(SQL_FetchInt(hndl, 3));
6074 g_bGoToClient[client] = view_as<bool>(SQL_FetchInt(hndl, 4));
6075 g_bShowTime[client] = view_as<bool>(SQL_FetchInt(hndl, 5));
6076 g_bHide[client] = view_as<bool>(SQL_FetchInt(hndl, 6));
6077 g_bShowSpecs[client] = view_as<bool>(SQL_FetchInt(hndl, 7));
6078 g_bStartWithUsp[client] = view_as<bool>(SQL_FetchInt(hndl, 9));
6079 g_bHideChat[client] = view_as<bool>(SQL_FetchInt(hndl, 10));
6080 g_bViewModel[client] = view_as<bool>(SQL_FetchInt(hndl, 11));
6081 g_bCheckpointsEnabled[client] = view_as<bool>(SQL_FetchInt(hndl, 12));
6082
6083 //org
6084 g_borg_AutoBhopClient[client] = g_bAutoBhopClient[client];
6085 g_borg_InfoPanel[client] = g_bInfoPanel[client];
6086 g_borg_EnableQuakeSounds[client] = g_bEnableQuakeSounds[client];
6087 g_borg_ShowNames[client] = g_bShowNames[client];
6088 g_borg_GoToClient[client] = g_bGoToClient[client];
6089 g_borg_ShowTime[client] = g_bShowTime[client];
6090 g_borg_Hide[client] = g_bHide[client];
6091 g_borg_StartWithUsp[client] = g_bStartWithUsp[client];
6092 g_borg_ShowSpecs[client] = g_bShowSpecs[client];
6093 g_borg_HideChat[client] = g_bHideChat[client];
6094 g_borg_ViewModel[client] = g_bViewModel[client];
6095 g_borg_CheckpointsEnabled[client] = g_bCheckpointsEnabled[client];
6096 }
6097 else
6098 {
6099 char szQuery[512];
6100 if (!IsValidClient(client))
6101 return;
6102
6103 //"INSERT INTO ck_playeroptions (steamid, speedmeter, quake_sounds, autobhop, shownames, goto, showtime, hideplayers, showspecs, knife, new1, new2, new3) VALUES('%s', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%i', '%s', '%i', '%i', '%i');";
6104
6105 Format(szQuery, sizeof(szQuery), sql_insertPlayerOptions, g_szSteamID[client], 1, 1, 1, 1, 1, 0, 0, 1, "weapon_knife", 0, 0, 1, 1);
6106 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, DBPrio_Low);
6107 g_borg_InfoPanel[client] = true;
6108 g_borg_EnableQuakeSounds[client] = true;
6109 g_borg_AutoBhopClient[client] = true;
6110 g_borg_ShowNames[client] = true;
6111 g_borg_GoToClient[client] = true;
6112 g_borg_ShowTime[client] = false;
6113 g_borg_Hide[client] = false;
6114 g_borg_ShowSpecs[client] = true;
6115 // weapon_knife
6116 g_borg_StartWithUsp[client] = false;
6117 g_borg_HideChat[client] = false;
6118 g_borg_ViewModel[client] = true;
6119 g_borg_CheckpointsEnabled[client] = true;
6120 }
6121 if (!g_bSettingsLoaded[client])
6122 db_viewPersonalFlags(client, g_szSteamID[client]);
6123 return;
6124}
6125
6126public void db_updatePlayerOptions(int client)
6127{
6128 if (g_borg_ViewModel[client] != g_bViewModel[client] || g_borg_HideChat[client] != g_bHideChat[client] || g_borg_StartWithUsp[client] != g_bStartWithUsp[client] || g_borg_AutoBhopClient[client] != g_bAutoBhopClient[client] || g_borg_InfoPanel[client] != g_bInfoPanel[client] || g_borg_EnableQuakeSounds[client] != g_bEnableQuakeSounds[client] || g_borg_ShowNames[client] != g_bShowNames[client] || g_borg_GoToClient[client] != g_bGoToClient[client] || g_borg_ShowTime[client] != g_bShowTime[client] || g_borg_Hide[client] != g_bHide[client] || g_borg_ShowSpecs[client] != g_bShowSpecs[client] || g_borg_CheckpointsEnabled[client] != g_bCheckpointsEnabled[client])
6129 {
6130 char szQuery[1024];
6131
6132 Format(szQuery, 1024, sql_updatePlayerOptions, BooltoInt(g_bInfoPanel[client]), BooltoInt(g_bEnableQuakeSounds[client]), BooltoInt(g_bAutoBhopClient[client]), BooltoInt(g_bShowNames[client]), BooltoInt(g_bGoToClient[client]), BooltoInt(g_bShowTime[client]), BooltoInt(g_bHide[client]), BooltoInt(g_bShowSpecs[client]), "weapon_knife", BooltoInt(g_bStartWithUsp[client]), BooltoInt(g_bHideChat[client]), BooltoInt(g_bViewModel[client]), BooltoInt(g_bCheckpointsEnabled[client]), g_szSteamID[client]);
6133 SQL_TQuery(g_hDb, SQL_CheckCallback, szQuery, client, DBPrio_Low);
6134 }
6135}
6136
6137//////////////////////////////
6138/// MENUS ////////////////////
6139//////////////////////////////
6140
6141public void db_selectTopProRecordHolders(int client)
6142{
6143 char szQuery[512];
6144 Format(szQuery, 512, sql_selectMapRecordHolders);
6145 SQL_TQuery(g_hDb, db_sql_selectMapRecordHoldersCallback, szQuery, client);
6146}
6147
6148public void db_sql_selectMapRecordHoldersCallback(Handle owner, Handle hndl, const char[] error, any data)
6149{
6150 if (hndl == null)
6151 {
6152 LogError("[ckSurf] SQL Error (db_sql_selectMapRecordHoldersCallback): %s", error);
6153 return;
6154 }
6155
6156 char szSteamID[32];
6157 char szRecords[64];
6158 char szQuery[256];
6159 int records = 0;
6160 if (SQL_HasResultSet(hndl))
6161 {
6162 int i = SQL_GetRowCount(hndl);
6163 int x = i;
6164 g_menuTopSurfersMenu[data] = new Menu(TopProHoldersHandler1);
6165 g_menuTopSurfersMenu[data].SetTitle("Top 5 Pro Surfers\n# Records Player");
6166 while (SQL_FetchRow(hndl))
6167 {
6168 SQL_FetchString(hndl, 0, szSteamID, 32);
6169 records = SQL_FetchInt(hndl, 1);
6170 if (records > 9)
6171 Format(szRecords, 64, "%i", records);
6172 else
6173 Format(szRecords, 64, "%i ", records);
6174
6175 Handle pack = CreateDataPack();
6176 WritePackCell(pack, data);
6177 WritePackString(pack, szRecords);
6178 WritePackCell(pack, i);
6179 WritePackString(pack, szSteamID);
6180 Format(szQuery, 256, sql_selectRankedPlayer, szSteamID);
6181 SQL_TQuery(g_hDb, db_sql_selectMapRecordHoldersCallback2, szQuery, pack);
6182 i--;
6183 }
6184 if (x == 0)
6185 {
6186 CPrintToChat(data, PREFIX_t, "NoRecordTop");
6187 ckTopMenu(data);
6188 }
6189 }
6190 else
6191 {
6192 CPrintToChat(data, PREFIX_t, "NoRecordTop");
6193 ckTopMenu(data);
6194 }
6195}
6196
6197public void db_sql_selectMapRecordHoldersCallback2(Handle owner, Handle hndl, const char[] error, any data)
6198{
6199 if (hndl == null)
6200 {
6201 LogError("[ckSurf] SQL Error (db_sql_selectMapRecordHoldersCallback2): %s", error);
6202 return;
6203 }
6204
6205 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
6206 {
6207 char szName[MAX_NAME_LENGTH];
6208 char szSteamID[32];
6209 char szRecords[64];
6210 char szValue[128];
6211
6212 ResetPack(data);
6213 int client = ReadPackCell(data);
6214 ReadPackString(data, szRecords, 64);
6215 int count = ReadPackCell(data);
6216 ReadPackString(data, szSteamID, 32);
6217 CloseHandle(data);
6218
6219 SQL_FetchString(hndl, 1, szName, MAX_NAME_LENGTH);
6220 Format(szValue, 128, " %s 禄 %s", szRecords, szName);
6221 g_menuTopSurfersMenu[client].AddItem(szSteamID, szValue, ITEMDRAW_DEFAULT);
6222 if (count == 1)
6223 {
6224 g_menuTopSurfersMenu[client].OptionFlags = MENUFLAG_BUTTON_EXIT;
6225 g_menuTopSurfersMenu[client].Display(client, MENU_TIME_FOREVER);
6226 }
6227 }
6228}
6229
6230public void db_selectTopPlayers(int client)
6231{
6232 char szQuery[128];
6233 Format(szQuery, 128, sql_selectTopPlayers);
6234 SQL_TQuery(g_hDb, db_selectTop100PlayersCallback, szQuery, client, DBPrio_Low);
6235}
6236
6237public void db_selectTop100PlayersCallback(Handle owner, Handle hndl, const char[] error, any data)
6238{
6239 if (hndl == null)
6240 {
6241 LogError("[ckSurf] SQL Error (db_selectTop100PlayersCallback): %s", error);
6242 return;
6243 }
6244
6245 char szValue[128];
6246 char szName[64];
6247 char szRank[16];
6248 char szSteamID[32];
6249 char szPerc[16];
6250 int points;
6251 Menu menu = new Menu(TopPlayersMenuHandler1);
6252 menu.SetTitle("Top 100 Players\n Rank Points Maps Player");
6253 menu.Pagination = 5;
6254 if (SQL_HasResultSet(hndl))
6255 {
6256 int i = 1;
6257 while (SQL_FetchRow(hndl))
6258 {
6259 SQL_FetchString(hndl, 0, szName, 64);
6260 if (i == 100)
6261 Format(szRank, 16, "[%i.]", i);
6262 else
6263 if (i < 10)
6264 Format(szRank, 16, "[0%i.] ", i);
6265 else
6266 Format(szRank, 16, "[%i.] ", i);
6267
6268 points = SQL_FetchInt(hndl, 1);
6269 int pro = SQL_FetchInt(hndl, 2);
6270 SQL_FetchString(hndl, 3, szSteamID, 32);
6271 float fperc;
6272 fperc = (float(pro) / (float(g_pr_MapCount))) * 100.0;
6273
6274 if (fperc < 10.0)
6275 Format(szPerc, 16, " %.1f ", fperc, PERCENT);
6276 else
6277 if (fperc == 100.0)
6278 Format(szPerc, 16, "100.0", PERCENT);
6279 else
6280 if (fperc > 100.0) //player profile not refreshed after removing maps
6281 Format(szPerc, 16, "100.0", PERCENT);
6282 else
6283 Format(szPerc, 16, "%.1f ", fperc, PERCENT);
6284
6285 if (points < 10)
6286 Format(szValue, 128, "%s %ip %s 禄 %s", szRank, points, szPerc, szName);
6287 else
6288 if (points < 100)
6289 Format(szValue, 128, "%s %ip %s 禄 %s", szRank, points, szPerc, szName);
6290 else
6291 if (points < 1000)
6292 Format(szValue, 128, "%s %ip %s 禄 %s", szRank, points, szPerc, szName);
6293 else
6294 if (points < 10000)
6295 Format(szValue, 128, "%s %ip %s 禄 %s", szRank, points, szPerc, szName);
6296 else
6297 if (points < 100000)
6298 Format(szValue, 128, "%s %ip %s 禄 %s", szRank, points, szPerc, szName);
6299 else
6300 Format(szValue, 128, "%s %ip %s 禄 %s", szRank, points, szPerc, szName);
6301
6302 menu.AddItem(szSteamID, szValue, ITEMDRAW_DEFAULT);
6303 i++;
6304 }
6305 if (i == 1)
6306 {
6307 CPrintToChat(data, PREFIX_t, "NoPlayerTop");
6308 }
6309 else
6310 {
6311 menu.OptionFlags = MENUFLAG_BUTTON_EXIT;
6312 menu.Display(data, MENU_TIME_FOREVER);
6313 }
6314 }
6315 else
6316 {
6317 CPrintToChat(data, PREFIX_t, "NoPlayerTop");
6318 }
6319}
6320
6321public void SQL_ViewPlayerProfile2Callback(Handle owner, Handle hndl, const char[] error, any data)
6322{
6323 if (hndl == null)
6324 {
6325 LogError("[ckSurf] SQL Error (SQL_ViewPlayerProfile2Callback): %s", error);
6326 return;
6327 }
6328
6329 if (SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
6330 {
6331 SQL_FetchString(hndl, 1, g_szProfileSteamId[data], 32);
6332 db_viewPlayerRank(data, g_szProfileSteamId[data]);
6333 }
6334 else
6335 if (IsClientInGame(data))
6336 CPrintToChat(data, PREFIX_t, "PlayerNotFound", g_szProfileName[data]);
6337}
6338
6339public int ProfileMenuHandler(Handle menu, MenuAction action, int client, int item)
6340{
6341 if (action == MenuAction_Select)
6342 {
6343 switch (item)
6344 {
6345 case 0:db_viewRecord(client, g_szProfileSteamId[client], g_szMapName);
6346 case 1:db_viewChallengeHistory(client, g_szProfileSteamId[client]);
6347 case 2:db_viewAllRecords(client, g_szProfileSteamId[client]);
6348 case 3:db_viewUnfinishedMaps(client, g_szProfileSteamId[client]);
6349 case 4:
6350 {
6351 if (g_bRecalcRankInProgess[client])
6352 {
6353 CPrintToChat(client, PREFIX_T, "Rc_in_progress", client);
6354 }
6355 else
6356 {
6357 g_bRecalcRankInProgess[client] = true;
6358 CPrintToChat(client, PREFIX_T, "Rc_PlayerRankStart", client);
6359 CalculatePlayerRank(client);
6360 }
6361 }
6362 }
6363 }
6364 else
6365 if (action == MenuAction_Cancel)
6366 {
6367 if (1 <= client <= MaxClients && IsValidClient(client))
6368 {
6369 switch (g_MenuLevel[client])
6370 {
6371 case 0:db_selectTopPlayers(client);
6372 case 3:db_selectTopChallengers(client);
6373 case 9:db_selectProSurfers(client);
6374 case 11:db_selectTopProRecordHolders(client);
6375
6376 }
6377 if (g_MenuLevel[client] < 0)
6378 {
6379 if (g_bSelectProfile[client])
6380 ProfileMenu(client, 0);
6381 }
6382 }
6383 }
6384 else
6385 if (action == MenuAction_End)
6386 {
6387 CloseHandle(menu);
6388 }
6389}
6390
6391public int TopPlayersMenuHandler1(Handle menu, MenuAction action, int client, int item)
6392{
6393 if (action == MenuAction_Select)
6394 {
6395 char info[32];
6396 GetMenuItem(menu, item, info, sizeof(info));
6397 g_MenuLevel[client] = 0;
6398 db_viewPlayerRank(client, info);
6399 }
6400 if (action == MenuAction_Cancel)
6401 {
6402 ckTopMenu(client);
6403 }
6404 else if (action == MenuAction_End)
6405 {
6406 CloseHandle(menu);
6407 }
6408}
6409
6410public int MapMenuHandler1(Handle menu, MenuAction action, int client, int item)
6411{
6412 if (action == MenuAction_Select)
6413 {
6414 char info[32];
6415 GetMenuItem(menu, item, info, sizeof(info));
6416 g_MenuLevel[client] = 1;
6417 db_viewPlayerRank(client, info);
6418 }
6419 if (action == MenuAction_Cancel)
6420 {
6421 ckTopMenu(client);
6422 }
6423 else if (action == MenuAction_End)
6424 {
6425 CloseHandle(menu);
6426 }
6427}
6428
6429public int MapTopMenuHandler2(Handle menu, MenuAction action, int client, int item)
6430{
6431 if (action == MenuAction_Select)
6432 {
6433 char info[32];
6434 GetMenuItem(menu, item, info, sizeof(info));
6435 g_MenuLevel[client] = 1;
6436 db_viewPlayerRank(client, info);
6437 }
6438}
6439
6440public void MapMenuHandler2(Handle menu, MenuAction action, int iClient, int iInfo)
6441{
6442 if (action == MenuAction_Select)
6443 {
6444 char info[32];
6445 GetMenuItem(menu, iInfo, info, sizeof(info));
6446 g_MenuLevel[iClient] = 8;
6447 db_viewPlayerRank(iClient, info);
6448 }
6449 if (action == MenuAction_Cancel)
6450 {
6451 ckTopMenu(iClient);
6452 }
6453 else if (action == MenuAction_End)
6454 {
6455 CloseHandle(menu);
6456 }
6457}
6458
6459
6460public int MapMenuHandler3(Handle menu, MenuAction action, int client, int item)
6461{
6462 if (action == MenuAction_Select)
6463 {
6464 char info[32];
6465 GetMenuItem(menu, item, info, sizeof(info));
6466 g_MenuLevel[client] = 9;
6467 db_viewPlayerRank(client, info);
6468 }
6469 if (action == MenuAction_Cancel)
6470 {
6471 ckTopMenu(client);
6472 }
6473 else if (action == MenuAction_End)
6474 {
6475 CloseHandle(menu);
6476 }
6477}
6478
6479
6480public int MenuHandler2(Handle menu, MenuAction action, int client, int iInfo)
6481{
6482 if (action == MenuAction_Cancel || action == MenuAction_Select)
6483 {
6484 ProfileMenu(client, -1);
6485 }
6486 else if (action == MenuAction_End)
6487 {
6488 CloseHandle(menu);
6489 }
6490}
6491
6492public int RecordPanelHandler(Handle menu, MenuAction action, int client, int item)
6493{
6494 if (action == MenuAction_Select)
6495 {
6496 ProfileMenu(client, -1);
6497 }
6498}
6499
6500public void RecordPanelHandler2(Handle menu, MenuAction action, int iClient, int iInfo)
6501{
6502 if (action == MenuAction_Select)
6503 {
6504 ckTopMenu(iClient);
6505 }
6506}