diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Leaderboard.cs b/Refresh.GameServer/Database/GameDatabaseContext.Leaderboard.cs index 810a457a..b771e1d2 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Leaderboard.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Leaderboard.cs @@ -9,7 +9,10 @@ namespace Refresh.GameServer.Database; public partial class GameDatabaseContext // Leaderboard { - public GameSubmittedScore SubmitScore(SerializedScore score, GameUser user, GameLevel level, TokenGame game) + public GameSubmittedScore SubmitScore(SerializedScore score, Token token, GameLevel level) + => this.SubmitScore(score, token.User, level, token.TokenGame, token.TokenPlatform); + + public GameSubmittedScore SubmitScore(SerializedScore score, GameUser user, GameLevel level, TokenGame game, TokenPlatform platform) { GameSubmittedScore newScore = new() { @@ -19,6 +22,7 @@ public GameSubmittedScore SubmitScore(SerializedScore score, GameUser user, Game Players = { user }, ScoreSubmitted = this._time.Now, Game = game, + Platform = platform, }; this._realm.Write(() => diff --git a/Refresh.GameServer/Database/GameDatabaseProvider.cs b/Refresh.GameServer/Database/GameDatabaseProvider.cs index 3ab46dc5..6f4a2f8b 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 => 103; + protected override ulong SchemaVersion => 104; protected override string Filename => "refreshGameServer.realm"; @@ -344,6 +344,24 @@ protected override void Migrate(Migration migration, ulong oldVersion) { newScore.Game = newScore.Level.GameVersion; } + + // In version 104 we started tracking the platform + if (oldVersion < 104) + { + // Determine the most reasonable platform for the score's game + TokenPlatform platform = newScore.Game switch + { + TokenGame.LittleBigPlanet1 => TokenPlatform.PS3, + TokenGame.LittleBigPlanet2 => TokenPlatform.PS3, + TokenGame.LittleBigPlanet3 => TokenPlatform.PS3, + TokenGame.LittleBigPlanetVita => TokenPlatform.Vita, + TokenGame.LittleBigPlanetPSP => TokenPlatform.PSP, + TokenGame.Website => throw new InvalidOperationException($"what? score id {newScore.ScoreId} by {newScore.Players[0].Username} is fucked"), + _ => throw new ArgumentOutOfRangeException(), + }; + + newScore.Platform = platform; + } } IQueryable? oldPlayLevelRelations = migration.OldRealm.DynamicApi.All("PlayLevelRelation"); diff --git a/Refresh.GameServer/Endpoints/ApiV3/DataTypes/Response/ApiGameScoreResponse.cs b/Refresh.GameServer/Endpoints/ApiV3/DataTypes/Response/ApiGameScoreResponse.cs index 385583af..cdb3c7ce 100644 --- a/Refresh.GameServer/Endpoints/ApiV3/DataTypes/Response/ApiGameScoreResponse.cs +++ b/Refresh.GameServer/Endpoints/ApiV3/DataTypes/Response/ApiGameScoreResponse.cs @@ -1,3 +1,4 @@ +using Refresh.GameServer.Authentication; using Refresh.GameServer.Types.UserData.Leaderboard; namespace Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response; @@ -12,6 +13,9 @@ public class ApiGameScoreResponse : IApiResponse, IDataConvertableFrom? scores = database.GetRankedScoresAroundScore(score, 5); Debug.Assert(scores != null); @@ -130,7 +130,7 @@ public Response SubmitScore(RequestContext context, GameUser user, GameDatabaseC return BadRequest; } - GameSubmittedScore score = database.SubmitScore(body, user, level, token.TokenGame); + GameSubmittedScore score = database.SubmitScore(body, token, level); IEnumerable? scores = database.GetRankedScoresAroundScore(score, 5); Debug.Assert(scores != null); diff --git a/Refresh.GameServer/Types/UserData/Leaderboard/GameSubmittedScore.cs b/Refresh.GameServer/Types/UserData/Leaderboard/GameSubmittedScore.cs index 1ae74f8a..4b938032 100644 --- a/Refresh.GameServer/Types/UserData/Leaderboard/GameSubmittedScore.cs +++ b/Refresh.GameServer/Types/UserData/Leaderboard/GameSubmittedScore.cs @@ -20,6 +20,14 @@ [Ignored] public TokenGame Game get => (TokenGame)this._Game; set => this._Game = (int)value; } + + // ReSharper disable once InconsistentNaming + public int _Platform { get; set; } + [Ignored] public TokenPlatform Platform + { + get => (TokenPlatform)this._Platform; + set => this._Platform = (int)value; + } public GameLevel Level { get; set; } public IList Players { get; } diff --git a/RefreshTests.GameServer/TestContext.cs b/RefreshTests.GameServer/TestContext.cs index a2061dbf..fb945062 100644 --- a/RefreshTests.GameServer/TestContext.cs +++ b/RefreshTests.GameServer/TestContext.cs @@ -127,11 +127,11 @@ public void FillLeaderboard(GameLevel level, int count, byte type) for (byte i = 0; i < count; i++) { GameUser scoreUser = this.CreateUser("score" + i); - this.SubmitScore(i, type, level, scoreUser, TokenGame.LittleBigPlanet2); + this.SubmitScore(i, type, level, scoreUser, TokenGame.LittleBigPlanet2, TokenPlatform.PS3); } } - public GameSubmittedScore SubmitScore(int score, byte type, GameLevel level, GameUser user, TokenGame game) + public GameSubmittedScore SubmitScore(int score, byte type, GameLevel level, GameUser user, TokenGame game, TokenPlatform platform) { SerializedScore scoreObject = new() { @@ -140,7 +140,7 @@ public GameSubmittedScore SubmitScore(int score, byte type, GameLevel level, Gam ScoreType = type, }; - GameSubmittedScore submittedScore = this.Database.SubmitScore(scoreObject, user, level, game); + GameSubmittedScore submittedScore = this.Database.SubmitScore(scoreObject, user, level, game, platform); Assert.That(submittedScore, Is.Not.Null); return submittedScore; diff --git a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs index 968653a6..c1507cf2 100644 --- a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs +++ b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs @@ -258,7 +258,7 @@ private void ScoreSegmentTest(int count, int expectedIndex, int leaderboardCount GameLevel level = context.CreateLevel(user); context.FillLeaderboard(level, leaderboardCount, 1); - GameSubmittedScore score = context.SubmitScore(submittedScore, 1, level, user, TokenGame.LittleBigPlanet2); + GameSubmittedScore score = context.SubmitScore(submittedScore, 1, level, user, TokenGame.LittleBigPlanet2, TokenPlatform.PS3); List scores = context.Database.GetRankedScoresAroundScore(score, count)! .Select(s => s.score.ScoreId) @@ -298,8 +298,8 @@ public void SeparatesLeaderboardsByType() GameLevel level = context.CreateLevel(user1); - GameSubmittedScore score1 = context.SubmitScore(0, 1, level, user1, TokenGame.LittleBigPlanet2); - GameSubmittedScore score2 = context.SubmitScore(0, 2, level, user2, TokenGame.LittleBigPlanet2); + GameSubmittedScore score1 = context.SubmitScore(0, 1, level, user1, TokenGame.LittleBigPlanet2, TokenPlatform.PS3); + GameSubmittedScore score2 = context.SubmitScore(0, 2, level, user2, TokenGame.LittleBigPlanet2, TokenPlatform.PS3); Assert.Multiple(() => { @@ -313,7 +313,7 @@ public void FailsWithInvalidNumber() { using TestContext context = this.GetServer(false); GameUser user = context.CreateUser(); - GameSubmittedScore score = context.SubmitScore(0, 1, context.CreateLevel(user), user, TokenGame.LittleBigPlanet2); + GameSubmittedScore score = context.SubmitScore(0, 1, context.CreateLevel(user), user, TokenGame.LittleBigPlanet2, TokenPlatform.PS3); Assert.That(() => context.Database.GetRankedScoresAroundScore(score, 2), Throws.ArgumentException); }