1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 17:33:22 +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.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Online.Solo;
using osu.Game.Scoring;
@ -15,8 +12,7 @@ namespace osu.Game.Tests.Visual.Ranking
{
public partial class TestSceneOverallRanking : OsuTestScene
{
[Cached(typeof(ISoloStatisticsWatcher))]
private MockSoloStatisticsWatcher soloStatisticsWatcher { get; } = new MockSoloStatisticsWatcher();
private OverallRanking overallRanking = null!;
[Test]
public void TestUpdatePending()
@ -28,9 +24,7 @@ namespace osu.Game.Tests.Visual.Ranking
public void TestAllIncreased()
{
createDisplay();
AddStep("trigger update success", () =>
{
soloStatisticsWatcher.TriggerSuccess(
displayUpdate(
new UserStatistics
{
GlobalRank = 12_345,
@ -49,16 +43,13 @@ namespace osu.Game.Tests.Visual.Ranking
TotalScore = 123_124_231_435,
PP = 5_434
});
});
}
[Test]
public void TestAllDecreased()
{
createDisplay();
AddStep("trigger update success", () =>
{
soloStatisticsWatcher.TriggerSuccess(
displayUpdate(
new UserStatistics
{
GlobalRank = 1_234,
@ -77,7 +68,6 @@ namespace osu.Game.Tests.Visual.Ranking
TotalScore = 123_123_543_456,
PP = 5_072
});
});
}
[Test]
@ -94,7 +84,7 @@ namespace osu.Game.Tests.Visual.Ranking
};
createDisplay();
AddStep("trigger update success", () => soloStatisticsWatcher.TriggerSuccess(statistics, statistics));
displayUpdate(statistics, statistics);
}
[Test]
@ -111,32 +101,17 @@ namespace osu.Game.Tests.Visual.Ranking
};
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,
Anchor = Anchor.Centre,
Origin = Anchor.Centre
});
private class MockSoloStatisticsWatcher : ISoloStatisticsWatcher
{
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));
}
}
private void displayUpdate(UserStatistics before, UserStatistics after) =>
AddStep("display update", () => overallRanking.StatisticsUpdate.Value = new SoloStatisticsUpdate(new ScoreInfo(), 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>
/// 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>
public partial class SoloStatisticsWatcher : Component, ISoloStatisticsWatcher
public partial class SoloStatisticsWatcher : Component
{
[Resolved]
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 Bindable<SoloStatisticsUpdate?> statisticsUpdate = new Bindable<SoloStatisticsUpdate?>();
public Bindable<SoloStatisticsUpdate?> StatisticsUpdate { get; } = new Bindable<SoloStatisticsUpdate?>();
private LoadingLayer loadingLayer = null!;
private FillFlowContainer content = null!;
[Resolved]
private ISoloStatisticsWatcher statisticsWatcher { get; set; } = null!;
public OverallRanking(ScoreInfo score)
{
this.score = score;
@ -53,12 +50,12 @@ namespace osu.Game.Screens.Ranking.Statistics.User
Spacing = new Vector2(10),
Children = new Drawable[]
{
new GlobalRankChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } },
new AccuracyChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } },
new MaximumComboChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } },
new RankedScoreChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } },
new TotalScoreChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } },
new PerformancePointsChangeRow { StatisticsUpdate = { BindTarget = statisticsUpdate } }
new GlobalRankChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new AccuracyChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new MaximumComboChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new RankedScoreChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new TotalScoreChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } },
new PerformancePointsChangeRow { StatisticsUpdate = { BindTarget = StatisticsUpdate } }
}
}
};
@ -68,8 +65,7 @@ namespace osu.Game.Screens.Ranking.Statistics.User
{
base.LoadComplete();
statisticsWatcher.RegisterForStatisticsUpdateAfter(score, update => statisticsUpdate.Value = update);
statisticsUpdate.BindValueChanged(onUpdateReceived, true);
StatisticsUpdate.BindValueChanged(onUpdateReceived, true);
FinishTransforms(true);
}