From 17709423b6cd57e04b289b28bb9a3c54bf7e3427 Mon Sep 17 00:00:00 2001 From: Hipposgrumm Date: Sun, 15 Jun 2025 23:07:45 -0600 Subject: [PATCH] Heavily optimized the entire PR. --- src/Model/DBContext.cs | 14 ------- src/Model/DailyHighscore.cs | 15 ------- src/Model/DailyHighscorePair.cs | 13 ------- src/Model/GameDataPair.cs | 1 + src/Model/Viking.cs | 1 - src/Services/GameDataService.cs | 69 ++++++++++++--------------------- 6 files changed, 25 insertions(+), 88 deletions(-) delete mode 100644 src/Model/DailyHighscore.cs delete mode 100644 src/Model/DailyHighscorePair.cs diff --git a/src/Model/DBContext.cs b/src/Model/DBContext.cs index ce01bc0..1832763 100644 --- a/src/Model/DBContext.cs +++ b/src/Model/DBContext.cs @@ -19,8 +19,6 @@ public class DBContext : DbContext { public DbSet RoomItems { get; set; } = null!; public DbSet GameData { get; set; } = null!; public DbSet GameDataPairs { get; set; } = null!; - public DbSet DailyHighscores { get; set; } = null!; - public DbSet DailyHighscorePairs { get; set; } = null!; public DbSet AchievementPoints { get; set; } = null!; public DbSet ProfileAnswers { get; set; } = null!; public DbSet MMORoles { get; set; } = null!; @@ -131,9 +129,6 @@ public class DBContext : DbContext { builder.Entity().HasMany(v => v.GameData) .WithOne(e => e.Viking); - builder.Entity().HasMany(v => v.DailyHighscores) - .WithOne(e => e.Viking); - builder.Entity().HasMany(v => v.SavedData) .WithOne(e => e.Viking); @@ -227,15 +222,6 @@ public class DBContext : DbContext { builder.Entity().HasOne(e => e.GameData) .WithMany(e => e.GameDataPairs); - builder.Entity().HasOne(e => e.Viking) - .WithMany(e => e.DailyHighscores); - - builder.Entity().HasMany(e => e.ScorePairs) - .WithOne(e => e.DailyScore); - - builder.Entity().HasOne(e => e.DailyScore) - .WithMany(e => e.ScorePairs); - // Others .. builder.Entity().HasOne(s => s.Viking) .WithMany(e => e.Images) diff --git a/src/Model/DailyHighscore.cs b/src/Model/DailyHighscore.cs deleted file mode 100644 index 4d89c0f..0000000 --- a/src/Model/DailyHighscore.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace sodoff.Model; - -public class DailyHighscore { - [Key] - public int Id { get; set; } - public int VikingId { get; set; } - public int GameId { get; set; } - public int Difficulty { get; set; } - public int GameLevel { get; set; } - public bool IsMultiplayer { get; set; } - public virtual ICollection ScorePairs { get; set; } = null!; - public virtual Viking Viking { get; set; } = null!; -} diff --git a/src/Model/DailyHighscorePair.cs b/src/Model/DailyHighscorePair.cs deleted file mode 100644 index 6b312f3..0000000 --- a/src/Model/DailyHighscorePair.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace sodoff.Model; - -public class DailyHighscorePair { - [Key] - public int Id { get; set; } - public int DailyScoreId { get; set; } - public string Name { get; set; } = null!; - public int Value { get; set; } - public DateTime DatePlayed { get; set; } - public virtual DailyHighscore DailyScore { get; set; } = null!; -} diff --git a/src/Model/GameDataPair.cs b/src/Model/GameDataPair.cs index 9543936..7e029e5 100644 --- a/src/Model/GameDataPair.cs +++ b/src/Model/GameDataPair.cs @@ -7,5 +7,6 @@ public class GameDataPair { public int GameDataId { get; set; } public string Name { get; set; } = null!; public int Value { get; set; } + public int DailyValue { get; set; } public virtual GameData GameData { get; set; } = null!; } diff --git a/src/Model/Viking.cs b/src/Model/Viking.cs index cd19e70..a787107 100644 --- a/src/Model/Viking.cs +++ b/src/Model/Viking.cs @@ -34,7 +34,6 @@ public class Viking { public virtual ICollection PairData { get; set; } = null!; public virtual ICollection InventoryItems { get; set; } = null!; public virtual ICollection GameData { get; set; } = null!; - public virtual ICollection DailyHighscores { get; set; } = null!; public virtual ICollection ProfileAnswers { get; set; } = null!; public virtual ICollection SavedData { get; set; } = null!; public virtual ICollection Parties { get; set; } = null!; diff --git a/src/Services/GameDataService.cs b/src/Services/GameDataService.cs index b76216a..2faa29a 100644 --- a/src/Services/GameDataService.cs +++ b/src/Services/GameDataService.cs @@ -31,20 +31,9 @@ public class GameDataService { viking.GameData.Add(gameData); } - gameData.DatePlayed = DateTime.UtcNow; - DailyHighscore? daily = viking.DailyHighscores.FirstOrDefault(x => x.GameId == gameId && x.IsMultiplayer == isMultiplayer && x.Difficulty == difficulty && x.GameLevel == gameLevel); - if (daily == null) { - daily = new DailyHighscore { - GameId = gameId, - Difficulty = difficulty, - GameLevel = gameLevel, - IsMultiplayer = isMultiplayer, - ScorePairs = new List() - }; - viking.DailyHighscores.Add(daily); - } - SavePairs(gameData, daily, xmlDocumentData); + SavePairs(gameData, xmlDocumentData); + gameData.DatePlayed = DateTime.UtcNow; ctx.SaveChanges(); return true; @@ -85,9 +74,12 @@ public class GameDataService { } public GameDataSummary GetDailyGameData(Viking viking, int gameId, bool isMultiplayer, int difficulty, int gameLevel, string key, int count, bool AscendingOrder, bool buddyFilter, string apiKey) { - IQueryable query = ctx.DailyHighscores - .Where(x => x.GameId == gameId && x.IsMultiplayer == false && x.Difficulty == difficulty && x.GameLevel == gameLevel) - .SelectMany(e => e.ScorePairs).Where(x => x.DatePlayed == DateTime.Today && x.Name == key); + IQueryable query = ctx.GameData + .Where(x => + x.GameId == gameId && x.IsMultiplayer == false && + x.Difficulty == difficulty && x.GameLevel == gameLevel && + x.DatePlayed.Date == DateTime.UtcNow.Date + ).SelectMany(e => e.GameDataPairs).Where(x => x.Name == key); // TODO: Buddy filter @@ -98,11 +90,11 @@ public class GameDataService { if (ClientVersion.GetVersion(apiKey) <= ClientVersion.Max_OldJS) // use DisplayName instead of Name selectedData = query.Select(e => new GameDataResponse( - XmlUtil.DeserializeXml(e.DailyScore.Viking.AvatarSerialized).DisplayName, e.DailyScore.Viking.Uid, e.DatePlayed, false, false, e.Value) + XmlUtil.DeserializeXml(e.GameData.Viking.AvatarSerialized).DisplayName, e.GameData.Viking.Uid, e.GameData.DatePlayed, false, false, e.DailyValue) ).Take(count).ToList(); else selectedData = query.Select(e => new GameDataResponse( - e.DailyScore.Viking.Name, e.DailyScore.Viking.Uid, e.DatePlayed, false, false, e.Value) + e.GameData.Viking.Name, e.GameData.Viking.Uid, e.GameData.DatePlayed, false, false, e.DailyValue) ).Take(count).ToList(); return GetSummaryFromResponse(viking, isMultiplayer, difficulty, gameLevel, key, selectedData); @@ -175,36 +167,23 @@ public class GameDataService { return gameData; } - private void SavePairs(Model.GameData gameData, DailyHighscore daily, string xmlDocumentData) { + private void SavePairs(Model.GameData gameData, string xmlDocumentData) { foreach (var pair in GetGameDataPairs(xmlDocumentData)) { GameDataPair? dbPair = gameData.GameDataPairs.FirstOrDefault(x => x.Name == pair.Name); - if (dbPair == null) - gameData.GameDataPairs.Add(pair); - else if (pair.Name == "time" && dbPair.Value > pair.Value) - dbPair.Value = pair.Value; - else if (pair.Name != "time" && dbPair.Value <= pair.Value) - dbPair.Value = pair.Value; - DailyHighscorePair? dailyPair = daily.ScorePairs.FirstOrDefault(x => x.Name == pair.Name); - if (dailyPair == null) { - daily.Difficulty = gameData.Difficulty; - daily.GameLevel = gameData.GameLevel; - dailyPair = new DailyHighscorePair { - Name = pair.Name, - Value = pair.Value, - DatePlayed = DateTime.Today - }; - daily.ScorePairs.Add(dailyPair); - } else if ( - (pair.Name == "time" && dailyPair.Value > pair.Value) || // Better Time - (pair.Name != "time" && dailyPair.Value <= pair.Value) || // Better Score - dailyPair.DatePlayed != DateTime.Today // Another Day - ) { - daily.Difficulty = gameData.Difficulty; - daily.GameLevel = gameData.GameLevel; - dailyPair.DatePlayed = DateTime.Today; - dailyPair.Value = pair.Value; - } + // If Name == "time" then (existing <= incoming) needs to be false (effectively (existing > incoming), as time should function). + // if Name is anything else, then second condition as normal. + bool newBest = (dbPair == null) || ((pair.Name == "time") != (dbPair.Value <= pair.Value)); + + if (dbPair == null) { + gameData.GameDataPairs.Add(pair); + dbPair = pair; + } else if (newBest) dbPair.Value = pair.Value; + + if ( + newBest || // Surpassed Score (or Unset) + gameData.DatePlayed.Date != DateTime.UtcNow.Date // Another Day + ) dbPair.DailyValue = pair.Value; } }