1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 12:33:03 +08:00
osu-lazer/osu.Game.Tests/Visual/SongSelect/TestSceneBeatmapLeaderboard.cs

468 lines
17 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2018-06-28 12:04:39 +08:00
using System;
2022-01-28 20:59:29 +08:00
using System.Collections.Generic;
2021-06-14 14:06:33 +08:00
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
2021-06-14 14:06:33 +08:00
using osu.Framework.Audio;
using osu.Framework.Extensions;
2019-03-25 00:02:36 +08:00
using osu.Framework.Graphics;
2021-06-14 14:06:33 +08:00
using osu.Framework.Platform;
using osu.Framework.Testing;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Leaderboards;
using osu.Game.Overlays;
2021-06-14 14:06:33 +08:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu;
2019-09-19 14:23:37 +08:00
using osu.Game.Rulesets.Osu.Mods;
2018-11-28 15:12:57 +08:00
using osu.Game.Scoring;
2019-03-25 00:02:36 +08:00
using osu.Game.Screens.Select.Leaderboards;
2021-06-14 14:06:33 +08:00
using osu.Game.Tests.Resources;
2019-03-25 00:02:36 +08:00
using osu.Game.Users;
using osuTK;
2018-04-13 17:19:50 +08:00
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.SongSelect
2018-04-13 17:19:50 +08:00
{
2022-11-24 13:32:20 +08:00
public partial class TestSceneBeatmapLeaderboard : OsuTestScene
2018-04-13 17:19:50 +08:00
{
private readonly FailableLeaderboard leaderboard;
[Cached(typeof(IDialogOverlay))]
2019-12-20 13:57:14 +08:00
private readonly DialogOverlay dialogOverlay;
private ScoreManager scoreManager = null!;
private RulesetStore rulesetStore = null!;
private BeatmapManager beatmapManager = null!;
2021-06-14 14:06:33 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
dependencies.Cache(rulesetStore = new RealmRulesetStore(Realm));
dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, null, dependencies.Get<AudioManager>(), Resources, dependencies.Get<GameHost>(), Beatmap.Default));
2022-08-22 20:31:30 +08:00
dependencies.Cache(scoreManager = new ScoreManager(rulesetStore, () => beatmapManager, LocalStorage, Realm, API));
Dependencies.Cache(Realm);
2021-06-14 14:06:33 +08:00
return dependencies;
}
2019-09-19 12:19:48 +08:00
public TestSceneBeatmapLeaderboard()
2018-04-13 17:19:50 +08:00
{
2021-06-14 14:06:33 +08:00
AddRange(new Drawable[]
{
2021-06-14 14:06:33 +08:00
dialogOverlay = new DialogOverlay
{
Depth = -1
},
leaderboard = new FailableLeaderboard
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Size = new Vector2(550f, 450f),
Scope = BeatmapLeaderboardScope.Global,
}
});
2021-06-14 14:06:33 +08:00
}
[Test]
public void TestLocalScoresDisplay()
{
BeatmapInfo beatmapInfo = null!;
2021-06-14 14:06:33 +08:00
AddStep(@"Set scope", () => leaderboard.Scope = BeatmapLeaderboardScope.Local);
2021-06-14 14:06:33 +08:00
AddStep(@"Set beatmap", () =>
2018-04-13 17:19:50 +08:00
{
beatmapManager.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
2021-06-14 14:06:33 +08:00
beatmapInfo = beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps.First();
2021-10-02 23:55:29 +08:00
leaderboard.BeatmapInfo = beatmapInfo;
});
2021-06-14 14:06:33 +08:00
clearScores();
checkDisplayedCount(0);
2021-06-14 14:06:33 +08:00
2023-02-08 14:22:17 +08:00
importMoreScores(() => beatmapInfo);
checkDisplayedCount(10);
2021-06-14 14:06:33 +08:00
2023-02-08 14:22:17 +08:00
importMoreScores(() => beatmapInfo);
checkDisplayedCount(20);
2021-06-14 14:06:33 +08:00
clearScores();
checkDisplayedCount(0);
}
2018-04-13 17:19:50 +08:00
2023-02-08 10:01:54 +08:00
[Test]
public void TestLocalScoresDisplayOnBeatmapEdit()
{
BeatmapInfo beatmapInfo = null!;
string originalHash = string.Empty;
2023-02-08 10:01:54 +08:00
AddStep(@"Set scope", () => leaderboard.Scope = BeatmapLeaderboardScope.Local);
AddStep(@"Import beatmap", () =>
2023-02-08 10:01:54 +08:00
{
beatmapManager.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
beatmapInfo = beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps.First();
leaderboard.BeatmapInfo = beatmapInfo;
});
clearScores();
checkDisplayedCount(0);
2023-02-08 10:01:54 +08:00
AddStep(@"Perform initial save to guarantee stable hash", () =>
{
IBeatmap beatmap = beatmapManager.GetWorkingBeatmap(beatmapInfo).Beatmap;
beatmapManager.Save(beatmapInfo, beatmap);
originalHash = beatmapInfo.Hash;
});
2023-02-08 14:22:17 +08:00
importMoreScores(() => beatmapInfo);
checkDisplayedCount(10);
checkStoredCount(10);
2023-02-08 10:01:54 +08:00
AddStep(@"Save with changes", () =>
2023-02-08 14:22:17 +08:00
{
IBeatmap beatmap = beatmapManager.GetWorkingBeatmap(beatmapInfo).Beatmap;
beatmap.Difficulty.ApproachRate = 12;
2023-02-08 14:22:17 +08:00
beatmapManager.Save(beatmapInfo, beatmap);
});
AddAssert("Hash changed", () => beatmapInfo.Hash, () => Is.Not.EqualTo(originalHash));
checkDisplayedCount(0);
checkStoredCount(10);
2023-02-08 10:01:54 +08:00
2023-02-08 14:22:17 +08:00
importMoreScores(() => beatmapInfo);
importMoreScores(() => beatmapInfo);
checkDisplayedCount(20);
checkStoredCount(30);
AddStep(@"Revert changes", () =>
{
IBeatmap beatmap = beatmapManager.GetWorkingBeatmap(beatmapInfo).Beatmap;
beatmap.Difficulty.ApproachRate = 8;
beatmapManager.Save(beatmapInfo, beatmap);
});
AddAssert("Hash restored", () => beatmapInfo.Hash, () => Is.EqualTo(originalHash));
checkDisplayedCount(10);
checkStoredCount(30);
2023-02-08 10:01:54 +08:00
clearScores();
checkDisplayedCount(0);
checkStoredCount(0);
2023-02-08 10:01:54 +08:00
}
[Test]
2021-06-14 14:06:33 +08:00
public void TestGlobalScoresDisplay()
{
2021-06-14 14:06:33 +08:00
AddStep(@"Set scope", () => leaderboard.Scope = BeatmapLeaderboardScope.Global);
AddStep(@"New Scores", () => leaderboard.SetScores(generateSampleScores(new BeatmapInfo())));
}
[Test]
public void TestPersonalBest()
{
2019-09-19 14:23:37 +08:00
AddStep(@"Show personal best", showPersonalBest);
AddStep("null personal best position", showPersonalBestWithNullPosition);
}
[Test]
public void TestPlaceholderStates()
{
AddStep("ensure no scores displayed", () => leaderboard.SetScores(null));
2022-01-31 00:12:03 +08:00
AddStep(@"Network failure", () => leaderboard.SetErrorState(LeaderboardState.NetworkFailure));
AddStep(@"No supporter", () => leaderboard.SetErrorState(LeaderboardState.NotSupporter));
AddStep(@"Not logged in", () => leaderboard.SetErrorState(LeaderboardState.NotLoggedIn));
AddStep(@"Ruleset unavailable", () => leaderboard.SetErrorState(LeaderboardState.RulesetUnavailable));
AddStep(@"Beatmap unavailable", () => leaderboard.SetErrorState(LeaderboardState.BeatmapUnavailable));
2022-01-31 00:12:03 +08:00
AddStep(@"None selected", () => leaderboard.SetErrorState(LeaderboardState.NoneSelected));
}
2020-02-20 13:51:25 +08:00
private void showPersonalBestWithNullPosition()
{
leaderboard.SetScores(leaderboard.Scores, new ScoreInfo
2020-02-20 13:51:25 +08:00
{
Rank = ScoreRank.XH,
Accuracy = 1,
MaxCombo = 244,
TotalScore = 1707827,
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock() },
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2020-02-20 13:51:25 +08:00
{
Id = 6602580,
Username = @"waaiiru",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.ES,
},
});
2018-04-13 17:19:50 +08:00
}
2019-09-19 14:23:37 +08:00
private void showPersonalBest()
{
leaderboard.SetScores(leaderboard.Scores, new ScoreInfo
2019-09-19 14:23:37 +08:00
{
Position = 999,
Rank = ScoreRank.XH,
Accuracy = 1,
MaxCombo = 244,
TotalScore = 1707827,
Ruleset = new OsuRuleset().RulesetInfo,
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
User = new APIUser
2019-09-19 14:23:37 +08:00
{
Id = 6602580,
Username = @"waaiiru",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.ES,
}
});
2019-09-19 14:23:37 +08:00
}
2023-02-08 14:22:17 +08:00
private void importMoreScores(Func<BeatmapInfo> beatmapInfo)
2018-04-13 17:19:50 +08:00
{
2023-02-08 14:22:17 +08:00
AddStep(@"Import new scores", () =>
2021-06-14 14:06:33 +08:00
{
foreach (var score in generateSampleScores(beatmapInfo()))
scoreManager.Import(score);
2021-06-14 14:06:33 +08:00
});
}
private void clearScores()
{
2021-12-13 18:01:20 +08:00
AddStep("Clear all scores", () => scoreManager.Delete());
2021-06-14 14:06:33 +08:00
}
private void checkDisplayedCount(int expected) =>
2023-02-08 14:22:17 +08:00
AddUntilStep($"{expected} scores displayed", () => leaderboard.ChildrenOfType<LeaderboardScore>().Count(), () => Is.EqualTo(expected));
2021-06-14 14:06:33 +08:00
private void checkStoredCount(int expected) =>
AddUntilStep($"Total scores stored is {expected}", () => Realm.Run(r => r.All<ScoreInfo>().Count(s => !s.DeletePending)), () => Is.EqualTo(expected));
2021-10-02 23:55:29 +08:00
private static ScoreInfo[] generateSampleScores(BeatmapInfo beatmapInfo)
2021-06-14 14:06:33 +08:00
{
return new[]
2018-04-13 17:19:50 +08:00
{
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
Rank = ScoreRank.XH,
Accuracy = 1,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now,
Mods = new Mod[]
{
new OsuModHidden(),
new OsuModHardRock(),
new OsuModFlashlight
{
FollowDelay = { Value = 200 },
SizeMultiplier = { Value = 5 },
},
new OsuModDifficultyAdjust
{
CircleSize = { Value = 11 },
ApproachRate = { Value = 10 },
OverallDifficulty = { Value = 10 },
DrainRate = { Value = 10 },
ExtendedLimits = { Value = true }
}
},
Ruleset = new OsuRuleset().RulesetInfo,
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 6602580,
Username = @"waaiiru",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.ES,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
Rank = ScoreRank.X,
Accuracy = 1,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now.AddSeconds(-30),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 4608074,
Username = @"Skycries",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.BR,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
Rank = ScoreRank.SH,
Accuracy = 1,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now.AddSeconds(-70),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 1014222,
Username = @"eLy",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.JP,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
Rank = ScoreRank.S,
Accuracy = 1,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now.AddMinutes(-40),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 1541390,
Username = @"Toukai",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.CA,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
Rank = ScoreRank.A,
Accuracy = 1,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now.AddHours(-2),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 2243452,
Username = @"Satoruu",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.VE,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
Rank = ScoreRank.B,
Accuracy = 0.9826,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now.AddHours(-25),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 2705430,
Username = @"Mooha",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.FR,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
Rank = ScoreRank.C,
Accuracy = 0.9654,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now.AddHours(-50),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 7151382,
Username = @"Mayuri Hana",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.TH,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
2019-06-09 00:34:03 +08:00
Rank = ScoreRank.D,
2018-04-13 17:19:50 +08:00
Accuracy = 0.6025,
MaxCombo = 244,
TotalScore = 1707827,
2022-02-17 17:38:29 +08:00
Date = DateTime.Now.AddHours(-72),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 2051389,
Username = @"FunOrange",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.CA,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
2019-06-09 00:34:03 +08:00
Rank = ScoreRank.D,
2018-04-13 17:19:50 +08:00
Accuracy = 0.5140,
MaxCombo = 244,
TotalScore = 1707827,
Date = DateTime.Now.AddMonths(-3),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 6169483,
Username = @"-Hebel-",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.MX,
2018-04-13 17:19:50 +08:00
},
},
2018-11-28 17:33:01 +08:00
new ScoreInfo
2018-04-13 17:19:50 +08:00
{
2019-06-09 00:34:03 +08:00
Rank = ScoreRank.D,
2018-04-13 17:19:50 +08:00
Accuracy = 0.4222,
MaxCombo = 244,
TotalScore = 1707827,
Date = DateTime.Now.AddYears(-2),
Mods = new Mod[] { new OsuModHidden(), new OsuModHardRock(), },
BeatmapInfo = beatmapInfo,
2023-02-07 16:52:47 +08:00
BeatmapHash = beatmapInfo.Hash,
Ruleset = new OsuRuleset().RulesetInfo,
User = new APIUser
2018-04-13 17:19:50 +08:00
{
Id = 6702666,
Username = @"prhtnsm",
2022-07-18 13:40:34 +08:00
CountryCode = CountryCode.DE,
2018-04-13 17:19:50 +08:00
},
},
};
}
2022-11-24 13:32:20 +08:00
private partial class FailableLeaderboard : BeatmapLeaderboard
2018-04-13 17:19:50 +08:00
{
2022-01-31 00:12:03 +08:00
public new void SetErrorState(LeaderboardState state) => base.SetErrorState(state);
public new void SetScores(IEnumerable<ScoreInfo>? scores, ScoreInfo? userScore = null) => base.SetScores(scores, userScore);
2018-04-13 17:19:50 +08:00
}
}
}