1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-15 20:05:29 +08:00

Use plain bindable flow instead of binding to watcher directly

This commit is contained in:
Bartłomiej Dach 2022-12-24 10:27:28 +01:00
parent c7f248e13c
commit 4e5109a649
No known key found for this signature in database
4 changed files with 53 additions and 105 deletions

View File

@ -1,10 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Online.Solo; using osu.Game.Online.Solo;
using osu.Game.Scoring; using osu.Game.Scoring;
@ -15,8 +12,7 @@ namespace osu.Game.Tests.Visual.Ranking
{ {
public partial class TestSceneOverallRanking : OsuTestScene public partial class TestSceneOverallRanking : OsuTestScene
{ {
[Cached(typeof(ISoloStatisticsWatcher))] private OverallRanking overallRanking = null!;
private MockSoloStatisticsWatcher soloStatisticsWatcher { get; } = new MockSoloStatisticsWatcher();
[Test] [Test]
public void TestUpdatePending() public void TestUpdatePending()
@ -28,9 +24,7 @@ namespace osu.Game.Tests.Visual.Ranking
public void TestAllIncreased() public void TestAllIncreased()
{ {
createDisplay(); createDisplay();
AddStep("trigger update success", () => displayUpdate(
{
soloStatisticsWatcher.TriggerSuccess(
new UserStatistics new UserStatistics
{ {
GlobalRank = 12_345, GlobalRank = 12_345,
@ -49,16 +43,13 @@ namespace osu.Game.Tests.Visual.Ranking
TotalScore = 123_124_231_435, TotalScore = 123_124_231_435,
PP = 5_434 PP = 5_434
}); });
});
} }
[Test] [Test]
public void TestAllDecreased() public void TestAllDecreased()
{ {
createDisplay(); createDisplay();
AddStep("trigger update success", () => displayUpdate(
{
soloStatisticsWatcher.TriggerSuccess(
new UserStatistics new UserStatistics
{ {
GlobalRank = 1_234, GlobalRank = 1_234,
@ -77,7 +68,6 @@ namespace osu.Game.Tests.Visual.Ranking
TotalScore = 123_123_543_456, TotalScore = 123_123_543_456,
PP = 5_072 PP = 5_072
}); });
});
} }
[Test] [Test]
@ -94,7 +84,7 @@ namespace osu.Game.Tests.Visual.Ranking
}; };
createDisplay(); createDisplay();
AddStep("trigger update success", () => soloStatisticsWatcher.TriggerSuccess(statistics, statistics)); displayUpdate(statistics, statistics);
} }
[Test] [Test]
@ -111,32 +101,17 @@ namespace osu.Game.Tests.Visual.Ranking
}; };
createDisplay(); createDisplay();
AddStep("trigger update success", () => soloStatisticsWatcher.TriggerSuccess(statistics, statistics)); displayUpdate(statistics, statistics);
} }
private void createDisplay() => AddStep("create display", () => Child = new OverallRanking(new ScoreInfo()) private void createDisplay() => AddStep("create display", () => Child = overallRanking = new OverallRanking(new ScoreInfo())
{ {
Width = 400, Width = 400,
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre Origin = Anchor.Centre
}); });
private class MockSoloStatisticsWatcher : ISoloStatisticsWatcher private void displayUpdate(UserStatistics before, UserStatistics after) =>
{ AddStep("display update", () => overallRanking.StatisticsUpdate.Value = new SoloStatisticsUpdate(new ScoreInfo(), before, after));
private ScoreInfo? score;
private Action<SoloStatisticsUpdate>? onUpdateReady;
public void RegisterForStatisticsUpdateAfter(ScoreInfo score, Action<SoloStatisticsUpdate> onUpdateReady)
{
this.score = score;
this.onUpdateReady = onUpdateReady;
}
public void TriggerSuccess(UserStatistics before, UserStatistics after)
{
Debug.Assert(score != null && onUpdateReady != null);
onUpdateReady.Invoke(new SoloStatisticsUpdate(score, before, after));
}
}
} }
} }

View File

@ -1,23 +0,0 @@
// 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 osu.Framework.Allocation;
using osu.Game.Scoring;
namespace osu.Game.Online.Solo
{
/// <summary>
/// A component that delivers updates to the logged in user's gameplay statistics after completed scores.
/// </summary>
[Cached]
public interface ISoloStatisticsWatcher
{
/// <summary>
/// Registers for a user statistics update after the given <paramref name="score"/> has been processed server-side.
/// </summary>
/// <param name="score">The score to listen for the statistics update for.</param>
/// <param name="onUpdateReady">The callback to be invoked once the statistics update has been prepared.</param>
void RegisterForStatisticsUpdateAfter(ScoreInfo score, Action<SoloStatisticsUpdate> onUpdateReady);
}
}

View File

@ -20,7 +20,7 @@ namespace osu.Game.Online.Solo
/// <summary> /// <summary>
/// A persistent component that binds to the spectator server and API in order to deliver updates about the logged in user's gameplay statistics. /// A persistent component that binds to the spectator server and API in order to deliver updates about the logged in user's gameplay statistics.
/// </summary> /// </summary>
public partial class SoloStatisticsWatcher : Component, ISoloStatisticsWatcher public partial class SoloStatisticsWatcher : Component
{ {
[Resolved] [Resolved]
private SpectatorClient spectatorClient { get; set; } = null!; private SpectatorClient spectatorClient { get; set; } = null!;

View File

@ -18,14 +18,11 @@ namespace osu.Game.Screens.Ranking.Statistics.User
private readonly ScoreInfo score; private readonly ScoreInfo score;
private readonly Bindable<SoloStatisticsUpdate?> statisticsUpdate = new Bindable<SoloStatisticsUpdate?>(); public Bindable<SoloStatisticsUpdate?> StatisticsUpdate { get; } = new Bindable<SoloStatisticsUpdate?>();
private LoadingLayer loadingLayer = null!; private LoadingLayer loadingLayer = null!;
private FillFlowContainer content = null!; private FillFlowContainer content = null!;
[Resolved]
private ISoloStatisticsWatcher statisticsWatcher { get; set; } = null!;
public OverallRanking(ScoreInfo score) public OverallRanking(ScoreInfo score)
{ {
this.score = score; this.score = score;
@ -53,12 +50,12 @@ namespace osu.Game.Screens.Ranking.Statistics.User
Spacing = new Vector2(10), Spacing = new Vector2(10),
Children = new Drawable[] Children = new Drawable[]
{ {
new GlobalRankChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } }, new GlobalRankChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new AccuracyChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } }, new AccuracyChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new MaximumComboChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } }, new MaximumComboChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new RankedScoreChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } }, new RankedScoreChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new TotalScoreChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } }, new TotalScoreChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new PerformancePointsChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } } new PerformancePointsChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } }
} }
} }
}; };
@ -68,8 +65,7 @@ namespace osu.Game.Screens.Ranking.Statistics.User
{ {
base.LoadComplete(); base.LoadComplete();
statisticsWatcher.RegisterForStatisticsUpdateAfter(score, update => statisticsUpdate.Value = update); StatisticsUpdate.BindValueChanged(onUpdateReceived, true);
statisticsUpdate.BindValueChanged(onUpdateReceived, true);
FinishTransforms(true); FinishTransforms(true);
} }