1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 19:32:55 +08:00

Merge pull request #13487 from peppy/fix-song-select-leaderboard-events

Add support for song select leaderboard to handle newly imported scores
This commit is contained in:
Dan Balasescu 2021-06-14 20:39:36 +09:00 committed by GitHub
commit 385d10eeae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 24 deletions

View File

@ -2,15 +2,22 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Graphics;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Online.Leaderboards;
using osu.Game.Overlays;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Scoring;
using osu.Game.Screens.Select.Leaderboards;
using osu.Game.Tests.Resources;
using osu.Game.Users;
using osuTK;
@ -23,32 +30,98 @@ namespace osu.Game.Tests.Visual.SongSelect
[Cached]
private readonly DialogOverlay dialogOverlay;
private ScoreManager scoreManager;
private RulesetStore rulesetStore;
private BeatmapManager beatmapManager;
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
dependencies.Cache(rulesetStore = new RulesetStore(ContextFactory));
dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, ContextFactory, rulesetStore, null, dependencies.Get<AudioManager>(), Resources, dependencies.Get<GameHost>(), Beatmap.Default));
dependencies.Cache(scoreManager = new ScoreManager(rulesetStore, () => beatmapManager, LocalStorage, null, ContextFactory));
return dependencies;
}
public TestSceneBeatmapLeaderboard()
{
Add(dialogOverlay = new DialogOverlay
AddRange(new Drawable[]
{
Depth = -1
dialogOverlay = new DialogOverlay
{
Depth = -1
},
leaderboard = new FailableLeaderboard
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Size = new Vector2(550f, 450f),
Scope = BeatmapLeaderboardScope.Global,
}
});
}
[Test]
public void TestLocalScoresDisplay()
{
BeatmapInfo beatmapInfo = null;
AddStep(@"Set scope", () => leaderboard.Scope = BeatmapLeaderboardScope.Local);
AddStep(@"Set beatmap", () =>
{
beatmapManager.Import(TestResources.GetQuickTestBeatmapForImport()).Wait();
beatmapInfo = beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps.First();
leaderboard.Beatmap = beatmapInfo;
});
Add(leaderboard = new FailableLeaderboard
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Size = new Vector2(550f, 450f),
Scope = BeatmapLeaderboardScope.Global,
});
clearScores();
checkCount(0);
AddStep(@"New Scores", newScores);
loadMoreScores(() => beatmapInfo);
checkCount(10);
loadMoreScores(() => beatmapInfo);
checkCount(20);
clearScores();
checkCount(0);
}
[Test]
public void TestGlobalScoresDisplay()
{
AddStep(@"Set scope", () => leaderboard.Scope = BeatmapLeaderboardScope.Global);
AddStep(@"New Scores", () => leaderboard.Scores = generateSampleScores(null));
}
[Test]
public void TestPersonalBest()
{
AddStep(@"Show personal best", showPersonalBest);
AddStep("null personal best position", showPersonalBestWithNullPosition);
}
[Test]
public void TestPlaceholderStates()
{
AddStep(@"Empty Scores", () => leaderboard.SetRetrievalState(PlaceholderState.NoScores));
AddStep(@"Network failure", () => leaderboard.SetRetrievalState(PlaceholderState.NetworkFailure));
AddStep(@"No supporter", () => leaderboard.SetRetrievalState(PlaceholderState.NotSupporter));
AddStep(@"Not logged in", () => leaderboard.SetRetrievalState(PlaceholderState.NotLoggedIn));
AddStep(@"Unavailable", () => leaderboard.SetRetrievalState(PlaceholderState.Unavailable));
AddStep(@"None selected", () => leaderboard.SetRetrievalState(PlaceholderState.NoneSelected));
}
[Test]
public void TestBeatmapStates()
{
foreach (BeatmapSetOnlineStatus status in Enum.GetValues(typeof(BeatmapSetOnlineStatus)))
AddStep($"{status} beatmap", () => showBeatmapWithStatus(status));
AddStep("null personal best position", showPersonalBestWithNullPosition);
}
private void showPersonalBestWithNullPosition()
@ -96,9 +169,26 @@ namespace osu.Game.Tests.Visual.SongSelect
};
}
private void newScores()
private void loadMoreScores(Func<BeatmapInfo> beatmapInfo)
{
var scores = new[]
AddStep(@"Load new scores via manager", () =>
{
foreach (var score in generateSampleScores(beatmapInfo()))
scoreManager.Import(score).Wait();
});
}
private void clearScores()
{
AddStep("Clear all scores", () => scoreManager.Delete(scoreManager.GetAllUsableScores()));
}
private void checkCount(int expected) =>
AddUntilStep("Correct count displayed", () => leaderboard.ChildrenOfType<LeaderboardScore>().Count() == expected);
private static ScoreInfo[] generateSampleScores(BeatmapInfo beatmap)
{
return new[]
{
new ScoreInfo
{
@ -107,6 +197,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 6602580,
@ -125,6 +216,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 4608074,
@ -143,6 +235,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 1014222,
@ -161,6 +254,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 1541390,
@ -179,6 +273,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 2243452,
@ -197,6 +292,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 2705430,
@ -215,6 +311,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 7151382,
@ -233,6 +330,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 2051389,
@ -251,6 +349,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 6169483,
@ -269,6 +368,7 @@ namespace osu.Game.Tests.Visual.SongSelect
MaxCombo = 244,
TotalScore = 1707827,
//Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
Beatmap = beatmap,
User = new User
{
Id = 6702666,
@ -281,8 +381,6 @@ namespace osu.Game.Tests.Visual.SongSelect
},
},
};
leaderboard.Scores = scores;
}
private void showBeatmapWithStatus(BeatmapSetOnlineStatus status)

View File

@ -44,9 +44,9 @@ namespace osu.Game.Online.Leaderboards
protected override Container<Drawable> Content => content;
private IEnumerable<TScoreInfo> scores;
private ICollection<TScoreInfo> scores;
public IEnumerable<TScoreInfo> Scores
public ICollection<TScoreInfo> Scores
{
get => scores;
set
@ -126,7 +126,7 @@ namespace osu.Game.Online.Leaderboards
return;
scope = value;
UpdateScores();
RefreshScores();
}
}
@ -154,7 +154,7 @@ namespace osu.Game.Online.Leaderboards
case PlaceholderState.NetworkFailure:
replacePlaceholder(new ClickablePlaceholder(@"Couldn't fetch scores!", FontAwesome.Solid.Sync)
{
Action = UpdateScores,
Action = RefreshScores
});
break;
@ -254,8 +254,6 @@ namespace osu.Game.Online.Leaderboards
apiState.BindValueChanged(onlineStateChanged, true);
}
public void RefreshScores() => UpdateScores();
private APIRequest getScoresRequest;
protected abstract bool IsOnlineScope { get; }
@ -267,12 +265,14 @@ namespace osu.Game.Online.Leaderboards
case APIState.Online:
case APIState.Offline:
if (IsOnlineScope)
UpdateScores();
RefreshScores();
break;
}
});
public void RefreshScores() => Scheduler.AddOnce(UpdateScores);
protected void UpdateScores()
{
// don't display any scores or placeholder until the first Scores_Set has been called.
@ -290,7 +290,7 @@ namespace osu.Game.Online.Leaderboards
getScoresRequest = FetchScores(scores => Schedule(() =>
{
Scores = scores;
Scores = scores.ToArray();
PlaceholderState = Scores.Any() ? PlaceholderState.Successful : PlaceholderState.NoScores;
}));

View File

@ -44,6 +44,8 @@ namespace osu.Game.Screens.Select.Leaderboards
private IBindable<WeakReference<ScoreInfo>> itemRemoved;
private IBindable<WeakReference<ScoreInfo>> itemAdded;
/// <summary>
/// Whether to apply the game's currently selected mods as a filter when retrieving scores.
/// </summary>
@ -85,6 +87,9 @@ namespace osu.Game.Screens.Select.Leaderboards
itemRemoved = scoreManager.ItemRemoved.GetBoundCopy();
itemRemoved.BindValueChanged(onScoreRemoved);
itemAdded = scoreManager.ItemUpdated.GetBoundCopy();
itemAdded.BindValueChanged(onScoreAdded);
}
protected override void Reset()
@ -93,7 +98,25 @@ namespace osu.Game.Screens.Select.Leaderboards
TopScore = null;
}
private void onScoreRemoved(ValueChangedEvent<WeakReference<ScoreInfo>> score) => Schedule(RefreshScores);
private void onScoreRemoved(ValueChangedEvent<WeakReference<ScoreInfo>> score) =>
scoreStoreChanged(score);
private void onScoreAdded(ValueChangedEvent<WeakReference<ScoreInfo>> score) =>
scoreStoreChanged(score);
private void scoreStoreChanged(ValueChangedEvent<WeakReference<ScoreInfo>> score)
{
if (Scope != BeatmapLeaderboardScope.Local)
return;
if (score.NewValue.TryGetTarget(out var scoreInfo))
{
if (Beatmap?.ID != scoreInfo.BeatmapInfoID)
return;
}
RefreshScores();
}
protected override bool IsOnlineScope => Scope != BeatmapLeaderboardScope.Local;