Skip to content

Commit d978813

Browse files
committed
more readable queries;formatting
1 parent 4faa2b5 commit d978813

File tree

7 files changed

+80
-75
lines changed

7 files changed

+80
-75
lines changed

src/ST-Commands/PlayerCommands.cs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public void PlayerReset(CCSPlayerController? player, CommandInfo command)
1818

1919
// To-do: players[userid].Timer.Reset() -> teleport player
2020
playerList[player.UserId ?? 0].Timer.Reset();
21-
if (CurrentMap.StartZone != new Vector(0,0,0))
22-
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StartZone, new QAngle(0,0,0), new Vector(0,0,0)));
21+
if (CurrentMap.StartZone != new Vector(0, 0, 0))
22+
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StartZone, new QAngle(0, 0, 0), new Vector(0, 0, 0)));
2323
return;
2424
}
2525

@@ -32,10 +32,10 @@ public void PlayerResetStage(CCSPlayerController? player, CommandInfo command)
3232

3333
// To-do: players[userid].Timer.Reset() -> teleport player
3434
Player SurfPlayer = playerList[player.UserId ?? 0];
35-
if (SurfPlayer.Timer.Stage != 0 && CurrentMap.StageStartZone[SurfPlayer.Timer.Stage] != new Vector(0,0,0))
36-
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StageStartZone[SurfPlayer.Timer.Stage], CurrentMap.StageStartZoneAngles[SurfPlayer.Timer.Stage], new Vector(0,0,0)));
35+
if (SurfPlayer.Timer.Stage != 0 && CurrentMap.StageStartZone[SurfPlayer.Timer.Stage] != new Vector(0, 0, 0))
36+
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StageStartZone[SurfPlayer.Timer.Stage], CurrentMap.StageStartZoneAngles[SurfPlayer.Timer.Stage], new Vector(0, 0, 0)));
3737
else // Reset back to map start
38-
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StartZone, new QAngle(0,0,0), new Vector(0,0,0)));
38+
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StartZone, new QAngle(0, 0, 0), new Vector(0, 0, 0)));
3939
return;
4040
}
4141

@@ -66,71 +66,59 @@ public void PlayerGoToStage(CCSPlayerController? player, CommandInfo command)
6666
return;
6767
}
6868

69-
if (CurrentMap.StageStartZone[stage] != new Vector(0,0,0))
69+
if (CurrentMap.StageStartZone[stage] != new Vector(0, 0, 0))
7070
{
7171
if (stage == 0)
72-
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StartZone, CurrentMap.StartZoneAngles, new Vector(0,0,0)));
72+
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StartZone, CurrentMap.StartZoneAngles, new Vector(0, 0, 0)));
7373
else
74-
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StageStartZone[stage], CurrentMap.StageStartZoneAngles[stage], new Vector(0,0,0)));
75-
74+
Server.NextFrame(() => player.PlayerPawn.Value!.Teleport(CurrentMap.StageStartZone[stage], CurrentMap.StageStartZoneAngles[stage], new Vector(0, 0, 0)));
75+
7676
playerList[player.UserId ?? 0].Timer.Reset();
7777
playerList[player.UserId ?? 0].Timer.IsStageMode = true;
7878

7979
// To-do: If you run this while you're in the start zone, endtouch for the start zone runs after you've teleported
8080
// causing the timer to start. This needs to be fixed.
8181
}
8282

83-
else
83+
else
8484
player.PrintToChat($"{PluginPrefix} {ChatColors.Red}Invalid stage provided. Usage: {ChatColors.Green}!s <stage>");
8585
}
8686

87-
// // Test command
88-
// [ConsoleCommand("css_savereplay", "Test")]
89-
// public void SaveReplay(CCSPlayerController? player, CommandInfo command) {
90-
// if(player == null)
91-
// return;
92-
93-
// foreach(var p in playerList.Values) {
94-
// if(p.Replay.Frames.Count() > 0) {
95-
// p.Replay.StopRecording();
96-
// p.Replay.SaveReplayData(p, DB!);
97-
// break;
98-
// }
99-
// }
100-
// }
101-
10287
// Test command
10388
[ConsoleCommand("css_spec", "Moves a player automaticlly into spectator mode")]
104-
public void MovePlayerToSpectator(CCSPlayerController? player, CommandInfo command) {
105-
if(player == null || player.Team == CsTeam.Spectator)
89+
public void MovePlayerToSpectator(CCSPlayerController? player, CommandInfo command)
90+
{
91+
if (player == null || player.Team == CsTeam.Spectator)
10692
return;
10793

10894
player.ChangeTeam(CsTeam.Spectator);
10995
}
11096

11197
[ConsoleCommand("css_replaybotpause", "Pause the replay bot playback")]
11298
[ConsoleCommand("css_rbpause", "Pause the replay bot playback")]
113-
public void PauseReplay(CCSPlayerController? player, CommandInfo command) {
114-
if(player == null
99+
public void PauseReplay(CCSPlayerController? player, CommandInfo command)
100+
{
101+
if (player == null
115102
|| player.Team != CsTeam.Spectator
116103
|| CurrentMap.ReplayBot.Controller == null
117104
|| !CurrentMap.ReplayBot.IsPlaying
118105
|| CurrentMap.ReplayBot.Controller.Pawn.SerialNum != player.ObserverPawn.Value!.ObserverServices!.ObserverTarget.SerialNum)
119106
return;
120-
107+
121108
CurrentMap.ReplayBot.Pause();
122109
}
123110

124111
[ConsoleCommand("css_replaybotflip", "Flips the replay bot between Forward/Backward playback")]
125112
[ConsoleCommand("css_rbflip", "Flips the replay bot between Forward/Backward playback")]
126-
public void ReverseReplay(CCSPlayerController? player, CommandInfo command) {
127-
if(player == null
113+
public void ReverseReplay(CCSPlayerController? player, CommandInfo command)
114+
{
115+
if (player == null
128116
|| player.Team != CsTeam.Spectator
129117
|| CurrentMap.ReplayBot.Controller == null
130118
|| !CurrentMap.ReplayBot.IsPlaying
131119
|| CurrentMap.ReplayBot.Controller.Pawn.SerialNum != player.ObserverPawn.Value!.ObserverServices!.ObserverTarget.SerialNum)
132120
return;
133-
121+
134122
CurrentMap.ReplayBot.FrameTickIncrement *= -1;
135123
}
136124
}

src/ST-Events/Players.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn
9797
connections = 1;
9898

9999
// Write new player to database
100-
Task<int> newPlayerTask = DB.Write($"INSERT INTO `Player` (`name`, `steam_id`, `country`, `join_date`, `last_seen`, `connections`) VALUES ('{MySqlHelper.EscapeString(name)}', {player.SteamID}, '{country}', {joinDate}, {lastSeen}, {connections});");
100+
Task<int> newPlayerTask = DB.Write($@"
101+
INSERT INTO `Player` (`name`, `steam_id`, `country`, `join_date`, `last_seen`, `connections`)
102+
VALUES ('{MySqlHelper.EscapeString(name)}', {player.SteamID}, '{country}', {joinDate}, {lastSeen}, {connections});
103+
");
101104
int newPlayerTaskRows = newPlayerTask.Result;
102105
if (newPlayerTaskRows != 1)
103106
throw new Exception($"CS2 Surf ERROR >> OnPlayerConnect -> Failed to write new player to database, this shouldnt happen. Player: {name} ({player.SteamID})");
@@ -174,9 +177,11 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
174177
else
175178
{
176179
// Update data in Player DB table
177-
Task<int> updatePlayerTask = DB.Write($"UPDATE `Player` SET country = '{playerList[player.UserId ?? 0].Profile.Country}', " +
178-
$"`last_seen` = {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}, `connections` = `connections` + 1 " +
179-
$"WHERE `id` = {playerList[player.UserId ?? 0].Profile.ID} LIMIT 1;");
180+
Task<int> updatePlayerTask = DB.Write($@"
181+
UPDATE `Player` SET country = '{playerList[player.UserId ?? 0].Profile.Country}',
182+
`last_seen` = {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}, `connections` = `connections` + 1
183+
WHERE `id` = {playerList[player.UserId ?? 0].Profile.ID} LIMIT 1;
184+
");
180185
if (updatePlayerTask.Result != 1)
181186
throw new Exception($"CS2 Surf ERROR >> OnPlayerDisconnect -> Failed to update player data in database. Player: {player.PlayerName} ({player.SteamID})");
182187
// Player disconnection to-do

src/ST-Events/TriggerStartTouch.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ internal HookResult OnTriggerStartTouch(DynamicHook handler)
7979
player.Stats.PB[style].Ticks = player.Timer.Ticks; // Reload the run_time for the HUD and also assign for the DB query
8080

8181
#if DEBUG
82-
Console.WriteLine($"CS2 Surf DEBUG >> OnTriggerStartTouch (Map end zone) -> " +
83-
$"============== INSERT INTO `MapTimes` " +
84-
$"(`player_id`, `map_id`, `style`, `type`, `stage`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`, `end_vel_x`, `end_vel_y`, `end_vel_z`, `run_date`) " +
85-
$"VALUES ({player.Profile.ID}, {CurrentMap.ID}, {style}, 0, 0, {player.Stats.ThisRun.Ticks}, " +
86-
$"{player.Stats.ThisRun.StartVelX}, {player.Stats.ThisRun.StartVelY}, {player.Stats.ThisRun.StartVelZ}, {velocity_x}, {velocity_y}, {velocity_z}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}) " + // To-do: get the `start_vel` values for the run from CP implementation
87-
$"ON DUPLICATE KEY UPDATE run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y), " +
88-
$"start_vel_z=VALUES(start_vel_z), end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), run_date=VALUES(run_date);");
82+
Console.WriteLine($@"CS2 Surf DEBUG >> OnTriggerStartTouch (Map end zone) ->
83+
============== INSERT INTO `MapTimes`
84+
(`player_id`, `map_id`, `style`, `type`, `stage`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`, `end_vel_x`, `end_vel_y`, `end_vel_z`, `run_date`)
85+
VALUES ({player.Profile.ID}, {CurrentMap.ID}, {style}, 0, 0, {player.Stats.ThisRun.Ticks},
86+
{player.Stats.ThisRun.StartVelX}, {player.Stats.ThisRun.StartVelY}, {player.Stats.ThisRun.StartVelZ}, {velocity_x}, {velocity_y}, {velocity_z}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()})
87+
ON DUPLICATE KEY UPDATE run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y),
88+
start_vel_z=VALUES(start_vel_z), end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), run_date=VALUES(run_date);
89+
");
8990
#endif
9091

9192
// Add entry in DB for the run

src/ST-Player/PlayerStats/CurrentRun.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ public void SaveMapTime(Player player, TimerDatabase DB)
5353
// Add entry in DB for the run
5454
// To-do: add `type`
5555
int style = player.Timer.Style;
56-
Task<int> updatePlayerRunTask = DB.Write($"INSERT INTO `MapTimes` " +
57-
$"(`player_id`, `map_id`, `style`, `type`, `stage`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`, `end_vel_x`, `end_vel_y`, `end_vel_z`, `run_date`) " +
58-
$"VALUES ({player.Profile.ID}, {player.CurrMap.ID}, {style}, 0, 0, {player.Stats.ThisRun.Ticks}, " +
59-
$"{player.Stats.ThisRun.StartVelX}, {player.Stats.ThisRun.StartVelY}, {player.Stats.ThisRun.StartVelZ}, {player.Stats.ThisRun.EndVelX}, {player.Stats.ThisRun.EndVelY}, {player.Stats.ThisRun.EndVelZ}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}) " +
60-
$"ON DUPLICATE KEY UPDATE run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y), " +
61-
$"start_vel_z=VALUES(start_vel_z), end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), run_date=VALUES(run_date);");
56+
Task<int> updatePlayerRunTask = DB.Write($@"
57+
INSERT INTO `MapTimes`
58+
(`player_id`, `map_id`, `style`, `type`, `stage`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`, `end_vel_x`, `end_vel_y`, `end_vel_z`, `run_date`)
59+
VALUES ({player.Profile.ID}, {player.CurrMap.ID}, {style}, 0, 0, {player.Stats.ThisRun.Ticks},
60+
{player.Stats.ThisRun.StartVelX}, {player.Stats.ThisRun.StartVelY}, {player.Stats.ThisRun.StartVelZ}, {player.Stats.ThisRun.EndVelX}, {player.Stats.ThisRun.EndVelY}, {player.Stats.ThisRun.EndVelZ}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()})
61+
ON DUPLICATE KEY UPDATE run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y),
62+
start_vel_z=VALUES(start_vel_z), end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), run_date=VALUES(run_date);
63+
");
6264
if (updatePlayerRunTask.Result <= 0)
6365
throw new Exception($"CS2 Surf ERROR >> internal class PersonalBest -> SaveMapTime -> Failed to insert/update player run in database. Player: {player.Profile.Name} ({player.Profile.SteamID})");
6466
updatePlayerRunTask.Dispose();
@@ -91,30 +93,33 @@ public void SaveMapTime(Player player, TimerDatabase DB)
9193

9294
#if DEBUG
9395
Console.WriteLine($"CP: {cp} | MapTime ID: {player.Stats.PB[style].ID} | Time: {runTime} | Ticks: {ticks} | startVelX: {startVelX} | startVelY: {startVelY} | startVelZ: {startVelZ} | endVelX: {endVelX} | endVelY: {endVelY} | endVelZ: {endVelZ}");
94-
Console.WriteLine($"CS2 Surf DEBUG >> internal class Checkpoint : PersonalBest -> SaveCurrentRunCheckpoints -> " +
95-
$"INSERT INTO `Checkpoints` " +
96-
$"(`maptime_id`, `cp`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`, " +
97-
$"`end_vel_x`, `end_vel_y`, `end_vel_z`, `attempts`, `end_touch`) " +
98-
$"VALUES ({player.Stats.PB[style].ID}, {cp}, {runTime}, {startVelX}, {startVelY}, {startVelZ}, {endVelX}, {endVelY}, {endVelZ}, {attempts}, {ticks}) ON DUPLICATE KEY UPDATE " +
99-
$"run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y), start_vel_z=VALUES(start_vel_z), " +
100-
$"end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), attempts=VALUES(attempts), end_touch=VALUES(end_touch);");
96+
Console.WriteLine($@"CS2 Surf DEBUG >> internal class Checkpoint : PersonalBest -> SaveCurrentRunCheckpoints ->
97+
INSERT INTO `Checkpoints`
98+
(`maptime_id`, `cp`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`,
99+
`end_vel_x`, `end_vel_y`, `end_vel_z`, `attempts`, `end_touch`)
100+
VALUES ({player.Stats.PB[style].ID}, {cp}, {runTime}, {startVelX}, {startVelY}, {startVelZ}, {endVelX}, {endVelY}, {endVelZ}, {attempts}, {ticks}) ON DUPLICATE KEY UPDATE
101+
run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y), start_vel_z=VALUES(start_vel_z),
102+
end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), attempts=VALUES(attempts), end_touch=VALUES(end_touch);
103+
");
101104
#endif
102105

103106
// Insert/Update CPs to database
104107
// To-do: Transactions?
105108
// Check if the player has PB object initialized and if the player's character is currently active in the game
106109
if (item.Value != null && player.Controller.PlayerPawn.Value != null)
107110
{
108-
Task<int> newPbTask = DB.Write($"INSERT INTO `Checkpoints` " +
109-
$"(`maptime_id`, `cp`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`, " +
110-
$"`end_vel_x`, `end_vel_y`, `end_vel_z`, `attempts`, `end_touch`) " +
111-
$"VALUES ({player.Stats.PB[style].ID}, {cp}, {runTime}, {startVelX}, {startVelY}, {startVelZ}, {endVelX}, {endVelY}, {endVelZ}, {attempts}, {ticks}) " +
112-
$"ON DUPLICATE KEY UPDATE " +
113-
$"run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y), start_vel_z=VALUES(start_vel_z), " +
114-
$"end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), attempts=VALUES(attempts), end_touch=VALUES(end_touch);");
111+
Task<int> newPbTask = DB.Write($@"
112+
INSERT INTO `Checkpoints`
113+
(`maptime_id`, `cp`, `run_time`, `start_vel_x`, `start_vel_y`, `start_vel_z`,
114+
`end_vel_x`, `end_vel_y`, `end_vel_z`, `attempts`, `end_touch`)
115+
VALUES ({player.Stats.PB[style].ID}, {cp}, {runTime}, {startVelX}, {startVelY}, {startVelZ}, {endVelX}, {endVelY}, {endVelZ}, {attempts}, {ticks})
116+
ON DUPLICATE KEY UPDATE
117+
run_time=VALUES(run_time), start_vel_x=VALUES(start_vel_x), start_vel_y=VALUES(start_vel_y), start_vel_z=VALUES(start_vel_z),
118+
end_vel_x=VALUES(end_vel_x), end_vel_y=VALUES(end_vel_y), end_vel_z=VALUES(end_vel_z), attempts=VALUES(attempts), end_touch=VALUES(end_touch);
119+
");
115120
if (newPbTask.Result <= 0)
116121
throw new Exception($"CS2 Surf ERROR >> internal class Checkpoint : PersonalBest -> SaveCurrentRunCheckpoints -> Inserting Checkpoints. CP: {cp} | Name: {player.Profile.Name}");
117-
122+
118123
newPbTask.Dispose();
119124
}
120125
}

src/ST-Player/PlayerStats/PlayerStats.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ public PlayerStats()
2828
/// </summary>
2929
public void LoadMapTimesData(Player player, TimerDatabase DB, int playerId = 0, int mapId = 0)
3030
{
31-
Task<MySqlDataReader> dbTask2 = DB.Query($"SELECT mainquery.*, (SELECT COUNT(*) FROM `MapTimes` AS subquery " +
32-
$"WHERE subquery.`map_id` = mainquery.`map_id` AND subquery.`style` = mainquery.`style` " +
33-
$"AND subquery.`run_time` <= mainquery.`run_time`) AS `rank` FROM `MapTimes` AS mainquery " +
34-
$"WHERE mainquery.`player_id` = {player.Profile.ID} AND mainquery.`map_id` = {player.CurrMap.ID}; ");
31+
Task<MySqlDataReader> dbTask2 = DB.Query($@"
32+
SELECT mainquery.*, (SELECT COUNT(*) FROM `MapTimes` AS subquery
33+
WHERE subquery.`map_id` = mainquery.`map_id` AND subquery.`style` = mainquery.`style`
34+
AND subquery.`run_time` <= mainquery.`run_time`) AS `rank` FROM `MapTimes` AS mainquery
35+
WHERE mainquery.`player_id` = {player.Profile.ID} AND mainquery.`map_id` = {player.CurrMap.ID};
36+
");
3537
MySqlDataReader playerStats = dbTask2.Result;
3638
int style = 0; // To-do: implement styles
3739
if (!playerStats.HasRows)

src/ST-Player/Replay/ReplayPlayer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public void LoadReplayData(TimerDatabase DB, Map current_map)
9696
if (this.Controller == null)
9797
return;
9898
// TODO: make query for wr too
99-
Task<MySqlDataReader> dbTask = DB.Query($"SELECT `replay_frames` FROM MapTimeReplay " +
100-
$"WHERE `map_id`={current_map.ID} AND `maptime_id`={current_map.WR[0].ID} ");
99+
Task<MySqlDataReader> dbTask = DB.Query($@"
100+
SELECT `replay_frames` FROM MapTimeReplay
101+
WHERE `map_id`={current_map.ID} AND `maptime_id`={current_map.WR[0].ID}
102+
");
101103
MySqlDataReader mapTimeReplay = dbTask.Result;
102104
if(!mapTimeReplay.HasRows)
103105
{

0 commit comments

Comments
 (0)