1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 05:27:40 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneGameplayLeaderboard.cs

96 lines
3.9 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.
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2020-12-15 14:22:14 +08:00
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Screens.Play.HUD;
using osu.Game.Users;
using osuTK;
namespace osu.Game.Tests.Visual.Gameplay
{
[TestFixture]
public class TestSceneGameplayLeaderboard : OsuTestScene
{
private readonly TestGameplayLeaderboard leaderboard;
private readonly BindableDouble playerScore = new BindableDouble();
public TestSceneGameplayLeaderboard()
{
Add(leaderboard = new TestGameplayLeaderboard
{
Anchor = Anchor.Centre,
Origin = Anchor.TopCentre,
Scale = new Vector2(2),
});
}
2020-12-15 14:22:14 +08:00
[SetUpSteps]
public void SetUpSteps()
2019-08-04 23:28:40 +08:00
{
2020-12-15 14:22:14 +08:00
AddStep("reset leaderboard", () =>
{
leaderboard.Clear();
2020-12-15 14:22:14 +08:00
playerScore.Value = 1222333;
});
AddStep("add local player", () => leaderboard.Add(createLeaderboardScore(playerScore, "You", true)));
2020-12-15 14:22:14 +08:00
AddSliderStep("set player score", 50, 5000000, 1222333, v => playerScore.Value = v);
2019-08-04 23:28:40 +08:00
}
2019-07-30 17:37:01 +08:00
[Test]
public void TestPlayerScore()
{
var player2Score = new BindableDouble(1234567);
var player3Score = new BindableDouble(1111111);
AddStep("add player 2", () => leaderboard.Add(createLeaderboardScore(player2Score, "Player 2")));
AddStep("add player 3", () => leaderboard.Add(createLeaderboardScore(player3Score, "Player 3")));
AddAssert("is player 2 position #1", () => leaderboard.CheckPositionByUsername("Player 2", 1));
AddAssert("is player position #2", () => leaderboard.CheckPositionByUsername("You", 2));
AddAssert("is player 3 position #3", () => leaderboard.CheckPositionByUsername("Player 3", 3));
AddStep("set score above player 3", () => player2Score.Value = playerScore.Value - 500);
AddAssert("is player position #1", () => leaderboard.CheckPositionByUsername("You", 1));
AddAssert("is player 2 position #2", () => leaderboard.CheckPositionByUsername("Player 2", 2));
AddAssert("is player 3 position #3", () => leaderboard.CheckPositionByUsername("Player 3", 3));
AddStep("set score below players", () => player2Score.Value = playerScore.Value - 123456);
AddAssert("is player position #1", () => leaderboard.CheckPositionByUsername("You", 1));
AddAssert("is player 3 position #2", () => leaderboard.CheckPositionByUsername("Player 3", 2));
AddAssert("is player 2 position #3", () => leaderboard.CheckPositionByUsername("Player 2", 3));
}
[Test]
public void TestRandomScores()
{
int playerNumber = 1;
AddRepeatStep("add player with random score", () => leaderboard.Add(createLeaderboardScore(new BindableDouble(RNG.Next(0, 5_000_000)), $"Player {playerNumber++}")), 10);
}
private static GameplayLeaderboardScore createLeaderboardScore(BindableDouble score, string username, bool localOrReplayPlayer = false)
{
return new GameplayLeaderboardScore(new User { Username = username }, localOrReplayPlayer)
{
TotalScore = { BindTarget = score },
};
}
private class TestGameplayLeaderboard : GameplayLeaderboard
{
public bool CheckPositionByUsername(string username, int? expectedPosition)
{
var scoreItem = this.FirstOrDefault(i => i.User.Username == username);
return scoreItem != null && scoreItem.ScorePosition == expectedPosition;
}
}
}
}