Skip to content

Commit

Permalink
Track the platform scores were achieved on (#325)
Browse files Browse the repository at this point in the history
Closes #241 

# Why
The main reason we'd want to do this is not only is it a cool metric to
track, but also the fact that we support both PS3 and RPCS3, and
naturally RPCS3 can be more susceptible to gameplay glitches depending
on your settings. I just think it's better to distinguish the two in
some way.

Eventually down the line, we'll also be able to track PPSSPP or Vita3K
when those two games achieve networking support in LBP for the same
reasons.

# How
We simply add a new property onto `GameSubmittedScore` that contains the
platform. Then, we use a best-effort migration that tries to match each
game to the most relevant platform. For the mainline games, this will be
PS3, LBP Vita is Vita, and LBP PSP is PSP.

We obviously can't go back in time and tell if a particular score was
done on RPCS3 so we just assume PS3 for now since that seems to be the
more popular choice anyways.

We also expose the game version and platform the score was performed on
to the API.
  • Loading branch information
jvyden authored Jan 3, 2024
2 parents 6e847d4 + 91a05ed commit 84e768e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
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

0 comments on commit 84e768e

Please sign in to comment.