[go: up one dir, main page]

Skip to content

Commit

Permalink
get/write/update stages and bonuses for map in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
tslashd committed Dec 26, 2023
1 parent b5064cf commit 62251f9
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/ST-Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Map
// Constructor
internal Map(string Name, TimerDatabase DB)
{
// Set map name
this.Name = Name;
// Gathering zones from the map
IEnumerable<CBaseTrigger> triggers = Utilities.FindAllEntitiesByDesignerName<CBaseTrigger>("trigger_multiple");
// Gathering info_teleport_destinations from the map
Expand Down Expand Up @@ -78,10 +80,12 @@ internal Map(string Name, TimerDatabase DB)
if (teleport.Entity!.Name != null && IsInZone(trigger.AbsOrigin!, trigger.Collision.BoundingRadius, teleport.AbsOrigin!))
{
this.StageStartZoneAngles[Int32.Parse(Regex.Match(trigger.Entity.Name, "[0-9][0-9]?").Value) - 1] = new QAngle(teleport.AbsRotation!.X, teleport.AbsRotation!.Y, teleport.AbsRotation!.Z);
this.Stages++; // Count stage zones for the map to populate DB
}
}
}


// Bonus start zones
else if (Regex.Match(trigger.Entity.Name, "^b([1-9][0-9]?|onus[1-9][0-9]?)_start$").Success)
{
this.BonusStartZone[Int32.Parse(Regex.Match(trigger.Entity.Name, "[0-9][0-9]?").Value) - 1] = new Vector(trigger.AbsOrigin!.X, trigger.AbsOrigin!.Y, trigger.AbsOrigin!.Z);
Expand All @@ -92,6 +96,7 @@ internal Map(string Name, TimerDatabase DB)
if (teleport.Entity!.Name != null && IsInZone(trigger.AbsOrigin!, trigger.Collision.BoundingRadius, teleport.AbsOrigin!))
{
this.BonusStartZoneAngles[Int32.Parse(Regex.Match(trigger.Entity.Name, "[0-9][0-9]?").Value) - 1] = new QAngle(teleport.AbsRotation!.X, teleport.AbsRotation!.Y, teleport.AbsRotation!.Z);
this.Bonuses++; // Count bonus zones for the map to populate DB
}
}
}
Expand All @@ -107,53 +112,61 @@ internal Map(string Name, TimerDatabase DB)
// Gather map information OR create entry
Task<MySqlDataReader> reader = DB.Query($"SELECT * FROM Maps WHERE name='{MySqlHelper.EscapeString(Name)}'");
MySqlDataReader mapData = reader.Result;
if (mapData.HasRows && mapData.Read())
bool updateData = false;
if (mapData.HasRows && mapData.Read()) // In here we can check whether MapData in DB is the same as the newly extracted data, if not, update it (as hookzones may have changed on map updates)
{
this.ID = mapData.GetInt32("id");
this.Name = Name;
this.Author = mapData.GetString("author") ?? "Unknown";
this.Tier = mapData.GetInt32("tier");
this.Stages = mapData.GetInt32("stages");
this.Bonuses = mapData.GetInt32("bonuses");
if (this.Stages != mapData.GetInt32("stages") || this.Bonuses != mapData.GetInt32("bonuses"))
updateData = true;
// this.Stages = mapData.GetInt32("stages"); // this should now be populated accordingly when looping through hookzones for the map
// this.Bonuses = mapData.GetInt32("bonuses"); // this should now be populated accordingly when looping through hookzones for the map
this.Ranked = mapData.GetBoolean("ranked");
this.DateAdded = mapData.GetInt32("date_added");
this.LastPlayed = mapData.GetInt32("last_played");
updateData = true;
mapData.Close();
}

else
{
mapData.Close();
Task<int> writer = DB.Write($"INSERT INTO Maps (name, author, tier, stages, ranked, date_added, last_played) VALUES ('{MySqlHelper.EscapeString(Name)}', 'Unknown', 0, 0, 0, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()})");
Task<int> writer = DB.Write($"INSERT INTO Maps (name, author, tier, stages, ranked, date_added, last_played) VALUES ('{MySqlHelper.EscapeString(Name)}', 'Unknown', {this.Stages}, {this.Bonuses}, 0, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()})");
int writerRows = writer.Result;
if (writerRows != 1)
throw new Exception($"CS2 Surf ERROR >> OnRoundStart -> new Map() -> Failed to write new map to database, this shouldnt happen. Map: {Name}");
throw new Exception($"CS2 Surf ERROR >> OnRoundStart -> new Map() -> Failed to write new map to database, this shouldn't happen. Map: {Name}");

Task<MySqlDataReader> postWriteReader = DB.Query($"SELECT * FROM Maps WHERE name='{MySqlHelper.EscapeString(Name)}'");
MySqlDataReader postWriteMapData = postWriteReader.Result;
if (postWriteMapData.HasRows && postWriteMapData.Read())
{
this.ID = postWriteMapData.GetInt32("id");
this.Author = postWriteMapData.GetString("author");
this.Tier = postWriteMapData.GetInt32("tier");
// this.Stages = -1; // this should now be populated accordingly when looping through hookzones for the map
// this.Bonuses = -1; // this should now be populated accordingly when looping through hookzones for the map
this.Ranked = postWriteMapData.GetBoolean("ranked");
this.DateAdded = postWriteMapData.GetInt32("date_added");
this.LastPlayed = this.DateAdded;
}
postWriteMapData.Close();
this.Name = Name;
this.Author = "Unknown";
this.Tier = -1;
this.Stages = -1;
this.Bonuses = -1;
this.Ranked = false;
this.DateAdded = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
this.LastPlayed = this.DateAdded;

return;
}

// Update the map's last played data in the DB
// Update last_played data
Task<int> updater = DB.Write($"UPDATE Maps SET last_played={(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()} WHERE id = {this.ID}");
// Update last_played data or update last_played, stages, and bonuses data
string query = $"UPDATE Maps SET last_played={(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()} WHERE id={this.ID}";
if (updateData) query = $"UPDATE Maps SET last_played={(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}, stages={this.Stages}, bonuses={this.Bonuses} WHERE id={this.ID}";
#if DEBUG
Console.WriteLine($"CS2 Surf ERROR >> OnRoundStart -> update Map() -> Update MapData: {query}");
#endif

Task<int> updater = DB.Write(query);
int lastPlayedUpdateRows = updater.Result;
if (lastPlayedUpdateRows != 1)
throw new Exception($"CS2 Surf ERROR >> OnRoundStart -> update Map() -> Failed to update map in database, this shouldnt happen. Map: {Name}");
throw new Exception($"CS2 Surf ERROR >> OnRoundStart -> update Map() -> Failed to update map in database, this shouldnt happen. Map: {Name} | was it 'big' update? {updateData}");
updater.Dispose();

// Initiates getting the World Records for the map
Expand Down

0 comments on commit 62251f9

Please sign in to comment.