Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track the platform scores were achieved on #325

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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(() =>
Expand Down
20 changes: 19 additions & 1 deletion Refresh.GameServer/Database/GameDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<dynamic>? oldPlayLevelRelations = migration.OldRealm.DynamicApi.All("PlayLevelRelation");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Refresh.GameServer.Authentication;
using Refresh.GameServer.Types.UserData.Leaderboard;

namespace Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response;
Expand All @@ -12,6 +13,9 @@ public class ApiGameScoreResponse : IApiResponse, IDataConvertableFrom<ApiGameSc
public required int Score { get; set; }
public required byte ScoreType { get; set; }

public required TokenGame Game { get; set; }
public required TokenPlatform Platform { get; set; }

public static ApiGameScoreResponse? FromOld(GameSubmittedScore? old)
{
if (old == null) return null;
Expand All @@ -24,6 +28,8 @@ public class ApiGameScoreResponse : IApiResponse, IDataConvertableFrom<ApiGameSc
ScoreSubmitted = old.ScoreSubmitted,
Score = old.Score,
ScoreType = old.ScoreType,
Game = old.Game,
Platform = old.Platform,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Response SubmitDeveloperScore(RequestContext context, GameUser user, Game
return BadRequest;
}

GameSubmittedScore score = database.SubmitScore(body, user, level, token.TokenGame);
GameSubmittedScore score = database.SubmitScore(body, token, level);

IEnumerable<ScoreWithRank>? scores = database.GetRankedScoresAroundScore(score, 5);
Debug.Assert(scores != null);
Expand Down Expand Up @@ -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<ScoreWithRank>? scores = database.GetRankedScoresAroundScore(score, 5);
Debug.Assert(scores != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameUser> Players { get; }
Expand Down
6 changes: 3 additions & 3 deletions RefreshTests.GameServer/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectId> scores = context.Database.GetRankedScoresAroundScore(score, count)!
.Select(s => s.score.ScoreId)
Expand Down Expand Up @@ -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(() =>
{
Expand All @@ -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);
}
Expand Down
Loading