From b18f11a282b0a3d34071fb4259a98c1490a377cc Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Fri, 13 Oct 2023 19:20:54 -0700 Subject: [PATCH 1/2] Implement count query parameter for play/user/{id} This is only used on PSP, so the logic only exists on PSP --- .../Database/GameDatabaseContext.Levels.cs | 2 +- .../Database/GameDatabaseContext.Relations.cs | 3 ++- .../Database/GameDatabaseProvider.cs | 17 +++++++++++++++-- .../DataTypes/Response/GameLevelResponse.cs | 2 +- .../Game/Levels/LeaderboardEndpoints.cs | 14 +++++++++++++- .../Types/Legacy/LegacyGameLevel.cs | 2 +- .../Types/Relations/PlayLevelRelation.cs | 1 + 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs b/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs index ee73c3ef..d4ab9379 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs @@ -186,7 +186,7 @@ public DatabaseList GetMostReplayedLevels(int count, int skip, TokenG IEnumerable mostPlayed = plays .AsEnumerable() .GroupBy(r => r.Level) - .Select(g => new { Level = g.Key, Count = g.Count() }) + .Select(g => new { Level = g.Key, Count = g.Sum(p => p.Count) }) .OrderByDescending(x => x.Count) .Select(x => x.Level) .FilterByGameVersion(gameVersion); diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs b/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs index 82aedc40..bf81dcf8 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Relations.cs @@ -210,13 +210,14 @@ public bool RateLevel(GameLevel level, GameUser user, RatingType type) #region Playing - public void PlayLevel(GameLevel level, GameUser user) + public void PlayLevel(GameLevel level, GameUser user, int count) { PlayLevelRelation relation = new() { Level = level, User = user, Timestamp = this._time.TimestampMilliseconds, + Count = count, }; UniquePlayLevelRelation? uniqueRelation = this._realm.All() diff --git a/Refresh.GameServer/Database/GameDatabaseProvider.cs b/Refresh.GameServer/Database/GameDatabaseProvider.cs index 3340097b..f32e92f8 100644 --- a/Refresh.GameServer/Database/GameDatabaseProvider.cs +++ b/Refresh.GameServer/Database/GameDatabaseProvider.cs @@ -32,7 +32,7 @@ protected GameDatabaseProvider(IDateTimeProvider time) this._time = time; } - protected override ulong SchemaVersion => 92; + protected override ulong SchemaVersion => 93; protected override string Filename => "refreshGameServer.realm"; @@ -309,7 +309,6 @@ protected override void Migrate(Migration migration, ulong oldVersion) IQueryable? oldScores = migration.OldRealm.DynamicApi.All("GameSubmittedScore"); IQueryable? newScores = migration.NewRealm.All(); - for (int i = 0; i < newScores.Count(); i++) { dynamic oldScore = oldScores.ElementAt(i); @@ -320,5 +319,19 @@ protected override void Migrate(Migration migration, ulong oldVersion) newScore.Game = newScore.Level.GameVersion; } } + + IQueryable? oldPlayLevelRelations = migration.OldRealm.DynamicApi.All("PlayLevelRelation"); + IQueryable? newPlayLevelRelations = migration.NewRealm.All(); + + for (int i = 0; i < newPlayLevelRelations.Count(); i++) + { + dynamic oldPlayLevelRelation = oldPlayLevelRelations.ElementAt(i); + PlayLevelRelation newPlayLevelRelation = newPlayLevelRelations.ElementAt(i); + + if (oldVersion < 93) + { + newPlayLevelRelation.Count = 1; + } + } } } \ No newline at end of file diff --git a/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameLevelResponse.cs b/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameLevelResponse.cs index c8022486..b70e44e7 100644 --- a/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameLevelResponse.cs +++ b/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameLevelResponse.cs @@ -96,7 +96,7 @@ public class GameLevelResponse : IDataConvertableFrom p.Count), UniquePlayCount = old.UniquePlays.Count(), YayCount = old.Ratings.Count(r => r._RatingType == (int)RatingType.Yay), BooCount = old.Ratings.Count(r => r._RatingType == (int)RatingType.Boo), diff --git a/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs b/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs index d5d1496c..e429fce9 100644 --- a/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs @@ -29,7 +29,19 @@ public Response PlayLevel(RequestContext context, GameUser user, GameDatabaseCon GameLevel? level = database.GetLevelById(id); if (level == null) return NotFound; - database.PlayLevel(level, user); + int count = 1; + //If we are on PSP and it has sent a `count` parameter... + if (context.IsPSP() && context.QueryString.Get("count") != null) + { + //Parse the count + if (!int.TryParse(context.QueryString["count"], out count)) + { + //If it fails, send a bad request back to the client + return BadRequest; + } + } + + database.PlayLevel(level, user, count); return OK; } diff --git a/Refresh.GameServer/Types/Legacy/LegacyGameLevel.cs b/Refresh.GameServer/Types/Legacy/LegacyGameLevel.cs index 290f6061..ef7b4b8f 100644 --- a/Refresh.GameServer/Types/Legacy/LegacyGameLevel.cs +++ b/Refresh.GameServer/Types/Legacy/LegacyGameLevel.cs @@ -61,7 +61,7 @@ public class LegacyGameLevel : IDataConvertableFrom LastUpdated = old.UpdateDate, TeamPick = old.TeamPicked, GameVersion = 1, - Plays = old.AllPlays.Count(), + Plays = old.AllPlays.Sum(p => p.Count), PlaysUnique = old.UniquePlays.Count(), PlaysComplete = old.Scores.Count(), CommentsEnabled = true, diff --git a/Refresh.GameServer/Types/Relations/PlayLevelRelation.cs b/Refresh.GameServer/Types/Relations/PlayLevelRelation.cs index b009fd47..853a29e4 100644 --- a/Refresh.GameServer/Types/Relations/PlayLevelRelation.cs +++ b/Refresh.GameServer/Types/Relations/PlayLevelRelation.cs @@ -11,4 +11,5 @@ public partial class PlayLevelRelation : IRealmObject public GameLevel Level { get; set; } public GameUser User { get; set; } public long Timestamp { get; set; } + public int Count { get; set; } } \ No newline at end of file From d41a87710edee0e75c7cb697c93720bd1a90cf1a Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Fri, 13 Oct 2023 19:22:32 -0700 Subject: [PATCH 2/2] Elaborate on play count migration step --- Refresh.GameServer/Database/GameDatabaseProvider.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Refresh.GameServer/Database/GameDatabaseProvider.cs b/Refresh.GameServer/Database/GameDatabaseProvider.cs index f32e92f8..23dfd7fb 100644 --- a/Refresh.GameServer/Database/GameDatabaseProvider.cs +++ b/Refresh.GameServer/Database/GameDatabaseProvider.cs @@ -328,6 +328,7 @@ protected override void Migrate(Migration migration, ulong oldVersion) dynamic oldPlayLevelRelation = oldPlayLevelRelations.ElementAt(i); PlayLevelRelation newPlayLevelRelation = newPlayLevelRelations.ElementAt(i); + //In version 93, we added a count to PlayLevelRelation if (oldVersion < 93) { newPlayLevelRelation.Count = 1;