<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Database;
using osu.Game.Online.Spectator;
using osu.Game.Replays.Legacy;
using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.Play.HUD;
using osu.Game.Tests.Visual.Online;

namespace osu.Game.Tests.Visual.Gameplay
{
    public class TestSceneMultiplayerGameplayLeaderboard : OsuTestScene
    {
        [Cached(typeof(SpectatorStreamingClient))]
        private TestMultiplayerStreaming streamingClient = new TestMultiplayerStreaming(16);

        [Cached(typeof(UserLookupCache))]
        private UserLookupCache lookupCache = new TestSceneCurrentlyPlayingDisplay.TestUserLookupCache();

        private MultiplayerGameplayLeaderboard leaderboard;

        protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };

        public TestSceneMultiplayerGameplayLeaderboard()
        {
            base.Content.Children = new Drawable[]
            {
                streamingClient,
                lookupCache,
                Content
            };
        }

        [SetUpSteps]
        public void SetUpSteps()
        {
            AddStep("create leaderboard", () =>
            {
                OsuScoreProcessor scoreProcessor;
                Beatmap.Value = CreateWorkingBeatmap(Ruleset.Value);

                var playable = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value);

                streamingClient.Start(Beatmap.Value.BeatmapInfo.OnlineBeatmapID ?? 0);

                Children = new Drawable[]
                {
                    scoreProcessor = new OsuScoreProcessor(),
                };

                scoreProcessor.ApplyBeatmap(playable);

                LoadComponentAsync(leaderboard = new MultiplayerGameplayLeaderboard(scoreProcessor, streamingClient.PlayingUsers.ToArray())
                {
                    Anchor = Anchor.Centre,
                    Origin = Anchor.Centre,
                }, Add);
            });

            AddUntilStep("wait for load", () => leaderboard.IsLoaded);
        }

        [Test]
        public void TestScoreUpdates()
        {
            AddRepeatStep("update state", () => streamingClient.RandomlyUpdateState(), 100);
        }

        public class TestMultiplayerStreaming : SpectatorStreamingClient
        {
            public new BindableList<int> PlayingUsers => (BindableList<int>)base.PlayingUsers;

            private readonly int totalUsers;

            public TestMultiplayerStreaming(int totalUsers)
            {
                this.totalUsers = totalUsers;
            }

            public void Start(int beatmapId)
            {
                for (int i = 0; i < totalUsers; i++)
                {
                    ((ISpectatorClient)this).UserBeganPlaying(i, new SpectatorState
                    {
                        BeatmapID = beatmapId,
                        RulesetID = 0,
                    });
                }
            }

            private readonly Dictionary<int, FrameHeader> lastHeaders = new Dictionary<int, FrameHeader>();

            public void RandomlyUpdateState()
            {
                foreach (var userId in PlayingUsers)
                {
                    if (RNG.Next(0, 1) == 1)
                        continue;

                    if (!lastHeaders.TryGetValue(userId, out var header))
                    {
                        lastHeaders[userId] = header = new FrameHeader(new ScoreInfo<commit_msg>Fix intended random factor not being random in test<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Database;
using osu.Game.Online.Spectator;
using osu.Game.Replays.Legacy;
using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.Play.HUD;
using osu.Game.Tests.Visual.Online;

namespace osu.Game.Tests.Visual.Gameplay
{
    public class TestSceneMultiplayerGameplayLeaderboard : OsuTestScene
    {
        [Cached(typeof(SpectatorStreamingClient))]
        private TestMultiplayerStreaming streamingClient = new TestMultiplayerStreaming(16);

        [Cached(typeof(UserLookupCache))]
        private UserLookupCache lookupCache = new TestSceneCurrentlyPlayingDisplay.TestUserLookupCache();

        private MultiplayerGameplayLeaderboard leaderboard;

        protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };

        public TestSceneMultiplayerGameplayLeaderboard()
        {
            base.Content.Children = new Drawable[]
            {
                streamingClient,
                lookupCache,
                Content
            };
        }

        [SetUpSteps]
        public void SetUpSteps()
        {
            AddStep("create leaderboard", () =>
            {
                OsuScoreProcessor scoreProcessor;
                Beatmap.Value = CreateWorkingBeatmap(Ruleset.Value);

                var playable = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value);

                streamingClient.Start(Beatmap.Value.BeatmapInfo.OnlineBeatmapID ?? 0);

                Children = new Drawable[]
                {
                    scoreProcessor = new OsuScoreProcessor(),
                };

                scoreProcessor.ApplyBeatmap(playable);

                LoadComponentAsync(leaderboard = new MultiplayerGameplayLeaderboard(scoreProcessor, streamingClient.PlayingUsers.ToArray())
                {
                    Anchor = Anchor.Centre,
                    Origin = Anchor.Centre,
                }, Add);
            });

            AddUntilStep("wait for load", () => leaderboard.IsLoaded);
        }

        [Test]
        public void TestScoreUpdates()
        {
            AddRepeatStep("update state", () => streamingClient.RandomlyUpdateState(), 100);
        }

        public class TestMultiplayerStreaming : SpectatorStreamingClient
        {
            public new BindableList<int> PlayingUsers => (BindableList<int>)base.PlayingUsers;

            private readonly int totalUsers;

            public TestMultiplayerStreaming(int totalUsers)
            {
                this.totalUsers = totalUsers;
            }

            public void Start(int beatmapId)
            {
                for (int i = 0; i < totalUsers; i++)
                {
                    ((ISpectatorClient)this).UserBeganPlaying(i, new SpectatorState
                    {
                        BeatmapID = beatmapId,
                        RulesetID = 0,
                    });
                }
            }

            private readonly Dictionary<int, FrameHeader> lastHeaders = new Dictionary<int, FrameHeader>();

            public void RandomlyUpdateState()
            {
                foreach (var userId in PlayingUsers)
                {
                    if (RNG.NextBool())
                        continue;

                    if (!lastHeaders.TryGetValue(userId, out var header))
                    {
                        lastHeaders[userId] = header = new FrameHeader(new ScoreInfo
{'fraction_non_alphanumeric': 0.08164548999874198, 'fraction_numerical': 0.002390237765756699, 'mean_word_length': 1.9206465833945627, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7489488', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1962, 'n_words': 425}