Skip to content

Commit

Permalink
Merge pull request #231 from bdach/make-sure-ranks-are-not-missing
Browse files Browse the repository at this point in the history
Ensure ranks are properly populated in spectated scores
  • Loading branch information
peppy authored May 7, 2024
2 parents 5487ab1 + 3723905 commit 681debc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
55 changes: 55 additions & 0 deletions osu.Server.Spectator.Tests/SpectatorHubTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Distributed;
Expand Down Expand Up @@ -436,5 +437,59 @@ await hub.EndPlaySession(new SpectatorState
mockScoreStorage.Verify(s => s.WriteAsync(It.Is<Score>(score => score.ScoreInfo.OnlineID == 456)), Times.Never);
mockReceiver.Verify(clients => clients.UserFinishedPlaying(streamer_id, It.Is<SpectatorState>(m => m.State == SpectatedUserState.Failed)), Times.Once());
}

[Fact]
public async Task ScoreRankPopulatedCorrectly()
{
AppSettings.SaveReplays = true;

Mock<IHubCallerClients<ISpectatorClient>> mockClients = new Mock<IHubCallerClients<ISpectatorClient>>();
Mock<ISpectatorClient> mockReceiver = new Mock<ISpectatorClient>();
mockClients.Setup(clients => clients.All).Returns(mockReceiver.Object);
mockClients.Setup(clients => clients.Group(SpectatorHub.GetGroupId(streamer_id))).Returns(mockReceiver.Object);

Mock<HubCallerContext> mockContext = new Mock<HubCallerContext>();

mockContext.Setup(context => context.UserIdentifier).Returns(streamer_id.ToString());
hub.Context = mockContext.Object;
hub.Clients = mockClients.Object;

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

await hub.BeginPlaySession(1234, new SpectatorState
{
BeatmapID = beatmap_id,
RulesetID = 0,
State = SpectatedUserState.Playing,
});

await hub.SendFrameData(new FrameDataBundle(
new FrameHeader(new ScoreInfo
{
Accuracy = 0.95,
Statistics = new Dictionary<HitResult, int>
{
[HitResult.Great] = 19,
[HitResult.Miss] = 1,
}
}, new ScoreProcessorStatistics()),
new[] { new LegacyReplayFrame(1234, 0, 0, ReplayButtonState.None) }));

await hub.EndPlaySession(new SpectatorState
{
BeatmapID = beatmap_id,
RulesetID = 0,
State = SpectatedUserState.Passed,
});

await scoreUploader.Flush();

mockScoreStorage.Verify(s => s.WriteAsync(It.Is<Score>(score => score.ScoreInfo.Rank == ScoreRank.A)), Times.Once);
mockReceiver.Verify(clients => clients.UserFinishedPlaying(streamer_id, It.Is<SpectatorState>(m => m.State == SpectatedUserState.Passed)), Times.Once());
}
}
}
5 changes: 5 additions & 0 deletions osu.Server.Spectator/Hubs/Spectator/SpectatorHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
Expand Down Expand Up @@ -116,6 +117,7 @@ public async Task SendFrameData(FrameDataBundle data)
if (score == null)
return;

score.ScoreInfo.Accuracy = data.Header.Accuracy;
score.ScoreInfo.Statistics = data.Header.Statistics;
score.ScoreInfo.MaxCombo = data.Header.MaxCombo;
score.ScoreInfo.Combo = data.Header.Combo;
Expand Down Expand Up @@ -166,6 +168,9 @@ private async Task processScore(Score score, long scoreToken)
return;

score.ScoreInfo.Date = DateTimeOffset.UtcNow;
// this call is a little expensive due to reflection usage, so only run it at the end of score processing
// even though in theory the rank could be recomputed after every replay frame.
score.ScoreInfo.Rank = StandardisedScoreMigrationTools.ComputeRank(score.ScoreInfo);

scoreUploader.Enqueue(scoreToken, score);
await scoreProcessedSubscriber.RegisterForNotificationAsync(Context.ConnectionId, Context.GetUserId(), scoreToken);
Expand Down
2 changes: 1 addition & 1 deletion osu.Server.Spectator/osu.Server.Spectator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="ppy.osu.Game.Rulesets.Osu" Version="2024.219.0" />
<PackageReference Include="ppy.osu.Game.Rulesets.Taiko" Version="2024.219.0" />
<PackageReference Include="ppy.osu.Server.OsuQueueProcessor" Version="2023.1207.0" />
<PackageReference Include="Sentry.AspNetCore" Version="4.1.1" />
<PackageReference Include="Sentry.AspNetCore" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 681debc

Please sign in to comment.