Skip to content

Commit

Permalink
Merge pull request #209 from peppy/update-tables
Browse files Browse the repository at this point in the history
Update to use new score storage backend
  • Loading branch information
peppy authored Jan 23, 2024
2 parents 85bd33f + 559123d commit 7f423ab
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 64 deletions.
21 changes: 6 additions & 15 deletions osu.Server.Spectator.Tests/ScoreUploaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ public ScoreUploaderTests()
mockDatabase = new Mock<IDatabaseAccess>();
mockDatabase.Setup(db => db.GetScoreFromToken(1)).Returns(Task.FromResult<SoloScore?>(new SoloScore
{
ScoreInfo =
{
ID = 2,
Passed = true
}
id = 2,
passed = true
}));

var databaseFactory = new Mock<IDatabaseFactory>();
Expand Down Expand Up @@ -124,11 +121,8 @@ public async Task ScoreUploadsWithDelayedScoreToken()
// Give the score a token.
mockDatabase.Setup(db => db.GetScoreFromToken(2)).Returns(Task.FromResult<SoloScore?>(new SoloScore
{
ScoreInfo =
{
ID = 3,
Passed = true
}
id = 3,
passed = true
}));

await uploader.Flush();
Expand All @@ -152,11 +146,8 @@ public async Task TimedOutScoreDoesNotUpload()
// Give the score a token now. It should still not upload because it has timed out.
mockDatabase.Setup(db => db.GetScoreFromToken(2)).Returns(Task.FromResult<SoloScore?>(new SoloScore
{
ScoreInfo =
{
ID = 3,
Passed = true
}
id = 3,
passed = true
}));

await uploader.Flush();
Expand Down
21 changes: 6 additions & 15 deletions osu.Server.Spectator.Tests/SpectatorHubTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,8 @@ public async Task ReplayDataIsSaved(bool savingEnabled)

mockDatabase.Setup(db => db.GetScoreFromToken(1234)).Returns(Task.FromResult<SoloScore?>(new SoloScore
{
ScoreInfo =
{
ID = 456,
Passed = true
}
id = 456,
passed = true
}));

await hub.BeginPlaySession(1234, state);
Expand Down Expand Up @@ -273,11 +270,8 @@ public async Task ScoresAreOnlySavedOnRankedBeatmaps(BeatmapOnlineStatus status,

mockDatabase.Setup(db => db.GetScoreFromToken(1234)).Returns(Task.FromResult<SoloScore?>(new SoloScore
{
ScoreInfo =
{
ID = 456,
Passed = true
}
id = 456,
passed = true
}));

mockDatabase.Setup(db => db.GetBeatmapAsync(beatmap_id)).Returns(Task.FromResult(new database_beatmap
Expand Down Expand Up @@ -318,11 +312,8 @@ public async Task FailedScoresAreNotSaved()

mockDatabase.Setup(db => db.GetScoreFromToken(1234)).Returns(Task.FromResult<SoloScore?>(new SoloScore
{
ScoreInfo =
{
ID = 456,
Passed = false
}
id = 456,
passed = false
}));

await hub.BeginPlaySession(1234, state);
Expand Down
6 changes: 3 additions & 3 deletions osu.Server.Spectator/Database/DatabaseAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public async Task MarkScoreHasReplay(Score score)
{
var connection = await getConnectionAsync();

await connection.ExecuteAsync("UPDATE `solo_scores` SET `has_replay` = 1 WHERE `id` = @scoreId", new
await connection.ExecuteAsync("UPDATE `scores` SET `has_replay` = 1 WHERE `id` = @scoreId", new
{
scoreId = score.ScoreInfo.OnlineID,
});
Expand All @@ -299,7 +299,7 @@ public async Task MarkScoreHasReplay(Score score)
var connection = await getConnectionAsync();

return await connection.QuerySingleOrDefaultAsync<SoloScore?>(
"SELECT * FROM `solo_scores` WHERE `id` = (SELECT `score_id` FROM `solo_score_tokens` WHERE `id` = @Id)", new
"SELECT * FROM `scores` WHERE `id` = (SELECT `score_id` FROM `score_tokens` WHERE `id` = @Id)", new
{
Id = token
});
Expand All @@ -309,7 +309,7 @@ public async Task<bool> IsScoreProcessedAsync(long scoreId)
{
var connection = await getConnectionAsync();

return await connection.QuerySingleOrDefaultAsync<bool>("SELECT 1 FROM `solo_scores_process_history` WHERE `score_id` = @ScoreId", new
return await connection.QuerySingleOrDefaultAsync<bool>("SELECT 1 FROM `score_process_history` WHERE `score_id` = @ScoreId", new
{
ScoreId = scoreId
});
Expand Down
81 changes: 54 additions & 27 deletions osu.Server.Spectator/Database/Models/SoloScore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,83 @@
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Scoring;

namespace osu.Server.Spectator.Database.Models
{
[SuppressMessage("ReSharper", "InconsistentNaming")]
[Serializable]
[Table("solo_scores")]
[Table("scores")]
public class SoloScore
{
public ulong id { get; set; }

public int user_id { get; set; }
public uint user_id { get; set; }

public int beatmap_id { get; set; }
public uint beatmap_id { get; set; }

public int ruleset_id { get; set; }
public ushort ruleset_id { get; set; }

public bool has_replay { get; set; }
public bool preserve { get; set; }
public bool ranked { get; set; } = true;

public ScoreRank rank { get; set; }

public bool passed { get; set; } = true;

public float accuracy { get; set; }

public uint max_combo { get; set; }

public uint total_score { get; set; }

public SoloScoreData ScoreData = new SoloScoreData();

public string data
{
get => JsonConvert.SerializeObject(ScoreInfo);
get => JsonConvert.SerializeObject(ScoreData);
set
{
var scoreInfo = JsonConvert.DeserializeObject<SoloScoreInfo>(value);

if (scoreInfo == null)
return;

ScoreInfo = scoreInfo;

ScoreInfo.ID = id;
ScoreInfo.BeatmapID = beatmap_id;
ScoreInfo.UserID = user_id;
ScoreInfo.RulesetID = ruleset_id;
var soloScoreData = JsonConvert.DeserializeObject<SoloScoreData>(value);
if (soloScoreData != null)
ScoreData = soloScoreData;
}
}

[JsonIgnore]
public bool preserve { get; set; }
public double? pp { get; set; }

[JsonIgnore]
public bool has_replay { get; set; }
public ulong? legacy_score_id { get; set; }
public uint? legacy_total_score { get; set; }

[JsonIgnore]
public SoloScoreInfo ScoreInfo = new SoloScoreInfo();
public DateTimeOffset? started_at { get; set; }
public DateTimeOffset ended_at { get; set; }

[JsonIgnore]
public DateTimeOffset created_at { get; set; }
public override string ToString() => $"score_id: {id} user_id: {user_id}";

[JsonIgnore]
public DateTimeOffset updated_at { get; set; }
public ushort? build_id { get; set; }

public override string ToString() => $"score_id: {id} user_id: {user_id}";
public SoloScoreInfo ToScoreInfo() => new SoloScoreInfo
{
BeatmapID = (int)beatmap_id,
RulesetID = ruleset_id,
BuildID = build_id,
Passed = passed,
TotalScore = total_score,
Accuracy = accuracy,
UserID = (int)user_id,
MaxCombo = (int)max_combo,
Rank = rank,
StartedAt = started_at,
EndedAt = ended_at,
Mods = ScoreData.Mods,
Statistics = ScoreData.Statistics,
MaximumStatistics = ScoreData.MaximumStatistics,
LegacyTotalScore = (int?)legacy_total_score,
LegacyScoreId = legacy_score_id,
ID = id,
PP = pp,
HasReplay = has_replay
};
}
}
24 changes: 24 additions & 0 deletions osu.Server.Spectator/Database/Models/SoloScoreData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using osu.Game.Online.API;
using osu.Game.Rulesets.Scoring;

namespace osu.Server.Spectator.Database.Models
{
[Serializable]
public class SoloScoreData
{
[JsonProperty("mods")]
public APIMod[] Mods { get; set; } = Array.Empty<APIMod>();

[JsonProperty("statistics")]
public Dictionary<HitResult, int> Statistics { get; set; } = new Dictionary<HitResult, int>();

[JsonProperty("maximum_statistics")]
public Dictionary<HitResult, int> MaximumStatistics { get; set; } = new Dictionary<HitResult, int>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down
6 changes: 3 additions & 3 deletions osu.Server.Spectator/Hubs/ScoreUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public async Task Flush()
return;
}

if (!dbScore.ScoreInfo.Passed)
if (!dbScore.passed)
return;

item.Score.ScoreInfo.OnlineID = dbScore.ScoreInfo.OnlineID;
item.Score.ScoreInfo.Passed = dbScore.ScoreInfo.Passed;
item.Score.ScoreInfo.OnlineID = (long)dbScore.id;
item.Score.ScoreInfo.Passed = dbScore.passed;

await scoreStorage.WriteAsync(item.Score);
await db.MarkScoreHasReplay(item.Score);
Expand Down

0 comments on commit 7f423ab

Please sign in to comment.