From 6fe1b68510397ccccaf4339d2974784864ad02bb Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 7 Apr 2021 22:30:40 +0900 Subject: [PATCH 01/88] Add a way to retrieve new WorkingBeatmap instances --- osu.Game/Beatmaps/BeatmapManager.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index b4ea898b7d..69b513d256 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -281,8 +281,9 @@ namespace osu.Game.Beatmaps /// /// The beatmap to lookup. /// The currently loaded . Allows for optimisation where elements are shared with the new beatmap. May be returned if beatmapInfo requested matches + /// Whether to bypass the cache and return a new instance. /// A instance correlating to the provided . - public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null) + public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null, bool bypassCache = false) { if (beatmapInfo?.ID > 0 && previous != null && previous.BeatmapInfo?.ID == beatmapInfo.ID) return previous; @@ -301,9 +302,14 @@ namespace osu.Game.Beatmaps lock (workingCache) { - var working = workingCache.FirstOrDefault(w => w.BeatmapInfo?.ID == beatmapInfo.ID); - if (working != null) - return working; + BeatmapManagerWorkingBeatmap working; + + if (!bypassCache) + { + working = workingCache.FirstOrDefault(w => w.BeatmapInfo?.ID == beatmapInfo.ID); + if (working != null) + return working; + } beatmapInfo.Metadata ??= beatmapInfo.BeatmapSet.Metadata; From d64b236f8619451b3827b3dd1ca6a263ee9c7ea9 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 8 Apr 2021 21:27:16 +0900 Subject: [PATCH 02/88] Add a container that provides an isolated gameplay context --- .../Spectate/GameplayIsolationContainer.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs new file mode 100644 index 0000000000..3ccb67d342 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs @@ -0,0 +1,43 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using osu.Framework.Allocation; +using osu.Framework.Bindables; +using osu.Framework.Graphics.Containers; +using osu.Game.Beatmaps; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Mods; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public class GameplayIsolationContainer : Container + { + [Cached] + private readonly Bindable ruleset = new Bindable(); + + [Cached] + private readonly Bindable beatmap = new Bindable(); + + [Cached] + private readonly Bindable> mods = new Bindable>(); + + public GameplayIsolationContainer(WorkingBeatmap beatmap, RulesetInfo ruleset, IReadOnlyList mods) + { + this.beatmap.Value = beatmap; + this.ruleset.Value = ruleset; + this.mods.Value = mods; + + beatmap.LoadTrack(); + } + + protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) + { + var dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); + dependencies.CacheAs(ruleset.BeginLease(false)); + dependencies.CacheAs(beatmap.BeginLease(false)); + dependencies.CacheAs(mods.BeginLease(false)); + return dependencies; + } + } +} From 709016f0d639f6899de78d5b2e153ec3775d829a Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 8 Apr 2021 22:07:00 +0900 Subject: [PATCH 03/88] Add initial multiplayer screen implementation --- .../Spectate/MultiplayerSpectator.cs | 146 ++++++++++++++++++ .../Spectate/MultiplayerSpectatorPlayer.cs | 19 +++ .../MultiplayerSpectatorPlayerLoader.cs | 26 ++++ .../Multiplayer/Spectate/PlayerGrid.cs | 13 ++ .../Multiplayer/Spectate/PlayerInstance.cs | 55 +++++++ .../Screens/Play/SpectatorPlayerLoader.cs | 7 +- 6 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayerLoader.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs new file mode 100644 index 0000000000..6b8249eae0 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -0,0 +1,146 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Linq; +using osu.Framework.Allocation; +using osu.Framework.Audio; +using osu.Framework.Bindables; +using osu.Framework.Graphics; +using osu.Framework.Testing; +using osu.Framework.Utils; +using osu.Game.Online.Rooms; +using osu.Game.Online.Spectator; +using osu.Game.Screens.Play; +using osu.Game.Screens.Spectate; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public class MultiplayerSpectator : SpectatorScreen + { + private const double min_duration_to_allow_playback = 50; + private const double max_sync_offset = 2; + + // Isolates beatmap/ruleset to this screen. + public override bool DisallowExternalBeatmapRulesetChanges => true; + + public bool AllPlayersLoaded => instances.All(p => p?.PlayerLoaded == true); + + [Resolved] + private SpectatorStreamingClient spectatorClient { get; set; } + + private readonly int[] userIds; + private readonly PlayerInstance[] instances; + + private PlayerGrid grid; + + public MultiplayerSpectator(PlaylistItem playlistItem, int[] userIds) + : this(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) + { + } + + private MultiplayerSpectator(int[] userIds) + : base(userIds) + { + this.userIds = userIds; + instances = new PlayerInstance[userIds.Length]; + } + + [BackgroundDependencyLoader] + private void load() + { + InternalChild = grid = new PlayerGrid + { + RelativeSizeAxes = Axes.Both + }; + } + + protected override void Update() + { + base.Update(); + updatePlayTime(); + } + + private bool gameplayStarted; + + private void updatePlayTime() + { + if (gameplayStarted) + { + ensurePlaying(instances.Select(i => i.Beatmap.Track.CurrentTime).Max()); + return; + } + + // Make sure all players are loaded. + if (!AllPlayersLoaded) + { + ensureAllStopped(); + return; + } + + if (!instances.All(i => i.Score.Replay.Frames.Count > 0)) + { + ensureAllStopped(); + return; + } + + gameplayStarted = true; + } + + private void ensureAllStopped() + { + foreach (var inst in instances) + inst.ChildrenOfType().SingleOrDefault()?.Stop(); + } + + private readonly BindableDouble catchupFrequencyAdjustment = new BindableDouble(2.0); + + private void ensurePlaying(double targetTime) + { + foreach (var inst in instances) + { + double lastFrameTime = inst.Score.Replay.Frames.Select(f => f.Time).Last(); + double currentTime = inst.Beatmap.Track.CurrentTime; + + // If we have enough frames to play back, start playback. + if (Precision.DefinitelyBigger(lastFrameTime, currentTime, min_duration_to_allow_playback)) + { + inst.ChildrenOfType().Single().Start(); + + if (targetTime < lastFrameTime && targetTime > currentTime + 16) + inst.Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + else + inst.Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + } + else + inst.Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + } + } + + protected override void OnUserStateChanged(int userId, SpectatorState spectatorState) + { + } + + protected override void StartGameplay(int userId, GameplayState gameplayState) + { + int userIndex = getIndexForUser(userId); + var existingInstance = instances[userIndex]; + + if (existingInstance != null) + grid.Remove(existingInstance); + + LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score), d => + { + if (instances[userIndex] == d) + grid.Add(d); + }); + } + + protected override void EndGameplay(int userId) + { + spectatorClient.StopWatchingUser(userId); + } + + private int getIndexForUser(int userId) => Array.IndexOf(userIds, userId); + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs new file mode 100644 index 0000000000..bad093f666 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -0,0 +1,19 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Game.Rulesets.Scoring; +using osu.Game.Scoring; +using osu.Game.Screens.Play; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public class MultiplayerSpectatorPlayer : SpectatorPlayer + { + public new ScoreProcessor ScoreProcessor => base.ScoreProcessor; + + public MultiplayerSpectatorPlayer(Score score) + : base(score) + { + } + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayerLoader.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayerLoader.cs new file mode 100644 index 0000000000..c8f16b5e90 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayerLoader.cs @@ -0,0 +1,26 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using osu.Game.Scoring; +using osu.Game.Screens.Menu; +using osu.Game.Screens.Play; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public class MultiplayerSpectatorPlayerLoader : SpectatorPlayerLoader + { + public MultiplayerSpectatorPlayerLoader(Score score, Func createPlayer) + : base(score, createPlayer) + { + } + + protected override void LogoArriving(OsuLogo logo, bool resuming) + { + } + + protected override void LogoExiting(OsuLogo logo) + { + } + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs index 830378f129..b2d114d9cc 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs @@ -75,6 +75,19 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate cellContainer.Add(cell); } + public void Remove(Drawable content) + { + var cell = cellContainer.FirstOrDefault(c => c.Content == content); + if (cell == null) + return; + + if (cell.IsMaximised) + toggleMaximisationState(cell); + + cellContainer.Remove(cell); + facadeContainer.Remove(facadeContainer[cell.FacadeIndex]); + } + /// /// The content added to this grid. /// diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs new file mode 100644 index 0000000000..ab7d3e2502 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -0,0 +1,55 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Game.Beatmaps; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Scoring; +using osu.Game.Scoring; +using osu.Game.Screens.Play; +using osu.Game.Users; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public class PlayerInstance : CompositeDrawable + { + public bool PlayerLoaded => stack?.CurrentScreen is Player; + + public User User => Score.ScoreInfo.User; + public ScoreProcessor ScoreProcessor => player?.ScoreProcessor; + + public WorkingBeatmap Beatmap { get; private set; } + public Ruleset Ruleset { get; private set; } + + public readonly Score Score; + + private OsuScreenStack stack; + private MultiplayerSpectatorPlayer player; + + public PlayerInstance(Score score) + { + Score = score; + } + + [BackgroundDependencyLoader] + private void load(BeatmapManager beatmapManager) + { + Beatmap = beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true); + Ruleset = Score.ScoreInfo.Ruleset.CreateInstance(); + + InternalChild = new GameplayIsolationContainer(Beatmap, Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) + { + RelativeSizeAxes = Axes.Both, + Child = new DrawSizePreservingFillContainer + { + RelativeSizeAxes = Axes.Both, + Child = stack = new OsuScreenStack() + } + }; + + stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => player = new MultiplayerSpectatorPlayer(Score))); + } + } +} diff --git a/osu.Game/Screens/Play/SpectatorPlayerLoader.cs b/osu.Game/Screens/Play/SpectatorPlayerLoader.cs index 580af81166..bdd23962dc 100644 --- a/osu.Game/Screens/Play/SpectatorPlayerLoader.cs +++ b/osu.Game/Screens/Play/SpectatorPlayerLoader.cs @@ -12,7 +12,12 @@ namespace osu.Game.Screens.Play public readonly ScoreInfo Score; public SpectatorPlayerLoader(Score score) - : base(() => new SpectatorPlayer(score)) + : this(score, () => new SpectatorPlayer(score)) + { + } + + public SpectatorPlayerLoader(Score score, Func createPlayer) + : base(createPlayer) { if (score.Replay == null) throw new ArgumentException($"{nameof(score)} must have a non-null {nameof(score.Replay)}.", nameof(score)); From 7d276144b871f219d07321480aea1852e9aa7d44 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 8 Apr 2021 22:13:54 +0900 Subject: [PATCH 04/88] Fix player sizing + masking --- .../Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index ab7d3e2502..5689a3222a 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -31,6 +31,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public PlayerInstance(Score score) { Score = score; + + RelativeSizeAxes = Axes.Both; + Masking = true; } [BackgroundDependencyLoader] From 1b5679b0d75509eae6ba24761965ec5904020fbd Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 8 Apr 2021 22:14:26 +0900 Subject: [PATCH 05/88] Refactor ctor --- .../Multiplayer/Spectate/MultiplayerSpectator.cs | 15 +++------------ osu.Game/Screens/Spectate/SpectatorScreen.cs | 6 +++--- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 6b8249eae0..8fa9dcf3af 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -9,7 +9,6 @@ using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Testing; using osu.Framework.Utils; -using osu.Game.Online.Rooms; using osu.Game.Online.Spectator; using osu.Game.Screens.Play; using osu.Game.Screens.Spectate; @@ -29,20 +28,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate [Resolved] private SpectatorStreamingClient spectatorClient { get; set; } - private readonly int[] userIds; private readonly PlayerInstance[] instances; - private PlayerGrid grid; - public MultiplayerSpectator(PlaylistItem playlistItem, int[] userIds) - : this(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) + public MultiplayerSpectator(int[] userIds) + : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) { - } - - private MultiplayerSpectator(int[] userIds) - : base(userIds) - { - this.userIds = userIds; instances = new PlayerInstance[userIds.Length]; } @@ -141,6 +132,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate spectatorClient.StopWatchingUser(userId); } - private int getIndexForUser(int userId) => Array.IndexOf(userIds, userId); + private int getIndexForUser(int userId) => Array.IndexOf(UserIds, userId); } } diff --git a/osu.Game/Screens/Spectate/SpectatorScreen.cs b/osu.Game/Screens/Spectate/SpectatorScreen.cs index 6dd3144fc8..b7e1b8496c 100644 --- a/osu.Game/Screens/Spectate/SpectatorScreen.cs +++ b/osu.Game/Screens/Spectate/SpectatorScreen.cs @@ -26,7 +26,7 @@ namespace osu.Game.Screens.Spectate /// public abstract class SpectatorScreen : OsuScreen { - private readonly int[] userIds; + protected readonly int[] UserIds; [Resolved] private BeatmapManager beatmaps { get; set; } @@ -55,7 +55,7 @@ namespace osu.Game.Screens.Spectate /// The users to spectate. protected SpectatorScreen(params int[] userIds) { - this.userIds = userIds; + this.UserIds = userIds; } protected override void LoadComplete() @@ -66,7 +66,7 @@ namespace osu.Game.Screens.Spectate spectatorClient.OnUserFinishedPlaying += userFinishedPlaying; spectatorClient.OnNewFrames += userSentFrames; - foreach (var id in userIds) + foreach (var id in UserIds) { userLookupCache.GetUserAsync(id).ContinueWith(u => Schedule(() => { From 7958a257e88d04a97e9b72e4a4493eb67dc28141 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 8 Apr 2021 22:14:30 +0900 Subject: [PATCH 06/88] Add tests --- .../Multiplayer/TestSceneMultiplayerScreen.cs | 327 ++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100644 osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs new file mode 100644 index 0000000000..d085827986 --- /dev/null +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs @@ -0,0 +1,327 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using NUnit.Framework; +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Framework.Testing; +using osu.Framework.Utils; +using osu.Game.Beatmaps; +using osu.Game.Database; +using osu.Game.Online; +using osu.Game.Online.Spectator; +using osu.Game.Replays.Legacy; +using osu.Game.Scoring; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; +using osu.Game.Screens.Play; +using osu.Game.Tests.Beatmaps.IO; +using osu.Game.Users; + +namespace osu.Game.Tests.Visual.Multiplayer +{ + public class TestSceneMultiplayerSpectator : MultiplayerTestScene + { + [Cached(typeof(SpectatorStreamingClient))] + private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient(); + + [Cached(typeof(UserLookupCache))] + private UserLookupCache lookupCache = new TestUserLookupCache(); + + [Resolved] + private OsuGameBase game { get; set; } + + [Resolved] + private BeatmapManager beatmapManager { get; set; } + + private MultiplayerSpectator spectator; + + private readonly List playingUserIds = new List(); + private readonly Dictionary nextFrame = new Dictionary(); + + private BeatmapSetInfo importedSet; + private BeatmapInfo importedBeatmap; + private int importedBeatmapId; + + [BackgroundDependencyLoader] + private void load() + { + importedSet = ImportBeatmapTest.LoadOszIntoOsu(game, virtualTrack: true).Result; + importedBeatmap = importedSet.Beatmaps.First(b => b.RulesetID == 0); + importedBeatmapId = importedBeatmap.OnlineBeatmapID ?? -1; + } + + public override void SetUpSteps() + { + base.SetUpSteps(); + + AddStep("reset sent frames", () => nextFrame.Clear()); + + AddStep("add streaming client", () => + { + Remove(testSpectatorStreamingClient); + Add(testSpectatorStreamingClient); + }); + + AddStep("finish previous gameplay", () => + { + foreach (var id in playingUserIds) + testSpectatorStreamingClient.EndPlay(id, importedBeatmapId); + playingUserIds.Clear(); + }); + } + + [Test] + public void TestGeneral() + { + int[] userIds = Enumerable.Range(0, 4).Select(i => 55 + i).ToArray(); + + start(userIds); + loadSpectateScreen(); + + sendFrames(userIds, 1000); + AddWaitStep("wait a bit", 20); + } + + [Test] + public void TestPlayersMustStartSimultaneously() + { + start(new[] { 55, 56 }); + loadSpectateScreen(); + + // Send frames for one player only, both should remain paused. + sendFrames(55, 20); + checkPausedInstant(55, true); + checkPausedInstant(56, true); + + // Send frames for the other player, both should now start playing. + sendFrames(56, 20); + checkPausedInstant(55, false); + checkPausedInstant(56, false); + } + + [Test] + public void TestPlayersDoNotStartSimultaneouslyIfBufferingFor15Seconds() + { + start(new[] { 55, 56 }); + loadSpectateScreen(); + + // Send frames for one player only, both should remain paused. + sendFrames(55, 20); + checkPausedInstant(55, true); + checkPausedInstant(56, true); + + // Wait 15 seconds... + AddWaitStep("wait 15 seconds", (int)(15000 / TimePerAction)); + + // Player 1 should start playing by itself, player 2 should remain paused. + checkPausedInstant(55, false); + checkPausedInstant(56, true); + } + + [Test] + public void TestPlayersContinueWhileOthersBuffer() + { + start(new[] { 55, 56 }); + loadSpectateScreen(); + + // Send initial frames for both players. A few more for player 1. + sendFrames(55, 20); + sendFrames(56, 10); + checkPausedInstant(55, false); + checkPausedInstant(56, false); + + // Eventually player 2 will pause, player 1 must remain running. + checkPaused(56, true); + checkPausedInstant(55, false); + + // Eventually both players will run out of frames and should pause. + checkPaused(55, true); + checkPausedInstant(56, true); + + // Send more frames for the first player only. Player 1 should start playing with player 2 remaining paused. + sendFrames(55, 20); + checkPausedInstant(56, true); + checkPausedInstant(55, false); + + // Send more frames for the second player. Both should be playing + sendFrames(56, 20); + checkPausedInstant(56, false); + checkPausedInstant(55, false); + } + + [Test] + public void TestPlayersCatchUpAfterFallingBehind() + { + start(new[] { 55, 56 }); + loadSpectateScreen(); + + // Send initial frames for both players. A few more for player 1. + sendFrames(55, 100); + sendFrames(56, 10); + checkPausedInstant(55, false); + checkPausedInstant(56, false); + + // Eventually player 2 will run out of frames and should pause. + checkPaused(56, true); + AddWaitStep("wait a few more frames", 10); + + // Send more frames for player 2. It should unpause. + sendFrames(56, 100); + checkPausedInstant(56, false); + + // Player 2 should catch up to player 1 after unpausing. + AddUntilStep("player 1 time == player 2 time", () => Precision.AlmostEquals(getGameplayTime(55), getGameplayTime(56), 16)); + } + + private void loadSpectateScreen() + { + AddStep("load screen", () => + { + Beatmap.Value = beatmapManager.GetWorkingBeatmap(importedBeatmap); + Ruleset.Value = importedBeatmap.Ruleset; + + LoadScreen(spectator = new MultiplayerSpectator(playingUserIds.ToArray())); + }); + + AddUntilStep("wait for screen load", () => spectator.LoadState == LoadState.Loaded && spectator.AllPlayersLoaded); + } + + private void start(int userId, int? beatmapId = null) => start(new[] { userId }, beatmapId); + + private void start(int[] userIds, int? beatmapId = null) + { + AddStep("start play", () => + { + foreach (int id in userIds) + { + Client.CurrentMatchPlayingUserIds.Add(id); + testSpectatorStreamingClient.StartPlay(id, beatmapId ?? importedBeatmapId); + playingUserIds.Add(id); + nextFrame[id] = 0; + } + }); + } + + private void finish(int userId, int? beatmapId = null) + { + AddStep("end play", () => + { + testSpectatorStreamingClient.EndPlay(userId, beatmapId ?? importedBeatmapId); + playingUserIds.Remove(userId); + nextFrame.Remove(userId); + }); + } + + private void sendFrames(int userId, int count = 10) => sendFrames(new[] { userId }, count); + + private void sendFrames(int[] userIds, int count = 10) + { + AddStep("send frames", () => + { + foreach (int id in userIds) + { + testSpectatorStreamingClient.SendFrames(id, nextFrame[id], count); + nextFrame[id] += count; + } + }); + } + + private void checkPaused(int userId, bool state) => + AddUntilStep($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().IsPaused.Value == state); + + private void checkPausedInstant(int userId, bool state) => + AddAssert($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().IsPaused.Value == state); + + private double getGameplayTime(int userId) => getPlayer(userId).ChildrenOfType().Single().GameplayClock.CurrentTime; + + private Player getPlayer(int userId) => getInstance(userId).ChildrenOfType().Single(); + + private PlayerInstance getInstance(int userId) => spectator.ChildrenOfType().Single(p => p.User.Id == userId); + + public class TestSpectatorStreamingClient : SpectatorStreamingClient + { + private readonly Dictionary userBeatmapDictionary = new Dictionary(); + private readonly Dictionary userSentStateDictionary = new Dictionary(); + + public TestSpectatorStreamingClient() + : base(new DevelopmentEndpointConfiguration()) + { + } + + public void StartPlay(int userId, int beatmapId) + { + userBeatmapDictionary[userId] = beatmapId; + userSentStateDictionary[userId] = false; + + sendState(userId, beatmapId); + } + + public void EndPlay(int userId, int beatmapId) + { + ((ISpectatorClient)this).UserFinishedPlaying(userId, new SpectatorState + { + BeatmapID = beatmapId, + RulesetID = 0, + }); + + userSentStateDictionary[userId] = false; + } + + public void SendFrames(int userId, int index, int count) + { + var frames = new List(); + + for (int i = index; i < index + count; i++) + { + var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1; + + frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState)); + } + + var bundle = new FrameDataBundle(new ScoreInfo { Combo = index + count }, frames); + ((ISpectatorClient)this).UserSentFrames(userId, bundle); + + if (!userSentStateDictionary[userId]) + sendState(userId, userBeatmapDictionary[userId]); + } + + public override void WatchUser(int userId) + { + if (userSentStateDictionary[userId]) + { + // usually the server would do this. + sendState(userId, userBeatmapDictionary[userId]); + } + + base.WatchUser(userId); + } + + private void sendState(int userId, int beatmapId) + { + ((ISpectatorClient)this).UserBeganPlaying(userId, new SpectatorState + { + BeatmapID = beatmapId, + RulesetID = 0, + }); + + userSentStateDictionary[userId] = true; + } + } + + internal class TestUserLookupCache : UserLookupCache + { + protected override Task ComputeValueAsync(int lookup, CancellationToken token = default) + { + return Task.FromResult(new User + { + Id = lookup, + Username = $"User {lookup}" + }); + } + } + } +} From ecd0b84d944ed82e9de9e244c32230918846055b Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 8 Apr 2021 22:15:07 +0900 Subject: [PATCH 07/88] Use max_sync_offset constant --- .../OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 8fa9dcf3af..90cf031584 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -98,7 +98,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { inst.ChildrenOfType().Single().Start(); - if (targetTime < lastFrameTime && targetTime > currentTime + 16) + if (targetTime < lastFrameTime && targetTime > currentTime + max_sync_offset) inst.Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); else inst.Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); From 8005f146a914b26bbe87d763d8ee7d073101711c Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 18:37:11 +0900 Subject: [PATCH 08/88] Fix inspection --- osu.Game/Screens/Spectate/SpectatorScreen.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/Spectate/SpectatorScreen.cs b/osu.Game/Screens/Spectate/SpectatorScreen.cs index b7e1b8496c..6fa045c962 100644 --- a/osu.Game/Screens/Spectate/SpectatorScreen.cs +++ b/osu.Game/Screens/Spectate/SpectatorScreen.cs @@ -55,7 +55,7 @@ namespace osu.Game.Screens.Spectate /// The users to spectate. protected SpectatorScreen(params int[] userIds) { - this.UserIds = userIds; + UserIds = userIds; } protected override void LoadComplete() From 4fa51d5ec87a9b5592968737c7e908cfaa0da10b Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 18:41:48 +0900 Subject: [PATCH 09/88] Add leaderboard to multiplayer spectate screen --- .../Spectate/MultiplayerSpectator.cs | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 90cf031584..d415669cb1 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -7,6 +7,7 @@ using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Bindables; using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; using osu.Framework.Testing; using osu.Framework.Utils; using osu.Game.Online.Spectator; @@ -30,6 +31,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly PlayerInstance[] instances; private PlayerGrid grid; + private MultiplayerSpectatorLeaderboard leaderboard; public MultiplayerSpectator(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) @@ -40,10 +42,35 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate [BackgroundDependencyLoader] private void load() { - InternalChild = grid = new PlayerGrid + Container leaderboardContainer; + + InternalChild = new GridContainer { - RelativeSizeAxes = Axes.Both + RelativeSizeAxes = Axes.Both, + ColumnDimensions = new[] + { + new Dimension(GridSizeMode.AutoSize) + }, + Content = new[] + { + new Drawable[] + { + leaderboardContainer = new Container + { + RelativeSizeAxes = Axes.Y, + AutoSizeAxes = Axes.X + }, + grid = new PlayerGrid { RelativeSizeAxes = Axes.Both } + } + } }; + + // Todo: This is not quite correct - it should be per-user to adjust for other mod combinations. + var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); + var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor(); + scoreProcessor.ApplyBeatmap(playableBeatmap); + + LoadComponentAsync(leaderboard = new MultiplayerSpectatorLeaderboard(scoreProcessor, UserIds) { Expanded = { Value = true } }, leaderboardContainer.Add); } protected override void Update() @@ -118,18 +145,25 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate var existingInstance = instances[userIndex]; if (existingInstance != null) + { grid.Remove(existingInstance); + leaderboard.RemoveClock(existingInstance.User.Id); + } LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score), d => { if (instances[userIndex] == d) + { grid.Add(d); + leaderboard.AddClock(d.User.Id, d.Beatmap.Track); + } }); } protected override void EndGameplay(int userId) { spectatorClient.StopWatchingUser(userId); + leaderboard.RemoveClock(userId); } private int getIndexForUser(int userId) => Array.IndexOf(UserIds, userId); From c93ce73123c142499f554fa77b752ec668573876 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 19:58:24 +0900 Subject: [PATCH 10/88] Move catchup logic inside PlayerInstance, fixup some edge cases --- .../Spectate/MultiplayerSpectator.cs | 37 +++------ .../Spectate/MultiplayerSpectatorPlayer.cs | 2 + .../Multiplayer/Spectate/PlayerInstance.cs | 79 ++++++++++++++++++- 3 files changed, 93 insertions(+), 25 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index d415669cb1..19c0d4e742 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -2,16 +2,13 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Diagnostics; using System.Linq; using osu.Framework.Allocation; -using osu.Framework.Audio; -using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Framework.Testing; using osu.Framework.Utils; using osu.Game.Online.Spectator; -using osu.Game.Screens.Play; using osu.Game.Screens.Spectate; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate @@ -19,7 +16,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public class MultiplayerSpectator : SpectatorScreen { private const double min_duration_to_allow_playback = 50; - private const double max_sync_offset = 2; // Isolates beatmap/ruleset to this screen. public override bool DisallowExternalBeatmapRulesetChanges => true; @@ -73,9 +69,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate LoadComponentAsync(leaderboard = new MultiplayerSpectatorLeaderboard(scoreProcessor, UserIds) { Expanded = { Value = true } }, leaderboardContainer.Add); } - protected override void Update() + protected override void UpdateAfterChildren() { - base.Update(); + base.UpdateAfterChildren(); updatePlayTime(); } @@ -85,7 +81,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { if (gameplayStarted) { - ensurePlaying(instances.Select(i => i.Beatmap.Track.CurrentTime).Max()); + ensurePlaying(instances.Select(i => i.GetCurrentTrackTime()).Max()); return; } @@ -108,30 +104,23 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private void ensureAllStopped() { foreach (var inst in instances) - inst.ChildrenOfType().SingleOrDefault()?.Stop(); + inst?.PauseGameplay(); } - private readonly BindableDouble catchupFrequencyAdjustment = new BindableDouble(2.0); - - private void ensurePlaying(double targetTime) + private void ensurePlaying(double targetTrackTime) { foreach (var inst in instances) { + Debug.Assert(inst != null); + double lastFrameTime = inst.Score.Replay.Frames.Select(f => f.Time).Last(); - double currentTime = inst.Beatmap.Track.CurrentTime; + double currentTime = inst.GetCurrentGameplayTime(); - // If we have enough frames to play back, start playback. - if (Precision.DefinitelyBigger(lastFrameTime, currentTime, min_duration_to_allow_playback)) - { - inst.ChildrenOfType().Single().Start(); + bool canContinuePlayback = Precision.DefinitelyBigger(lastFrameTime, currentTime, min_duration_to_allow_playback); + if (!canContinuePlayback) + continue; - if (targetTime < lastFrameTime && targetTime > currentTime + max_sync_offset) - inst.Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); - else - inst.Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); - } - else - inst.Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + inst.ContinueGameplay(targetTrackTime); } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index bad093f666..1634438850 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -11,6 +11,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public new ScoreProcessor ScoreProcessor => base.ScoreProcessor; + public new GameplayClockContainer GameplayClockContainer => base.GameplayClockContainer; + public MultiplayerSpectatorPlayer(Score score) : base(score) { diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 5689a3222a..acac753fe2 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -2,6 +2,8 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; +using osu.Framework.Audio; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; @@ -15,10 +17,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class PlayerInstance : CompositeDrawable { + private const double catchup_rate = 2; + private const double max_sync_offset = catchup_rate * 2; // Double the catchup rate to prevent ringing. + public bool PlayerLoaded => stack?.CurrentScreen is Player; public User User => Score.ScoreInfo.User; - public ScoreProcessor ScoreProcessor => player?.ScoreProcessor; public WorkingBeatmap Beatmap { get; private set; } public Ruleset Ruleset { get; private set; } @@ -54,5 +58,78 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => player = new MultiplayerSpectatorPlayer(Score))); } + + protected override void UpdateAfterChildren() + { + base.UpdateAfterChildren(); + updateCatchup(); + } + + private readonly BindableDouble catchupFrequencyAdjustment = new BindableDouble(catchup_rate); + private double targetTrackTime; + private bool isCatchingUp; + + private void updateCatchup() + { + if (player?.IsLoaded != true) + return; + + if (Score.Replay.Frames.Count == 0) + return; + + if (player.GameplayClockContainer.IsPaused.Value) + return; + + double currentTime = Beatmap.Track.CurrentTime; + bool catchupRequired = targetTrackTime > currentTime + max_sync_offset; + + // Skip catchup if nothing needs to be done. + if (catchupRequired == isCatchingUp) + return; + + if (catchupRequired) + { + Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + isCatchingUp = true; + } + else + { + Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + isCatchingUp = false; + } + } + + public double GetCurrentGameplayTime() + { + if (player?.IsLoaded != true) + return 0; + + return player.GameplayClockContainer.GameplayClock.CurrentTime; + } + + public double GetCurrentTrackTime() + { + if (player?.IsLoaded != true) + return 0; + + return Beatmap.Track.CurrentTime; + } + + public void ContinueGameplay(double targetTrackTime) + { + if (player?.IsLoaded != true) + return; + + player.GameplayClockContainer.Start(); + this.targetTrackTime = targetTrackTime; + } + + public void PauseGameplay() + { + if (player?.IsLoaded != true) + return; + + player.GameplayClockContainer.Stop(); + } } } From 49b7519c53a765d9bb4576dd1f0c529a0027fd24 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 20:03:34 +0900 Subject: [PATCH 11/88] Refactor gameplay starting logic --- .../Spectate/MultiplayerSpectator.cs | 36 ++++--------------- .../Multiplayer/Spectate/PlayerInstance.cs | 1 - 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 19c0d4e742..b0747f5027 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -72,43 +72,21 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate protected override void UpdateAfterChildren() { base.UpdateAfterChildren(); - updatePlayTime(); + updateGameplayPlayingState(); } - private bool gameplayStarted; - - private void updatePlayTime() + private void updateGameplayPlayingState() { - if (gameplayStarted) + // Make sure all players are loaded and have frames before starting any. + if (!AllPlayersLoaded || !instances.All(i => i.Score.Replay.Frames.Count > 0)) { - ensurePlaying(instances.Select(i => i.GetCurrentTrackTime()).Max()); + foreach (var inst in instances) + inst?.PauseGameplay(); return; } - // Make sure all players are loaded. - if (!AllPlayersLoaded) - { - ensureAllStopped(); - return; - } + double targetTrackTime = instances.Select(i => i.GetCurrentGameplayTime()).Max(); - if (!instances.All(i => i.Score.Replay.Frames.Count > 0)) - { - ensureAllStopped(); - return; - } - - gameplayStarted = true; - } - - private void ensureAllStopped() - { - foreach (var inst in instances) - inst?.PauseGameplay(); - } - - private void ensurePlaying(double targetTrackTime) - { foreach (var inst in instances) { Debug.Assert(inst != null); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index acac753fe2..ca58d888fa 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -8,7 +8,6 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; using osu.Game.Rulesets; -using osu.Game.Rulesets.Scoring; using osu.Game.Scoring; using osu.Game.Screens.Play; using osu.Game.Users; From eccd269ccee483973c260daa44a0dddc7740f9c0 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 20:17:57 +0900 Subject: [PATCH 12/88] Implement maximum start delay --- .../Multiplayer/TestSceneMultiplayerScreen.cs | 2 +- .../Spectate/MultiplayerSpectator.cs | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs index d085827986..897219dbad 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs @@ -110,7 +110,7 @@ namespace osu.Game.Tests.Visual.Multiplayer loadSpectateScreen(); // Send frames for one player only, both should remain paused. - sendFrames(55, 20); + sendFrames(55, 1000); checkPausedInstant(55, true); checkPausedInstant(56, true); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index b0747f5027..d8ec25fedb 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using osu.Framework.Allocation; @@ -16,6 +17,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public class MultiplayerSpectator : SpectatorScreen { private const double min_duration_to_allow_playback = 50; + private const double maximum_start_delay = 15000; // Isolates beatmap/ruleset to this screen. public override bool DisallowExternalBeatmapRulesetChanges => true; @@ -28,6 +30,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly PlayerInstance[] instances; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; + private double? loadStartTime; public MultiplayerSpectator(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) @@ -72,22 +75,39 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate protected override void UpdateAfterChildren() { base.UpdateAfterChildren(); + + loadStartTime ??= Time.Current; + updateGameplayPlayingState(); } + private bool canStartGameplay => + // All players must be loaded. + AllPlayersLoaded + && ( + // All players have frames... + instances.All(i => i.Score.Replay.Frames.Count > 0) + // Or any player has frames and the maximum start delay has been exceeded. + || (Time.Current - loadStartTime > maximum_start_delay + && instances.Any(i => i.Score.Replay.Frames.Count > 0)) + ); + private void updateGameplayPlayingState() { // Make sure all players are loaded and have frames before starting any. - if (!AllPlayersLoaded || !instances.All(i => i.Score.Replay.Frames.Count > 0)) + if (!canStartGameplay) { foreach (var inst in instances) inst?.PauseGameplay(); return; } - double targetTrackTime = instances.Select(i => i.GetCurrentGameplayTime()).Max(); + // Not all instances may be in a valid gameplay state (see canStartGameplay). Only control the ones that are. + IEnumerable validInstances = instances.Where(i => i.Score.Replay.Frames.Count > 0); - foreach (var inst in instances) + double targetTrackTime = validInstances.Select(i => i.GetCurrentTrackTime()).Max(); + + foreach (var inst in validInstances) { Debug.Assert(inst != null); From 61c400b1a1187a17a17467a02a061fb87dca65f4 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 20:18:26 +0900 Subject: [PATCH 13/88] Fix filename --- ...SceneMultiplayerScreen.cs => TestSceneMultiplayerSpectator.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename osu.Game.Tests/Visual/Multiplayer/{TestSceneMultiplayerScreen.cs => TestSceneMultiplayerSpectator.cs} (100%) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs similarity index 100% rename from osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerScreen.cs rename to osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs From 3e46d6401eeaef7a63ed6506f8b8d2840b53767c Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 20:22:30 +0900 Subject: [PATCH 14/88] Remove some unnecessary code --- .../Multiplayer/Spectate/MultiplayerSpectator.cs | 2 +- .../OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index d8ec25fedb..db5de45f72 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -82,7 +82,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate } private bool canStartGameplay => - // All players must be loaded. + // All players must be loaded, and... AllPlayersLoaded && ( // All players have frames... diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index ca58d888fa..6b5f102543 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -7,7 +7,6 @@ using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; -using osu.Game.Rulesets; using osu.Game.Scoring; using osu.Game.Screens.Play; using osu.Game.Users; @@ -24,7 +23,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public User User => Score.ScoreInfo.User; public WorkingBeatmap Beatmap { get; private set; } - public Ruleset Ruleset { get; private set; } public readonly Score Score; @@ -43,7 +41,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private void load(BeatmapManager beatmapManager) { Beatmap = beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true); - Ruleset = Score.ScoreInfo.Ruleset.CreateInstance(); InternalChild = new GameplayIsolationContainer(Beatmap, Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) { @@ -87,15 +84,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate return; if (catchupRequired) - { Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); - isCatchingUp = true; - } else - { Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); - isCatchingUp = false; - } + + isCatchingUp = catchupRequired; } public double GetCurrentGameplayTime() From 6eddc6c59e6c63c219798bc0fb92a59e0d3c3b96 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 21:03:45 +0900 Subject: [PATCH 15/88] Enable spectating multiplayer matches --- .../Online/Multiplayer/MultiplayerClient.cs | 3 --- .../Screens/OnlinePlay/Match/RoomSubScreen.cs | 6 ++++-- .../Multiplayer/MultiplayerMatchSubScreen.cs | 20 +++++++++++++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/osu.Game/Online/Multiplayer/MultiplayerClient.cs b/osu.Game/Online/Multiplayer/MultiplayerClient.cs index 37e11cc576..4529dfd0a7 100644 --- a/osu.Game/Online/Multiplayer/MultiplayerClient.cs +++ b/osu.Game/Online/Multiplayer/MultiplayerClient.cs @@ -96,9 +96,6 @@ namespace osu.Game.Online.Multiplayer if (!IsConnected.Value) return Task.CompletedTask; - if (newState == MultiplayerUserState.Spectating) - return Task.CompletedTask; // Not supported yet. - return connection.InvokeAsync(nameof(IMultiplayerServer.ChangeState), newState); } diff --git a/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs b/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs index 706da05d15..0d1ed5d88e 100644 --- a/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs @@ -148,10 +148,12 @@ namespace osu.Game.Screens.OnlinePlay.Match return base.OnExiting(next); } - protected void StartPlay(Func player) + protected void StartPlay(Func player) => PushTopLevelScreen(() => new PlayerLoader(player)); + + protected void PushTopLevelScreen(Func screen) { sampleStart?.Play(); - ParentScreen?.Push(new PlayerLoader(player)); + ParentScreen?.Push(screen()); } private void selectedItemChanged() diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index 90cef0107c..035eba4f30 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -25,6 +25,7 @@ using osu.Game.Screens.OnlinePlay.Match; using osu.Game.Screens.OnlinePlay.Match.Components; using osu.Game.Screens.OnlinePlay.Multiplayer.Match; using osu.Game.Screens.OnlinePlay.Multiplayer.Participants; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; using osu.Game.Screens.Play.HUD; using osu.Game.Users; using osuTK; @@ -405,11 +406,22 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer } } - private void onRoomUpdated() + private void onRoomUpdated() => Scheduler.Add(() => { - // user mods may have changed. - Scheduler.AddOnce(UpdateMods); - } + if (client.Room == null) + return; + + Debug.Assert(client.LocalUser != null); + + UpdateMods(); + + if (client.LocalUser.State == MultiplayerUserState.Spectating + && (client.Room.State == MultiplayerRoomState.Playing || client.Room.State == MultiplayerRoomState.WaitingForLoad) + && ParentScreen.IsCurrentScreen()) + { + PushTopLevelScreen(() => new MultiplayerSpectator(client.CurrentMatchPlayingUserIds.ToArray())); + } + }); private void onLoadRequested() { From 4409c1a36f18dbae1484dd6ef65413a275e0b57b Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 9 Apr 2021 22:01:21 +0900 Subject: [PATCH 16/88] Increase sync offset to prevent constant catchups --- .../Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 6b5f102543..f0994ec375 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -16,7 +16,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public class PlayerInstance : CompositeDrawable { private const double catchup_rate = 2; - private const double max_sync_offset = catchup_rate * 2; // Double the catchup rate to prevent ringing. + private const double max_sync_offset = 50; public bool PlayerLoaded => stack?.CurrentScreen is Player; From 627dd960b07198135b941c1a82a70338058fd21d Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 13 Apr 2021 20:52:20 +0900 Subject: [PATCH 17/88] Disable player input for now --- .../Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index f0994ec375..32cc3b48d6 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -123,5 +123,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate player.GameplayClockContainer.Stop(); } + + // Player interferes with global input, so disable input for now. + public override bool PropagatePositionalInputSubTree => false; + public override bool PropagateNonPositionalInputSubTree => false; } } From 20823abb309ac0fd908bdd03b532abfbdbd22afd Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 13 Apr 2021 22:10:35 +0900 Subject: [PATCH 18/88] Make resyncing a bit more resilient --- .../TestSceneMultiplayerSpectator.cs | 33 ++++++++++++++++- .../Multiplayer/Spectate/PlayerInstance.cs | 35 +++++++++++++++---- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index 897219dbad..1090f1d10e 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -1,12 +1,15 @@ // Copyright (c) ppy Pty Ltd . 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; using System.Threading.Tasks; using NUnit.Framework; using osu.Framework.Allocation; +using osu.Framework.Audio; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Testing; using osu.Framework.Utils; @@ -153,6 +156,27 @@ namespace osu.Game.Tests.Visual.Multiplayer checkPausedInstant(55, false); } + [Test] + public void TestPlayerStartsCatchingUpOnlyAfterExceedingMaxOffset() + { + start(new[] { 55, 56 }); + loadSpectateScreen(); + + sendFrames(55, 1000); + sendFrames(56, 1000); + + Bindable slowDownAdjustment; + + AddStep("slow down player 2", () => + { + slowDownAdjustment = new Bindable(0.99); + getInstance(56).Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, slowDownAdjustment); + }); + + AddUntilStep("exceeded min offset but not catching up", () => getGameplayOffset(55, 56) > PlayerInstance.MAX_OFFSET && !getInstance(56).IsCatchingUp); + AddUntilStep("catching up or caught up", () => getInstance(56).IsCatchingUp || Math.Abs(getGameplayOffset(55, 56)) < PlayerInstance.SYNC_TARGET * 2); + } + [Test] public void TestPlayersCatchUpAfterFallingBehind() { @@ -174,7 +198,9 @@ namespace osu.Game.Tests.Visual.Multiplayer checkPausedInstant(56, false); // Player 2 should catch up to player 1 after unpausing. - AddUntilStep("player 1 time == player 2 time", () => Precision.AlmostEquals(getGameplayTime(55), getGameplayTime(56), 16)); + AddUntilStep("player 2 not catching up", () => !getInstance(56).IsCatchingUp); + AddAssert("player 1 time == player 2 time", () => Math.Abs(getGameplayOffset(55, 56)) <= 2 * PlayerInstance.SYNC_TARGET); + AddWaitStep("wait a bit", 5); } private void loadSpectateScreen() @@ -236,6 +262,11 @@ namespace osu.Game.Tests.Visual.Multiplayer private void checkPausedInstant(int userId, bool state) => AddAssert($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().IsPaused.Value == state); + /// + /// Returns time(user1) - time(user2). + /// + private double getGameplayOffset(int user1, int user2) => getGameplayTime(user1) - getGameplayTime(user2); + private double getGameplayTime(int userId) => getPlayer(userId).ChildrenOfType().Single().GameplayClock.CurrentTime; private Player getPlayer(int userId) => getInstance(userId).ChildrenOfType().Single(); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 32cc3b48d6..a4a3a0c133 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -6,6 +6,7 @@ using osu.Framework.Audio; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Framework.Logging; using osu.Game.Beatmaps; using osu.Game.Scoring; using osu.Game.Screens.Play; @@ -15,8 +16,20 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class PlayerInstance : CompositeDrawable { + /// + /// The rate at which a user catches up after becoming desynchronised. + /// private const double catchup_rate = 2; - private const double max_sync_offset = 50; + + /// + /// The offset from the expected time at which to START synchronisation. + /// + public const double MAX_OFFSET = 50; + + /// + /// The maximum offset from the expected time at which to STOP synchronisation. + /// + public const double SYNC_TARGET = 16; public bool PlayerLoaded => stack?.CurrentScreen is Player; @@ -26,6 +39,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public readonly Score Score; + public bool IsCatchingUp { get; private set; } + private OsuScreenStack stack; private MultiplayerSpectatorPlayer player; @@ -63,7 +78,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly BindableDouble catchupFrequencyAdjustment = new BindableDouble(catchup_rate); private double targetTrackTime; - private bool isCatchingUp; private void updateCatchup() { @@ -77,18 +91,27 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate return; double currentTime = Beatmap.Track.CurrentTime; - bool catchupRequired = targetTrackTime > currentTime + max_sync_offset; + double timeBehind = targetTrackTime - currentTime; - // Skip catchup if nothing needs to be done. - if (catchupRequired == isCatchingUp) + double offsetForCatchup = IsCatchingUp ? SYNC_TARGET : MAX_OFFSET; + bool catchupRequired = timeBehind > offsetForCatchup; + + // Skip catchup if no work needs to be done. + if (catchupRequired == IsCatchingUp) return; if (catchupRequired) + { Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + Logger.Log($"{User.Id} catchup started (behind: {timeBehind})"); + } else + { Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + Logger.Log($"{User.Id} catchup finished (behind: {timeBehind})"); + } - isCatchingUp = catchupRequired; + IsCatchingUp = catchupRequired; } public double GetCurrentGameplayTime() From 3039b7b0f9731e881dc79f23a741d7b3b29df18d Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 13 Apr 2021 22:40:10 +0900 Subject: [PATCH 19/88] Make tests a bit more resilient --- .../Visual/Multiplayer/TestSceneMultiplayerSpectator.cs | 3 +-- .../Multiplayer/Spectate/MultiplayerSpectator.cs | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index 1090f1d10e..1ac012c0b5 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -199,8 +199,7 @@ namespace osu.Game.Tests.Visual.Multiplayer // Player 2 should catch up to player 1 after unpausing. AddUntilStep("player 2 not catching up", () => !getInstance(56).IsCatchingUp); - AddAssert("player 1 time == player 2 time", () => Math.Abs(getGameplayOffset(55, 56)) <= 2 * PlayerInstance.SYNC_TARGET); - AddWaitStep("wait a bit", 5); + AddWaitStep("wait a bit", 10); } private void loadSpectateScreen() diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index db5de45f72..636e7b9a49 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -30,7 +30,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly PlayerInstance[] instances; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; - private double? loadStartTime; + private double? loadFinishTime; public MultiplayerSpectator(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) @@ -76,7 +76,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { base.UpdateAfterChildren(); - loadStartTime ??= Time.Current; + if (AllPlayersLoaded) + loadFinishTime ??= Time.Current; updateGameplayPlayingState(); } @@ -88,7 +89,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate // All players have frames... instances.All(i => i.Score.Replay.Frames.Count > 0) // Or any player has frames and the maximum start delay has been exceeded. - || (Time.Current - loadStartTime > maximum_start_delay + || (Time.Current - loadFinishTime > maximum_start_delay && instances.Any(i => i.Score.Replay.Frames.Count > 0)) ); From d49b90877e906960cfc081894cc3ecd8687f35db Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 13 Apr 2021 23:21:35 +0900 Subject: [PATCH 20/88] Fix operation remaining in progress --- .../OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index 035eba4f30..9a68ff908d 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -420,6 +420,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer && ParentScreen.IsCurrentScreen()) { PushTopLevelScreen(() => new MultiplayerSpectator(client.CurrentMatchPlayingUserIds.ToArray())); + + // If the current user was host, they started the match and the in-progres operation needs to be stopped now. + readyClickOperation?.Dispose(); + readyClickOperation = null; } }); From 56e1bffdfd237ceb45db074982b912e27612640e Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 13 Apr 2021 23:40:10 +0900 Subject: [PATCH 21/88] Populate initial user states --- osu.Game/Online/Multiplayer/StatefulMultiplayerClient.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/osu.Game/Online/Multiplayer/StatefulMultiplayerClient.cs b/osu.Game/Online/Multiplayer/StatefulMultiplayerClient.cs index 2ddc10db0f..fe1201ef89 100644 --- a/osu.Game/Online/Multiplayer/StatefulMultiplayerClient.cs +++ b/osu.Game/Online/Multiplayer/StatefulMultiplayerClient.cs @@ -144,6 +144,9 @@ namespace osu.Game.Online.Multiplayer Room = joinedRoom; apiRoom = room; defaultPlaylistItemId = apiRoom.Playlist.FirstOrDefault()?.ID ?? 0; + + foreach (var user in joinedRoom.Users) + updateUserPlayingState(user.UserID, user.State); }, cancellationSource.Token).ConfigureAwait(false); // Update room settings. From 77830527e7e06df39fbf8b8ae341b9847bef2795 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 13 Apr 2021 23:49:23 +0900 Subject: [PATCH 22/88] Fix spectate button being disabled during play --- .../OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs index 4b3fb5d00f..465af037e2 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs @@ -81,7 +81,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match break; } - button.Enabled.Value = Client.Room?.State == MultiplayerRoomState.Open && !operationInProgress.Value; + button.Enabled.Value = !operationInProgress.Value; } private class ButtonWithTrianglesExposed : TriangleButton From 69b01e727008bb8dd7d15bce834e72aee9ae3ca1 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 14 Apr 2021 00:58:03 +0900 Subject: [PATCH 23/88] Add some debugging --- .../OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs | 4 ++++ .../Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 636e7b9a49..a56104439d 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -8,6 +8,7 @@ using System.Linq; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Framework.Logging; using osu.Framework.Utils; using osu.Game.Online.Spectator; using osu.Game.Screens.Spectate; @@ -108,6 +109,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate double targetTrackTime = validInstances.Select(i => i.GetCurrentTrackTime()).Max(); + var instanceTimes = string.Join(',', validInstances.Select(i => $" {i.User.Id}: {(int)i.GetCurrentTrackTime()}")); + Logger.Log($"target: {(int)targetTrackTime},{instanceTimes}"); + foreach (var inst in validInstances) { Debug.Assert(inst != null); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index a4a3a0c133..12a710e9f0 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -103,12 +103,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate if (catchupRequired) { Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); - Logger.Log($"{User.Id} catchup started (behind: {timeBehind})"); + Logger.Log($"{User.Id} catchup started (behind: {(int)timeBehind})"); } else { Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); - Logger.Log($"{User.Id} catchup finished (behind: {timeBehind})"); + Logger.Log($"{User.Id} catchup finished (behind: {(int)timeBehind})"); } IsCatchingUp = catchupRequired; From 774cca38c4c66627c3f501712f9e40cba1ef8474 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 14 Apr 2021 20:39:14 +0900 Subject: [PATCH 24/88] Make spectating instances use custom GCC --- .../Spectate/MultiplayerSpectator.cs | 48 ++++++++++++------- .../Spectate/MultiplayerSpectatorPlayer.cs | 33 ++++++++++++- .../Multiplayer/Spectate/PlayerInstance.cs | 29 ++++++----- 3 files changed, 75 insertions(+), 35 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index a56104439d..931d13942b 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -11,6 +11,7 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Logging; using osu.Framework.Utils; using osu.Game.Online.Spectator; +using osu.Game.Screens.Play; using osu.Game.Screens.Spectate; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate @@ -29,6 +30,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private SpectatorStreamingClient spectatorClient { get; set; } private readonly PlayerInstance[] instances; + private GameplayClockContainer gameplayClockContainer; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; private double? loadFinishTime; @@ -44,23 +46,26 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { Container leaderboardContainer; - InternalChild = new GridContainer + InternalChild = gameplayClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0) { - RelativeSizeAxes = Axes.Both, - ColumnDimensions = new[] + Child = new GridContainer { - new Dimension(GridSizeMode.AutoSize) - }, - Content = new[] - { - new Drawable[] + RelativeSizeAxes = Axes.Both, + ColumnDimensions = new[] { - leaderboardContainer = new Container + new Dimension(GridSizeMode.AutoSize) + }, + Content = new[] + { + new Drawable[] { - RelativeSizeAxes = Axes.Y, - AutoSizeAxes = Axes.X - }, - grid = new PlayerGrid { RelativeSizeAxes = Axes.Both } + leaderboardContainer = new Container + { + RelativeSizeAxes = Axes.Y, + AutoSizeAxes = Axes.X + }, + grid = new PlayerGrid { RelativeSizeAxes = Axes.Both } + } } } }; @@ -94,6 +99,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate && instances.Any(i => i.Score.Replay.Frames.Count > 0)) ); + private bool firstStartFrame = true; + private void updateGameplayPlayingState() { // Make sure all players are loaded and have frames before starting any. @@ -104,13 +111,16 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate return; } + if (firstStartFrame) + gameplayClockContainer.Restart(); + // Not all instances may be in a valid gameplay state (see canStartGameplay). Only control the ones that are. IEnumerable validInstances = instances.Where(i => i.Score.Replay.Frames.Count > 0); - double targetTrackTime = validInstances.Select(i => i.GetCurrentTrackTime()).Max(); + double targetGameplayTime = gameplayClockContainer.GameplayClock.CurrentTime; - var instanceTimes = string.Join(',', validInstances.Select(i => $" {i.User.Id}: {(int)i.GetCurrentTrackTime()}")); - Logger.Log($"target: {(int)targetTrackTime},{instanceTimes}"); + var instanceTimes = string.Join(',', validInstances.Select(i => $" {i.User.Id}: {(int)i.GetCurrentGameplayTime()}")); + Logger.Log($"target: {(int)targetGameplayTime},{instanceTimes}"); foreach (var inst in validInstances) { @@ -123,8 +133,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate if (!canContinuePlayback) continue; - inst.ContinueGameplay(targetTrackTime); + inst.ContinueGameplay(targetGameplayTime); } + + firstStartFrame = false; } protected override void OnUserStateChanged(int userId, SpectatorState spectatorState) @@ -142,7 +154,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate leaderboard.RemoveClock(existingInstance.User.Id); } - LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score), d => + LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, gameplayClockContainer.GameplayClock), d => { if (instances[userIndex] == d) { diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index 1634438850..abb7ec83d8 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -1,6 +1,9 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using osu.Framework.Bindables; +using osu.Framework.Timing; +using osu.Game.Beatmaps; using osu.Game.Rulesets.Scoring; using osu.Game.Scoring; using osu.Game.Screens.Play; @@ -11,11 +14,37 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public new ScoreProcessor ScoreProcessor => base.ScoreProcessor; - public new GameplayClockContainer GameplayClockContainer => base.GameplayClockContainer; + public new SubGameplayClockContainer GameplayClockContainer => (SubGameplayClockContainer)base.GameplayClockContainer; - public MultiplayerSpectatorPlayer(Score score) + private readonly GameplayClock gameplayClock; + + public MultiplayerSpectatorPlayer(Score score, GameplayClock gameplayClock) : base(score) { + this.gameplayClock = gameplayClock; } + + protected override GameplayClockContainer CreateGameplayClockContainer(WorkingBeatmap beatmap, double gameplayStart) + => new SubGameplayClockContainer(gameplayClock); + } + + public class SubGameplayClockContainer : GameplayClockContainer + { + public new DecoupleableInterpolatingFramedClock AdjustableClock => base.AdjustableClock; + + public SubGameplayClockContainer(IClock sourceClock) + : base(sourceClock) + { + } + + protected override void OnIsPausedChanged(ValueChangedEvent isPaused) + { + if (isPaused.NewValue) + AdjustableClock.Stop(); + else + AdjustableClock.Start(); + } + + protected override GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source); } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 12a710e9f0..724a685cb7 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -2,8 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; -using osu.Framework.Audio; -using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Logging; @@ -38,15 +36,17 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public WorkingBeatmap Beatmap { get; private set; } public readonly Score Score; + private readonly GameplayClock gameplayClock; public bool IsCatchingUp { get; private set; } private OsuScreenStack stack; private MultiplayerSpectatorPlayer player; - public PlayerInstance(Score score) + public PlayerInstance(Score score, GameplayClock gameplayClock) { Score = score; + this.gameplayClock = gameplayClock; RelativeSizeAxes = Axes.Both; Masking = true; @@ -67,7 +67,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate } }; - stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => player = new MultiplayerSpectatorPlayer(Score))); + stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => player = new MultiplayerSpectatorPlayer(Score, gameplayClock))); } protected override void UpdateAfterChildren() @@ -76,8 +76,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate updateCatchup(); } - private readonly BindableDouble catchupFrequencyAdjustment = new BindableDouble(catchup_rate); - private double targetTrackTime; + private double targetGameplayTime; private void updateCatchup() { @@ -91,7 +90,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate return; double currentTime = Beatmap.Track.CurrentTime; - double timeBehind = targetTrackTime - currentTime; + double timeBehind = targetGameplayTime - currentTime; double offsetForCatchup = IsCatchingUp ? SYNC_TARGET : MAX_OFFSET; bool catchupRequired = timeBehind > offsetForCatchup; @@ -102,12 +101,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate if (catchupRequired) { - Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + // player.GameplayClockContainer.AdjustableClock.Rate = catchup_rate; Logger.Log($"{User.Id} catchup started (behind: {(int)timeBehind})"); } else { - Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment); + // player.GameplayClockContainer.AdjustableClock.Rate = 1; Logger.Log($"{User.Id} catchup finished (behind: {(int)timeBehind})"); } @@ -122,21 +121,21 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate return player.GameplayClockContainer.GameplayClock.CurrentTime; } - public double GetCurrentTrackTime() + public bool IsPlaying() { - if (player?.IsLoaded != true) - return 0; + if (player.IsLoaded != true) + return false; - return Beatmap.Track.CurrentTime; + return player.GameplayClockContainer.GameplayClock.IsRunning; } - public void ContinueGameplay(double targetTrackTime) + public void ContinueGameplay(double targetGameplayTime) { if (player?.IsLoaded != true) return; player.GameplayClockContainer.Start(); - this.targetTrackTime = targetTrackTime; + this.targetGameplayTime = targetGameplayTime; } public void PauseGameplay() From 6fc7488a67ddf580358369327c78ddea5d7aab8d Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 16:33:59 +0900 Subject: [PATCH 25/88] Reimplement syncing logic as a new component --- .../OnlinePlay/MultiplayerSyncManagerTest.cs | 213 ++++++++++++++++++ .../Spectate/IMultiplayerSlaveClock.cs | 17 ++ .../Spectate/IMultiplayerSyncManager.cs | 16 ++ .../Spectate/MultiplayerSyncManager.cs | 162 +++++++++++++ 4 files changed, 408 insertions(+) create mode 100644 osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSyncManager.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs diff --git a/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs b/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs new file mode 100644 index 0000000000..2a6dfa9c8d --- /dev/null +++ b/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs @@ -0,0 +1,213 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using NUnit.Framework; +using osu.Framework.Bindables; +using osu.Framework.Testing; +using osu.Framework.Timing; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; +using osu.Game.Tests.Visual; + +namespace osu.Game.Tests.OnlinePlay +{ + [HeadlessTest] + public class MultiplayerSyncManagerTest : OsuTestScene + { + private TestManualClock master; + private MultiplayerSyncManager syncManager; + + private TestSlaveClock slave1; + private TestSlaveClock slave2; + + [SetUp] + public void Setup() + { + syncManager = new MultiplayerSyncManager(master = new TestManualClock()); + syncManager.AddSlave(slave1 = new TestSlaveClock(1)); + syncManager.AddSlave(slave2 = new TestSlaveClock(2)); + + Schedule(() => Child = syncManager); + } + + [Test] + public void TestMasterClockStartsWhenAllSlavesHaveFrames() + { + setWaiting(() => slave1, false); + assertMasterState(false); + assertSlaveState(() => slave1, false); + assertSlaveState(() => slave2, false); + + setWaiting(() => slave2, false); + assertMasterState(true); + assertSlaveState(() => slave1, true); + assertSlaveState(() => slave2, true); + } + + [Test] + public void TestMasterClockDoesNotStartWhenNoneReadyForMaximumDelayTime() + { + AddWaitStep($"wait {MultiplayerSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + assertMasterState(false); + } + + [Test] + public void TestMasterClockStartsWhenAnyReadyForMaximumDelayTime() + { + setWaiting(() => slave1, false); + AddWaitStep($"wait {MultiplayerSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + assertMasterState(true); + } + + [Test] + public void TestSlaveDoesNotCatchUpWhenSlightlyOutOfSync() + { + setAllWaiting(false); + + setMasterTime(MultiplayerSyncManager.SYNC_TARGET + 1); + assertCatchingUp(() => slave1, false); + } + + [Test] + public void TestSlaveStartsCatchingUpWhenTooFarBehind() + { + setAllWaiting(false); + + setMasterTime(MultiplayerSyncManager.MAX_SYNC_OFFSET + 1); + assertCatchingUp(() => slave1, true); + assertCatchingUp(() => slave2, true); + } + + [Test] + public void TestSlaveKeepsCatchingUpWhenSlightlyOutOfSync() + { + setAllWaiting(false); + + setMasterTime(MultiplayerSyncManager.MAX_SYNC_OFFSET + 1); + setSlaveTime(() => slave1, MultiplayerSyncManager.SYNC_TARGET + 1); + assertCatchingUp(() => slave1, true); + } + + [Test] + public void TestSlaveStopsCatchingUpWhenInSync() + { + setAllWaiting(false); + + setMasterTime(MultiplayerSyncManager.MAX_SYNC_OFFSET + 2); + setSlaveTime(() => slave1, MultiplayerSyncManager.SYNC_TARGET); + assertCatchingUp(() => slave1, false); + assertCatchingUp(() => slave2, true); + } + + [Test] + public void TestSlaveDoesNotStopWhenSlightlyAhead() + { + setAllWaiting(false); + + setSlaveTime(() => slave1, -MultiplayerSyncManager.SYNC_TARGET); + assertCatchingUp(() => slave1, false); + assertSlaveState(() => slave1, true); + } + + [Test] + public void TestSlaveStopsWhenTooFarAheadAndStartsWhenBackInSync() + { + setAllWaiting(false); + + setSlaveTime(() => slave1, -MultiplayerSyncManager.SYNC_TARGET - 1); + + // This is a silent catchup, where IsCatchingUp = false but IsRunning = false also. + assertCatchingUp(() => slave1, false); + assertSlaveState(() => slave1, false); + + setMasterTime(1); + assertCatchingUp(() => slave1, false); + assertSlaveState(() => slave1, true); + } + + [Test] + public void TestInSyncSlaveDoesNotStartIfWaitingOnFrames() + { + setAllWaiting(false); + + assertSlaveState(() => slave1, true); + setWaiting(() => slave1, true); + assertSlaveState(() => slave1, false); + } + + private void setWaiting(Func slave, bool waiting) + => AddStep($"set slave {slave().Id} waiting = {waiting}", () => slave().WaitingOnFrames.Value = waiting); + + private void setAllWaiting(bool waiting) => AddStep($"set all slaves waiting = {waiting}", () => + { + slave1.WaitingOnFrames.Value = waiting; + slave2.WaitingOnFrames.Value = waiting; + }); + + private void setMasterTime(double time) + => AddStep($"set master = {time}", () => master.Seek(time)); + + /// + /// slave.Time = master.Time - offsetFromMaster + /// + private void setSlaveTime(Func slave, double offsetFromMaster) + => AddStep($"set slave {slave().Id} = master - {offsetFromMaster}", () => slave().Seek(master.CurrentTime - offsetFromMaster)); + + private void assertMasterState(bool running) + => AddAssert($"master clock {(running ? "is" : "is not")} running", () => master.IsRunning == running); + + private void assertCatchingUp(Func slave, bool catchingUp) => + AddAssert($"slave {slave().Id} {(catchingUp ? "is" : "is not")} catching up", () => slave().IsCatchingUp == catchingUp); + + private void assertSlaveState(Func slave, bool running) + => AddAssert($"slave {slave().Id} {(running ? "is" : "is not")} running", () => slave().IsRunning == running); + + private class TestSlaveClock : TestManualClock, IMultiplayerSlaveClock + { + public readonly Bindable WaitingOnFrames = new Bindable(true); + IBindable IMultiplayerSlaveClock.WaitingOnFrames => WaitingOnFrames; + + public double LastFrameTime => 0; + + double IMultiplayerSlaveClock.LastFrameTime => LastFrameTime; + + public bool IsCatchingUp { get; set; } + + public readonly int Id; + + public TestSlaveClock(int id) + { + Id = id; + + WaitingOnFrames.BindValueChanged(waiting => + { + if (waiting.NewValue) + Stop(); + else + Start(); + }); + } + } + + private class TestManualClock : ManualClock, IAdjustableClock + { + public void Start() => IsRunning = true; + + public void Stop() => IsRunning = false; + + public bool Seek(double position) + { + CurrentTime = position; + return true; + } + + public void Reset() + { + } + + public void ResetSpeedAdjustments() + { + } + } + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs new file mode 100644 index 0000000000..e0fca45bdd --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs @@ -0,0 +1,17 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Bindables; +using osu.Framework.Timing; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public interface IMultiplayerSlaveClock : IAdjustableClock + { + IBindable WaitingOnFrames { get; } + + double LastFrameTime { get; } + + bool IsCatchingUp { get; set; } + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSyncManager.cs new file mode 100644 index 0000000000..2c50596823 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSyncManager.cs @@ -0,0 +1,16 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Timing; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public interface IMultiplayerSyncManager + { + IAdjustableClock Master { get; } + + void AddSlave(IMultiplayerSlaveClock clock); + + void RemoveSlave(IMultiplayerSlaveClock clock); + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs new file mode 100644 index 0000000000..42f6536e90 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs @@ -0,0 +1,162 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using System.Linq; +using osu.Framework.Graphics; +using osu.Framework.Logging; +using osu.Framework.Timing; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public class MultiplayerSyncManager : Component, IMultiplayerSyncManager + { + /// + /// The offset from the master clock to which slaves should be synchronised to. + /// + public const double SYNC_TARGET = 16; + + /// + /// The offset from the master clock at which slaves begin resynchronising. + /// + public const double MAX_SYNC_OFFSET = 50; + + /// + /// The maximum delay to start gameplay, if any (but not all) slaves are ready. + /// + public const double MAXIMUM_START_DELAY = 15000; + + /// + /// The catchup rate. + /// + public const double CATCHUP_RATE = 2; + + /// + /// The master clock which is used to control the timing of all slave clocks. + /// + public IAdjustableClock Master { get; } + + /// + /// The slave clocks. + /// + private readonly List slaves = new List(); + + private bool hasStarted; + private double? firstStartAttemptTime; + + public MultiplayerSyncManager(IAdjustableClock master) + { + Master = master; + } + + public void AddSlave(IMultiplayerSlaveClock clock) => slaves.Add(clock); + + public void RemoveSlave(IMultiplayerSlaveClock clock) => slaves.Remove(clock); + + protected override void Update() + { + base.Update(); + + if (!attemptStart()) + { + // Ensure all slaves are stopped until the start succeeds. + foreach (var slave in slaves) + slave.Stop(); + return; + } + + updateCatchup(); + updateMasterClock(); + } + + /// + /// Attempts to start playback. Awaits for all slaves to have available frames for up to milliseconds. + /// + /// Whether playback was started and syncing should occur. + private bool attemptStart() + { + if (hasStarted) + return true; + + if (slaves.Count == 0) + return false; + + firstStartAttemptTime ??= Time.Current; + + int readyCount = slaves.Count(s => !s.WaitingOnFrames.Value); + + if (readyCount == slaves.Count) + { + Logger.Log("Gameplay started (all ready)."); + return hasStarted = true; + } + + if (readyCount > 0 && (Time.Current - firstStartAttemptTime) > MAXIMUM_START_DELAY) + { + Logger.Log($"Gameplay started (maximum delay exceeded, {readyCount}/{slaves.Count} ready)."); + return hasStarted = true; + } + + return false; + } + + /// + /// Updates the catchup states of all slave clocks. + /// + private void updateCatchup() + { + for (int i = 0; i < slaves.Count; i++) + { + var slave = slaves[i]; + double timeDelta = Master.CurrentTime - slave.CurrentTime; + + // Check that the slave isn't too far ahead. + // This is a quiet case in which the catchup is done by the master clock, so IsCatchingUp is not set on the slave. + if (timeDelta < -SYNC_TARGET) + { + slave.Stop(); + continue; + } + + // Make sure the slave is running if it can. + if (!slave.WaitingOnFrames.Value) + slave.Start(); + + if (slave.IsCatchingUp) + { + // Stop the slave from catching up if it's within the sync target. + if (timeDelta <= SYNC_TARGET) + { + slave.IsCatchingUp = false; + Logger.Log($"Slave {i} catchup finished (delta = {timeDelta})"); + } + } + else + { + // Make the slave start catching up if it's exceeded the maximum allowable sync offset. + if (timeDelta > MAX_SYNC_OFFSET) + { + slave.IsCatchingUp = true; + Logger.Log($"Slave {i} catchup started (too far behind, delta = {timeDelta})"); + } + } + } + } + + /// + /// Updates the master clock's running state. + /// + private void updateMasterClock() + { + bool anyInSync = slaves.Any(s => !s.IsCatchingUp); + + if (Master.IsRunning != anyInSync) + { + if (anyInSync) + Master.Start(); + else + Master.Stop(); + } + } + } +} From 33ad7850cb432a2f2d7cfbbe60ae302f353443a4 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 16:45:59 +0900 Subject: [PATCH 26/88] Remove LastFrameTime --- osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs | 4 ---- .../OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs | 2 -- 2 files changed, 6 deletions(-) diff --git a/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs b/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs index 2a6dfa9c8d..6ee867c6f8 100644 --- a/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs +++ b/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs @@ -167,10 +167,6 @@ namespace osu.Game.Tests.OnlinePlay public readonly Bindable WaitingOnFrames = new Bindable(true); IBindable IMultiplayerSlaveClock.WaitingOnFrames => WaitingOnFrames; - public double LastFrameTime => 0; - - double IMultiplayerSlaveClock.LastFrameTime => LastFrameTime; - public bool IsCatchingUp { get; set; } public readonly int Id; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs index e0fca45bdd..e90eed68ab 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs @@ -10,8 +10,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { IBindable WaitingOnFrames { get; } - double LastFrameTime { get; } - bool IsCatchingUp { get; set; } } } From fe3ba2b80ee1ad0034217ec1693646a6f5d31d95 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 19:07:25 +0900 Subject: [PATCH 27/88] Implement IAdjustableClock on GameplayClockContainer --- .../Screens/Play/GameplayClockContainer.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/osu.Game/Screens/Play/GameplayClockContainer.cs b/osu.Game/Screens/Play/GameplayClockContainer.cs index 6d863f0094..bbffdc2325 100644 --- a/osu.Game/Screens/Play/GameplayClockContainer.cs +++ b/osu.Game/Screens/Play/GameplayClockContainer.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . 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.Framework.Bindables; using osu.Framework.Graphics; @@ -9,7 +10,7 @@ using osu.Framework.Timing; namespace osu.Game.Screens.Play { - public abstract class GameplayClockContainer : Container + public abstract class GameplayClockContainer : Container, IAdjustableClock { /// /// The final clock which is exposed to underlying components. @@ -90,5 +91,37 @@ namespace osu.Game.Screens.Play protected virtual IFrameBasedClock ClockToProcess => AdjustableClock; protected abstract GameplayClock CreateGameplayClock(IFrameBasedClock source); + + #region IAdjustableClock + + bool IAdjustableClock.Seek(double position) + { + Seek(position); + return true; + } + + void IAdjustableClock.Reset() + { + Restart(); + Stop(); + } + + public void ResetSpeedAdjustments() + { + } + + double IAdjustableClock.Rate + { + get => GameplayClock.Rate; + set => throw new NotSupportedException(); + } + + double IClock.Rate => GameplayClock.Rate; + + public double CurrentTime => GameplayClock.CurrentTime; + + public bool IsRunning => GameplayClock.IsRunning; + + #endregion } } From 1705d472b5ff9a6194e0b18ddae14f01c1b91286 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 19:12:52 +0900 Subject: [PATCH 28/88] Reimplement multiplayer syncing using new master/slave clocks --- .../TestSceneMultiplayerSpectator.cs | 30 +---- .../Spectate/MultiplayerSlaveClock.cs | 78 +++++++++++++ .../Spectate/MultiplayerSpectator.cs | 86 +++------------ .../Spectate/MultiplayerSpectatorPlayer.cs | 9 +- .../Spectate/MultiplayerSyncManager.cs | 5 - .../Multiplayer/Spectate/PlayerInstance.cs | 103 +----------------- 6 files changed, 104 insertions(+), 207 deletions(-) create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSlaveClock.cs diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index 1ac012c0b5..ab6324df2d 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -1,15 +1,12 @@ // Copyright (c) ppy Pty Ltd . 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; using System.Threading.Tasks; using NUnit.Framework; using osu.Framework.Allocation; -using osu.Framework.Audio; -using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Testing; using osu.Framework.Utils; @@ -156,27 +153,6 @@ namespace osu.Game.Tests.Visual.Multiplayer checkPausedInstant(55, false); } - [Test] - public void TestPlayerStartsCatchingUpOnlyAfterExceedingMaxOffset() - { - start(new[] { 55, 56 }); - loadSpectateScreen(); - - sendFrames(55, 1000); - sendFrames(56, 1000); - - Bindable slowDownAdjustment; - - AddStep("slow down player 2", () => - { - slowDownAdjustment = new Bindable(0.99); - getInstance(56).Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, slowDownAdjustment); - }); - - AddUntilStep("exceeded min offset but not catching up", () => getGameplayOffset(55, 56) > PlayerInstance.MAX_OFFSET && !getInstance(56).IsCatchingUp); - AddUntilStep("catching up or caught up", () => getInstance(56).IsCatchingUp || Math.Abs(getGameplayOffset(55, 56)) < PlayerInstance.SYNC_TARGET * 2); - } - [Test] public void TestPlayersCatchUpAfterFallingBehind() { @@ -184,7 +160,7 @@ namespace osu.Game.Tests.Visual.Multiplayer loadSpectateScreen(); // Send initial frames for both players. A few more for player 1. - sendFrames(55, 100); + sendFrames(55, 1000); sendFrames(56, 10); checkPausedInstant(55, false); checkPausedInstant(56, false); @@ -194,11 +170,11 @@ namespace osu.Game.Tests.Visual.Multiplayer AddWaitStep("wait a few more frames", 10); // Send more frames for player 2. It should unpause. - sendFrames(56, 100); + sendFrames(56, 1000); checkPausedInstant(56, false); // Player 2 should catch up to player 1 after unpausing. - AddUntilStep("player 2 not catching up", () => !getInstance(56).IsCatchingUp); + AddUntilStep("player 2 not catching up", () => !getInstance(56).GameplayClock.IsCatchingUp); AddWaitStep("wait a bit", 10); } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSlaveClock.cs new file mode 100644 index 0000000000..84a87271a2 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSlaveClock.cs @@ -0,0 +1,78 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using osu.Framework.Bindables; +using osu.Framework.Timing; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +{ + public class MultiplayerSlaveClock : IFrameBasedClock, IMultiplayerSlaveClock + { + /// + /// The catchup rate. + /// + public const double CATCHUP_RATE = 2; + + private readonly IFrameBasedClock masterClock; + + public MultiplayerSlaveClock(IFrameBasedClock masterClock) + { + this.masterClock = masterClock; + } + + public double CurrentTime { get; private set; } + + public bool IsRunning { get; private set; } + + public void Reset() => CurrentTime = 0; + + public void Start() => IsRunning = true; + + public void Stop() => IsRunning = false; + + public bool Seek(double position) => true; + + public void ResetSpeedAdjustments() + { + } + + public double Rate => IsCatchingUp ? CATCHUP_RATE : 1; + + double IAdjustableClock.Rate + { + get => Rate; + set => throw new NotSupportedException(); + } + + double IClock.Rate => Rate; + + public void ProcessFrame() + { + masterClock.ProcessFrame(); + + ElapsedFrameTime = 0; + FramesPerSecond = 0; + + if (IsRunning) + { + double elapsedSource = masterClock.ElapsedFrameTime; + double elapsed = elapsedSource * Rate; + + CurrentTime += elapsed; + ElapsedFrameTime = elapsed; + FramesPerSecond = masterClock.FramesPerSecond; + } + } + + public double ElapsedFrameTime { get; private set; } + + public double FramesPerSecond { get; private set; } + + public FrameTimeInfo TimeInfo => new FrameTimeInfo { Elapsed = ElapsedFrameTime, Current = CurrentTime }; + + public IBindable WaitingOnFrames { get; } = new Bindable(); + + public bool IsCatchingUp { get; set; } + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 931d13942b..be5a8cf8c0 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -2,14 +2,10 @@ // See the LICENCE file in the repository root for full licence text. using System; -using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Framework.Logging; -using osu.Framework.Utils; using osu.Game.Online.Spectator; using osu.Game.Screens.Play; using osu.Game.Screens.Spectate; @@ -18,9 +14,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiplayerSpectator : SpectatorScreen { - private const double min_duration_to_allow_playback = 50; - private const double maximum_start_delay = 15000; - // Isolates beatmap/ruleset to this screen. public override bool DisallowExternalBeatmapRulesetChanges => true; @@ -30,10 +23,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private SpectatorStreamingClient spectatorClient { get; set; } private readonly PlayerInstance[] instances; - private GameplayClockContainer gameplayClockContainer; + private MasterGameplayClockContainer masterClockContainer; + private IMultiplayerSyncManager syncManager; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; - private double? loadFinishTime; public MultiplayerSpectator(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) @@ -46,7 +39,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { Container leaderboardContainer; - InternalChild = gameplayClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0) + masterClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0) { Child = new GridContainer { @@ -70,6 +63,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate } }; + InternalChildren = new[] + { + (Drawable)(syncManager = new MultiplayerSyncManager(masterClockContainer)), + masterClockContainer + }; + // Todo: This is not quite correct - it should be per-user to adjust for other mod combinations. var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor(); @@ -78,65 +77,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate LoadComponentAsync(leaderboard = new MultiplayerSpectatorLeaderboard(scoreProcessor, UserIds) { Expanded = { Value = true } }, leaderboardContainer.Add); } - protected override void UpdateAfterChildren() + protected override void LoadComplete() { - base.UpdateAfterChildren(); + base.LoadComplete(); - if (AllPlayersLoaded) - loadFinishTime ??= Time.Current; - - updateGameplayPlayingState(); - } - - private bool canStartGameplay => - // All players must be loaded, and... - AllPlayersLoaded - && ( - // All players have frames... - instances.All(i => i.Score.Replay.Frames.Count > 0) - // Or any player has frames and the maximum start delay has been exceeded. - || (Time.Current - loadFinishTime > maximum_start_delay - && instances.Any(i => i.Score.Replay.Frames.Count > 0)) - ); - - private bool firstStartFrame = true; - - private void updateGameplayPlayingState() - { - // Make sure all players are loaded and have frames before starting any. - if (!canStartGameplay) - { - foreach (var inst in instances) - inst?.PauseGameplay(); - return; - } - - if (firstStartFrame) - gameplayClockContainer.Restart(); - - // Not all instances may be in a valid gameplay state (see canStartGameplay). Only control the ones that are. - IEnumerable validInstances = instances.Where(i => i.Score.Replay.Frames.Count > 0); - - double targetGameplayTime = gameplayClockContainer.GameplayClock.CurrentTime; - - var instanceTimes = string.Join(',', validInstances.Select(i => $" {i.User.Id}: {(int)i.GetCurrentGameplayTime()}")); - Logger.Log($"target: {(int)targetGameplayTime},{instanceTimes}"); - - foreach (var inst in validInstances) - { - Debug.Assert(inst != null); - - double lastFrameTime = inst.Score.Replay.Frames.Select(f => f.Time).Last(); - double currentTime = inst.GetCurrentGameplayTime(); - - bool canContinuePlayback = Precision.DefinitelyBigger(lastFrameTime, currentTime, min_duration_to_allow_playback); - if (!canContinuePlayback) - continue; - - inst.ContinueGameplay(targetGameplayTime); - } - - firstStartFrame = false; + masterClockContainer.Stop(); + masterClockContainer.Restart(); } protected override void OnUserStateChanged(int userId, SpectatorState spectatorState) @@ -151,15 +97,17 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate if (existingInstance != null) { grid.Remove(existingInstance); + syncManager.RemoveSlave(existingInstance.GameplayClock); leaderboard.RemoveClock(existingInstance.User.Id); } - LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, gameplayClockContainer.GameplayClock), d => + LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, new MultiplayerSlaveClock(masterClockContainer.GameplayClock)), d => { if (instances[userIndex] == d) { grid.Add(d); - leaderboard.AddClock(d.User.Id, d.Beatmap.Track); + syncManager.AddSlave(d.GameplayClock); + leaderboard.AddClock(d.User.Id, d.GameplayClock); } }); } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index abb7ec83d8..c82c407c2c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -4,7 +4,6 @@ using osu.Framework.Bindables; using osu.Framework.Timing; using osu.Game.Beatmaps; -using osu.Game.Rulesets.Scoring; using osu.Game.Scoring; using osu.Game.Screens.Play; @@ -12,13 +11,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiplayerSpectatorPlayer : SpectatorPlayer { - public new ScoreProcessor ScoreProcessor => base.ScoreProcessor; + private readonly MultiplayerSlaveClock gameplayClock; - public new SubGameplayClockContainer GameplayClockContainer => (SubGameplayClockContainer)base.GameplayClockContainer; - - private readonly GameplayClock gameplayClock; - - public MultiplayerSpectatorPlayer(Score score, GameplayClock gameplayClock) + public MultiplayerSpectatorPlayer(Score score, MultiplayerSlaveClock gameplayClock) : base(score) { this.gameplayClock = gameplayClock; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs index 42f6536e90..8cbfa823cf 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs @@ -26,11 +26,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// public const double MAXIMUM_START_DELAY = 15000; - /// - /// The catchup rate. - /// - public const double CATCHUP_RATE = 2; - /// /// The master clock which is used to control the timing of all slave clocks. /// diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 724a685cb7..631ac2d52e 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -4,7 +4,6 @@ using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Framework.Logging; using osu.Game.Beatmaps; using osu.Game.Scoring; using osu.Game.Screens.Play; @@ -14,21 +13,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class PlayerInstance : CompositeDrawable { - /// - /// The rate at which a user catches up after becoming desynchronised. - /// - private const double catchup_rate = 2; - - /// - /// The offset from the expected time at which to START synchronisation. - /// - public const double MAX_OFFSET = 50; - - /// - /// The maximum offset from the expected time at which to STOP synchronisation. - /// - public const double SYNC_TARGET = 16; - public bool PlayerLoaded => stack?.CurrentScreen is Player; public User User => Score.ScoreInfo.User; @@ -36,17 +20,14 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public WorkingBeatmap Beatmap { get; private set; } public readonly Score Score; - private readonly GameplayClock gameplayClock; - - public bool IsCatchingUp { get; private set; } + public readonly MultiplayerSlaveClock GameplayClock; private OsuScreenStack stack; - private MultiplayerSpectatorPlayer player; - public PlayerInstance(Score score, GameplayClock gameplayClock) + public PlayerInstance(Score score, MultiplayerSlaveClock gameplayClock) { Score = score; - this.gameplayClock = gameplayClock; + GameplayClock = gameplayClock; RelativeSizeAxes = Axes.Both; Masking = true; @@ -67,83 +48,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate } }; - stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => player = new MultiplayerSpectatorPlayer(Score, gameplayClock))); - } - - protected override void UpdateAfterChildren() - { - base.UpdateAfterChildren(); - updateCatchup(); - } - - private double targetGameplayTime; - - private void updateCatchup() - { - if (player?.IsLoaded != true) - return; - - if (Score.Replay.Frames.Count == 0) - return; - - if (player.GameplayClockContainer.IsPaused.Value) - return; - - double currentTime = Beatmap.Track.CurrentTime; - double timeBehind = targetGameplayTime - currentTime; - - double offsetForCatchup = IsCatchingUp ? SYNC_TARGET : MAX_OFFSET; - bool catchupRequired = timeBehind > offsetForCatchup; - - // Skip catchup if no work needs to be done. - if (catchupRequired == IsCatchingUp) - return; - - if (catchupRequired) - { - // player.GameplayClockContainer.AdjustableClock.Rate = catchup_rate; - Logger.Log($"{User.Id} catchup started (behind: {(int)timeBehind})"); - } - else - { - // player.GameplayClockContainer.AdjustableClock.Rate = 1; - Logger.Log($"{User.Id} catchup finished (behind: {(int)timeBehind})"); - } - - IsCatchingUp = catchupRequired; - } - - public double GetCurrentGameplayTime() - { - if (player?.IsLoaded != true) - return 0; - - return player.GameplayClockContainer.GameplayClock.CurrentTime; - } - - public bool IsPlaying() - { - if (player.IsLoaded != true) - return false; - - return player.GameplayClockContainer.GameplayClock.IsRunning; - } - - public void ContinueGameplay(double targetGameplayTime) - { - if (player?.IsLoaded != true) - return; - - player.GameplayClockContainer.Start(); - this.targetGameplayTime = targetGameplayTime; - } - - public void PauseGameplay() - { - if (player?.IsLoaded != true) - return; - - player.GameplayClockContainer.Stop(); + stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => new MultiplayerSpectatorPlayer(Score, GameplayClock))); } // Player interferes with global input, so disable input for now. From df4fce2c570b885176cb70f1f2232610ad2a7848 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 19:16:00 +0900 Subject: [PATCH 29/88] Rename classes --- ...s => MultiplayerCatchupSyncManagerTest.cs} | 50 +++++++++---------- ....cs => IMultiplayerSpectatorSlaveClock.cs} | 2 +- ...cs => IMultiplayerSpectatorSyncManager.cs} | 6 +-- ...er.cs => MultiplayerCatchupSyncManager.cs} | 10 ++-- .../Spectate/MultiplayerSpectator.cs | 6 +-- .../Spectate/MultiplayerSpectatorPlayer.cs | 4 +- ...k.cs => MultiplayerSpectatorSlaveClock.cs} | 4 +- .../Multiplayer/Spectate/PlayerInstance.cs | 4 +- 8 files changed, 43 insertions(+), 43 deletions(-) rename osu.Game.Tests/OnlinePlay/{MultiplayerSyncManagerTest.cs => MultiplayerCatchupSyncManagerTest.cs} (70%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{IMultiplayerSlaveClock.cs => IMultiplayerSpectatorSlaveClock.cs} (83%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{IMultiplayerSyncManager.cs => IMultiplayerSpectatorSyncManager.cs} (62%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerSyncManager.cs => MultiplayerCatchupSyncManager.cs} (91%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerSlaveClock.cs => MultiplayerSpectatorSlaveClock.cs} (91%) diff --git a/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs b/osu.Game.Tests/OnlinePlay/MultiplayerCatchupSyncManagerTest.cs similarity index 70% rename from osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs rename to osu.Game.Tests/OnlinePlay/MultiplayerCatchupSyncManagerTest.cs index 6ee867c6f8..f15fb5cfd1 100644 --- a/osu.Game.Tests/OnlinePlay/MultiplayerSyncManagerTest.cs +++ b/osu.Game.Tests/OnlinePlay/MultiplayerCatchupSyncManagerTest.cs @@ -12,22 +12,22 @@ using osu.Game.Tests.Visual; namespace osu.Game.Tests.OnlinePlay { [HeadlessTest] - public class MultiplayerSyncManagerTest : OsuTestScene + public class MultiplayerCatchupSyncManagerTest : OsuTestScene { private TestManualClock master; - private MultiplayerSyncManager syncManager; + private MultiplayerCatchupSyncManager catchupSyncManager; - private TestSlaveClock slave1; - private TestSlaveClock slave2; + private TestSpectatorSlaveClock slave1; + private TestSpectatorSlaveClock slave2; [SetUp] public void Setup() { - syncManager = new MultiplayerSyncManager(master = new TestManualClock()); - syncManager.AddSlave(slave1 = new TestSlaveClock(1)); - syncManager.AddSlave(slave2 = new TestSlaveClock(2)); + catchupSyncManager = new MultiplayerCatchupSyncManager(master = new TestManualClock()); + catchupSyncManager.AddSlave(slave1 = new TestSpectatorSlaveClock(1)); + catchupSyncManager.AddSlave(slave2 = new TestSpectatorSlaveClock(2)); - Schedule(() => Child = syncManager); + Schedule(() => Child = catchupSyncManager); } [Test] @@ -47,7 +47,7 @@ namespace osu.Game.Tests.OnlinePlay [Test] public void TestMasterClockDoesNotStartWhenNoneReadyForMaximumDelayTime() { - AddWaitStep($"wait {MultiplayerSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + AddWaitStep($"wait {MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY / TimePerAction)); assertMasterState(false); } @@ -55,7 +55,7 @@ namespace osu.Game.Tests.OnlinePlay public void TestMasterClockStartsWhenAnyReadyForMaximumDelayTime() { setWaiting(() => slave1, false); - AddWaitStep($"wait {MultiplayerSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + AddWaitStep($"wait {MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY / TimePerAction)); assertMasterState(true); } @@ -64,7 +64,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerSyncManager.SYNC_TARGET + 1); + setMasterTime(MultiplayerCatchupSyncManager.SYNC_TARGET + 1); assertCatchingUp(() => slave1, false); } @@ -73,7 +73,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerSyncManager.MAX_SYNC_OFFSET + 1); + setMasterTime(MultiplayerCatchupSyncManager.MAX_SYNC_OFFSET + 1); assertCatchingUp(() => slave1, true); assertCatchingUp(() => slave2, true); } @@ -83,8 +83,8 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerSyncManager.MAX_SYNC_OFFSET + 1); - setSlaveTime(() => slave1, MultiplayerSyncManager.SYNC_TARGET + 1); + setMasterTime(MultiplayerCatchupSyncManager.MAX_SYNC_OFFSET + 1); + setSlaveTime(() => slave1, MultiplayerCatchupSyncManager.SYNC_TARGET + 1); assertCatchingUp(() => slave1, true); } @@ -93,8 +93,8 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerSyncManager.MAX_SYNC_OFFSET + 2); - setSlaveTime(() => slave1, MultiplayerSyncManager.SYNC_TARGET); + setMasterTime(MultiplayerCatchupSyncManager.MAX_SYNC_OFFSET + 2); + setSlaveTime(() => slave1, MultiplayerCatchupSyncManager.SYNC_TARGET); assertCatchingUp(() => slave1, false); assertCatchingUp(() => slave2, true); } @@ -104,7 +104,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setSlaveTime(() => slave1, -MultiplayerSyncManager.SYNC_TARGET); + setSlaveTime(() => slave1, -MultiplayerCatchupSyncManager.SYNC_TARGET); assertCatchingUp(() => slave1, false); assertSlaveState(() => slave1, true); } @@ -114,7 +114,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setSlaveTime(() => slave1, -MultiplayerSyncManager.SYNC_TARGET - 1); + setSlaveTime(() => slave1, -MultiplayerCatchupSyncManager.SYNC_TARGET - 1); // This is a silent catchup, where IsCatchingUp = false but IsRunning = false also. assertCatchingUp(() => slave1, false); @@ -135,7 +135,7 @@ namespace osu.Game.Tests.OnlinePlay assertSlaveState(() => slave1, false); } - private void setWaiting(Func slave, bool waiting) + private void setWaiting(Func slave, bool waiting) => AddStep($"set slave {slave().Id} waiting = {waiting}", () => slave().WaitingOnFrames.Value = waiting); private void setAllWaiting(bool waiting) => AddStep($"set all slaves waiting = {waiting}", () => @@ -150,28 +150,28 @@ namespace osu.Game.Tests.OnlinePlay /// /// slave.Time = master.Time - offsetFromMaster /// - private void setSlaveTime(Func slave, double offsetFromMaster) + private void setSlaveTime(Func slave, double offsetFromMaster) => AddStep($"set slave {slave().Id} = master - {offsetFromMaster}", () => slave().Seek(master.CurrentTime - offsetFromMaster)); private void assertMasterState(bool running) => AddAssert($"master clock {(running ? "is" : "is not")} running", () => master.IsRunning == running); - private void assertCatchingUp(Func slave, bool catchingUp) => + private void assertCatchingUp(Func slave, bool catchingUp) => AddAssert($"slave {slave().Id} {(catchingUp ? "is" : "is not")} catching up", () => slave().IsCatchingUp == catchingUp); - private void assertSlaveState(Func slave, bool running) + private void assertSlaveState(Func slave, bool running) => AddAssert($"slave {slave().Id} {(running ? "is" : "is not")} running", () => slave().IsRunning == running); - private class TestSlaveClock : TestManualClock, IMultiplayerSlaveClock + private class TestSpectatorSlaveClock : TestManualClock, IMultiplayerSpectatorSlaveClock { public readonly Bindable WaitingOnFrames = new Bindable(true); - IBindable IMultiplayerSlaveClock.WaitingOnFrames => WaitingOnFrames; + IBindable IMultiplayerSpectatorSlaveClock.WaitingOnFrames => WaitingOnFrames; public bool IsCatchingUp { get; set; } public readonly int Id; - public TestSlaveClock(int id) + public TestSpectatorSlaveClock(int id) { Id = id; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSlaveClock.cs similarity index 83% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSlaveClock.cs index e90eed68ab..fe2c2dfec9 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSlaveClock.cs @@ -6,7 +6,7 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public interface IMultiplayerSlaveClock : IAdjustableClock + public interface IMultiplayerSpectatorSlaveClock : IAdjustableClock { IBindable WaitingOnFrames { get; } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSyncManager.cs similarity index 62% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSyncManager.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSyncManager.cs index 2c50596823..5643bde68f 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSyncManager.cs @@ -5,12 +5,12 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public interface IMultiplayerSyncManager + public interface IMultiplayerSpectatorSyncManager { IAdjustableClock Master { get; } - void AddSlave(IMultiplayerSlaveClock clock); + void AddSlave(IMultiplayerSpectatorSlaveClock clock); - void RemoveSlave(IMultiplayerSlaveClock clock); + void RemoveSlave(IMultiplayerSpectatorSlaveClock clock); } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerCatchupSyncManager.cs similarity index 91% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerCatchupSyncManager.cs index 8cbfa823cf..873f34626d 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerCatchupSyncManager.cs @@ -9,7 +9,7 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class MultiplayerSyncManager : Component, IMultiplayerSyncManager + public class MultiplayerCatchupSyncManager : Component, IMultiplayerSpectatorSyncManager { /// /// The offset from the master clock to which slaves should be synchronised to. @@ -34,19 +34,19 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// /// The slave clocks. /// - private readonly List slaves = new List(); + private readonly List slaves = new List(); private bool hasStarted; private double? firstStartAttemptTime; - public MultiplayerSyncManager(IAdjustableClock master) + public MultiplayerCatchupSyncManager(IAdjustableClock master) { Master = master; } - public void AddSlave(IMultiplayerSlaveClock clock) => slaves.Add(clock); + public void AddSlave(IMultiplayerSpectatorSlaveClock clock) => slaves.Add(clock); - public void RemoveSlave(IMultiplayerSlaveClock clock) => slaves.Remove(clock); + public void RemoveSlave(IMultiplayerSpectatorSlaveClock clock) => slaves.Remove(clock); protected override void Update() { diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index be5a8cf8c0..3dade4d32c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -24,7 +24,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly PlayerInstance[] instances; private MasterGameplayClockContainer masterClockContainer; - private IMultiplayerSyncManager syncManager; + private IMultiplayerSpectatorSyncManager syncManager; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; @@ -65,7 +65,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate InternalChildren = new[] { - (Drawable)(syncManager = new MultiplayerSyncManager(masterClockContainer)), + (Drawable)(syncManager = new MultiplayerCatchupSyncManager(masterClockContainer)), masterClockContainer }; @@ -101,7 +101,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate leaderboard.RemoveClock(existingInstance.User.Id); } - LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, new MultiplayerSlaveClock(masterClockContainer.GameplayClock)), d => + LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, new MultiplayerSpectatorSlaveClock(masterClockContainer.GameplayClock)), d => { if (instances[userIndex] == d) { diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index c82c407c2c..7fe1416224 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -11,9 +11,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiplayerSpectatorPlayer : SpectatorPlayer { - private readonly MultiplayerSlaveClock gameplayClock; + private readonly MultiplayerSpectatorSlaveClock gameplayClock; - public MultiplayerSpectatorPlayer(Score score, MultiplayerSlaveClock gameplayClock) + public MultiplayerSpectatorPlayer(Score score, MultiplayerSpectatorSlaveClock gameplayClock) : base(score) { this.gameplayClock = gameplayClock; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorSlaveClock.cs similarity index 91% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSlaveClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorSlaveClock.cs index 84a87271a2..6960d24295 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorSlaveClock.cs @@ -7,7 +7,7 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class MultiplayerSlaveClock : IFrameBasedClock, IMultiplayerSlaveClock + public class MultiplayerSpectatorSlaveClock : IFrameBasedClock, IMultiplayerSpectatorSlaveClock { /// /// The catchup rate. @@ -16,7 +16,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly IFrameBasedClock masterClock; - public MultiplayerSlaveClock(IFrameBasedClock masterClock) + public MultiplayerSpectatorSlaveClock(IFrameBasedClock masterClock) { this.masterClock = masterClock; } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 631ac2d52e..384c1f1f2b 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -20,11 +20,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public WorkingBeatmap Beatmap { get; private set; } public readonly Score Score; - public readonly MultiplayerSlaveClock GameplayClock; + public readonly MultiplayerSpectatorSlaveClock GameplayClock; private OsuScreenStack stack; - public PlayerInstance(Score score, MultiplayerSlaveClock gameplayClock) + public PlayerInstance(Score score, MultiplayerSpectatorSlaveClock gameplayClock) { Score = score; GameplayClock = gameplayClock; From 82fcabb8f0d94b083b25306d1647c1925c598f5c Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 19:32:55 +0900 Subject: [PATCH 30/88] More refactorings/renamespacings/xmldocs --- ...s => MultiplayerCatchUpSyncManagerTest.cs} | 48 +++++++++++-------- .../IMultiplayerSpectatorSlaveClock.cs | 15 ------ .../IMultiplayerSpectatorSyncManager.cs | 16 ------- .../Spectate/MultiplayerSpectator.cs | 7 +-- .../Spectate/MultiplayerSpectatorPlayer.cs | 5 +- .../Multiplayer/Spectate/PlayerInstance.cs | 5 +- .../Spectate/Sync/ISpectatorSlaveClock.cs | 24 ++++++++++ .../Spectate/Sync/ISpectatorSyncManager.cs | 30 ++++++++++++ .../SpectatorCatchUpSlaveClock.cs} | 6 +-- .../SpectatorCatchUpSyncManager.cs} | 15 +++--- 10 files changed, 105 insertions(+), 66 deletions(-) rename osu.Game.Tests/OnlinePlay/{MultiplayerCatchupSyncManagerTest.cs => MultiplayerCatchUpSyncManagerTest.cs} (76%) delete mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSlaveClock.cs delete mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSyncManager.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSlaveClock.cs create mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerSpectatorSlaveClock.cs => Sync/SpectatorCatchUpSlaveClock.cs} (89%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerCatchupSyncManager.cs => Sync/SpectatorCatchUpSyncManager.cs} (88%) diff --git a/osu.Game.Tests/OnlinePlay/MultiplayerCatchupSyncManagerTest.cs b/osu.Game.Tests/OnlinePlay/MultiplayerCatchUpSyncManagerTest.cs similarity index 76% rename from osu.Game.Tests/OnlinePlay/MultiplayerCatchupSyncManagerTest.cs rename to osu.Game.Tests/OnlinePlay/MultiplayerCatchUpSyncManagerTest.cs index f15fb5cfd1..6aa1219d53 100644 --- a/osu.Game.Tests/OnlinePlay/MultiplayerCatchupSyncManagerTest.cs +++ b/osu.Game.Tests/OnlinePlay/MultiplayerCatchUpSyncManagerTest.cs @@ -6,16 +6,16 @@ using NUnit.Framework; using osu.Framework.Bindables; using osu.Framework.Testing; using osu.Framework.Timing; -using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Tests.Visual; namespace osu.Game.Tests.OnlinePlay { [HeadlessTest] - public class MultiplayerCatchupSyncManagerTest : OsuTestScene + public class MultiplayerCatchUpSyncManagerTest : OsuTestScene { private TestManualClock master; - private MultiplayerCatchupSyncManager catchupSyncManager; + private SpectatorCatchUpSyncManager syncManager; private TestSpectatorSlaveClock slave1; private TestSpectatorSlaveClock slave2; @@ -23,11 +23,11 @@ namespace osu.Game.Tests.OnlinePlay [SetUp] public void Setup() { - catchupSyncManager = new MultiplayerCatchupSyncManager(master = new TestManualClock()); - catchupSyncManager.AddSlave(slave1 = new TestSpectatorSlaveClock(1)); - catchupSyncManager.AddSlave(slave2 = new TestSpectatorSlaveClock(2)); + syncManager = new SpectatorCatchUpSyncManager(master = new TestManualClock()); + syncManager.AddSlave(slave1 = new TestSpectatorSlaveClock(1)); + syncManager.AddSlave(slave2 = new TestSpectatorSlaveClock(2)); - Schedule(() => Child = catchupSyncManager); + Schedule(() => Child = syncManager); } [Test] @@ -47,7 +47,7 @@ namespace osu.Game.Tests.OnlinePlay [Test] public void TestMasterClockDoesNotStartWhenNoneReadyForMaximumDelayTime() { - AddWaitStep($"wait {MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + AddWaitStep($"wait {SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); assertMasterState(false); } @@ -55,7 +55,7 @@ namespace osu.Game.Tests.OnlinePlay public void TestMasterClockStartsWhenAnyReadyForMaximumDelayTime() { setWaiting(() => slave1, false); - AddWaitStep($"wait {MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(MultiplayerCatchupSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + AddWaitStep($"wait {SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); assertMasterState(true); } @@ -64,7 +64,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerCatchupSyncManager.SYNC_TARGET + 1); + setMasterTime(SpectatorCatchUpSyncManager.SYNC_TARGET + 1); assertCatchingUp(() => slave1, false); } @@ -73,7 +73,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerCatchupSyncManager.MAX_SYNC_OFFSET + 1); + setMasterTime(SpectatorCatchUpSyncManager.MAX_SYNC_OFFSET + 1); assertCatchingUp(() => slave1, true); assertCatchingUp(() => slave2, true); } @@ -83,8 +83,8 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerCatchupSyncManager.MAX_SYNC_OFFSET + 1); - setSlaveTime(() => slave1, MultiplayerCatchupSyncManager.SYNC_TARGET + 1); + setMasterTime(SpectatorCatchUpSyncManager.MAX_SYNC_OFFSET + 1); + setSlaveTime(() => slave1, SpectatorCatchUpSyncManager.SYNC_TARGET + 1); assertCatchingUp(() => slave1, true); } @@ -93,8 +93,8 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(MultiplayerCatchupSyncManager.MAX_SYNC_OFFSET + 2); - setSlaveTime(() => slave1, MultiplayerCatchupSyncManager.SYNC_TARGET); + setMasterTime(SpectatorCatchUpSyncManager.MAX_SYNC_OFFSET + 2); + setSlaveTime(() => slave1, SpectatorCatchUpSyncManager.SYNC_TARGET); assertCatchingUp(() => slave1, false); assertCatchingUp(() => slave2, true); } @@ -104,7 +104,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setSlaveTime(() => slave1, -MultiplayerCatchupSyncManager.SYNC_TARGET); + setSlaveTime(() => slave1, -SpectatorCatchUpSyncManager.SYNC_TARGET); assertCatchingUp(() => slave1, false); assertSlaveState(() => slave1, true); } @@ -114,7 +114,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setSlaveTime(() => slave1, -MultiplayerCatchupSyncManager.SYNC_TARGET - 1); + setSlaveTime(() => slave1, -SpectatorCatchUpSyncManager.SYNC_TARGET - 1); // This is a silent catchup, where IsCatchingUp = false but IsRunning = false also. assertCatchingUp(() => slave1, false); @@ -162,10 +162,10 @@ namespace osu.Game.Tests.OnlinePlay private void assertSlaveState(Func slave, bool running) => AddAssert($"slave {slave().Id} {(running ? "is" : "is not")} running", () => slave().IsRunning == running); - private class TestSpectatorSlaveClock : TestManualClock, IMultiplayerSpectatorSlaveClock + private class TestSpectatorSlaveClock : TestManualClock, ISpectatorSlaveClock { public readonly Bindable WaitingOnFrames = new Bindable(true); - IBindable IMultiplayerSpectatorSlaveClock.WaitingOnFrames => WaitingOnFrames; + IBindable ISpectatorSlaveClock.WaitingOnFrames => WaitingOnFrames; public bool IsCatchingUp { get; set; } @@ -183,6 +183,16 @@ namespace osu.Game.Tests.OnlinePlay Start(); }); } + + public void ProcessFrame() + { + } + + public double ElapsedFrameTime => 0; + + public double FramesPerSecond => 0; + + public FrameTimeInfo TimeInfo => default; } private class TestManualClock : ManualClock, IAdjustableClock diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSlaveClock.cs deleted file mode 100644 index fe2c2dfec9..0000000000 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSlaveClock.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -using osu.Framework.Bindables; -using osu.Framework.Timing; - -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate -{ - public interface IMultiplayerSpectatorSlaveClock : IAdjustableClock - { - IBindable WaitingOnFrames { get; } - - bool IsCatchingUp { get; set; } - } -} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSyncManager.cs deleted file mode 100644 index 5643bde68f..0000000000 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/IMultiplayerSpectatorSyncManager.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -using osu.Framework.Timing; - -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate -{ - public interface IMultiplayerSpectatorSyncManager - { - IAdjustableClock Master { get; } - - void AddSlave(IMultiplayerSpectatorSlaveClock clock); - - void RemoveSlave(IMultiplayerSpectatorSlaveClock clock); - } -} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 3dade4d32c..441839b401 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -7,6 +7,7 @@ using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Online.Spectator; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; using osu.Game.Screens.Spectate; @@ -24,7 +25,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly PlayerInstance[] instances; private MasterGameplayClockContainer masterClockContainer; - private IMultiplayerSpectatorSyncManager syncManager; + private ISpectatorSyncManager syncManager; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; @@ -65,7 +66,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate InternalChildren = new[] { - (Drawable)(syncManager = new MultiplayerCatchupSyncManager(masterClockContainer)), + (Drawable)(syncManager = new SpectatorCatchUpSyncManager(masterClockContainer)), masterClockContainer }; @@ -101,7 +102,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate leaderboard.RemoveClock(existingInstance.User.Id); } - LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, new MultiplayerSpectatorSlaveClock(masterClockContainer.GameplayClock)), d => + LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, new SpectatorCatchUpSlaveClock(masterClockContainer.GameplayClock)), d => { if (instances[userIndex] == d) { diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index 7fe1416224..1a7e0dd36a 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -5,15 +5,16 @@ using osu.Framework.Bindables; using osu.Framework.Timing; using osu.Game.Beatmaps; using osu.Game.Scoring; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiplayerSpectatorPlayer : SpectatorPlayer { - private readonly MultiplayerSpectatorSlaveClock gameplayClock; + private readonly SpectatorCatchUpSlaveClock gameplayClock; - public MultiplayerSpectatorPlayer(Score score, MultiplayerSpectatorSlaveClock gameplayClock) + public MultiplayerSpectatorPlayer(Score score, SpectatorCatchUpSlaveClock gameplayClock) : base(score) { this.gameplayClock = gameplayClock; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 384c1f1f2b..401732f7fb 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -6,6 +6,7 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; using osu.Game.Scoring; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; using osu.Game.Users; @@ -20,11 +21,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public WorkingBeatmap Beatmap { get; private set; } public readonly Score Score; - public readonly MultiplayerSpectatorSlaveClock GameplayClock; + public readonly SpectatorCatchUpSlaveClock GameplayClock; private OsuScreenStack stack; - public PlayerInstance(Score score, MultiplayerSpectatorSlaveClock gameplayClock) + public PlayerInstance(Score score, SpectatorCatchUpSlaveClock gameplayClock) { Score = score; GameplayClock = gameplayClock; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSlaveClock.cs new file mode 100644 index 0000000000..d8733d4322 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSlaveClock.cs @@ -0,0 +1,24 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Bindables; +using osu.Framework.Timing; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync +{ + /// + /// A clock which is used by s and managed by an . + /// + public interface ISpectatorSlaveClock : IFrameBasedClock, IAdjustableClock + { + /// + /// Whether this clock is waiting on frames to continue playback. + /// + IBindable WaitingOnFrames { get; } + + /// + /// Whether this clock is resynchronising to the master clock. + /// + bool IsCatchingUp { get; set; } + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs new file mode 100644 index 0000000000..107f9637a3 --- /dev/null +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs @@ -0,0 +1,30 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Timing; + +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync +{ + /// + /// Manages the synchronisation between one or more slave clocks in relation to a master clock. + /// + public interface ISpectatorSyncManager + { + /// + /// The master clock which slaves should synchronise to. + /// + IAdjustableClock Master { get; } + + /// + /// Adds a slave clock. + /// + /// The clock to add. + void AddSlave(ISpectatorSlaveClock clock); + + /// + /// Removes a slave clock. + /// + /// The clock to remove. + void RemoveSlave(ISpectatorSlaveClock clock); + } +} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs similarity index 89% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorSlaveClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs index 6960d24295..e4d25894c8 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs @@ -5,9 +5,9 @@ using System; using osu.Framework.Bindables; using osu.Framework.Timing; -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { - public class MultiplayerSpectatorSlaveClock : IFrameBasedClock, IMultiplayerSpectatorSlaveClock + public class SpectatorCatchUpSlaveClock : ISpectatorSlaveClock { /// /// The catchup rate. @@ -16,7 +16,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly IFrameBasedClock masterClock; - public MultiplayerSpectatorSlaveClock(IFrameBasedClock masterClock) + public SpectatorCatchUpSlaveClock(IFrameBasedClock masterClock) { this.masterClock = masterClock; } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerCatchupSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSyncManager.cs similarity index 88% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerCatchupSyncManager.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSyncManager.cs index 873f34626d..46e86177ca 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerCatchupSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSyncManager.cs @@ -7,9 +7,12 @@ using osu.Framework.Graphics; using osu.Framework.Logging; using osu.Framework.Timing; -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { - public class MultiplayerCatchupSyncManager : Component, IMultiplayerSpectatorSyncManager + /// + /// A which synchronises de-synced slave clocks through catchup. + /// + public class SpectatorCatchUpSyncManager : Component, ISpectatorSyncManager { /// /// The offset from the master clock to which slaves should be synchronised to. @@ -34,19 +37,19 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// /// The slave clocks. /// - private readonly List slaves = new List(); + private readonly List slaves = new List(); private bool hasStarted; private double? firstStartAttemptTime; - public MultiplayerCatchupSyncManager(IAdjustableClock master) + public SpectatorCatchUpSyncManager(IAdjustableClock master) { Master = master; } - public void AddSlave(IMultiplayerSpectatorSlaveClock clock) => slaves.Add(clock); + public void AddSlave(ISpectatorSlaveClock clock) => slaves.Add(clock); - public void RemoveSlave(IMultiplayerSpectatorSlaveClock clock) => slaves.Remove(clock); + public void RemoveSlave(ISpectatorSlaveClock clock) => slaves.Remove(clock); protected override void Update() { From 33cc5c5cb36456479bacf67ccdb4e585ce8e7ca5 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 19:35:57 +0900 Subject: [PATCH 31/88] A few more xmldocs --- .../Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs | 10 +++++----- .../Spectate/Sync/SpectatorCatchUpSlaveClock.cs | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs index 107f9637a3..5edbb3bc2c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs @@ -6,7 +6,7 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// Manages the synchronisation between one or more slave clocks in relation to a master clock. + /// Manages the synchronisation between one or more s in relation to a master clock. /// public interface ISpectatorSyncManager { @@ -16,15 +16,15 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync IAdjustableClock Master { get; } /// - /// Adds a slave clock. + /// Adds an to manage. /// - /// The clock to add. + /// The to add. void AddSlave(ISpectatorSlaveClock clock); /// - /// Removes a slave clock. + /// Removes an , stopping it from being managed by this . /// - /// The clock to remove. + /// The to remove. void RemoveSlave(ISpectatorSlaveClock clock); } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs index e4d25894c8..4b529021de 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs @@ -7,10 +7,13 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { + /// + /// A which catches up using rate adjustment. + /// public class SpectatorCatchUpSlaveClock : ISpectatorSlaveClock { /// - /// The catchup rate. + /// The catch up rate. /// public const double CATCHUP_RATE = 2; From b391a8f94e23b66a82825fc180a8fd10db0ffba3 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 15 Apr 2021 19:37:45 +0900 Subject: [PATCH 32/88] Properly bind WaitingOnFrames --- .../Spectate/MultiplayerSpectatorPlayer.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index 1a7e0dd36a..19cc9f18ad 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Timing; using osu.Game.Beatmaps; @@ -12,14 +13,20 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiplayerSpectatorPlayer : SpectatorPlayer { - private readonly SpectatorCatchUpSlaveClock gameplayClock; + private readonly ISpectatorSlaveClock gameplayClock; - public MultiplayerSpectatorPlayer(Score score, SpectatorCatchUpSlaveClock gameplayClock) + public MultiplayerSpectatorPlayer(Score score, ISpectatorSlaveClock gameplayClock) : base(score) { this.gameplayClock = gameplayClock; } + [BackgroundDependencyLoader] + private void load() + { + gameplayClock.WaitingOnFrames.BindTo(DrawableRuleset.FrameStableClock.WaitingOnFrames); + } + protected override GameplayClockContainer CreateGameplayClockContainer(WorkingBeatmap beatmap, double gameplayStart) => new SubGameplayClockContainer(gameplayClock); } From 5ac0eb02cd0bffd1c7876178c5b8ad50bad3b165 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 12:25:29 +0900 Subject: [PATCH 33/88] Always add player instances at first, populate later --- .../TestSceneMultiplayerSpectator.cs | 46 ++++++++++++++----- .../Spectate/MultiplayerSpectator.cs | 27 ++++------- .../Multiplayer/Spectate/PlayerInstance.cs | 23 +++++----- 3 files changed, 56 insertions(+), 40 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index ab6324df2d..4026258830 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -26,7 +26,7 @@ namespace osu.Game.Tests.Visual.Multiplayer public class TestSceneMultiplayerSpectator : MultiplayerTestScene { [Cached(typeof(SpectatorStreamingClient))] - private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient(); + private TestSpectatorStreamingClient streamingClient = new TestSpectatorStreamingClient(); [Cached(typeof(UserLookupCache))] private UserLookupCache lookupCache = new TestUserLookupCache(); @@ -62,18 +62,42 @@ namespace osu.Game.Tests.Visual.Multiplayer AddStep("add streaming client", () => { - Remove(testSpectatorStreamingClient); - Add(testSpectatorStreamingClient); + Remove(streamingClient); + Add(streamingClient); }); AddStep("finish previous gameplay", () => { foreach (var id in playingUserIds) - testSpectatorStreamingClient.EndPlay(id, importedBeatmapId); + streamingClient.EndPlay(id, importedBeatmapId); playingUserIds.Clear(); }); } + [Test] + public void TestDelayedStart() + { + AddStep("start players silently", () => + { + Client.CurrentMatchPlayingUserIds.Add(55); + Client.CurrentMatchPlayingUserIds.Add(56); + playingUserIds.Add(55); + playingUserIds.Add(56); + nextFrame[55] = 0; + nextFrame[56] = 0; + }); + + loadSpectateScreen(false); + + AddWaitStep("wait a bit", 10); + AddStep("load player 55", () => streamingClient.StartPlay(55, importedBeatmapId)); + AddUntilStep("one player added", () => spectator.ChildrenOfType().Count() == 1); + + AddWaitStep("wait a bit", 10); + AddStep("load player 56", () => streamingClient.StartPlay(56, importedBeatmapId)); + AddUntilStep("two players added", () => spectator.ChildrenOfType().Count() == 2); + } + [Test] public void TestGeneral() { @@ -178,7 +202,7 @@ namespace osu.Game.Tests.Visual.Multiplayer AddWaitStep("wait a bit", 10); } - private void loadSpectateScreen() + private void loadSpectateScreen(bool waitForPlayerLoad = true) { AddStep("load screen", () => { @@ -188,7 +212,7 @@ namespace osu.Game.Tests.Visual.Multiplayer LoadScreen(spectator = new MultiplayerSpectator(playingUserIds.ToArray())); }); - AddUntilStep("wait for screen load", () => spectator.LoadState == LoadState.Loaded && spectator.AllPlayersLoaded); + AddUntilStep("wait for screen load", () => spectator.LoadState == LoadState.Loaded && (!waitForPlayerLoad || spectator.AllPlayersLoaded)); } private void start(int userId, int? beatmapId = null) => start(new[] { userId }, beatmapId); @@ -200,7 +224,7 @@ namespace osu.Game.Tests.Visual.Multiplayer foreach (int id in userIds) { Client.CurrentMatchPlayingUserIds.Add(id); - testSpectatorStreamingClient.StartPlay(id, beatmapId ?? importedBeatmapId); + streamingClient.StartPlay(id, beatmapId ?? importedBeatmapId); playingUserIds.Add(id); nextFrame[id] = 0; } @@ -211,7 +235,7 @@ namespace osu.Game.Tests.Visual.Multiplayer { AddStep("end play", () => { - testSpectatorStreamingClient.EndPlay(userId, beatmapId ?? importedBeatmapId); + streamingClient.EndPlay(userId, beatmapId ?? importedBeatmapId); playingUserIds.Remove(userId); nextFrame.Remove(userId); }); @@ -225,7 +249,7 @@ namespace osu.Game.Tests.Visual.Multiplayer { foreach (int id in userIds) { - testSpectatorStreamingClient.SendFrames(id, nextFrame[id], count); + streamingClient.SendFrames(id, nextFrame[id], count); nextFrame[id] += count; } }); @@ -246,7 +270,7 @@ namespace osu.Game.Tests.Visual.Multiplayer private Player getPlayer(int userId) => getInstance(userId).ChildrenOfType().Single(); - private PlayerInstance getInstance(int userId) => spectator.ChildrenOfType().Single(p => p.User.Id == userId); + private PlayerInstance getInstance(int userId) => spectator.ChildrenOfType().Single(p => p.UserId == userId); public class TestSpectatorStreamingClient : SpectatorStreamingClient { @@ -297,7 +321,7 @@ namespace osu.Game.Tests.Visual.Multiplayer public override void WatchUser(int userId) { - if (userSentStateDictionary[userId]) + if (userSentStateDictionary.TryGetValue(userId, out var sent) && sent) { // usually the server would do this. sendState(userId, userBeatmapDictionary[userId]); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 441839b401..08b9938e79 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -32,7 +32,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public MultiplayerSpectator(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) { - instances = new PlayerInstance[userIds.Length]; + instances = new PlayerInstance[UserIds.Length]; } [BackgroundDependencyLoader] @@ -70,6 +70,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate masterClockContainer }; + for (int i = 0; i < UserIds.Length; i++) + grid.Add(instances[i] = new PlayerInstance(UserIds[i], new SpectatorCatchUpSlaveClock(masterClockContainer.GameplayClock))); + // Todo: This is not quite correct - it should be per-user to adjust for other mod combinations. var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor(); @@ -93,24 +96,14 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate protected override void StartGameplay(int userId, GameplayState gameplayState) { int userIndex = getIndexForUser(userId); - var existingInstance = instances[userIndex]; - if (existingInstance != null) - { - grid.Remove(existingInstance); - syncManager.RemoveSlave(existingInstance.GameplayClock); - leaderboard.RemoveClock(existingInstance.User.Id); - } + var instance = instances[userIndex]; + syncManager.RemoveSlave(instance.GameplayClock); + leaderboard.RemoveClock(instance.UserId); - LoadComponentAsync(instances[userIndex] = new PlayerInstance(gameplayState.Score, new SpectatorCatchUpSlaveClock(masterClockContainer.GameplayClock)), d => - { - if (instances[userIndex] == d) - { - grid.Add(d); - syncManager.AddSlave(d.GameplayClock); - leaderboard.AddClock(d.User.Id, d.GameplayClock); - } - }); + instance.LoadPlayer(gameplayState.Score); + syncManager.AddSlave(instance.GameplayClock); + leaderboard.AddClock(instance.UserId, instance.GameplayClock); } protected override void EndGameplay(int userId) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 401732f7fb..8c54fc9ec2 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -8,7 +8,6 @@ using osu.Game.Beatmaps; using osu.Game.Scoring; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; -using osu.Game.Users; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { @@ -16,30 +15,30 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public bool PlayerLoaded => stack?.CurrentScreen is Player; - public User User => Score.ScoreInfo.User; - - public WorkingBeatmap Beatmap { get; private set; } - - public readonly Score Score; + public readonly int UserId; public readonly SpectatorCatchUpSlaveClock GameplayClock; + public Score Score { get; private set; } + + [Resolved] + private BeatmapManager beatmapManager { get; set; } + private OsuScreenStack stack; - public PlayerInstance(Score score, SpectatorCatchUpSlaveClock gameplayClock) + public PlayerInstance(int userId, SpectatorCatchUpSlaveClock gameplayClock) { - Score = score; + UserId = userId; GameplayClock = gameplayClock; RelativeSizeAxes = Axes.Both; Masking = true; } - [BackgroundDependencyLoader] - private void load(BeatmapManager beatmapManager) + public void LoadPlayer(Score score) { - Beatmap = beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true); + Score = score; - InternalChild = new GameplayIsolationContainer(Beatmap, Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) + InternalChild = new GameplayIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) { RelativeSizeAxes = Axes.Both, Child = new DrawSizePreservingFillContainer From 1c086d99deae8c03b1412b13ec6f5c00e0abfe94 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 13:28:32 +0900 Subject: [PATCH 34/88] Add loading spinner --- .../Spectate/MultiplayerSpectator.cs | 8 ++----- .../Multiplayer/Spectate/PlayerInstance.cs | 24 +++++++++++++------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 08b9938e79..e1af95b870 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -95,13 +95,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate protected override void StartGameplay(int userId, GameplayState gameplayState) { - int userIndex = getIndexForUser(userId); + var instance = instances[getIndexForUser(userId)]; + instance.LoadScore(gameplayState.Score); - var instance = instances[userIndex]; - syncManager.RemoveSlave(instance.GameplayClock); - leaderboard.RemoveClock(instance.UserId); - - instance.LoadPlayer(gameplayState.Score); syncManager.AddSlave(instance.GameplayClock); leaderboard.AddClock(instance.UserId, instance.GameplayClock); } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 8c54fc9ec2..31d2d458df 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -1,10 +1,12 @@ // Copyright (c) ppy Pty Ltd . 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.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; +using osu.Game.Graphics.UserInterface; using osu.Game.Scoring; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; @@ -23,6 +25,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate [Resolved] private BeatmapManager beatmapManager { get; set; } + private readonly Container content; + private readonly LoadingLayer loadingLayer; private OsuScreenStack stack; public PlayerInstance(int userId, SpectatorCatchUpSlaveClock gameplayClock) @@ -32,23 +36,29 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate RelativeSizeAxes = Axes.Both; Masking = true; + + InternalChildren = new Drawable[] + { + content = new DrawSizePreservingFillContainer { RelativeSizeAxes = Axes.Both }, + loadingLayer = new LoadingLayer(true) { State = { Value = Visibility.Visible } } + }; } - public void LoadPlayer(Score score) + public void LoadScore(Score score) { + if (Score != null) + throw new InvalidOperationException($"Cannot load a new score on a {nameof(PlayerInstance)} with an existing score."); + Score = score; - InternalChild = new GameplayIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) + content.Child = new GameplayIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) { RelativeSizeAxes = Axes.Both, - Child = new DrawSizePreservingFillContainer - { - RelativeSizeAxes = Axes.Both, - Child = stack = new OsuScreenStack() - } + Child = stack = new OsuScreenStack() }; stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => new MultiplayerSpectatorPlayer(Score, GameplayClock))); + loadingLayer.Hide(); } // Player interferes with global input, so disable input for now. From f98ffbb1b365294b424ee28938072ba59b9cf1e2 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 20:15:42 +0900 Subject: [PATCH 35/88] Remove ClockToProcess, always process underlying clock --- osu.Game/Screens/Play/GameplayClock.cs | 18 +++++++++--------- .../Screens/Play/GameplayClockContainer.cs | 4 +--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/osu.Game/Screens/Play/GameplayClock.cs b/osu.Game/Screens/Play/GameplayClock.cs index db4b5d300b..54aa395f5f 100644 --- a/osu.Game/Screens/Play/GameplayClock.cs +++ b/osu.Game/Screens/Play/GameplayClock.cs @@ -19,7 +19,7 @@ namespace osu.Game.Screens.Play /// public class GameplayClock : IFrameBasedClock { - private readonly IFrameBasedClock underlyingClock; + internal readonly IFrameBasedClock UnderlyingClock; public readonly BindableBool IsPaused = new BindableBool(); @@ -30,12 +30,12 @@ namespace osu.Game.Screens.Play public GameplayClock(IFrameBasedClock underlyingClock) { - this.underlyingClock = underlyingClock; + UnderlyingClock = underlyingClock; } - public double CurrentTime => underlyingClock.CurrentTime; + public double CurrentTime => UnderlyingClock.CurrentTime; - public double Rate => underlyingClock.Rate; + public double Rate => UnderlyingClock.Rate; /// /// The rate of gameplay when playback is at 100%. @@ -59,19 +59,19 @@ namespace osu.Game.Screens.Play } } - public bool IsRunning => underlyingClock.IsRunning; + public bool IsRunning => UnderlyingClock.IsRunning; public void ProcessFrame() { // intentionally not updating the underlying clock (handled externally). } - public double ElapsedFrameTime => underlyingClock.ElapsedFrameTime; + public double ElapsedFrameTime => UnderlyingClock.ElapsedFrameTime; - public double FramesPerSecond => underlyingClock.FramesPerSecond; + public double FramesPerSecond => UnderlyingClock.FramesPerSecond; - public FrameTimeInfo TimeInfo => underlyingClock.TimeInfo; + public FrameTimeInfo TimeInfo => UnderlyingClock.TimeInfo; - public IClock Source => underlyingClock; + public IClock Source => UnderlyingClock; } } diff --git a/osu.Game/Screens/Play/GameplayClockContainer.cs b/osu.Game/Screens/Play/GameplayClockContainer.cs index 6d863f0094..b7dc55277f 100644 --- a/osu.Game/Screens/Play/GameplayClockContainer.cs +++ b/osu.Game/Screens/Play/GameplayClockContainer.cs @@ -80,15 +80,13 @@ namespace osu.Game.Screens.Play protected override void Update() { if (!IsPaused.Value) - ClockToProcess.ProcessFrame(); + GameplayClock.UnderlyingClock.ProcessFrame(); base.Update(); } protected abstract void OnIsPausedChanged(ValueChangedEvent isPaused); - protected virtual IFrameBasedClock ClockToProcess => AdjustableClock; - protected abstract GameplayClock CreateGameplayClock(IFrameBasedClock source); } } From 7d5d7088cd4dd1be6a0c1ec5f7a8113a2ab68aa4 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 20:51:07 +0900 Subject: [PATCH 36/88] Remove now unnecessary override --- .../Spectate/MultiplayerSpectatorPlayer.cs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index 19cc9f18ad..d4ff65cd01 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -2,7 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; -using osu.Framework.Bindables; using osu.Framework.Timing; using osu.Game.Beatmaps; using osu.Game.Scoring; @@ -33,21 +32,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public class SubGameplayClockContainer : GameplayClockContainer { - public new DecoupleableInterpolatingFramedClock AdjustableClock => base.AdjustableClock; - public SubGameplayClockContainer(IClock sourceClock) : base(sourceClock) { } - protected override void OnIsPausedChanged(ValueChangedEvent isPaused) - { - if (isPaused.NewValue) - AdjustableClock.Stop(); - else - AdjustableClock.Start(); - } - protected override GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source); } } From 4c5d4752b13807081be44f17a736822f6f57e3dc Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 22:47:52 +0900 Subject: [PATCH 37/88] Rename classes to reduce redundant naming --- ...rTest.cs => TestCaseCatchUpSyncManager.cs} | 48 +++++++++---------- .../Spectate/MultiplayerSpectator.cs | 6 +-- .../Spectate/MultiplayerSpectatorPlayer.cs | 4 +- .../Multiplayer/Spectate/PlayerInstance.cs | 4 +- ...chUpSlaveClock.cs => CatchUpSlaveClock.cs} | 6 +-- ...UpSyncManager.cs => CatchUpSyncManager.cs} | 12 ++--- ...ISpectatorSlaveClock.cs => ISlaveClock.cs} | 4 +- ...pectatorSyncManager.cs => ISyncManager.cs} | 16 +++---- 8 files changed, 50 insertions(+), 50 deletions(-) rename osu.Game.Tests/OnlinePlay/{MultiplayerCatchUpSyncManagerTest.cs => TestCaseCatchUpSyncManager.cs} (73%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/{SpectatorCatchUpSlaveClock.cs => CatchUpSlaveClock.cs} (90%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/{SpectatorCatchUpSyncManager.cs => CatchUpSyncManager.cs} (90%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/{ISpectatorSlaveClock.cs => ISlaveClock.cs} (83%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/{ISpectatorSyncManager.cs => ISyncManager.cs} (50%) diff --git a/osu.Game.Tests/OnlinePlay/MultiplayerCatchUpSyncManagerTest.cs b/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs similarity index 73% rename from osu.Game.Tests/OnlinePlay/MultiplayerCatchUpSyncManagerTest.cs rename to osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs index 6aa1219d53..a8c0a763e9 100644 --- a/osu.Game.Tests/OnlinePlay/MultiplayerCatchUpSyncManagerTest.cs +++ b/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs @@ -12,20 +12,20 @@ using osu.Game.Tests.Visual; namespace osu.Game.Tests.OnlinePlay { [HeadlessTest] - public class MultiplayerCatchUpSyncManagerTest : OsuTestScene + public class TestCaseCatchUpSyncManager : OsuTestScene { private TestManualClock master; - private SpectatorCatchUpSyncManager syncManager; + private CatchUpSyncManager syncManager; - private TestSpectatorSlaveClock slave1; - private TestSpectatorSlaveClock slave2; + private TestSlaveClock slave1; + private TestSlaveClock slave2; [SetUp] public void Setup() { - syncManager = new SpectatorCatchUpSyncManager(master = new TestManualClock()); - syncManager.AddSlave(slave1 = new TestSpectatorSlaveClock(1)); - syncManager.AddSlave(slave2 = new TestSpectatorSlaveClock(2)); + syncManager = new CatchUpSyncManager(master = new TestManualClock()); + syncManager.AddSlave(slave1 = new TestSlaveClock(1)); + syncManager.AddSlave(slave2 = new TestSlaveClock(2)); Schedule(() => Child = syncManager); } @@ -47,7 +47,7 @@ namespace osu.Game.Tests.OnlinePlay [Test] public void TestMasterClockDoesNotStartWhenNoneReadyForMaximumDelayTime() { - AddWaitStep($"wait {SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + AddWaitStep($"wait {CatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(CatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); assertMasterState(false); } @@ -55,7 +55,7 @@ namespace osu.Game.Tests.OnlinePlay public void TestMasterClockStartsWhenAnyReadyForMaximumDelayTime() { setWaiting(() => slave1, false); - AddWaitStep($"wait {SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(SpectatorCatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); + AddWaitStep($"wait {CatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(CatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); assertMasterState(true); } @@ -64,7 +64,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(SpectatorCatchUpSyncManager.SYNC_TARGET + 1); + setMasterTime(CatchUpSyncManager.SYNC_TARGET + 1); assertCatchingUp(() => slave1, false); } @@ -73,7 +73,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(SpectatorCatchUpSyncManager.MAX_SYNC_OFFSET + 1); + setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 1); assertCatchingUp(() => slave1, true); assertCatchingUp(() => slave2, true); } @@ -83,8 +83,8 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(SpectatorCatchUpSyncManager.MAX_SYNC_OFFSET + 1); - setSlaveTime(() => slave1, SpectatorCatchUpSyncManager.SYNC_TARGET + 1); + setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 1); + setSlaveTime(() => slave1, CatchUpSyncManager.SYNC_TARGET + 1); assertCatchingUp(() => slave1, true); } @@ -93,8 +93,8 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setMasterTime(SpectatorCatchUpSyncManager.MAX_SYNC_OFFSET + 2); - setSlaveTime(() => slave1, SpectatorCatchUpSyncManager.SYNC_TARGET); + setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 2); + setSlaveTime(() => slave1, CatchUpSyncManager.SYNC_TARGET); assertCatchingUp(() => slave1, false); assertCatchingUp(() => slave2, true); } @@ -104,7 +104,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setSlaveTime(() => slave1, -SpectatorCatchUpSyncManager.SYNC_TARGET); + setSlaveTime(() => slave1, -CatchUpSyncManager.SYNC_TARGET); assertCatchingUp(() => slave1, false); assertSlaveState(() => slave1, true); } @@ -114,7 +114,7 @@ namespace osu.Game.Tests.OnlinePlay { setAllWaiting(false); - setSlaveTime(() => slave1, -SpectatorCatchUpSyncManager.SYNC_TARGET - 1); + setSlaveTime(() => slave1, -CatchUpSyncManager.SYNC_TARGET - 1); // This is a silent catchup, where IsCatchingUp = false but IsRunning = false also. assertCatchingUp(() => slave1, false); @@ -135,7 +135,7 @@ namespace osu.Game.Tests.OnlinePlay assertSlaveState(() => slave1, false); } - private void setWaiting(Func slave, bool waiting) + private void setWaiting(Func slave, bool waiting) => AddStep($"set slave {slave().Id} waiting = {waiting}", () => slave().WaitingOnFrames.Value = waiting); private void setAllWaiting(bool waiting) => AddStep($"set all slaves waiting = {waiting}", () => @@ -150,28 +150,28 @@ namespace osu.Game.Tests.OnlinePlay /// /// slave.Time = master.Time - offsetFromMaster /// - private void setSlaveTime(Func slave, double offsetFromMaster) + private void setSlaveTime(Func slave, double offsetFromMaster) => AddStep($"set slave {slave().Id} = master - {offsetFromMaster}", () => slave().Seek(master.CurrentTime - offsetFromMaster)); private void assertMasterState(bool running) => AddAssert($"master clock {(running ? "is" : "is not")} running", () => master.IsRunning == running); - private void assertCatchingUp(Func slave, bool catchingUp) => + private void assertCatchingUp(Func slave, bool catchingUp) => AddAssert($"slave {slave().Id} {(catchingUp ? "is" : "is not")} catching up", () => slave().IsCatchingUp == catchingUp); - private void assertSlaveState(Func slave, bool running) + private void assertSlaveState(Func slave, bool running) => AddAssert($"slave {slave().Id} {(running ? "is" : "is not")} running", () => slave().IsRunning == running); - private class TestSpectatorSlaveClock : TestManualClock, ISpectatorSlaveClock + private class TestSlaveClock : TestManualClock, ISlaveClock { public readonly Bindable WaitingOnFrames = new Bindable(true); - IBindable ISpectatorSlaveClock.WaitingOnFrames => WaitingOnFrames; + IBindable ISlaveClock.WaitingOnFrames => WaitingOnFrames; public bool IsCatchingUp { get; set; } public readonly int Id; - public TestSpectatorSlaveClock(int id) + public TestSlaveClock(int id) { Id = id; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index e1af95b870..23cc787e20 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -25,7 +25,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly PlayerInstance[] instances; private MasterGameplayClockContainer masterClockContainer; - private ISpectatorSyncManager syncManager; + private ISyncManager syncManager; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; @@ -66,12 +66,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate InternalChildren = new[] { - (Drawable)(syncManager = new SpectatorCatchUpSyncManager(masterClockContainer)), + (Drawable)(syncManager = new CatchUpSyncManager(masterClockContainer)), masterClockContainer }; for (int i = 0; i < UserIds.Length; i++) - grid.Add(instances[i] = new PlayerInstance(UserIds[i], new SpectatorCatchUpSlaveClock(masterClockContainer.GameplayClock))); + grid.Add(instances[i] = new PlayerInstance(UserIds[i], new CatchUpSlaveClock(masterClockContainer.GameplayClock))); // Todo: This is not quite correct - it should be per-user to adjust for other mod combinations. var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index d4ff65cd01..55f483586c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -12,9 +12,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiplayerSpectatorPlayer : SpectatorPlayer { - private readonly ISpectatorSlaveClock gameplayClock; + private readonly ISlaveClock gameplayClock; - public MultiplayerSpectatorPlayer(Score score, ISpectatorSlaveClock gameplayClock) + public MultiplayerSpectatorPlayer(Score score, ISlaveClock gameplayClock) : base(score) { this.gameplayClock = gameplayClock; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 31d2d458df..407732ee2e 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -18,7 +18,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public bool PlayerLoaded => stack?.CurrentScreen is Player; public readonly int UserId; - public readonly SpectatorCatchUpSlaveClock GameplayClock; + public readonly CatchUpSlaveClock GameplayClock; public Score Score { get; private set; } @@ -29,7 +29,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly LoadingLayer loadingLayer; private OsuScreenStack stack; - public PlayerInstance(int userId, SpectatorCatchUpSlaveClock gameplayClock) + public PlayerInstance(int userId, CatchUpSlaveClock gameplayClock) { UserId = userId; GameplayClock = gameplayClock; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs similarity index 90% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs index 4b529021de..f128ea619b 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs @@ -8,9 +8,9 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// A which catches up using rate adjustment. + /// A which catches up using rate adjustment. /// - public class SpectatorCatchUpSlaveClock : ISpectatorSlaveClock + public class CatchUpSlaveClock : ISlaveClock { /// /// The catch up rate. @@ -19,7 +19,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync private readonly IFrameBasedClock masterClock; - public SpectatorCatchUpSlaveClock(IFrameBasedClock masterClock) + public CatchUpSlaveClock(IFrameBasedClock masterClock) { this.masterClock = masterClock; } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs similarity index 90% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSyncManager.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs index 46e86177ca..68bfef6500 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/SpectatorCatchUpSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs @@ -10,9 +10,9 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// A which synchronises de-synced slave clocks through catchup. + /// A which synchronises de-synced slave clocks through catchup. /// - public class SpectatorCatchUpSyncManager : Component, ISpectatorSyncManager + public class CatchUpSyncManager : Component, ISyncManager { /// /// The offset from the master clock to which slaves should be synchronised to. @@ -37,19 +37,19 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync /// /// The slave clocks. /// - private readonly List slaves = new List(); + private readonly List slaves = new List(); private bool hasStarted; private double? firstStartAttemptTime; - public SpectatorCatchUpSyncManager(IAdjustableClock master) + public CatchUpSyncManager(IAdjustableClock master) { Master = master; } - public void AddSlave(ISpectatorSlaveClock clock) => slaves.Add(clock); + public void AddSlave(ISlaveClock clock) => slaves.Add(clock); - public void RemoveSlave(ISpectatorSlaveClock clock) => slaves.Remove(clock); + public void RemoveSlave(ISlaveClock clock) => slaves.Remove(clock); protected override void Update() { diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs similarity index 83% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSlaveClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs index d8733d4322..f45cb1fde6 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs @@ -7,9 +7,9 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// A clock which is used by s and managed by an . + /// A clock which is used by s and managed by an . /// - public interface ISpectatorSlaveClock : IFrameBasedClock, IAdjustableClock + public interface ISlaveClock : IFrameBasedClock, IAdjustableClock { /// /// Whether this clock is waiting on frames to continue playback. diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs similarity index 50% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs index 5edbb3bc2c..d63ac7e1ca 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs @@ -6,9 +6,9 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// Manages the synchronisation between one or more s in relation to a master clock. + /// Manages the synchronisation between one or more s in relation to a master clock. /// - public interface ISpectatorSyncManager + public interface ISyncManager { /// /// The master clock which slaves should synchronise to. @@ -16,15 +16,15 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync IAdjustableClock Master { get; } /// - /// Adds an to manage. + /// Adds an to manage. /// - /// The to add. - void AddSlave(ISpectatorSlaveClock clock); + /// The to add. + void AddSlave(ISlaveClock clock); /// - /// Removes an , stopping it from being managed by this . + /// Removes an , stopping it from being managed by this . /// - /// The to remove. - void RemoveSlave(ISpectatorSlaveClock clock); + /// The to remove. + void RemoveSlave(ISlaveClock clock); } } From 72ebcb157f585869b5b41593d0d262561ffa7369 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 22:57:27 +0900 Subject: [PATCH 38/88] Dispose track on dispose --- .../Spectate/GameplayIsolationContainer.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs index 3ccb67d342..7b6a544084 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using osu.Framework.Allocation; +using osu.Framework.Audio.Track; using osu.Framework.Bindables; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; @@ -22,13 +23,15 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate [Cached] private readonly Bindable> mods = new Bindable>(); + private readonly Track track; + public GameplayIsolationContainer(WorkingBeatmap beatmap, RulesetInfo ruleset, IReadOnlyList mods) { this.beatmap.Value = beatmap; this.ruleset.Value = ruleset; this.mods.Value = mods; - beatmap.LoadTrack(); + track = beatmap.LoadTrack(); } protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) @@ -39,5 +42,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate dependencies.CacheAs(mods.BeginLease(false)); return dependencies; } + + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + track?.Dispose(); + } } } From 724fe3d37847ee70044e90c21c219ff79da03cc4 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 22:57:34 +0900 Subject: [PATCH 39/88] Remove unnecessary method --- .../OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs index b2d114d9cc..830378f129 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs @@ -75,19 +75,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate cellContainer.Add(cell); } - public void Remove(Drawable content) - { - var cell = cellContainer.FirstOrDefault(c => c.Content == content); - if (cell == null) - return; - - if (cell.IsMaximised) - toggleMaximisationState(cell); - - cellContainer.Remove(cell); - facadeContainer.Remove(facadeContainer[cell.FacadeIndex]); - } - /// /// The content added to this grid. /// From d5b26b0ab506a62ececcf94781df307023ca0362 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Apr 2021 23:01:34 +0900 Subject: [PATCH 40/88] Fix incorrect test spectator client implementation --- .../Visual/Multiplayer/TestSceneMultiplayerSpectator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index 4026258830..e395ebd29d 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -321,7 +321,7 @@ namespace osu.Game.Tests.Visual.Multiplayer public override void WatchUser(int userId) { - if (userSentStateDictionary.TryGetValue(userId, out var sent) && sent) + if (!PlayingUsers.Contains(userId) && userSentStateDictionary.TryGetValue(userId, out var sent) && sent) { // usually the server would do this. sendState(userId, userBeatmapDictionary[userId]); From f32d00c0d99ecccdbf1452c210f0a75769932298 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 21 Apr 2021 17:13:01 +0900 Subject: [PATCH 41/88] Fix post-merge errors --- .../OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs | 2 +- osu.Game/Screens/Play/GameplayClockContainer.cs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 23cc787e20..a5bd449e7e 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -86,7 +86,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate base.LoadComplete(); masterClockContainer.Stop(); - masterClockContainer.Restart(); + masterClockContainer.Reset(); } protected override void OnUserStateChanged(int userId, SpectatorState spectatorState) diff --git a/osu.Game/Screens/Play/GameplayClockContainer.cs b/osu.Game/Screens/Play/GameplayClockContainer.cs index dac3dad5d2..4109fd32bc 100644 --- a/osu.Game/Screens/Play/GameplayClockContainer.cs +++ b/osu.Game/Screens/Play/GameplayClockContainer.cs @@ -154,11 +154,7 @@ namespace osu.Game.Screens.Play return true; } - void IAdjustableClock.Reset() - { - Restart(); - Stop(); - } + void IAdjustableClock.Reset() => Reset(); public void ResetSpeedAdjustments() { From 2bea6256137a82375ac2e0f05a39f95b4f286503 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 21 Apr 2021 23:21:03 +0900 Subject: [PATCH 42/88] Fix initial playback states not being correct --- .../TestSceneMultiplayerSpectator.cs | 11 ++----- .../Spectate/MultiplayerSpectator.cs | 14 +++------ .../Spectate/MultiplayerSpectatorPlayer.cs | 31 ++++++++++++++++--- .../Spectate/Sync/CatchUpSlaveClock.cs | 2 +- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index e395ebd29d..9ec7bfb60b 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -256,17 +256,10 @@ namespace osu.Game.Tests.Visual.Multiplayer } private void checkPaused(int userId, bool state) => - AddUntilStep($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().IsPaused.Value == state); + AddUntilStep($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().GameplayClock.IsRunning != state); private void checkPausedInstant(int userId, bool state) => - AddAssert($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().IsPaused.Value == state); - - /// - /// Returns time(user1) - time(user2). - /// - private double getGameplayOffset(int user1, int user2) => getGameplayTime(user1) - getGameplayTime(user2); - - private double getGameplayTime(int userId) => getPlayer(userId).ChildrenOfType().Single().GameplayClock.CurrentTime; + AddAssert($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().GameplayClock.IsRunning != state); private Player getPlayer(int userId) => getInstance(userId).ChildrenOfType().Single(); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index a5bd449e7e..2db470da67 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -39,10 +39,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private void load() { Container leaderboardContainer; + masterClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0); - masterClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0) + InternalChildren = new[] { - Child = new GridContainer + (Drawable)(syncManager = new CatchUpSyncManager(masterClockContainer)), + masterClockContainer.WithChild(new GridContainer { RelativeSizeAxes = Axes.Both, ColumnDimensions = new[] @@ -61,13 +63,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate grid = new PlayerGrid { RelativeSizeAxes = Axes.Both } } } - } - }; - - InternalChildren = new[] - { - (Drawable)(syncManager = new CatchUpSyncManager(masterClockContainer)), - masterClockContainer + }) }; for (int i = 0; i < UserIds.Length; i++) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index 55f483586c..5af0d19a4d 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; +using osu.Framework.Bindables; using osu.Framework.Timing; using osu.Game.Beatmaps; using osu.Game.Scoring; @@ -12,22 +13,31 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiplayerSpectatorPlayer : SpectatorPlayer { - private readonly ISlaveClock gameplayClock; + private readonly Bindable waitingOnFrames = new Bindable(true); + private readonly Score score; + private readonly ISlaveClock slaveClock; - public MultiplayerSpectatorPlayer(Score score, ISlaveClock gameplayClock) + public MultiplayerSpectatorPlayer(Score score, ISlaveClock slaveClock) : base(score) { - this.gameplayClock = gameplayClock; + this.score = score; + this.slaveClock = slaveClock; } [BackgroundDependencyLoader] private void load() { - gameplayClock.WaitingOnFrames.BindTo(DrawableRuleset.FrameStableClock.WaitingOnFrames); + slaveClock.WaitingOnFrames.BindTo(waitingOnFrames); + } + + protected override void UpdateAfterChildren() + { + base.UpdateAfterChildren(); + waitingOnFrames.Value = DrawableRuleset.FrameStableClock.WaitingOnFrames.Value || score.Replay.Frames.Count == 0; } protected override GameplayClockContainer CreateGameplayClockContainer(WorkingBeatmap beatmap, double gameplayStart) - => new SubGameplayClockContainer(gameplayClock); + => new SubGameplayClockContainer(slaveClock); } public class SubGameplayClockContainer : GameplayClockContainer @@ -37,6 +47,17 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { } + protected override void Update() + { + // The slave clock's running state is controlled by the sync manager, but the local pausing state needs to be updated to stop gameplay. + if (SourceClock.IsRunning) + Start(); + else + Stop(); + + base.Update(); + } + protected override GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source); } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs index f128ea619b..cefe5ff04f 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs @@ -74,7 +74,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync public FrameTimeInfo TimeInfo => new FrameTimeInfo { Elapsed = ElapsedFrameTime, Current = CurrentTime }; - public IBindable WaitingOnFrames { get; } = new Bindable(); + public IBindable WaitingOnFrames { get; } = new Bindable(true); public bool IsCatchingUp { get; set; } } From 1ca2152e6191e0da98abdf034f1d50c20734fe7b Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 21 Apr 2021 23:22:36 +0900 Subject: [PATCH 43/88] Privatise + rename to SlaveGameplayClockContainer --- .../Spectate/MultiplayerSpectatorPlayer.cs | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs index 5af0d19a4d..69b38c61f7 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs @@ -37,27 +37,27 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate } protected override GameplayClockContainer CreateGameplayClockContainer(WorkingBeatmap beatmap, double gameplayStart) - => new SubGameplayClockContainer(slaveClock); - } + => new SlaveGameplayClockContainer(slaveClock); - public class SubGameplayClockContainer : GameplayClockContainer - { - public SubGameplayClockContainer(IClock sourceClock) - : base(sourceClock) + private class SlaveGameplayClockContainer : GameplayClockContainer { + public SlaveGameplayClockContainer(IClock sourceClock) + : base(sourceClock) + { + } + + protected override void Update() + { + // The slave clock's running state is controlled by the sync manager, but the local pausing state needs to be updated to stop gameplay. + if (SourceClock.IsRunning) + Start(); + else + Stop(); + + base.Update(); + } + + protected override GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source); } - - protected override void Update() - { - // The slave clock's running state is controlled by the sync manager, but the local pausing state needs to be updated to stop gameplay. - if (SourceClock.IsRunning) - Start(); - else - Stop(); - - base.Update(); - } - - protected override GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source); } } From 6588859c32296e9538f819a49328382f314b62bc Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 22:29:18 +0900 Subject: [PATCH 44/88] Remove loggings --- .../Multiplayer/Spectate/Sync/CatchUpSyncManager.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs index 68bfef6500..1d3fcd824c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using osu.Framework.Graphics; -using osu.Framework.Logging; using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync @@ -84,16 +83,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync int readyCount = slaves.Count(s => !s.WaitingOnFrames.Value); if (readyCount == slaves.Count) - { - Logger.Log("Gameplay started (all ready)."); return hasStarted = true; - } if (readyCount > 0 && (Time.Current - firstStartAttemptTime) > MAXIMUM_START_DELAY) - { - Logger.Log($"Gameplay started (maximum delay exceeded, {readyCount}/{slaves.Count} ready)."); return hasStarted = true; - } return false; } @@ -124,19 +117,13 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { // Stop the slave from catching up if it's within the sync target. if (timeDelta <= SYNC_TARGET) - { slave.IsCatchingUp = false; - Logger.Log($"Slave {i} catchup finished (delta = {timeDelta})"); - } } else { // Make the slave start catching up if it's exceeded the maximum allowable sync offset. if (timeDelta > MAX_SYNC_OFFSET) - { slave.IsCatchingUp = true; - Logger.Log($"Slave {i} catchup started (too far behind, delta = {timeDelta})"); - } } } } From 64579d50ac160215d5a8f60dc5f5fa8526b27fbc Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 22:59:47 +0900 Subject: [PATCH 45/88] Use only single PlayerInstance for hit sample playback --- .../TestSceneMultiplayerSpectator.cs | 46 ++++++++++++-- .../Spectate/MultiplayerSpectator.cs | 20 +++++++ .../Multiplayer/Spectate/PlayerInstance.cs | 60 +++++++++++++++++-- 3 files changed, 117 insertions(+), 9 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index 9ec7bfb60b..cd89d3bcc1 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -198,10 +198,40 @@ namespace osu.Game.Tests.Visual.Multiplayer checkPausedInstant(56, false); // Player 2 should catch up to player 1 after unpausing. - AddUntilStep("player 2 not catching up", () => !getInstance(56).GameplayClock.IsCatchingUp); + waitForCatchup(56); AddWaitStep("wait a bit", 10); } + [Test] + public void TestMostInSyncUserIsAudioSource() + { + start(new[] { 55, 56 }); + loadSpectateScreen(); + + assertVolume(55, 0); + assertVolume(56, 0); + + sendFrames(55, 10); + sendFrames(56, 20); + assertVolume(55, 1); + assertVolume(56, 0); + + checkPaused(55, true); + assertVolume(55, 0); + assertVolume(56, 1); + + sendFrames(55, 100); + waitForCatchup(55); + checkPaused(56, true); + assertVolume(55, 1); + assertVolume(56, 0); + + sendFrames(56, 100); + waitForCatchup(56); + assertVolume(55, 1); + assertVolume(56, 0); + } + private void loadSpectateScreen(bool waitForPlayerLoad = true) { AddStep("load screen", () => @@ -255,11 +285,17 @@ namespace osu.Game.Tests.Visual.Multiplayer }); } - private void checkPaused(int userId, bool state) => - AddUntilStep($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().GameplayClock.IsRunning != state); + private void checkPaused(int userId, bool state) + => AddUntilStep($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().GameplayClock.IsRunning != state); - private void checkPausedInstant(int userId, bool state) => - AddAssert($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().GameplayClock.IsRunning != state); + private void checkPausedInstant(int userId, bool state) + => AddAssert($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().GameplayClock.IsRunning != state); + + private void assertVolume(int userId, double volume) + => AddAssert($"{userId} volume is {volume}", () => getInstance(userId).Volume.Value == volume); + + private void waitForCatchup(int userId) + => AddUntilStep($"{userId} not catching up", () => !getInstance(userId).GameplayClock.IsCatchingUp); private Player getPlayer(int userId) => getInstance(userId).ChildrenOfType().Single(); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs index 2db470da67..33dd57586c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs @@ -3,6 +3,7 @@ using System; using System.Linq; +using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -28,6 +29,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private ISyncManager syncManager; private PlayerGrid grid; private MultiplayerSpectatorLeaderboard leaderboard; + private PlayerInstance currentAudioSource; public MultiplayerSpectator(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) @@ -85,6 +87,24 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate masterClockContainer.Reset(); } + protected override void Update() + { + base.Update(); + + if (!isCandidateAudioSource(currentAudioSource?.GameplayClock)) + { + currentAudioSource = instances.Where(i => isCandidateAudioSource(i.GameplayClock)) + .OrderBy(i => Math.Abs(i.GameplayClock.CurrentTime - syncManager.Master.CurrentTime)) + .FirstOrDefault(); + + foreach (var instance in instances) + instance.Volume.Value = instance == currentAudioSource ? 1 : 0; + } + } + + private bool isCandidateAudioSource([CanBeNull] ISlaveClock clock) + => clock?.IsRunning == true && !clock.IsCatchingUp && !clock.WaitingOnFrames.Value; + protected override void OnUserStateChanged(int userId, SpectatorState spectatorState) { } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 407732ee2e..80d6727599 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -3,6 +3,8 @@ using System; using osu.Framework.Allocation; +using osu.Framework.Audio; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; @@ -13,7 +15,7 @@ using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class PlayerInstance : CompositeDrawable + public class PlayerInstance : CompositeDrawable, IAdjustableAudioComponent { public bool PlayerLoaded => stack?.CurrentScreen is Player; @@ -25,8 +27,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate [Resolved] private BeatmapManager beatmapManager { get; set; } - private readonly Container content; + private readonly Container gameplayContent; private readonly LoadingLayer loadingLayer; + private readonly AudioContainer audioContainer; private OsuScreenStack stack; public PlayerInstance(int userId, CatchUpSlaveClock gameplayClock) @@ -39,7 +42,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate InternalChildren = new Drawable[] { - content = new DrawSizePreservingFillContainer { RelativeSizeAxes = Axes.Both }, + audioContainer = new AudioContainer + { + RelativeSizeAxes = Axes.Both, + Child = gameplayContent = new DrawSizePreservingFillContainer { RelativeSizeAxes = Axes.Both }, + }, loadingLayer = new LoadingLayer(true) { State = { Value = Visibility.Visible } } }; } @@ -51,7 +58,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate Score = score; - content.Child = new GameplayIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) + gameplayContent.Child = new GameplayIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) { RelativeSizeAxes = Axes.Both, Child = stack = new OsuScreenStack() @@ -64,5 +71,50 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate // Player interferes with global input, so disable input for now. public override bool PropagatePositionalInputSubTree => false; public override bool PropagateNonPositionalInputSubTree => false; + + #region IAdjustableAudioComponent + + public IBindable AggregateVolume => audioContainer.AggregateVolume; + + public IBindable AggregateBalance => audioContainer.AggregateBalance; + + public IBindable AggregateFrequency => audioContainer.AggregateFrequency; + + public IBindable AggregateTempo => audioContainer.AggregateTempo; + + public void BindAdjustments(IAggregateAudioAdjustment component) + { + audioContainer.BindAdjustments(component); + } + + public void UnbindAdjustments(IAggregateAudioAdjustment component) + { + audioContainer.UnbindAdjustments(component); + } + + public void AddAdjustment(AdjustableProperty type, IBindable adjustBindable) + { + audioContainer.AddAdjustment(type, adjustBindable); + } + + public void RemoveAdjustment(AdjustableProperty type, IBindable adjustBindable) + { + audioContainer.RemoveAdjustment(type, adjustBindable); + } + + public void RemoveAllAdjustments(AdjustableProperty type) + { + audioContainer.RemoveAllAdjustments(type); + } + + public BindableNumber Volume => audioContainer.Volume; + + public BindableNumber Balance => audioContainer.Balance; + + public BindableNumber Frequency => audioContainer.Frequency; + + public BindableNumber Tempo => audioContainer.Tempo; + + #endregion } } From 22a3f3ad3e9f022fb85e1258aa0a2184a58fe671 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 23:04:54 +0900 Subject: [PATCH 46/88] Revert unnecessary BeatmapManager change --- osu.Game/Beatmaps/BeatmapManager.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index abb1f5a93c..5e975de77c 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -281,9 +281,8 @@ namespace osu.Game.Beatmaps /// /// The beatmap to lookup. /// The currently loaded . Allows for optimisation where elements are shared with the new beatmap. May be returned if beatmapInfo requested matches - /// Whether to bypass the cache and return a new instance. /// A instance correlating to the provided . - public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null, bool bypassCache = false) + public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null) { if (beatmapInfo?.ID > 0 && previous != null && previous.BeatmapInfo?.ID == beatmapInfo.ID) return previous; @@ -302,14 +301,9 @@ namespace osu.Game.Beatmaps lock (workingCache) { - BeatmapManagerWorkingBeatmap working; - - if (!bypassCache) - { - working = workingCache.FirstOrDefault(w => w.BeatmapInfo?.ID == beatmapInfo.ID); - if (working != null) - return working; - } + var working = workingCache.FirstOrDefault(w => w.BeatmapInfo?.ID == beatmapInfo.ID); + if (working != null) + return working; beatmapInfo.Metadata ??= beatmapInfo.BeatmapSet.Metadata; From fd0b030cf431ecb0ab3ea45bbcac68ee0545a207 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 23:37:33 +0900 Subject: [PATCH 47/88] Refactor gameplay screen creation --- .../Screens/OnlinePlay/Match/RoomSubScreen.cs | 13 +++++++----- .../Multiplayer/MultiplayerMatchSubScreen.cs | 21 ++++++++++++------- .../Playlists/PlaylistsRoomSubScreen.cs | 9 +++----- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs b/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs index 0d1ed5d88e..68bdd9160e 100644 --- a/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs @@ -17,7 +17,6 @@ using osu.Game.Online.Rooms; using osu.Game.Overlays; using osu.Game.Overlays.Mods; using osu.Game.Rulesets.Mods; -using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Match { @@ -148,14 +147,18 @@ namespace osu.Game.Screens.OnlinePlay.Match return base.OnExiting(next); } - protected void StartPlay(Func player) => PushTopLevelScreen(() => new PlayerLoader(player)); - - protected void PushTopLevelScreen(Func screen) + protected void StartPlay() { sampleStart?.Play(); - ParentScreen?.Push(screen()); + ParentScreen?.Push(CreateGameplayScreen()); } + /// + /// Creates the gameplay screen to be entered. + /// + /// The screen to enter. + protected abstract Screen CreateGameplayScreen(); + private void selectedItemChanged() { updateWorkingBeatmap(); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index 9a68ff908d..c5d7610b53 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -416,10 +416,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer UpdateMods(); if (client.LocalUser.State == MultiplayerUserState.Spectating - && (client.Room.State == MultiplayerRoomState.Playing || client.Room.State == MultiplayerRoomState.WaitingForLoad) - && ParentScreen.IsCurrentScreen()) + && (client.Room.State == MultiplayerRoomState.Playing || client.Room.State == MultiplayerRoomState.WaitingForLoad)) { - PushTopLevelScreen(() => new MultiplayerSpectator(client.CurrentMatchPlayingUserIds.ToArray())); + StartPlay(); // If the current user was host, they started the match and the in-progres operation needs to be stopped now. readyClickOperation?.Dispose(); @@ -429,16 +428,22 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer private void onLoadRequested() { - Debug.Assert(client.Room != null); - - int[] userIds = client.CurrentMatchPlayingUserIds.ToArray(); - - StartPlay(() => new MultiplayerPlayer(SelectedItem.Value, userIds)); + StartPlay(); readyClickOperation?.Dispose(); readyClickOperation = null; } + protected override Screen CreateGameplayScreen() + { + Debug.Assert(client.LocalUser != null); + + if (client.LocalUser.State == MultiplayerUserState.Spectating) + return new MultiSpectatorScreen(client.CurrentMatchPlayingUserIds.ToArray()); + + return new MultiplayerPlayer(SelectedItem.Value, client.CurrentMatchPlayingUserIds.ToArray()); + } + protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); diff --git a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsRoomSubScreen.cs b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsRoomSubScreen.cs index 6542d01e64..11bc55823f 100644 --- a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsRoomSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsRoomSubScreen.cs @@ -218,10 +218,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists }, new Drawable[] { - new Footer - { - OnStart = onStart, - } + new Footer { OnStart = StartPlay } } }, RowDimensions = new[] @@ -274,9 +271,9 @@ namespace osu.Game.Screens.OnlinePlay.Playlists }, true); } - private void onStart() => StartPlay(() => new PlaylistsPlayer(SelectedItem.Value) + protected override Screen CreateGameplayScreen() => new PlaylistsPlayer(SelectedItem.Value) { Exited = () => leaderboard.RefreshScores() - }); + }; } } From 4aceb75eb2f8c5c40ff796ecc8d8fd22c3a6ddf1 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 23:37:45 +0900 Subject: [PATCH 48/88] Disable spectate button on closed rooms Doesn't have an effect normally - only for safety purposes in case we allow entering the match subscreen after a match has finished in the future. --- .../OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs index 465af037e2..04150902bc 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs @@ -81,7 +81,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match break; } - button.Enabled.Value = !operationInProgress.Value; + button.Enabled.Value = Client.Room?.State != MultiplayerRoomState.Closed && !operationInProgress.Value; } private class ButtonWithTrianglesExposed : TriangleButton From 8a0ba3a05557e3d1f20521e577bb61d7604c9a97 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 23:38:51 +0900 Subject: [PATCH 49/88] Merge GameplayIsolationContainer into PlayerInstance, remove track --- .../Spectate/GameplayIsolationContainer.cs | 52 ------------------- .../Multiplayer/Spectate/PlayerInstance.cs | 35 ++++++++++++- 2 files changed, 33 insertions(+), 54 deletions(-) delete mode 100644 osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs deleted file mode 100644 index 7b6a544084..0000000000 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/GameplayIsolationContainer.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -using System.Collections.Generic; -using osu.Framework.Allocation; -using osu.Framework.Audio.Track; -using osu.Framework.Bindables; -using osu.Framework.Graphics.Containers; -using osu.Game.Beatmaps; -using osu.Game.Rulesets; -using osu.Game.Rulesets.Mods; - -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate -{ - public class GameplayIsolationContainer : Container - { - [Cached] - private readonly Bindable ruleset = new Bindable(); - - [Cached] - private readonly Bindable beatmap = new Bindable(); - - [Cached] - private readonly Bindable> mods = new Bindable>(); - - private readonly Track track; - - public GameplayIsolationContainer(WorkingBeatmap beatmap, RulesetInfo ruleset, IReadOnlyList mods) - { - this.beatmap.Value = beatmap; - this.ruleset.Value = ruleset; - this.mods.Value = mods; - - track = beatmap.LoadTrack(); - } - - protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) - { - var dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); - dependencies.CacheAs(ruleset.BeginLease(false)); - dependencies.CacheAs(beatmap.BeginLease(false)); - dependencies.CacheAs(mods.BeginLease(false)); - return dependencies; - } - - protected override void Dispose(bool isDisposing) - { - base.Dispose(isDisposing); - track?.Dispose(); - } - } -} diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs index 80d6727599..8007c9e068 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Collections.Generic; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Bindables; @@ -9,6 +10,8 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; using osu.Game.Graphics.UserInterface; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Mods; using osu.Game.Scoring; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; @@ -58,13 +61,13 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate Score = score; - gameplayContent.Child = new GameplayIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) + gameplayContent.Child = new PlayerIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods) { RelativeSizeAxes = Axes.Both, Child = stack = new OsuScreenStack() }; - stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => new MultiplayerSpectatorPlayer(Score, GameplayClock))); + stack.Push(new MultiSpectatorPlayerLoader(Score, () => new MultiSpectatorPlayer(Score, GameplayClock))); loadingLayer.Hide(); } @@ -116,5 +119,33 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public BindableNumber Tempo => audioContainer.Tempo; #endregion + + private class PlayerIsolationContainer : Container + { + [Cached] + private readonly Bindable ruleset = new Bindable(); + + [Cached] + private readonly Bindable beatmap = new Bindable(); + + [Cached] + private readonly Bindable> mods = new Bindable>(); + + public PlayerIsolationContainer(WorkingBeatmap beatmap, RulesetInfo ruleset, IReadOnlyList mods) + { + this.beatmap.Value = beatmap; + this.ruleset.Value = ruleset; + this.mods.Value = mods; + } + + protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) + { + var dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); + dependencies.CacheAs(ruleset.BeginLease(false)); + dependencies.CacheAs(beatmap.BeginLease(false)); + dependencies.CacheAs(mods.BeginLease(false)); + return dependencies; + } + } } } From ee259497519f71d360616b6f2dc92247bfdfe43b Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 23:39:02 +0900 Subject: [PATCH 50/88] Rename classes --- .../Multiplayer/TestSceneMultiplayerSpectator.cs | 12 ++++++------ .../TestSceneMultiplayerSpectatorLeaderboard.cs | 4 ++-- ...orLeaderboard.cs => MultiSpectatorLeaderboard.cs} | 4 ++-- ...yerSpectatorPlayer.cs => MultiSpectatorPlayer.cs} | 4 ++-- ...PlayerLoader.cs => MultiSpectatorPlayerLoader.cs} | 4 ++-- ...ltiplayerSpectator.cs => MultiSpectatorScreen.cs} | 8 ++++---- .../Multiplayer/Spectate/Sync/ISlaveClock.cs | 2 +- 7 files changed, 19 insertions(+), 19 deletions(-) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerSpectatorLeaderboard.cs => MultiSpectatorLeaderboard.cs} (92%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerSpectatorPlayer.cs => MultiSpectatorPlayer.cs} (93%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerSpectatorPlayerLoader.cs => MultiSpectatorPlayerLoader.cs} (75%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{MultiplayerSpectator.cs => MultiSpectatorScreen.cs} (93%) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index cd89d3bcc1..0b77a1a9e2 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -37,7 +37,7 @@ namespace osu.Game.Tests.Visual.Multiplayer [Resolved] private BeatmapManager beatmapManager { get; set; } - private MultiplayerSpectator spectator; + private MultiSpectatorScreen spectatorScreen; private readonly List playingUserIds = new List(); private readonly Dictionary nextFrame = new Dictionary(); @@ -91,11 +91,11 @@ namespace osu.Game.Tests.Visual.Multiplayer AddWaitStep("wait a bit", 10); AddStep("load player 55", () => streamingClient.StartPlay(55, importedBeatmapId)); - AddUntilStep("one player added", () => spectator.ChildrenOfType().Count() == 1); + AddUntilStep("one player added", () => spectatorScreen.ChildrenOfType().Count() == 1); AddWaitStep("wait a bit", 10); AddStep("load player 56", () => streamingClient.StartPlay(56, importedBeatmapId)); - AddUntilStep("two players added", () => spectator.ChildrenOfType().Count() == 2); + AddUntilStep("two players added", () => spectatorScreen.ChildrenOfType().Count() == 2); } [Test] @@ -239,10 +239,10 @@ namespace osu.Game.Tests.Visual.Multiplayer Beatmap.Value = beatmapManager.GetWorkingBeatmap(importedBeatmap); Ruleset.Value = importedBeatmap.Ruleset; - LoadScreen(spectator = new MultiplayerSpectator(playingUserIds.ToArray())); + LoadScreen(spectatorScreen = new MultiSpectatorScreen(playingUserIds.ToArray())); }); - AddUntilStep("wait for screen load", () => spectator.LoadState == LoadState.Loaded && (!waitForPlayerLoad || spectator.AllPlayersLoaded)); + AddUntilStep("wait for screen load", () => spectatorScreen.LoadState == LoadState.Loaded && (!waitForPlayerLoad || spectatorScreen.AllPlayersLoaded)); } private void start(int userId, int? beatmapId = null) => start(new[] { userId }, beatmapId); @@ -299,7 +299,7 @@ namespace osu.Game.Tests.Visual.Multiplayer private Player getPlayer(int userId) => getInstance(userId).ChildrenOfType().Single(); - private PlayerInstance getInstance(int userId) => spectator.ChildrenOfType().Single(p => p.UserId == userId); + private PlayerInstance getInstance(int userId) => spectatorScreen.ChildrenOfType().Single(p => p.UserId == userId); public class TestSpectatorStreamingClient : SpectatorStreamingClient { diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectatorLeaderboard.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectatorLeaderboard.cs index 3b2cfb1c7b..8368186219 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectatorLeaderboard.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectatorLeaderboard.cs @@ -54,7 +54,7 @@ namespace osu.Game.Tests.Visual.Multiplayer [SetUpSteps] public new void SetUpSteps() { - MultiplayerSpectatorLeaderboard leaderboard = null; + MultiSpectatorLeaderboard leaderboard = null; AddStep("reset", () => { @@ -78,7 +78,7 @@ namespace osu.Game.Tests.Visual.Multiplayer var scoreProcessor = new OsuScoreProcessor(); scoreProcessor.ApplyBeatmap(playable); - LoadComponentAsync(leaderboard = new MultiplayerSpectatorLeaderboard(scoreProcessor, clocks.Keys.ToArray()) { Expanded = { Value = true } }, Add); + LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, clocks.Keys.ToArray()) { Expanded = { Value = true } }, Add); }); AddUntilStep("wait for load", () => leaderboard.IsLoaded); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorLeaderboard.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorLeaderboard.cs similarity index 92% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorLeaderboard.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorLeaderboard.cs index 1b9e2bda2d..96c7702048 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorLeaderboard.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorLeaderboard.cs @@ -9,9 +9,9 @@ using osu.Game.Screens.Play.HUD; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class MultiplayerSpectatorLeaderboard : MultiplayerGameplayLeaderboard + public class MultiSpectatorLeaderboard : MultiplayerGameplayLeaderboard { - public MultiplayerSpectatorLeaderboard(ScoreProcessor scoreProcessor, int[] userIds) + public MultiSpectatorLeaderboard(ScoreProcessor scoreProcessor, int[] userIds) : base(scoreProcessor, userIds) { } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs similarity index 93% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs index 69b38c61f7..0101c00041 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs @@ -11,13 +11,13 @@ using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class MultiplayerSpectatorPlayer : SpectatorPlayer + public class MultiSpectatorPlayer : SpectatorPlayer { private readonly Bindable waitingOnFrames = new Bindable(true); private readonly Score score; private readonly ISlaveClock slaveClock; - public MultiplayerSpectatorPlayer(Score score, ISlaveClock slaveClock) + public MultiSpectatorPlayer(Score score, ISlaveClock slaveClock) : base(score) { this.score = score; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayerLoader.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayerLoader.cs similarity index 75% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayerLoader.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayerLoader.cs index c8f16b5e90..fce44296f3 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectatorPlayerLoader.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayerLoader.cs @@ -8,9 +8,9 @@ using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class MultiplayerSpectatorPlayerLoader : SpectatorPlayerLoader + public class MultiSpectatorPlayerLoader : SpectatorPlayerLoader { - public MultiplayerSpectatorPlayerLoader(Score score, Func createPlayer) + public MultiSpectatorPlayerLoader(Score score, Func createPlayer) : base(score, createPlayer) { } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs similarity index 93% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index 33dd57586c..52b7898028 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiplayerSpectator.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -14,7 +14,7 @@ using osu.Game.Screens.Spectate; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class MultiplayerSpectator : SpectatorScreen + public class MultiSpectatorScreen : SpectatorScreen { // Isolates beatmap/ruleset to this screen. public override bool DisallowExternalBeatmapRulesetChanges => true; @@ -28,10 +28,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private MasterGameplayClockContainer masterClockContainer; private ISyncManager syncManager; private PlayerGrid grid; - private MultiplayerSpectatorLeaderboard leaderboard; + private MultiSpectatorLeaderboard leaderboard; private PlayerInstance currentAudioSource; - public MultiplayerSpectator(int[] userIds) + public MultiSpectatorScreen(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) { instances = new PlayerInstance[UserIds.Length]; @@ -76,7 +76,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor(); scoreProcessor.ApplyBeatmap(playableBeatmap); - LoadComponentAsync(leaderboard = new MultiplayerSpectatorLeaderboard(scoreProcessor, UserIds) { Expanded = { Value = true } }, leaderboardContainer.Add); + LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, UserIds) { Expanded = { Value = true } }, leaderboardContainer.Add); } protected override void LoadComplete() diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs index f45cb1fde6..08d69f9560 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs @@ -7,7 +7,7 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// A clock which is used by s and managed by an . + /// A clock which is used by s and managed by an . /// public interface ISlaveClock : IFrameBasedClock, IAdjustableClock { From 4f0857f946e96d5c0c9da4440587e09488ca083e Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 22 Apr 2021 23:52:22 +0900 Subject: [PATCH 51/88] Xmldocs and general refactorings --- .../TestSceneMultiplayerSpectator.cs | 2 +- .../Spectate/MultiSpectatorLeaderboard.cs | 2 +- .../Spectate/MultiSpectatorPlayer.cs | 17 +++++++++-- .../Spectate/MultiSpectatorPlayerLoader.cs | 6 +++- .../Spectate/MultiSpectatorScreen.cs | 18 +++++++++--- .../{PlayerInstance.cs => PlayerArea.cs} | 29 +++++++++++++++---- 6 files changed, 59 insertions(+), 15 deletions(-) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{PlayerInstance.cs => PlayerArea.cs} (83%) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs index 0b77a1a9e2..a82dea5640 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs @@ -299,7 +299,7 @@ namespace osu.Game.Tests.Visual.Multiplayer private Player getPlayer(int userId) => getInstance(userId).ChildrenOfType().Single(); - private PlayerInstance getInstance(int userId) => spectatorScreen.ChildrenOfType().Single(p => p.UserId == userId); + private PlayerArea getInstance(int userId) => spectatorScreen.ChildrenOfType().Single(p => p.UserId == userId); public class TestSpectatorStreamingClient : SpectatorStreamingClient { diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorLeaderboard.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorLeaderboard.cs index 96c7702048..ab3ead68b5 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorLeaderboard.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorLeaderboard.cs @@ -11,7 +11,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { public class MultiSpectatorLeaderboard : MultiplayerGameplayLeaderboard { - public MultiSpectatorLeaderboard(ScoreProcessor scoreProcessor, int[] userIds) + public MultiSpectatorLeaderboard([NotNull] ScoreProcessor scoreProcessor, int[] userIds) : base(scoreProcessor, userIds) { } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs index 0101c00041..4ed949893c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Timing; @@ -11,13 +12,21 @@ using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { + /// + /// A single spectated player within a . + /// public class MultiSpectatorPlayer : SpectatorPlayer { private readonly Bindable waitingOnFrames = new Bindable(true); private readonly Score score; private readonly ISlaveClock slaveClock; - public MultiSpectatorPlayer(Score score, ISlaveClock slaveClock) + /// + /// Creates a new . + /// + /// The score containing the player's replay. + /// The clock controlling the gameplay running state. + public MultiSpectatorPlayer([NotNull] Score score, [NotNull] ISlaveClock slaveClock) : base(score) { this.score = score; @@ -33,6 +42,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate protected override void UpdateAfterChildren() { base.UpdateAfterChildren(); + + // This is required because the frame stable clock is set to WaitingOnFrames = false for one frame. waitingOnFrames.Value = DrawableRuleset.FrameStableClock.WaitingOnFrames.Value || score.Replay.Frames.Count == 0; } @@ -41,14 +52,14 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private class SlaveGameplayClockContainer : GameplayClockContainer { - public SlaveGameplayClockContainer(IClock sourceClock) + public SlaveGameplayClockContainer([NotNull] IClock sourceClock) : base(sourceClock) { } protected override void Update() { - // The slave clock's running state is controlled by the sync manager, but the local pausing state needs to be updated to stop gameplay. + // The slave clock's running state is controlled externally, but the local pausing state needs to be updated to stop gameplay. if (SourceClock.IsRunning) Start(); else diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayerLoader.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayerLoader.cs index fce44296f3..5a1d28e9c4 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayerLoader.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayerLoader.cs @@ -2,15 +2,19 @@ // See the LICENCE file in the repository root for full licence text. using System; +using JetBrains.Annotations; using osu.Game.Scoring; using osu.Game.Screens.Menu; using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { + /// + /// Used to load a single in a . + /// public class MultiSpectatorPlayerLoader : SpectatorPlayerLoader { - public MultiSpectatorPlayerLoader(Score score, Func createPlayer) + public MultiSpectatorPlayerLoader([NotNull] Score score, [NotNull] Func createPlayer) : base(score, createPlayer) { } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index 52b7898028..609905a312 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -14,27 +14,37 @@ using osu.Game.Screens.Spectate; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { + /// + /// A that spectates multiple users in a match. + /// public class MultiSpectatorScreen : SpectatorScreen { // Isolates beatmap/ruleset to this screen. public override bool DisallowExternalBeatmapRulesetChanges => true; + /// + /// Whether all spectating players have finished loading. + /// public bool AllPlayersLoaded => instances.All(p => p?.PlayerLoaded == true); [Resolved] private SpectatorStreamingClient spectatorClient { get; set; } - private readonly PlayerInstance[] instances; + private readonly PlayerArea[] instances; private MasterGameplayClockContainer masterClockContainer; private ISyncManager syncManager; private PlayerGrid grid; private MultiSpectatorLeaderboard leaderboard; - private PlayerInstance currentAudioSource; + private PlayerArea currentAudioSource; + /// + /// Creates a new . + /// + /// The players to spectate. public MultiSpectatorScreen(int[] userIds) : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) { - instances = new PlayerInstance[UserIds.Length]; + instances = new PlayerArea[UserIds.Length]; } [BackgroundDependencyLoader] @@ -69,7 +79,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate }; for (int i = 0; i < UserIds.Length; i++) - grid.Add(instances[i] = new PlayerInstance(UserIds[i], new CatchUpSlaveClock(masterClockContainer.GameplayClock))); + grid.Add(instances[i] = new PlayerArea(UserIds[i], new CatchUpSlaveClock(masterClockContainer.GameplayClock))); // Todo: This is not quite correct - it should be per-user to adjust for other mod combinations. var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs similarity index 83% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs index 8007c9e068..494f182251 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Bindables; @@ -18,13 +19,31 @@ using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { - public class PlayerInstance : CompositeDrawable, IAdjustableAudioComponent + /// + /// Provides an area for and manages the hierarchy of a spectated player within a . + /// + public class PlayerArea : CompositeDrawable, IAdjustableAudioComponent { + /// + /// Whether a is loaded in the area. + /// public bool PlayerLoaded => stack?.CurrentScreen is Player; + /// + /// The user id this corresponds to. + /// public readonly int UserId; - public readonly CatchUpSlaveClock GameplayClock; + /// + /// The used to control the gameplay running state of a loaded . + /// + [NotNull] + public readonly ISlaveClock GameplayClock; + + /// + /// The currently-loaded score. + /// + [CanBeNull] public Score Score { get; private set; } [Resolved] @@ -35,7 +54,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly AudioContainer audioContainer; private OsuScreenStack stack; - public PlayerInstance(int userId, CatchUpSlaveClock gameplayClock) + public PlayerArea(int userId, [NotNull] ISlaveClock gameplayClock) { UserId = userId; GameplayClock = gameplayClock; @@ -54,10 +73,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate }; } - public void LoadScore(Score score) + public void LoadScore([NotNull] Score score) { if (Score != null) - throw new InvalidOperationException($"Cannot load a new score on a {nameof(PlayerInstance)} with an existing score."); + throw new InvalidOperationException($"Cannot load a new score on a {nameof(PlayerArea)} that has an existing score."); Score = score; From 90ecda91af351c248882469195f4a964dd2636bf Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 23 Apr 2021 00:06:54 +0900 Subject: [PATCH 52/88] Fix exception --- .../OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index c5d7610b53..aac03622e3 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -416,7 +416,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer UpdateMods(); if (client.LocalUser.State == MultiplayerUserState.Spectating - && (client.Room.State == MultiplayerRoomState.Playing || client.Room.State == MultiplayerRoomState.WaitingForLoad)) + && (client.Room.State == MultiplayerRoomState.Playing || client.Room.State == MultiplayerRoomState.WaitingForLoad) + && ParentScreen.IsCurrentScreen()) { StartPlay(); From b25340653df7e657884cfd077c53eea43613f25b Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 23 Apr 2021 00:49:14 +0900 Subject: [PATCH 53/88] Fix failing tests --- .../TestSceneMultiplayerSpectateButton.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs index e65e4a68a7..afbc9c32b3 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs @@ -120,9 +120,12 @@ namespace osu.Game.Tests.Visual.Multiplayer }; }); - [Test] - public void TestEnabledWhenRoomOpen() + [TestCase(MultiplayerRoomState.Open)] + [TestCase(MultiplayerRoomState.WaitingForLoad)] + [TestCase(MultiplayerRoomState.Playing)] + public void TestEnabledWhenRoomOpenOrInGameplay(MultiplayerRoomState roomState) { + AddStep($"change room to {roomState}", () => Client.ChangeRoomState(roomState)); assertSpectateButtonEnablement(true); } @@ -137,12 +140,10 @@ namespace osu.Game.Tests.Visual.Multiplayer AddAssert("user is idle", () => Client.Room?.Users[0].State == MultiplayerUserState.Idle); } - [TestCase(MultiplayerRoomState.WaitingForLoad)] - [TestCase(MultiplayerRoomState.Playing)] [TestCase(MultiplayerRoomState.Closed)] - public void TestDisabledDuringGameplayOrClosed(MultiplayerRoomState roomState) + public void TestDisabledWhenClosed(MultiplayerRoomState roomState) { - AddStep($"change user to {roomState}", () => Client.ChangeRoomState(roomState)); + AddStep($"change room to {roomState}", () => Client.ChangeRoomState(roomState)); assertSpectateButtonEnablement(false); } From 575ec7c528af97d00e7aa3e3fce940ae5e64350f Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 23 Apr 2021 19:09:54 +0900 Subject: [PATCH 54/88] Document + refactor max player limitation --- .../Multiplayer/Spectate/MultiSpectatorScreen.cs | 2 +- .../OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index 609905a312..eb59d090be 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -42,7 +42,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// /// The players to spectate. public MultiSpectatorScreen(int[] userIds) - : base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray()) + : base(userIds.Take(PlayerGrid.MAX_PLAYERS).ToArray()) { instances = new PlayerArea[UserIds.Length]; } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs index 830378f129..6638d47dca 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerGrid.cs @@ -15,6 +15,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// public partial class PlayerGrid : CompositeDrawable { + /// + /// A temporary limitation on the number of players, because only layouts up to 16 players are supported for a single screen. + /// Todo: Can be removed in the future with scrolling support + performance improvements. + /// + public const int MAX_PLAYERS = 16; + private const float player_spacing = 5; /// @@ -58,11 +64,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// Adds a new cell with content to this grid. /// /// The content the cell should contain. - /// If more than 16 cells are added. + /// If more than cells are added. public void Add(Drawable content) { - if (cellContainer.Count == 16) - throw new InvalidOperationException("Only 16 cells are supported."); + if (cellContainer.Count == MAX_PLAYERS) + throw new InvalidOperationException($"Only {MAX_PLAYERS} cells are supported."); int index = cellContainer.Count; From 63a948425513a3bba7c660850a5d562bf3efc3ec Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 23 Apr 2021 19:11:47 +0900 Subject: [PATCH 55/88] Expose WaitingOnFrames as mutable bindable --- osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs | 3 +-- .../OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs | 2 +- .../OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs b/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs index a8c0a763e9..2c10be9c90 100644 --- a/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs +++ b/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs @@ -164,8 +164,7 @@ namespace osu.Game.Tests.OnlinePlay private class TestSlaveClock : TestManualClock, ISlaveClock { - public readonly Bindable WaitingOnFrames = new Bindable(true); - IBindable ISlaveClock.WaitingOnFrames => WaitingOnFrames; + public Bindable WaitingOnFrames { get; } = new Bindable(true); public bool IsCatchingUp { get; set; } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs index cefe5ff04f..21241f8158 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs @@ -74,7 +74,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync public FrameTimeInfo TimeInfo => new FrameTimeInfo { Elapsed = ElapsedFrameTime, Current = CurrentTime }; - public IBindable WaitingOnFrames { get; } = new Bindable(true); + public Bindable WaitingOnFrames { get; } = new Bindable(true); public bool IsCatchingUp { get; set; } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs index 08d69f9560..6b7a4f5221 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs @@ -14,7 +14,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync /// /// Whether this clock is waiting on frames to continue playback. /// - IBindable WaitingOnFrames { get; } + Bindable WaitingOnFrames { get; } /// /// Whether this clock is resynchronising to the master clock. From b18635341e7692dfbfcbc7e3b35b0efd23a409e3 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 23 Apr 2021 19:12:30 +0900 Subject: [PATCH 56/88] Rename file --- ...CaseCatchUpSyncManager.cs => TestSceneCatchUpSyncManager.cs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename osu.Game.Tests/OnlinePlay/{TestCaseCatchUpSyncManager.cs => TestSceneCatchUpSyncManager.cs} (99%) diff --git a/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs similarity index 99% rename from osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs rename to osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs index 2c10be9c90..93801bd5d7 100644 --- a/osu.Game.Tests/OnlinePlay/TestCaseCatchUpSyncManager.cs +++ b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs @@ -12,7 +12,7 @@ using osu.Game.Tests.Visual; namespace osu.Game.Tests.OnlinePlay { [HeadlessTest] - public class TestCaseCatchUpSyncManager : OsuTestScene + public class TestSceneCatchUpSyncManager : OsuTestScene { private TestManualClock master; private CatchUpSyncManager syncManager; From b41897fd9b34e09ba252638c75d0173e61f7c94a Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 23 Apr 2021 19:23:52 +0900 Subject: [PATCH 57/88] Rename testscene to match class --- ...MultiplayerSpectator.cs => TestSceneMultiSpectatorScreen.cs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename osu.Game.Tests/Visual/Multiplayer/{TestSceneMultiplayerSpectator.cs => TestSceneMultiSpectatorScreen.cs} (99%) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs similarity index 99% rename from osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs rename to osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs index a82dea5640..9cc8eaf876 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectator.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs @@ -23,7 +23,7 @@ using osu.Game.Users; namespace osu.Game.Tests.Visual.Multiplayer { - public class TestSceneMultiplayerSpectator : MultiplayerTestScene + public class TestSceneMultiSpectatorScreen : MultiplayerTestScene { [Cached(typeof(SpectatorStreamingClient))] private TestSpectatorStreamingClient streamingClient = new TestSpectatorStreamingClient(); From aa99c192d025240f6571eb87f856d3235e07e91c Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 26 Apr 2021 16:21:12 +0900 Subject: [PATCH 58/88] Fix type in inline comment --- .../Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index aac03622e3..0cd3b448d2 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -421,7 +421,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer { StartPlay(); - // If the current user was host, they started the match and the in-progres operation needs to be stopped now. + // If the current user was host, they started the match and the in-progress operation needs to be stopped now. readyClickOperation?.Dispose(); readyClickOperation = null; } From 6d30a1a80f5f7e19893a6340c23aca44b0f20a47 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 26 Apr 2021 16:45:20 +0900 Subject: [PATCH 59/88] Reference constant for test startup delay --- .../Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs index 9cc8eaf876..82bbc7b6d3 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs @@ -17,6 +17,7 @@ using osu.Game.Online.Spectator; using osu.Game.Replays.Legacy; using osu.Game.Scoring; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; using osu.Game.Tests.Beatmaps.IO; using osu.Game.Users; @@ -128,7 +129,7 @@ namespace osu.Game.Tests.Visual.Multiplayer } [Test] - public void TestPlayersDoNotStartSimultaneouslyIfBufferingFor15Seconds() + public void TestPlayersDoNotStartSimultaneouslyIfBufferingForMaximumStartDelay() { start(new[] { 55, 56 }); loadSpectateScreen(); @@ -138,8 +139,8 @@ namespace osu.Game.Tests.Visual.Multiplayer checkPausedInstant(55, true); checkPausedInstant(56, true); - // Wait 15 seconds... - AddWaitStep("wait 15 seconds", (int)(15000 / TimePerAction)); + // Wait for the start delay seconds... + AddWaitStep("wait maximum start delay seconds", (int)(CatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); // Player 1 should start playing by itself, player 2 should remain paused. checkPausedInstant(55, false); From 55f383c71ea4577017c85b229bfc1127434b229a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 26 Apr 2021 16:48:32 +0900 Subject: [PATCH 60/88] Rename test to match new `MultiSpectatorLeaderboard` class name --- ...orLeaderboard.cs => TestSceneMultiSpectatorLeaderboard.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename osu.Game.Tests/Visual/Multiplayer/{TestSceneMultiplayerSpectatorLeaderboard.cs => TestSceneMultiSpectatorLeaderboard.cs} (98%) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectatorLeaderboard.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs similarity index 98% rename from osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectatorLeaderboard.cs rename to osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs index 8368186219..da76adf83f 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectatorLeaderboard.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs @@ -24,7 +24,7 @@ using osu.Game.Users; namespace osu.Game.Tests.Visual.Multiplayer { - public class TestSceneMultiplayerSpectatorLeaderboard : MultiplayerTestScene + public class TestSceneMultiSpectatorLeaderboard : MultiplayerTestScene { [Cached(typeof(SpectatorStreamingClient))] private TestSpectatorStreamingClient streamingClient = new TestSpectatorStreamingClient(); @@ -41,7 +41,7 @@ namespace osu.Game.Tests.Visual.Multiplayer { 56, new ManualClock() } }; - public TestSceneMultiplayerSpectatorLeaderboard() + public TestSceneMultiSpectatorLeaderboard() { base.Content.AddRange(new Drawable[] { From 737a15c2d4c549423d763f29b93680a1391606a5 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 26 Apr 2021 17:04:39 +0900 Subject: [PATCH 61/88] Extract out test player IDs to constants --- .../TestSceneMultiSpectatorScreen.cs | 133 +++++++++--------- 1 file changed, 68 insertions(+), 65 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs index 82bbc7b6d3..512311ff03 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs @@ -47,6 +47,9 @@ namespace osu.Game.Tests.Visual.Multiplayer private BeatmapInfo importedBeatmap; private int importedBeatmapId; + private const int player_1_id = 55; + private const int player_2_id = 56; + [BackgroundDependencyLoader] private void load() { @@ -80,29 +83,29 @@ namespace osu.Game.Tests.Visual.Multiplayer { AddStep("start players silently", () => { - Client.CurrentMatchPlayingUserIds.Add(55); - Client.CurrentMatchPlayingUserIds.Add(56); - playingUserIds.Add(55); - playingUserIds.Add(56); - nextFrame[55] = 0; - nextFrame[56] = 0; + Client.CurrentMatchPlayingUserIds.Add(player_1_id); + Client.CurrentMatchPlayingUserIds.Add(player_2_id); + playingUserIds.Add(player_1_id); + playingUserIds.Add(player_2_id); + nextFrame[player_1_id] = 0; + nextFrame[player_2_id] = 0; }); loadSpectateScreen(false); AddWaitStep("wait a bit", 10); - AddStep("load player 55", () => streamingClient.StartPlay(55, importedBeatmapId)); + AddStep("load player first_player_id", () => streamingClient.StartPlay(player_1_id, importedBeatmapId)); AddUntilStep("one player added", () => spectatorScreen.ChildrenOfType().Count() == 1); AddWaitStep("wait a bit", 10); - AddStep("load player 56", () => streamingClient.StartPlay(56, importedBeatmapId)); + AddStep("load player second_player_id", () => streamingClient.StartPlay(player_2_id, importedBeatmapId)); AddUntilStep("two players added", () => spectatorScreen.ChildrenOfType().Count() == 2); } [Test] public void TestGeneral() { - int[] userIds = Enumerable.Range(0, 4).Select(i => 55 + i).ToArray(); + int[] userIds = Enumerable.Range(0, 4).Select(i => player_1_id + i).ToArray(); start(userIds); loadSpectateScreen(); @@ -114,123 +117,123 @@ namespace osu.Game.Tests.Visual.Multiplayer [Test] public void TestPlayersMustStartSimultaneously() { - start(new[] { 55, 56 }); + start(new[] { player_1_id, player_2_id }); loadSpectateScreen(); // Send frames for one player only, both should remain paused. - sendFrames(55, 20); - checkPausedInstant(55, true); - checkPausedInstant(56, true); + sendFrames(player_1_id, 20); + checkPausedInstant(player_1_id, true); + checkPausedInstant(player_2_id, true); // Send frames for the other player, both should now start playing. - sendFrames(56, 20); - checkPausedInstant(55, false); - checkPausedInstant(56, false); + sendFrames(player_2_id, 20); + checkPausedInstant(player_1_id, false); + checkPausedInstant(player_2_id, false); } [Test] public void TestPlayersDoNotStartSimultaneouslyIfBufferingForMaximumStartDelay() { - start(new[] { 55, 56 }); + start(new[] { player_1_id, player_2_id }); loadSpectateScreen(); // Send frames for one player only, both should remain paused. - sendFrames(55, 1000); - checkPausedInstant(55, true); - checkPausedInstant(56, true); + sendFrames(player_1_id, 1000); + checkPausedInstant(player_1_id, true); + checkPausedInstant(player_2_id, true); // Wait for the start delay seconds... AddWaitStep("wait maximum start delay seconds", (int)(CatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); // Player 1 should start playing by itself, player 2 should remain paused. - checkPausedInstant(55, false); - checkPausedInstant(56, true); + checkPausedInstant(player_1_id, false); + checkPausedInstant(player_2_id, true); } [Test] public void TestPlayersContinueWhileOthersBuffer() { - start(new[] { 55, 56 }); + start(new[] { player_1_id, player_2_id }); loadSpectateScreen(); // Send initial frames for both players. A few more for player 1. - sendFrames(55, 20); - sendFrames(56, 10); - checkPausedInstant(55, false); - checkPausedInstant(56, false); + sendFrames(player_1_id, 20); + sendFrames(player_2_id, 10); + checkPausedInstant(player_1_id, false); + checkPausedInstant(player_2_id, false); // Eventually player 2 will pause, player 1 must remain running. - checkPaused(56, true); - checkPausedInstant(55, false); + checkPaused(player_2_id, true); + checkPausedInstant(player_1_id, false); // Eventually both players will run out of frames and should pause. - checkPaused(55, true); - checkPausedInstant(56, true); + checkPaused(player_1_id, true); + checkPausedInstant(player_2_id, true); // Send more frames for the first player only. Player 1 should start playing with player 2 remaining paused. - sendFrames(55, 20); - checkPausedInstant(56, true); - checkPausedInstant(55, false); + sendFrames(player_1_id, 20); + checkPausedInstant(player_2_id, true); + checkPausedInstant(player_1_id, false); // Send more frames for the second player. Both should be playing - sendFrames(56, 20); - checkPausedInstant(56, false); - checkPausedInstant(55, false); + sendFrames(player_2_id, 20); + checkPausedInstant(player_2_id, false); + checkPausedInstant(player_1_id, false); } [Test] public void TestPlayersCatchUpAfterFallingBehind() { - start(new[] { 55, 56 }); + start(new[] { player_1_id, player_2_id }); loadSpectateScreen(); // Send initial frames for both players. A few more for player 1. - sendFrames(55, 1000); - sendFrames(56, 10); - checkPausedInstant(55, false); - checkPausedInstant(56, false); + sendFrames(player_1_id, 1000); + sendFrames(player_2_id, 10); + checkPausedInstant(player_1_id, false); + checkPausedInstant(player_2_id, false); // Eventually player 2 will run out of frames and should pause. - checkPaused(56, true); + checkPaused(player_2_id, true); AddWaitStep("wait a few more frames", 10); // Send more frames for player 2. It should unpause. - sendFrames(56, 1000); - checkPausedInstant(56, false); + sendFrames(player_2_id, 1000); + checkPausedInstant(player_2_id, false); // Player 2 should catch up to player 1 after unpausing. - waitForCatchup(56); + waitForCatchup(player_2_id); AddWaitStep("wait a bit", 10); } [Test] public void TestMostInSyncUserIsAudioSource() { - start(new[] { 55, 56 }); + start(new[] { player_1_id, player_2_id }); loadSpectateScreen(); - assertVolume(55, 0); - assertVolume(56, 0); + assertVolume(player_1_id, 0); + assertVolume(player_2_id, 0); - sendFrames(55, 10); - sendFrames(56, 20); - assertVolume(55, 1); - assertVolume(56, 0); + sendFrames(player_1_id, 10); + sendFrames(player_2_id, 20); + assertVolume(player_1_id, 1); + assertVolume(player_2_id, 0); - checkPaused(55, true); - assertVolume(55, 0); - assertVolume(56, 1); + checkPaused(player_1_id, true); + assertVolume(player_1_id, 0); + assertVolume(player_2_id, 1); - sendFrames(55, 100); - waitForCatchup(55); - checkPaused(56, true); - assertVolume(55, 1); - assertVolume(56, 0); + sendFrames(player_1_id, 100); + waitForCatchup(player_1_id); + checkPaused(player_2_id, true); + assertVolume(player_1_id, 1); + assertVolume(player_2_id, 0); - sendFrames(56, 100); - waitForCatchup(56); - assertVolume(55, 1); - assertVolume(56, 0); + sendFrames(player_2_id, 100); + waitForCatchup(player_2_id); + assertVolume(player_1_id, 1); + assertVolume(player_2_id, 0); } private void loadSpectateScreen(bool waitForPlayerLoad = true) From 5b4cb71cc782e4f8ddc2d9e1d6872ef0787f4caf Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 26 Apr 2021 17:19:44 +0900 Subject: [PATCH 62/88] Change terminology from "slave" to "player clock" --- .../OnlinePlay/TestSceneCatchUpSyncManager.cs | 104 +++++++++--------- .../Spectate/MultiSpectatorPlayer.cs | 18 +-- .../Spectate/MultiSpectatorScreen.cs | 8 +- .../Multiplayer/Spectate/PlayerArea.cs | 6 +- ...lock.cs => CatchUpSpectatorPlayerClock.cs} | 6 +- .../Spectate/Sync/CatchUpSyncManager.cs | 74 ++++++------- ...SlaveClock.cs => ISpectatorPlayerClock.cs} | 2 +- .../Multiplayer/Spectate/Sync/ISyncManager.cs | 18 +-- 8 files changed, 118 insertions(+), 118 deletions(-) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/{CatchUpSlaveClock.cs => CatchUpSpectatorPlayerClock.cs} (90%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/{ISlaveClock.cs => ISpectatorPlayerClock.cs} (90%) diff --git a/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs index 93801bd5d7..73ec5fb934 100644 --- a/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs +++ b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs @@ -17,31 +17,31 @@ namespace osu.Game.Tests.OnlinePlay private TestManualClock master; private CatchUpSyncManager syncManager; - private TestSlaveClock slave1; - private TestSlaveClock slave2; + private TestSpectatorPlayerClock player1; + private TestSpectatorPlayerClock player2; [SetUp] public void Setup() { syncManager = new CatchUpSyncManager(master = new TestManualClock()); - syncManager.AddSlave(slave1 = new TestSlaveClock(1)); - syncManager.AddSlave(slave2 = new TestSlaveClock(2)); + syncManager.AddPlayerClock(player1 = new TestSpectatorPlayerClock(1)); + syncManager.AddPlayerClock(player2 = new TestSpectatorPlayerClock(2)); Schedule(() => Child = syncManager); } [Test] - public void TestMasterClockStartsWhenAllSlavesHaveFrames() + public void TestMasterClockStartsWhenAllPlayerClocksHaveFrames() { - setWaiting(() => slave1, false); + setWaiting(() => player1, false); assertMasterState(false); - assertSlaveState(() => slave1, false); - assertSlaveState(() => slave2, false); + assertPlayerClockState(() => player1, false); + assertPlayerClockState(() => player2, false); - setWaiting(() => slave2, false); + setWaiting(() => player2, false); assertMasterState(true); - assertSlaveState(() => slave1, true); - assertSlaveState(() => slave2, true); + assertPlayerClockState(() => player1, true); + assertPlayerClockState(() => player2, true); } [Test] @@ -54,115 +54,115 @@ namespace osu.Game.Tests.OnlinePlay [Test] public void TestMasterClockStartsWhenAnyReadyForMaximumDelayTime() { - setWaiting(() => slave1, false); + setWaiting(() => player1, false); AddWaitStep($"wait {CatchUpSyncManager.MAXIMUM_START_DELAY} milliseconds", (int)Math.Ceiling(CatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); assertMasterState(true); } [Test] - public void TestSlaveDoesNotCatchUpWhenSlightlyOutOfSync() + public void TestPlayerClockDoesNotCatchUpWhenSlightlyOutOfSync() { setAllWaiting(false); setMasterTime(CatchUpSyncManager.SYNC_TARGET + 1); - assertCatchingUp(() => slave1, false); + assertCatchingUp(() => player1, false); } [Test] - public void TestSlaveStartsCatchingUpWhenTooFarBehind() + public void TestPlayerClockStartsCatchingUpWhenTooFarBehind() { setAllWaiting(false); setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 1); - assertCatchingUp(() => slave1, true); - assertCatchingUp(() => slave2, true); + assertCatchingUp(() => player1, true); + assertCatchingUp(() => player2, true); } [Test] - public void TestSlaveKeepsCatchingUpWhenSlightlyOutOfSync() + public void TestPlayerClockKeepsCatchingUpWhenSlightlyOutOfSync() { setAllWaiting(false); setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 1); - setSlaveTime(() => slave1, CatchUpSyncManager.SYNC_TARGET + 1); - assertCatchingUp(() => slave1, true); + setPlayerClockTime(() => player1, CatchUpSyncManager.SYNC_TARGET + 1); + assertCatchingUp(() => player1, true); } [Test] - public void TestSlaveStopsCatchingUpWhenInSync() + public void TestPlayerClockStopsCatchingUpWhenInSync() { setAllWaiting(false); setMasterTime(CatchUpSyncManager.MAX_SYNC_OFFSET + 2); - setSlaveTime(() => slave1, CatchUpSyncManager.SYNC_TARGET); - assertCatchingUp(() => slave1, false); - assertCatchingUp(() => slave2, true); + setPlayerClockTime(() => player1, CatchUpSyncManager.SYNC_TARGET); + assertCatchingUp(() => player1, false); + assertCatchingUp(() => player2, true); } [Test] - public void TestSlaveDoesNotStopWhenSlightlyAhead() + public void TestPlayerClockDoesNotStopWhenSlightlyAhead() { setAllWaiting(false); - setSlaveTime(() => slave1, -CatchUpSyncManager.SYNC_TARGET); - assertCatchingUp(() => slave1, false); - assertSlaveState(() => slave1, true); + setPlayerClockTime(() => player1, -CatchUpSyncManager.SYNC_TARGET); + assertCatchingUp(() => player1, false); + assertPlayerClockState(() => player1, true); } [Test] - public void TestSlaveStopsWhenTooFarAheadAndStartsWhenBackInSync() + public void TestPlayerClockStopsWhenTooFarAheadAndStartsWhenBackInSync() { setAllWaiting(false); - setSlaveTime(() => slave1, -CatchUpSyncManager.SYNC_TARGET - 1); + setPlayerClockTime(() => player1, -CatchUpSyncManager.SYNC_TARGET - 1); // This is a silent catchup, where IsCatchingUp = false but IsRunning = false also. - assertCatchingUp(() => slave1, false); - assertSlaveState(() => slave1, false); + assertCatchingUp(() => player1, false); + assertPlayerClockState(() => player1, false); setMasterTime(1); - assertCatchingUp(() => slave1, false); - assertSlaveState(() => slave1, true); + assertCatchingUp(() => player1, false); + assertPlayerClockState(() => player1, true); } [Test] - public void TestInSyncSlaveDoesNotStartIfWaitingOnFrames() + public void TestInSyncPlayerClockDoesNotStartIfWaitingOnFrames() { setAllWaiting(false); - assertSlaveState(() => slave1, true); - setWaiting(() => slave1, true); - assertSlaveState(() => slave1, false); + assertPlayerClockState(() => player1, true); + setWaiting(() => player1, true); + assertPlayerClockState(() => player1, false); } - private void setWaiting(Func slave, bool waiting) - => AddStep($"set slave {slave().Id} waiting = {waiting}", () => slave().WaitingOnFrames.Value = waiting); + private void setWaiting(Func playerClock, bool waiting) + => AddStep($"set player clock {playerClock().Id} waiting = {waiting}", () => playerClock().WaitingOnFrames.Value = waiting); - private void setAllWaiting(bool waiting) => AddStep($"set all slaves waiting = {waiting}", () => + private void setAllWaiting(bool waiting) => AddStep($"set all player clocks waiting = {waiting}", () => { - slave1.WaitingOnFrames.Value = waiting; - slave2.WaitingOnFrames.Value = waiting; + player1.WaitingOnFrames.Value = waiting; + player2.WaitingOnFrames.Value = waiting; }); private void setMasterTime(double time) => AddStep($"set master = {time}", () => master.Seek(time)); /// - /// slave.Time = master.Time - offsetFromMaster + /// clock.Time = master.Time - offsetFromMaster /// - private void setSlaveTime(Func slave, double offsetFromMaster) - => AddStep($"set slave {slave().Id} = master - {offsetFromMaster}", () => slave().Seek(master.CurrentTime - offsetFromMaster)); + private void setPlayerClockTime(Func playerClock, double offsetFromMaster) + => AddStep($"set player clock {playerClock().Id} = master - {offsetFromMaster}", () => playerClock().Seek(master.CurrentTime - offsetFromMaster)); private void assertMasterState(bool running) => AddAssert($"master clock {(running ? "is" : "is not")} running", () => master.IsRunning == running); - private void assertCatchingUp(Func slave, bool catchingUp) => - AddAssert($"slave {slave().Id} {(catchingUp ? "is" : "is not")} catching up", () => slave().IsCatchingUp == catchingUp); + private void assertCatchingUp(Func playerClock, bool catchingUp) => + AddAssert($"player clock {playerClock().Id} {(catchingUp ? "is" : "is not")} catching up", () => playerClock().IsCatchingUp == catchingUp); - private void assertSlaveState(Func slave, bool running) - => AddAssert($"slave {slave().Id} {(running ? "is" : "is not")} running", () => slave().IsRunning == running); + private void assertPlayerClockState(Func playerClock, bool running) + => AddAssert($"player clock {playerClock().Id} {(running ? "is" : "is not")} running", () => playerClock().IsRunning == running); - private class TestSlaveClock : TestManualClock, ISlaveClock + private class TestSpectatorPlayerClock : TestManualClock, ISpectatorPlayerClock { public Bindable WaitingOnFrames { get; } = new Bindable(true); @@ -170,7 +170,7 @@ namespace osu.Game.Tests.OnlinePlay public readonly int Id; - public TestSlaveClock(int id) + public TestSpectatorPlayerClock(int id) { Id = id; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs index 4ed949893c..a17519c3aa 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs @@ -19,24 +19,24 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { private readonly Bindable waitingOnFrames = new Bindable(true); private readonly Score score; - private readonly ISlaveClock slaveClock; + private readonly ISpectatorPlayerClock spectatorPlayerClock; /// /// Creates a new . /// /// The score containing the player's replay. - /// The clock controlling the gameplay running state. - public MultiSpectatorPlayer([NotNull] Score score, [NotNull] ISlaveClock slaveClock) + /// The clock controlling the gameplay running state. + public MultiSpectatorPlayer([NotNull] Score score, [NotNull] ISpectatorPlayerClock spectatorPlayerClock) : base(score) { this.score = score; - this.slaveClock = slaveClock; + this.spectatorPlayerClock = spectatorPlayerClock; } [BackgroundDependencyLoader] private void load() { - slaveClock.WaitingOnFrames.BindTo(waitingOnFrames); + spectatorPlayerClock.WaitingOnFrames.BindTo(waitingOnFrames); } protected override void UpdateAfterChildren() @@ -48,18 +48,18 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate } protected override GameplayClockContainer CreateGameplayClockContainer(WorkingBeatmap beatmap, double gameplayStart) - => new SlaveGameplayClockContainer(slaveClock); + => new SpectatorGameplayClockContainer(spectatorPlayerClock); - private class SlaveGameplayClockContainer : GameplayClockContainer + private class SpectatorGameplayClockContainer : GameplayClockContainer { - public SlaveGameplayClockContainer([NotNull] IClock sourceClock) + public SpectatorGameplayClockContainer([NotNull] IClock sourceClock) : base(sourceClock) { } protected override void Update() { - // The slave clock's running state is controlled externally, but the local pausing state needs to be updated to stop gameplay. + // The player clock's running state is controlled externally, but the local pausing state needs to be updated to stop gameplay. if (SourceClock.IsRunning) Start(); else diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index eb59d090be..6a8c10e336 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -79,7 +79,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate }; for (int i = 0; i < UserIds.Length; i++) - grid.Add(instances[i] = new PlayerArea(UserIds[i], new CatchUpSlaveClock(masterClockContainer.GameplayClock))); + grid.Add(instances[i] = new PlayerArea(UserIds[i], new CatchUpSpectatorPlayerClock(masterClockContainer.GameplayClock))); // Todo: This is not quite correct - it should be per-user to adjust for other mod combinations. var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); @@ -104,7 +104,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate if (!isCandidateAudioSource(currentAudioSource?.GameplayClock)) { currentAudioSource = instances.Where(i => isCandidateAudioSource(i.GameplayClock)) - .OrderBy(i => Math.Abs(i.GameplayClock.CurrentTime - syncManager.Master.CurrentTime)) + .OrderBy(i => Math.Abs(i.GameplayClock.CurrentTime - syncManager.MasterClock.CurrentTime)) .FirstOrDefault(); foreach (var instance in instances) @@ -112,7 +112,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate } } - private bool isCandidateAudioSource([CanBeNull] ISlaveClock clock) + private bool isCandidateAudioSource([CanBeNull] ISpectatorPlayerClock clock) => clock?.IsRunning == true && !clock.IsCatchingUp && !clock.WaitingOnFrames.Value; protected override void OnUserStateChanged(int userId, SpectatorState spectatorState) @@ -124,7 +124,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate var instance = instances[getIndexForUser(userId)]; instance.LoadScore(gameplayState.Score); - syncManager.AddSlave(instance.GameplayClock); + syncManager.AddPlayerClock(instance.GameplayClock); leaderboard.AddClock(instance.UserId, instance.GameplayClock); } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs index 494f182251..2accfaec0c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs @@ -35,10 +35,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public readonly int UserId; /// - /// The used to control the gameplay running state of a loaded . + /// The used to control the gameplay running state of a loaded . /// [NotNull] - public readonly ISlaveClock GameplayClock; + public readonly ISpectatorPlayerClock GameplayClock; /// /// The currently-loaded score. @@ -54,7 +54,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly AudioContainer audioContainer; private OsuScreenStack stack; - public PlayerArea(int userId, [NotNull] ISlaveClock gameplayClock) + public PlayerArea(int userId, [NotNull] ISpectatorPlayerClock gameplayClock) { UserId = userId; GameplayClock = gameplayClock; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs similarity index 90% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs index 21241f8158..a8ed7b415f 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs @@ -8,9 +8,9 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// A which catches up using rate adjustment. + /// A which catches up using rate adjustment. /// - public class CatchUpSlaveClock : ISlaveClock + public class CatchUpSpectatorPlayerClock : ISpectatorPlayerClock { /// /// The catch up rate. @@ -19,7 +19,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync private readonly IFrameBasedClock masterClock; - public CatchUpSlaveClock(IFrameBasedClock masterClock) + public CatchUpSpectatorPlayerClock(IFrameBasedClock masterClock) { this.masterClock = masterClock; } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs index 1d3fcd824c..f5c780e8b1 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs @@ -9,46 +9,46 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// A which synchronises de-synced slave clocks through catchup. + /// A which synchronises de-synced player clocks through catchup. /// public class CatchUpSyncManager : Component, ISyncManager { /// - /// The offset from the master clock to which slaves should be synchronised to. + /// The offset from the master clock to which player clocks should be synchronised to. /// public const double SYNC_TARGET = 16; /// - /// The offset from the master clock at which slaves begin resynchronising. + /// The offset from the master clock at which player clocks begin resynchronising. /// public const double MAX_SYNC_OFFSET = 50; /// - /// The maximum delay to start gameplay, if any (but not all) slaves are ready. + /// The maximum delay to start gameplay, if any (but not all) player clocks are ready. /// public const double MAXIMUM_START_DELAY = 15000; /// - /// The master clock which is used to control the timing of all slave clocks. + /// The master clock which is used to control the timing of all player clocks clocks. /// - public IAdjustableClock Master { get; } + public IAdjustableClock MasterClock { get; } /// - /// The slave clocks. + /// The player clocks. /// - private readonly List slaves = new List(); + private readonly List playerClocks = new List(); private bool hasStarted; private double? firstStartAttemptTime; public CatchUpSyncManager(IAdjustableClock master) { - Master = master; + MasterClock = master; } - public void AddSlave(ISlaveClock clock) => slaves.Add(clock); + public void AddPlayerClock(ISpectatorPlayerClock clock) => playerClocks.Add(clock); - public void RemoveSlave(ISlaveClock clock) => slaves.Remove(clock); + public void RemovePlayerClock(ISpectatorPlayerClock clock) => playerClocks.Remove(clock); protected override void Update() { @@ -56,9 +56,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync if (!attemptStart()) { - // Ensure all slaves are stopped until the start succeeds. - foreach (var slave in slaves) - slave.Stop(); + // Ensure all player clocks are stopped until the start succeeds. + foreach (var clock in playerClocks) + clock.Stop(); return; } @@ -67,7 +67,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync } /// - /// Attempts to start playback. Awaits for all slaves to have available frames for up to milliseconds. + /// Attempts to start playback. Waits for all player clocks to have available frames for up to milliseconds. /// /// Whether playback was started and syncing should occur. private bool attemptStart() @@ -75,14 +75,14 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync if (hasStarted) return true; - if (slaves.Count == 0) + if (playerClocks.Count == 0) return false; firstStartAttemptTime ??= Time.Current; - int readyCount = slaves.Count(s => !s.WaitingOnFrames.Value); + int readyCount = playerClocks.Count(s => !s.WaitingOnFrames.Value); - if (readyCount == slaves.Count) + if (readyCount == playerClocks.Count) return hasStarted = true; if (readyCount > 0 && (Time.Current - firstStartAttemptTime) > MAXIMUM_START_DELAY) @@ -92,38 +92,38 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync } /// - /// Updates the catchup states of all slave clocks. + /// Updates the catchup states of all player clocks clocks. /// private void updateCatchup() { - for (int i = 0; i < slaves.Count; i++) + for (int i = 0; i < playerClocks.Count; i++) { - var slave = slaves[i]; - double timeDelta = Master.CurrentTime - slave.CurrentTime; + var clock = playerClocks[i]; + double timeDelta = MasterClock.CurrentTime - clock.CurrentTime; - // Check that the slave isn't too far ahead. - // This is a quiet case in which the catchup is done by the master clock, so IsCatchingUp is not set on the slave. + // Check that the player clock isn't too far ahead. + // This is a quiet case in which the catchup is done by the master clock, so IsCatchingUp is not set on the player clock. if (timeDelta < -SYNC_TARGET) { - slave.Stop(); + clock.Stop(); continue; } - // Make sure the slave is running if it can. - if (!slave.WaitingOnFrames.Value) - slave.Start(); + // Make sure the player clock is running if it can. + if (!clock.WaitingOnFrames.Value) + clock.Start(); - if (slave.IsCatchingUp) + if (clock.IsCatchingUp) { - // Stop the slave from catching up if it's within the sync target. + // Stop the player clock from catching up if it's within the sync target. if (timeDelta <= SYNC_TARGET) - slave.IsCatchingUp = false; + clock.IsCatchingUp = false; } else { - // Make the slave start catching up if it's exceeded the maximum allowable sync offset. + // Make the player clock start catching up if it's exceeded the maximum allowable sync offset. if (timeDelta > MAX_SYNC_OFFSET) - slave.IsCatchingUp = true; + clock.IsCatchingUp = true; } } } @@ -133,14 +133,14 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync /// private void updateMasterClock() { - bool anyInSync = slaves.Any(s => !s.IsCatchingUp); + bool anyInSync = playerClocks.Any(s => !s.IsCatchingUp); - if (Master.IsRunning != anyInSync) + if (MasterClock.IsRunning != anyInSync) { if (anyInSync) - Master.Start(); + MasterClock.Start(); else - Master.Stop(); + MasterClock.Stop(); } } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs similarity index 90% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs index 6b7a4f5221..46f80288c6 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISlaveClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs @@ -9,7 +9,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync /// /// A clock which is used by s and managed by an . /// - public interface ISlaveClock : IFrameBasedClock, IAdjustableClock + public interface ISpectatorPlayerClock : IFrameBasedClock, IAdjustableClock { /// /// Whether this clock is waiting on frames to continue playback. diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs index d63ac7e1ca..1b83ea8cf3 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs @@ -6,25 +6,25 @@ using osu.Framework.Timing; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync { /// - /// Manages the synchronisation between one or more s in relation to a master clock. + /// Manages the synchronisation between one or more s in relation to a master clock. /// public interface ISyncManager { /// - /// The master clock which slaves should synchronise to. + /// The master clock which player clocks should synchronise to. /// - IAdjustableClock Master { get; } + IAdjustableClock MasterClock { get; } /// - /// Adds an to manage. + /// Adds an to manage. /// - /// The to add. - void AddSlave(ISlaveClock clock); + /// The to add. + void AddPlayerClock(ISpectatorPlayerClock clock); /// - /// Removes an , stopping it from being managed by this . + /// Removes an , stopping it from being managed by this . /// - /// The to remove. - void RemoveSlave(ISlaveClock clock); + /// The to remove. + void RemovePlayerClock(ISpectatorPlayerClock clock); } } From 120fb8974df0c4a4fcf390e7aaf8ab94d021aa2a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 26 Apr 2021 17:22:16 +0900 Subject: [PATCH 63/88] Combine more instances of test player IDs --- .../StatefulMultiplayerClientTest.cs | 4 +- .../Visual/Gameplay/TestSceneSpectator.cs | 3 +- .../TestSceneMultiSpectatorLeaderboard.cs | 42 +++--- .../TestSceneMultiSpectatorScreen.cs | 133 +++++++++--------- .../TestSceneMultiplayerMatchSubScreen.cs | 4 +- .../TestSceneMultiplayerSpectateButton.cs | 10 +- .../Multiplayer/MultiplayerTestScene.cs | 3 + 7 files changed, 100 insertions(+), 99 deletions(-) diff --git a/osu.Game.Tests/NonVisual/Multiplayer/StatefulMultiplayerClientTest.cs b/osu.Game.Tests/NonVisual/Multiplayer/StatefulMultiplayerClientTest.cs index 377a33b527..14589f8e6c 100644 --- a/osu.Game.Tests/NonVisual/Multiplayer/StatefulMultiplayerClientTest.cs +++ b/osu.Game.Tests/NonVisual/Multiplayer/StatefulMultiplayerClientTest.cs @@ -53,9 +53,9 @@ namespace osu.Game.Tests.NonVisual.Multiplayer Client.RoomSetupAction = room => { room.State = MultiplayerRoomState.Playing; - room.Users.Add(new MultiplayerRoomUser(55) + room.Users.Add(new MultiplayerRoomUser(PLAYER_1_ID) { - User = new User { Id = 55 }, + User = new User { Id = PLAYER_1_ID }, State = MultiplayerUserState.Playing }); }; diff --git a/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs b/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs index 74ce66096e..0777571d02 100644 --- a/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs +++ b/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs @@ -23,6 +23,7 @@ using osu.Game.Rulesets.UI; using osu.Game.Scoring; using osu.Game.Screens.Play; using osu.Game.Tests.Beatmaps.IO; +using osu.Game.Tests.Visual.Multiplayer; using osu.Game.Users; namespace osu.Game.Tests.Visual.Gameplay @@ -238,7 +239,7 @@ namespace osu.Game.Tests.Visual.Gameplay public class TestSpectatorStreamingClient : SpectatorStreamingClient { - public readonly User StreamingUser = new User { Id = 55, Username = "Test user" }; + public readonly User StreamingUser = new User { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Test user" }; public new BindableList PlayingUsers => (BindableList)base.PlayingUsers; diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs index da76adf83f..d3a5daeac6 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs @@ -37,8 +37,8 @@ namespace osu.Game.Tests.Visual.Multiplayer private readonly Dictionary clocks = new Dictionary { - { 55, new ManualClock() }, - { 56, new ManualClock() } + { PLAYER_1_ID, new ManualClock() }, + { PLAYER_2_ID, new ManualClock() } }; public TestSceneMultiSpectatorLeaderboard() @@ -95,46 +95,46 @@ namespace osu.Game.Tests.Visual.Multiplayer { AddStep("send frames", () => { - // For user 55, send frames in sets of 1. - // For user 56, send frames in sets of 10. + // For player 1, send frames in sets of 1. + // For player 2, send frames in sets of 10. for (int i = 0; i < 100; i++) { - streamingClient.SendFrames(55, i, 1); + streamingClient.SendFrames(PLAYER_1_ID, i, 1); if (i % 10 == 0) - streamingClient.SendFrames(56, i, 10); + streamingClient.SendFrames(PLAYER_2_ID, i, 10); } }); - assertCombo(55, 1); - assertCombo(56, 10); + assertCombo(PLAYER_1_ID, 1); + assertCombo(PLAYER_2_ID, 10); - // Advance to a point where only user 55's frame changes. + // Advance to a point where only user player 1's frame changes. setTime(500); - assertCombo(55, 5); - assertCombo(56, 10); + assertCombo(PLAYER_1_ID, 5); + assertCombo(PLAYER_2_ID, 10); // Advance to a point where both user's frame changes. setTime(1100); - assertCombo(55, 11); - assertCombo(56, 20); + assertCombo(PLAYER_1_ID, 11); + assertCombo(PLAYER_2_ID, 20); - // Advance user 56 only to a point where its frame changes. - setTime(56, 2100); - assertCombo(55, 11); - assertCombo(56, 30); + // Advance user player 2 only to a point where its frame changes. + setTime(PLAYER_2_ID, 2100); + assertCombo(PLAYER_1_ID, 11); + assertCombo(PLAYER_2_ID, 30); // Advance both users beyond their last frame setTime(101 * 100); - assertCombo(55, 100); - assertCombo(56, 100); + assertCombo(PLAYER_1_ID, 100); + assertCombo(PLAYER_2_ID, 100); } [Test] public void TestNoFrames() { - assertCombo(55, 0); - assertCombo(56, 0); + assertCombo(PLAYER_1_ID, 0); + assertCombo(PLAYER_2_ID, 0); } private void setTime(double time) => AddStep($"set time {time}", () => diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs index 512311ff03..7abeed2cbf 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs @@ -47,9 +47,6 @@ namespace osu.Game.Tests.Visual.Multiplayer private BeatmapInfo importedBeatmap; private int importedBeatmapId; - private const int player_1_id = 55; - private const int player_2_id = 56; - [BackgroundDependencyLoader] private void load() { @@ -83,29 +80,29 @@ namespace osu.Game.Tests.Visual.Multiplayer { AddStep("start players silently", () => { - Client.CurrentMatchPlayingUserIds.Add(player_1_id); - Client.CurrentMatchPlayingUserIds.Add(player_2_id); - playingUserIds.Add(player_1_id); - playingUserIds.Add(player_2_id); - nextFrame[player_1_id] = 0; - nextFrame[player_2_id] = 0; + Client.CurrentMatchPlayingUserIds.Add(PLAYER_1_ID); + Client.CurrentMatchPlayingUserIds.Add(PLAYER_2_ID); + playingUserIds.Add(PLAYER_1_ID); + playingUserIds.Add(PLAYER_2_ID); + nextFrame[PLAYER_1_ID] = 0; + nextFrame[PLAYER_2_ID] = 0; }); loadSpectateScreen(false); AddWaitStep("wait a bit", 10); - AddStep("load player first_player_id", () => streamingClient.StartPlay(player_1_id, importedBeatmapId)); + AddStep("load player first_player_id", () => streamingClient.StartPlay(PLAYER_1_ID, importedBeatmapId)); AddUntilStep("one player added", () => spectatorScreen.ChildrenOfType().Count() == 1); AddWaitStep("wait a bit", 10); - AddStep("load player second_player_id", () => streamingClient.StartPlay(player_2_id, importedBeatmapId)); + AddStep("load player second_player_id", () => streamingClient.StartPlay(PLAYER_2_ID, importedBeatmapId)); AddUntilStep("two players added", () => spectatorScreen.ChildrenOfType().Count() == 2); } [Test] public void TestGeneral() { - int[] userIds = Enumerable.Range(0, 4).Select(i => player_1_id + i).ToArray(); + int[] userIds = Enumerable.Range(0, 4).Select(i => PLAYER_1_ID + i).ToArray(); start(userIds); loadSpectateScreen(); @@ -117,123 +114,123 @@ namespace osu.Game.Tests.Visual.Multiplayer [Test] public void TestPlayersMustStartSimultaneously() { - start(new[] { player_1_id, player_2_id }); + start(new[] { PLAYER_1_ID, PLAYER_2_ID }); loadSpectateScreen(); // Send frames for one player only, both should remain paused. - sendFrames(player_1_id, 20); - checkPausedInstant(player_1_id, true); - checkPausedInstant(player_2_id, true); + sendFrames(PLAYER_1_ID, 20); + checkPausedInstant(PLAYER_1_ID, true); + checkPausedInstant(PLAYER_2_ID, true); // Send frames for the other player, both should now start playing. - sendFrames(player_2_id, 20); - checkPausedInstant(player_1_id, false); - checkPausedInstant(player_2_id, false); + sendFrames(PLAYER_2_ID, 20); + checkPausedInstant(PLAYER_1_ID, false); + checkPausedInstant(PLAYER_2_ID, false); } [Test] public void TestPlayersDoNotStartSimultaneouslyIfBufferingForMaximumStartDelay() { - start(new[] { player_1_id, player_2_id }); + start(new[] { PLAYER_1_ID, PLAYER_2_ID }); loadSpectateScreen(); // Send frames for one player only, both should remain paused. - sendFrames(player_1_id, 1000); - checkPausedInstant(player_1_id, true); - checkPausedInstant(player_2_id, true); + sendFrames(PLAYER_1_ID, 1000); + checkPausedInstant(PLAYER_1_ID, true); + checkPausedInstant(PLAYER_2_ID, true); // Wait for the start delay seconds... AddWaitStep("wait maximum start delay seconds", (int)(CatchUpSyncManager.MAXIMUM_START_DELAY / TimePerAction)); // Player 1 should start playing by itself, player 2 should remain paused. - checkPausedInstant(player_1_id, false); - checkPausedInstant(player_2_id, true); + checkPausedInstant(PLAYER_1_ID, false); + checkPausedInstant(PLAYER_2_ID, true); } [Test] public void TestPlayersContinueWhileOthersBuffer() { - start(new[] { player_1_id, player_2_id }); + start(new[] { PLAYER_1_ID, PLAYER_2_ID }); loadSpectateScreen(); // Send initial frames for both players. A few more for player 1. - sendFrames(player_1_id, 20); - sendFrames(player_2_id, 10); - checkPausedInstant(player_1_id, false); - checkPausedInstant(player_2_id, false); + sendFrames(PLAYER_1_ID, 20); + sendFrames(PLAYER_2_ID, 10); + checkPausedInstant(PLAYER_1_ID, false); + checkPausedInstant(PLAYER_2_ID, false); // Eventually player 2 will pause, player 1 must remain running. - checkPaused(player_2_id, true); - checkPausedInstant(player_1_id, false); + checkPaused(PLAYER_2_ID, true); + checkPausedInstant(PLAYER_1_ID, false); // Eventually both players will run out of frames and should pause. - checkPaused(player_1_id, true); - checkPausedInstant(player_2_id, true); + checkPaused(PLAYER_1_ID, true); + checkPausedInstant(PLAYER_2_ID, true); // Send more frames for the first player only. Player 1 should start playing with player 2 remaining paused. - sendFrames(player_1_id, 20); - checkPausedInstant(player_2_id, true); - checkPausedInstant(player_1_id, false); + sendFrames(PLAYER_1_ID, 20); + checkPausedInstant(PLAYER_2_ID, true); + checkPausedInstant(PLAYER_1_ID, false); // Send more frames for the second player. Both should be playing - sendFrames(player_2_id, 20); - checkPausedInstant(player_2_id, false); - checkPausedInstant(player_1_id, false); + sendFrames(PLAYER_2_ID, 20); + checkPausedInstant(PLAYER_2_ID, false); + checkPausedInstant(PLAYER_1_ID, false); } [Test] public void TestPlayersCatchUpAfterFallingBehind() { - start(new[] { player_1_id, player_2_id }); + start(new[] { PLAYER_1_ID, PLAYER_2_ID }); loadSpectateScreen(); // Send initial frames for both players. A few more for player 1. - sendFrames(player_1_id, 1000); - sendFrames(player_2_id, 10); - checkPausedInstant(player_1_id, false); - checkPausedInstant(player_2_id, false); + sendFrames(PLAYER_1_ID, 1000); + sendFrames(PLAYER_2_ID, 10); + checkPausedInstant(PLAYER_1_ID, false); + checkPausedInstant(PLAYER_2_ID, false); // Eventually player 2 will run out of frames and should pause. - checkPaused(player_2_id, true); + checkPaused(PLAYER_2_ID, true); AddWaitStep("wait a few more frames", 10); // Send more frames for player 2. It should unpause. - sendFrames(player_2_id, 1000); - checkPausedInstant(player_2_id, false); + sendFrames(PLAYER_2_ID, 1000); + checkPausedInstant(PLAYER_2_ID, false); // Player 2 should catch up to player 1 after unpausing. - waitForCatchup(player_2_id); + waitForCatchup(PLAYER_2_ID); AddWaitStep("wait a bit", 10); } [Test] public void TestMostInSyncUserIsAudioSource() { - start(new[] { player_1_id, player_2_id }); + start(new[] { PLAYER_1_ID, PLAYER_2_ID }); loadSpectateScreen(); - assertVolume(player_1_id, 0); - assertVolume(player_2_id, 0); + assertVolume(PLAYER_1_ID, 0); + assertVolume(PLAYER_2_ID, 0); - sendFrames(player_1_id, 10); - sendFrames(player_2_id, 20); - assertVolume(player_1_id, 1); - assertVolume(player_2_id, 0); + sendFrames(PLAYER_1_ID, 10); + sendFrames(PLAYER_2_ID, 20); + assertVolume(PLAYER_1_ID, 1); + assertVolume(PLAYER_2_ID, 0); - checkPaused(player_1_id, true); - assertVolume(player_1_id, 0); - assertVolume(player_2_id, 1); + checkPaused(PLAYER_1_ID, true); + assertVolume(PLAYER_1_ID, 0); + assertVolume(PLAYER_2_ID, 1); - sendFrames(player_1_id, 100); - waitForCatchup(player_1_id); - checkPaused(player_2_id, true); - assertVolume(player_1_id, 1); - assertVolume(player_2_id, 0); + sendFrames(PLAYER_1_ID, 100); + waitForCatchup(PLAYER_1_ID); + checkPaused(PLAYER_2_ID, true); + assertVolume(PLAYER_1_ID, 1); + assertVolume(PLAYER_2_ID, 0); - sendFrames(player_2_id, 100); - waitForCatchup(player_2_id); - assertVolume(player_1_id, 1); - assertVolume(player_2_id, 0); + sendFrames(PLAYER_2_ID, 100); + waitForCatchup(PLAYER_2_ID); + assertVolume(PLAYER_1_ID, 1); + assertVolume(PLAYER_2_ID, 0); } private void loadSpectateScreen(bool waitForPlayerLoad = true) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerMatchSubScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerMatchSubScreen.cs index 7c6c158b5a..f611d5fecf 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerMatchSubScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerMatchSubScreen.cs @@ -119,8 +119,8 @@ namespace osu.Game.Tests.Visual.Multiplayer AddStep("join other user (ready)", () => { - Client.AddUser(new User { Id = 55 }); - Client.ChangeUserState(55, MultiplayerUserState.Ready); + Client.AddUser(new User { Id = PLAYER_1_ID }); + Client.ChangeUserState(PLAYER_1_ID, MultiplayerUserState.Ready); }); AddStep("click spectate button", () => diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs index afbc9c32b3..e59b342176 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerSpectateButton.cs @@ -157,8 +157,8 @@ namespace osu.Game.Tests.Visual.Multiplayer [Test] public void TestReadyButtonEnabledWhenHostAndUsersReady() { - AddStep("add user", () => Client.AddUser(new User { Id = 55 })); - AddStep("set user ready", () => Client.ChangeUserState(55, MultiplayerUserState.Ready)); + AddStep("add user", () => Client.AddUser(new User { Id = PLAYER_1_ID })); + AddStep("set user ready", () => Client.ChangeUserState(PLAYER_1_ID, MultiplayerUserState.Ready)); addClickSpectateButtonStep(); assertReadyButtonEnablement(true); @@ -169,11 +169,11 @@ namespace osu.Game.Tests.Visual.Multiplayer { AddStep("add user and transfer host", () => { - Client.AddUser(new User { Id = 55 }); - Client.TransferHost(55); + Client.AddUser(new User { Id = PLAYER_1_ID }); + Client.TransferHost(PLAYER_1_ID); }); - AddStep("set user ready", () => Client.ChangeUserState(55, MultiplayerUserState.Ready)); + AddStep("set user ready", () => Client.ChangeUserState(PLAYER_1_ID, MultiplayerUserState.Ready)); addClickSpectateButtonStep(); assertReadyButtonEnablement(false); diff --git a/osu.Game/Tests/Visual/Multiplayer/MultiplayerTestScene.cs b/osu.Game/Tests/Visual/Multiplayer/MultiplayerTestScene.cs index 7775c2bd24..db344b28dd 100644 --- a/osu.Game/Tests/Visual/Multiplayer/MultiplayerTestScene.cs +++ b/osu.Game/Tests/Visual/Multiplayer/MultiplayerTestScene.cs @@ -16,6 +16,9 @@ namespace osu.Game.Tests.Visual.Multiplayer { public abstract class MultiplayerTestScene : RoomTestScene { + public const int PLAYER_1_ID = 55; + public const int PLAYER_2_ID = 56; + [Cached(typeof(StatefulMultiplayerClient))] public TestMultiplayerClient Client { get; } From 6626e70c95557a17c07216b5514ae4d9440d92a3 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 26 Apr 2021 17:30:27 +0900 Subject: [PATCH 64/88] Pass in master clock instead of slave clock --- .../OnlinePlay/TestSceneCatchUpSyncManager.cs | 5 ++++ .../Spectate/MultiSpectatorScreen.cs | 5 +++- .../Multiplayer/Spectate/PlayerArea.cs | 8 ++++--- .../Sync/CatchUpSpectatorPlayerClock.cs | 23 +++++++++++-------- .../Spectate/Sync/ISpectatorPlayerClock.cs | 5 ++++ 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs index 73ec5fb934..75ba362146 100644 --- a/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs +++ b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs @@ -168,6 +168,11 @@ namespace osu.Game.Tests.OnlinePlay public bool IsCatchingUp { get; set; } + public IFrameBasedClock Source + { + set => throw new NotImplementedException(); + } + public readonly int Id; public TestSpectatorPlayerClock(int id) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index 6a8c10e336..86c975d12f 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -79,7 +79,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate }; for (int i = 0; i < UserIds.Length; i++) - grid.Add(instances[i] = new PlayerArea(UserIds[i], new CatchUpSpectatorPlayerClock(masterClockContainer.GameplayClock))); + { + grid.Add(instances[i] = new PlayerArea(UserIds[i], masterClockContainer.GameplayClock)); + syncManager.AddPlayerClock(instances[i].GameplayClock); + } // Todo: This is not quite correct - it should be per-user to adjust for other mod combinations. var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs index 2accfaec0c..3ef1693ca6 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs @@ -9,6 +9,7 @@ using osu.Framework.Audio; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Framework.Timing; using osu.Game.Beatmaps; using osu.Game.Graphics.UserInterface; using osu.Game.Rulesets; @@ -38,7 +39,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// The used to control the gameplay running state of a loaded . /// [NotNull] - public readonly ISpectatorPlayerClock GameplayClock; + public readonly ISpectatorPlayerClock GameplayClock = new CatchUpSpectatorPlayerClock(); /// /// The currently-loaded score. @@ -54,10 +55,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate private readonly AudioContainer audioContainer; private OsuScreenStack stack; - public PlayerArea(int userId, [NotNull] ISpectatorPlayerClock gameplayClock) + public PlayerArea(int userId, IFrameBasedClock masterClock) { UserId = userId; - GameplayClock = gameplayClock; RelativeSizeAxes = Axes.Both; Masking = true; @@ -71,6 +71,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate }, loadingLayer = new LoadingLayer(true) { State = { Value = Visibility.Visible } } }; + + GameplayClock.Source = masterClock; } public void LoadScore([NotNull] Score score) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs index a8ed7b415f..e2a6b7c9ae 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs @@ -1,6 +1,8 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +#nullable enable + using System; using osu.Framework.Bindables; using osu.Framework.Timing; @@ -17,12 +19,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync /// public const double CATCHUP_RATE = 2; - private readonly IFrameBasedClock masterClock; - - public CatchUpSpectatorPlayerClock(IFrameBasedClock masterClock) - { - this.masterClock = masterClock; - } + /// + /// The source clock. + /// + public IFrameBasedClock? Source { get; set; } public double CurrentTime { get; private set; } @@ -52,19 +52,22 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync public void ProcessFrame() { - masterClock.ProcessFrame(); - ElapsedFrameTime = 0; FramesPerSecond = 0; + if (Source == null) + return; + + Source.ProcessFrame(); + if (IsRunning) { - double elapsedSource = masterClock.ElapsedFrameTime; + double elapsedSource = Source.ElapsedFrameTime; double elapsed = elapsedSource * Rate; CurrentTime += elapsed; ElapsedFrameTime = elapsed; - FramesPerSecond = masterClock.FramesPerSecond; + FramesPerSecond = Source.FramesPerSecond; } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs index 46f80288c6..311969c92c 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs @@ -20,5 +20,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync /// Whether this clock is resynchronising to the master clock. /// bool IsCatchingUp { get; set; } + + /// + /// The source clock + /// + IFrameBasedClock Source { set; } } } From d7618b63fa38512a7449c65eb6d92bbca140cb54 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 26 Apr 2021 17:35:13 +0900 Subject: [PATCH 65/88] Fix test failure --- .../Multiplayer/Spectate/Sync/CatchUpSyncManager.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs index f5c780e8b1..5912c0803b 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs @@ -78,15 +78,18 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync if (playerClocks.Count == 0) return false; - firstStartAttemptTime ??= Time.Current; - int readyCount = playerClocks.Count(s => !s.WaitingOnFrames.Value); if (readyCount == playerClocks.Count) return hasStarted = true; - if (readyCount > 0 && (Time.Current - firstStartAttemptTime) > MAXIMUM_START_DELAY) - return hasStarted = true; + if (readyCount > 0) + { + firstStartAttemptTime ??= Time.Current; + + if (Time.Current - firstStartAttemptTime > MAXIMUM_START_DELAY) + return hasStarted = true; + } return false; } From 94d0b064931736c7e6947a13b35ec0f39a67889e Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 26 Apr 2021 19:01:30 +0900 Subject: [PATCH 66/88] Expose mute adjustment instead --- .../TestSceneMultiSpectatorScreen.cs | 24 +++---- .../Spectate/MultiSpectatorScreen.cs | 2 +- .../Multiplayer/Spectate/PlayerArea.cs | 64 +++++-------------- 3 files changed, 30 insertions(+), 60 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs index 7abeed2cbf..a4856d1ee8 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs @@ -209,28 +209,28 @@ namespace osu.Game.Tests.Visual.Multiplayer start(new[] { PLAYER_1_ID, PLAYER_2_ID }); loadSpectateScreen(); - assertVolume(PLAYER_1_ID, 0); - assertVolume(PLAYER_2_ID, 0); + assertMuted(PLAYER_1_ID, true); + assertMuted(PLAYER_2_ID, true); sendFrames(PLAYER_1_ID, 10); sendFrames(PLAYER_2_ID, 20); - assertVolume(PLAYER_1_ID, 1); - assertVolume(PLAYER_2_ID, 0); + assertMuted(PLAYER_1_ID, false); + assertMuted(PLAYER_2_ID, true); checkPaused(PLAYER_1_ID, true); - assertVolume(PLAYER_1_ID, 0); - assertVolume(PLAYER_2_ID, 1); + assertMuted(PLAYER_1_ID, true); + assertMuted(PLAYER_2_ID, false); sendFrames(PLAYER_1_ID, 100); waitForCatchup(PLAYER_1_ID); checkPaused(PLAYER_2_ID, true); - assertVolume(PLAYER_1_ID, 1); - assertVolume(PLAYER_2_ID, 0); + assertMuted(PLAYER_1_ID, false); + assertMuted(PLAYER_2_ID, true); sendFrames(PLAYER_2_ID, 100); waitForCatchup(PLAYER_2_ID); - assertVolume(PLAYER_1_ID, 1); - assertVolume(PLAYER_2_ID, 0); + assertMuted(PLAYER_1_ID, false); + assertMuted(PLAYER_2_ID, true); } private void loadSpectateScreen(bool waitForPlayerLoad = true) @@ -292,8 +292,8 @@ namespace osu.Game.Tests.Visual.Multiplayer private void checkPausedInstant(int userId, bool state) => AddAssert($"{userId} is {(state ? "paused" : "playing")}", () => getPlayer(userId).ChildrenOfType().First().GameplayClock.IsRunning != state); - private void assertVolume(int userId, double volume) - => AddAssert($"{userId} volume is {volume}", () => getInstance(userId).Volume.Value == volume); + private void assertMuted(int userId, bool muted) + => AddAssert($"{userId} {(muted ? "is" : "is not")} muted", () => getInstance(userId).Mute == muted); private void waitForCatchup(int userId) => AddUntilStep($"{userId} not catching up", () => !getInstance(userId).GameplayClock.IsCatchingUp); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index 86c975d12f..2bd0ad8748 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -111,7 +111,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate .FirstOrDefault(); foreach (var instance in instances) - instance.Volume.Value = instance == currentAudioSource ? 1 : 0; + instance.Mute = instance != currentAudioSource; } } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs index 3ef1693ca6..89c69cc666 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs @@ -23,7 +23,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// /// Provides an area for and manages the hierarchy of a spectated player within a . /// - public class PlayerArea : CompositeDrawable, IAdjustableAudioComponent + public class PlayerArea : CompositeDrawable { /// /// Whether a is loaded in the area. @@ -50,9 +50,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate [Resolved] private BeatmapManager beatmapManager { get; set; } + private readonly BindableDouble volumeAdjustment = new BindableDouble(); private readonly Container gameplayContent; private readonly LoadingLayer loadingLayer; - private readonly AudioContainer audioContainer; private OsuScreenStack stack; public PlayerArea(int userId, IFrameBasedClock masterClock) @@ -62,6 +62,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate RelativeSizeAxes = Axes.Both; Masking = true; + AudioContainer audioContainer; InternalChildren = new Drawable[] { audioContainer = new AudioContainer @@ -72,6 +73,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate loadingLayer = new LoadingLayer(true) { State = { Value = Visibility.Visible } } }; + audioContainer.AddAdjustment(AdjustableProperty.Volume, volumeAdjustment); + GameplayClock.Source = masterClock; } @@ -92,55 +95,22 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate loadingLayer.Hide(); } + private bool mute = true; + + public bool Mute + { + get => mute; + set + { + mute = value; + volumeAdjustment.Value = value ? 0 : 1; + } + } + // Player interferes with global input, so disable input for now. public override bool PropagatePositionalInputSubTree => false; public override bool PropagateNonPositionalInputSubTree => false; - #region IAdjustableAudioComponent - - public IBindable AggregateVolume => audioContainer.AggregateVolume; - - public IBindable AggregateBalance => audioContainer.AggregateBalance; - - public IBindable AggregateFrequency => audioContainer.AggregateFrequency; - - public IBindable AggregateTempo => audioContainer.AggregateTempo; - - public void BindAdjustments(IAggregateAudioAdjustment component) - { - audioContainer.BindAdjustments(component); - } - - public void UnbindAdjustments(IAggregateAudioAdjustment component) - { - audioContainer.UnbindAdjustments(component); - } - - public void AddAdjustment(AdjustableProperty type, IBindable adjustBindable) - { - audioContainer.AddAdjustment(type, adjustBindable); - } - - public void RemoveAdjustment(AdjustableProperty type, IBindable adjustBindable) - { - audioContainer.RemoveAdjustment(type, adjustBindable); - } - - public void RemoveAllAdjustments(AdjustableProperty type) - { - audioContainer.RemoveAllAdjustments(type); - } - - public BindableNumber Volume => audioContainer.Volume; - - public BindableNumber Balance => audioContainer.Balance; - - public BindableNumber Frequency => audioContainer.Frequency; - - public BindableNumber Tempo => audioContainer.Tempo; - - #endregion - private class PlayerIsolationContainer : Container { [Cached] From 7e11d520d587a905f64bf25d023c5515c046b812 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 26 Apr 2021 21:25:34 +0900 Subject: [PATCH 67/88] Remove finished players from multi spectator screen --- .../Spectate/MultiSpectatorScreen.cs | 15 ++++---- osu.Game/Screens/Spectate/SpectatorScreen.cs | 35 ++++++++++++++----- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index 2bd0ad8748..c3917e321d 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -27,6 +27,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// public bool AllPlayersLoaded => instances.All(p => p?.PlayerLoaded == true); + private readonly int[] userIds; + [Resolved] private SpectatorStreamingClient spectatorClient { get; set; } @@ -44,7 +46,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public MultiSpectatorScreen(int[] userIds) : base(userIds.Take(PlayerGrid.MAX_PLAYERS).ToArray()) { - instances = new PlayerArea[UserIds.Length]; + this.userIds = GetUserIds().ToArray(); + instances = new PlayerArea[this.userIds.Length]; } [BackgroundDependencyLoader] @@ -78,9 +81,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate }) }; - for (int i = 0; i < UserIds.Length; i++) + for (int i = 0; i < userIds.Length; i++) { - grid.Add(instances[i] = new PlayerArea(UserIds[i], masterClockContainer.GameplayClock)); + grid.Add(instances[i] = new PlayerArea(userIds[i], masterClockContainer.GameplayClock)); syncManager.AddPlayerClock(instances[i].GameplayClock); } @@ -89,7 +92,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor(); scoreProcessor.ApplyBeatmap(playableBeatmap); - LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, UserIds) { Expanded = { Value = true } }, leaderboardContainer.Add); + LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, userIds) { Expanded = { Value = true } }, leaderboardContainer.Add); } protected override void LoadComplete() @@ -133,10 +136,10 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate protected override void EndGameplay(int userId) { - spectatorClient.StopWatchingUser(userId); + RemoveUser(userId); leaderboard.RemoveClock(userId); } - private int getIndexForUser(int userId) => Array.IndexOf(UserIds, userId); + private int getIndexForUser(int userId) => Array.IndexOf(userIds, userId); } } diff --git a/osu.Game/Screens/Spectate/SpectatorScreen.cs b/osu.Game/Screens/Spectate/SpectatorScreen.cs index 36768a512d..4aca379ebe 100644 --- a/osu.Game/Screens/Spectate/SpectatorScreen.cs +++ b/osu.Game/Screens/Spectate/SpectatorScreen.cs @@ -7,6 +7,7 @@ using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using JetBrains.Annotations; +using NuGet.Packaging; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Game.Beatmaps; @@ -26,7 +27,8 @@ namespace osu.Game.Screens.Spectate /// public abstract class SpectatorScreen : OsuScreen { - protected readonly int[] UserIds; + protected IEnumerable GetUserIds() => userIds; + private readonly HashSet userIds = new HashSet(); [Resolved] private BeatmapManager beatmaps { get; set; } @@ -54,7 +56,7 @@ namespace osu.Game.Screens.Spectate /// The users to spectate. protected SpectatorScreen(params int[] userIds) { - UserIds = userIds; + this.userIds.AddRange(userIds); } protected override void LoadComplete() @@ -80,20 +82,18 @@ namespace osu.Game.Screens.Spectate private Task populateAllUsers() { - var userLookupTasks = new Task[UserIds.Length]; + var userLookupTasks = new List(); - for (int i = 0; i < UserIds.Length; i++) + foreach (var u in userIds) { - var userId = UserIds[i]; - - userLookupTasks[i] = userLookupCache.GetUserAsync(userId).ContinueWith(task => + userLookupTasks.Add(userLookupCache.GetUserAsync(u).ContinueWith(task => { if (!task.IsCompletedSuccessfully) return; lock (stateLock) - userMap[userId] = task.Result; - }); + userMap[u] = task.Result; + })); } return Task.WhenAll(userLookupTasks); @@ -239,6 +239,23 @@ namespace osu.Game.Screens.Spectate /// The user to end gameplay for. protected abstract void EndGameplay(int userId); + /// + /// Stops spectating a user. + /// + /// The user to stop spectating. + protected void RemoveUser(int userId) + { + lock (stateLock) + { + userFinishedPlaying(userId, null); + + userIds.Remove(userId); + userMap.Remove(userId); + + spectatorClient.StopWatchingUser(userId); + } + } + protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); From ed93e26e52b70127ff3e34db5671b25fb6172299 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 26 Apr 2021 21:55:38 +0900 Subject: [PATCH 68/88] Use single method for starting/restarting spectator screen --- .../OnlinePlay/Multiplayer/Multiplayer.cs | 2 +- .../Multiplayer/MultiplayerMatchSubScreen.cs | 31 +++++++------------ .../Spectate/MultiSpectatorScreen.cs | 7 +++++ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Multiplayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Multiplayer.cs index ae22e1fcec..085c824bdc 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Multiplayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Multiplayer.cs @@ -21,7 +21,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer { base.OnResuming(last); - if (client.Room != null) + if (client.Room != null && client.LocalUser?.State != MultiplayerUserState.Spectating) client.ChangeState(MultiplayerUserState.Idle); } diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index 0cd3b448d2..b8347d0537 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -406,29 +406,22 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer } } - private void onRoomUpdated() => Scheduler.Add(() => + private void onRoomUpdated() { - if (client.Room == null) - return; - - Debug.Assert(client.LocalUser != null); - - UpdateMods(); - - if (client.LocalUser.State == MultiplayerUserState.Spectating - && (client.Room.State == MultiplayerRoomState.Playing || client.Room.State == MultiplayerRoomState.WaitingForLoad) - && ParentScreen.IsCurrentScreen()) - { - StartPlay(); - - // If the current user was host, they started the match and the in-progress operation needs to be stopped now. - readyClickOperation?.Dispose(); - readyClickOperation = null; - } - }); + Scheduler.AddOnce(UpdateMods); + } private void onLoadRequested() { + // If the user is spectating, the multi-spectator screen may still be the current screen. + if (!ParentScreen.IsCurrentScreen()) + { + ParentScreen.MakeCurrent(); + + Schedule(onLoadRequested); + return; + } + StartPlay(); readyClickOperation?.Dispose(); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index c3917e321d..85ef375fae 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -140,6 +140,13 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate leaderboard.RemoveClock(userId); } + public override bool OnBackButton() + { + // On a manual exit, set the player state back to idle. + multiplayerClient.ChangeState(MultiplayerUserState.Idle); + return base.OnBackButton(); + } + private int getIndexForUser(int userId) => Array.IndexOf(userIds, userId); } } From 630a6dc46ac262742f9a8a6c1c68e98100c789dc Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 26 Apr 2021 22:23:44 +0900 Subject: [PATCH 69/88] Fix missing dependency --- .../OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index 85ef375fae..ef31a5a7f0 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -7,6 +7,7 @@ using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Game.Online.Multiplayer; using osu.Game.Online.Spectator; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; @@ -32,6 +33,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate [Resolved] private SpectatorStreamingClient spectatorClient { get; set; } + [Resolved] + private StatefulMultiplayerClient multiplayerClient { get; set; } + private readonly PlayerArea[] instances; private MasterGameplayClockContainer masterClockContainer; private ISyncManager syncManager; From 6da4105da6c4b2bcf53f5b2dbc61cfe5b22533c5 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 3 May 2021 13:38:53 +0900 Subject: [PATCH 70/88] Remove Sync namespace (feels unnecessary) --- osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs | 2 +- .../Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs | 1 - .../Spectate/{Sync => }/CatchUpSpectatorPlayerClock.cs | 2 +- .../Multiplayer/Spectate/{Sync => }/CatchUpSyncManager.cs | 2 +- .../Multiplayer/Spectate/{Sync => }/ISpectatorPlayerClock.cs | 2 +- .../OnlinePlay/Multiplayer/Spectate/{Sync => }/ISyncManager.cs | 2 +- .../OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs | 1 - .../OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs | 1 - osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs | 1 - 9 files changed, 5 insertions(+), 9 deletions(-) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{Sync => }/CatchUpSpectatorPlayerClock.cs (97%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{Sync => }/CatchUpSyncManager.cs (98%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{Sync => }/ISpectatorPlayerClock.cs (93%) rename osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/{Sync => }/ISyncManager.cs (94%) diff --git a/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs index 75ba362146..d4e591cf09 100644 --- a/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs +++ b/osu.Game.Tests/OnlinePlay/TestSceneCatchUpSyncManager.cs @@ -6,7 +6,7 @@ using NUnit.Framework; using osu.Framework.Bindables; using osu.Framework.Testing; using osu.Framework.Timing; -using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; +using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; using osu.Game.Tests.Visual; namespace osu.Game.Tests.OnlinePlay diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs index a4856d1ee8..db431c0a82 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs @@ -17,7 +17,6 @@ using osu.Game.Online.Spectator; using osu.Game.Replays.Legacy; using osu.Game.Scoring; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; -using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; using osu.Game.Tests.Beatmaps.IO; using osu.Game.Users; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSpectatorPlayerClock.cs similarity index 97% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSpectatorPlayerClock.cs index e2a6b7c9ae..9e1a020eca 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSpectatorPlayerClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSpectatorPlayerClock.cs @@ -7,7 +7,7 @@ using System; using osu.Framework.Bindables; using osu.Framework.Timing; -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { /// /// A which catches up using rate adjustment. diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs similarity index 98% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs index 5912c0803b..6028bc84f7 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/CatchUpSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs @@ -6,7 +6,7 @@ using System.Linq; using osu.Framework.Graphics; using osu.Framework.Timing; -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { /// /// A which synchronises de-synced player clocks through catchup. diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/ISpectatorPlayerClock.cs similarity index 93% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/ISpectatorPlayerClock.cs index 311969c92c..1a5231e602 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISpectatorPlayerClock.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/ISpectatorPlayerClock.cs @@ -4,7 +4,7 @@ using osu.Framework.Bindables; using osu.Framework.Timing; -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { /// /// A clock which is used by s and managed by an . diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/ISyncManager.cs similarity index 94% rename from osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs rename to osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/ISyncManager.cs index 1b83ea8cf3..bd698108f6 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/Sync/ISyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/ISyncManager.cs @@ -3,7 +3,7 @@ using osu.Framework.Timing; -namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync +namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate { /// /// Manages the synchronisation between one or more s in relation to a master clock. diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs index a17519c3aa..0fe9e01d9d 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorPlayer.cs @@ -7,7 +7,6 @@ using osu.Framework.Bindables; using osu.Framework.Timing; using osu.Game.Beatmaps; using osu.Game.Scoring; -using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index ef31a5a7f0..fd2c79f8e4 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -9,7 +9,6 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Online.Multiplayer; using osu.Game.Online.Spectator; -using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; using osu.Game.Screens.Spectate; diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs index 89c69cc666..50b5fc2110 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs @@ -15,7 +15,6 @@ using osu.Game.Graphics.UserInterface; using osu.Game.Rulesets; using osu.Game.Rulesets.Mods; using osu.Game.Scoring; -using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync; using osu.Game.Screens.Play; namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate From 66ae6e58d18b8c1f5be9d73095c2ea9aac73791c Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 3 May 2021 14:01:10 +0900 Subject: [PATCH 71/88] Reword comment regarding LoadRequested special case to be easier to understand context --- .../OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index b8347d0537..e6ce135660 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -413,7 +413,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer private void onLoadRequested() { - // If the user is spectating, the multi-spectator screen may still be the current screen. + // In the case of spectating, IMultiplayerClient.LoadRequested can be fired while the game is still spectating a previous session. + // For now, we want to game to switch to the new game so need to request exiting from the play screen. if (!ParentScreen.IsCurrentScreen()) { ParentScreen.MakeCurrent(); From dc5ee31d946918c2cbb2d0727e6039544b3a38d3 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 3 May 2021 14:04:20 +0900 Subject: [PATCH 72/88] Use switch for screen construction --- .../Multiplayer/MultiplayerMatchSubScreen.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index e6ce135660..ab22d40181 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -433,10 +433,16 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer { Debug.Assert(client.LocalUser != null); - if (client.LocalUser.State == MultiplayerUserState.Spectating) - return new MultiSpectatorScreen(client.CurrentMatchPlayingUserIds.ToArray()); + int[] userIds = client.CurrentMatchPlayingUserIds.ToArray(); - return new MultiplayerPlayer(SelectedItem.Value, client.CurrentMatchPlayingUserIds.ToArray()); + switch (client.LocalUser.State) + { + case MultiplayerUserState.Spectating: + return new MultiSpectatorScreen(userIds); + + default: + return new MultiplayerPlayer(SelectedItem.Value, userIds); + } } protected override void Dispose(bool isDisposing) From c065092e72f67e41d02e6d3a75637f7722c26b4d Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 3 May 2021 14:25:52 +0900 Subject: [PATCH 73/88] Fix weird access to userIds in `MultiplayerSpectatorScreen` --- .../Multiplayer/Spectate/MultiSpectatorScreen.cs | 16 ++++++---------- osu.Game/Screens/Spectate/SpectatorScreen.cs | 6 +++--- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index fd2c79f8e4..be4450787a 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -27,8 +27,6 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate /// public bool AllPlayersLoaded => instances.All(p => p?.PlayerLoaded == true); - private readonly int[] userIds; - [Resolved] private SpectatorStreamingClient spectatorClient { get; set; } @@ -49,8 +47,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public MultiSpectatorScreen(int[] userIds) : base(userIds.Take(PlayerGrid.MAX_PLAYERS).ToArray()) { - this.userIds = GetUserIds().ToArray(); - instances = new PlayerArea[this.userIds.Length]; + instances = new PlayerArea[UserIds.Count]; } [BackgroundDependencyLoader] @@ -84,9 +81,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate }) }; - for (int i = 0; i < userIds.Length; i++) + for (int i = 0; i < UserIds.Count; i++) { - grid.Add(instances[i] = new PlayerArea(userIds[i], masterClockContainer.GameplayClock)); + grid.Add(instances[i] = new PlayerArea(UserIds[i], masterClockContainer.GameplayClock)); syncManager.AddPlayerClock(instances[i].GameplayClock); } @@ -95,7 +92,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor(); scoreProcessor.ApplyBeatmap(playableBeatmap); - LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, userIds) { Expanded = { Value = true } }, leaderboardContainer.Add); + LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, UserIds.ToArray()) { Expanded = { Value = true } }, leaderboardContainer.Add); } protected override void LoadComplete() @@ -130,7 +127,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate protected override void StartGameplay(int userId, GameplayState gameplayState) { - var instance = instances[getIndexForUser(userId)]; + var instance = instances.Single(i => i.UserId == userId); + instance.LoadScore(gameplayState.Score); syncManager.AddPlayerClock(instance.GameplayClock); @@ -149,7 +147,5 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate multiplayerClient.ChangeState(MultiplayerUserState.Idle); return base.OnBackButton(); } - - private int getIndexForUser(int userId) => Array.IndexOf(userIds, userId); } } diff --git a/osu.Game/Screens/Spectate/SpectatorScreen.cs b/osu.Game/Screens/Spectate/SpectatorScreen.cs index 4aca379ebe..bcebd51954 100644 --- a/osu.Game/Screens/Spectate/SpectatorScreen.cs +++ b/osu.Game/Screens/Spectate/SpectatorScreen.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using JetBrains.Annotations; -using NuGet.Packaging; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Game.Beatmaps; @@ -27,8 +26,9 @@ namespace osu.Game.Screens.Spectate /// public abstract class SpectatorScreen : OsuScreen { - protected IEnumerable GetUserIds() => userIds; - private readonly HashSet userIds = new HashSet(); + protected IReadOnlyList UserIds => userIds; + + private readonly List userIds = new List(); [Resolved] private BeatmapManager beatmaps { get; set; } From 2aa21e2affb3f9993ec42b87228236e768aa7e05 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 3 May 2021 14:37:11 +0900 Subject: [PATCH 74/88] Adjust documentation in `CatchUpSyncManager` --- .../OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs index 6028bc84f7..efc12eaaa5 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/CatchUpSyncManager.cs @@ -14,7 +14,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public class CatchUpSyncManager : Component, ISyncManager { /// - /// The offset from the master clock to which player clocks should be synchronised to. + /// The offset from the master clock to which player clocks should remain within to be considered in-sync. /// public const double SYNC_TARGET = 16; @@ -102,6 +102,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate for (int i = 0; i < playerClocks.Count; i++) { var clock = playerClocks[i]; + + // How far this player's clock is out of sync, compared to the master clock. + // A negative value means the player is running fast (ahead); a positive value means the player is running behind (catching up). double timeDelta = MasterClock.CurrentTime - clock.CurrentTime; // Check that the player clock isn't too far ahead. From b1a19b6dd6f6ea6782828fdd1cc23883bf9c947a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 3 May 2021 14:41:55 +0900 Subject: [PATCH 75/88] Add xmldoc for `PlayerIsolationContainer` --- osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs index 50b5fc2110..fe79e5db72 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerArea.cs @@ -110,6 +110,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate public override bool PropagatePositionalInputSubTree => false; public override bool PropagateNonPositionalInputSubTree => false; + /// + /// Isolates each player instance from the game-wide ruleset/beatmap/mods (to allow for different players having different settings). + /// private class PlayerIsolationContainer : Container { [Cached] From 54abf8f6f685a31487579d5f5bde1db7d4d67479 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 3 May 2021 14:48:04 +0900 Subject: [PATCH 76/88] Vertically centre leaderboard for now --- .../Multiplayer/Spectate/MultiSpectatorScreen.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs index be4450787a..8c7b7bab01 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs @@ -92,7 +92,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor(); scoreProcessor.ApplyBeatmap(playableBeatmap); - LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, UserIds.ToArray()) { Expanded = { Value = true } }, leaderboardContainer.Add); + LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, UserIds.ToArray()) + { + Expanded = { Value = true }, + Anchor = Anchor.CentreLeft, + Origin = Anchor.CentreLeft, + }, leaderboardContainer.Add); } protected override void LoadComplete() From 811282a975ca48c91239599c0521dbe60a3811f8 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 11 May 2021 19:01:41 +0900 Subject: [PATCH 77/88] Add failing test --- .../Multiplayer/TestSceneMultiplayer.cs | 93 +++++++++++++++++++ .../Multiplayer/TestMultiplayerClient.cs | 10 +- 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs index 78bc51e47b..519d76fc93 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs @@ -1,9 +1,24 @@ // Copyright (c) ppy Pty Ltd . 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.Allocation; +using osu.Framework.Audio; +using osu.Framework.Platform; +using osu.Framework.Screens; +using osu.Framework.Testing; +using osu.Game.Beatmaps; using osu.Game.Online.Multiplayer; +using osu.Game.Online.Rooms; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Osu; using osu.Game.Screens.OnlinePlay.Components; +using osu.Game.Screens.OnlinePlay.Multiplayer.Match; +using osu.Game.Tests.Beatmaps; +using osu.Game.Tests.Resources; +using osu.Game.Users; +using osuTK.Input; namespace osu.Game.Tests.Visual.Multiplayer { @@ -11,7 +26,85 @@ namespace osu.Game.Tests.Visual.Multiplayer { private TestMultiplayer multiplayerScreen; + private BeatmapManager beatmaps; + private RulesetStore rulesets; + private BeatmapSetInfo importedSet; + + private TestMultiplayerClient client => multiplayerScreen.Client; + private Room room => client.APIRoom; + public TestSceneMultiplayer() + { + loadMultiplayer(); + } + + [BackgroundDependencyLoader] + private void load(GameHost host, AudioManager audio) + { + Dependencies.Cache(rulesets = new RulesetStore(ContextFactory)); + Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, host, Beatmap.Default)); + beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).Wait(); + + importedSet = beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.All).First(); + } + + [Test] + public void TestMatchStartWhileSpectatingWithNoBeatmap() + { + loadMultiplayer(); + + AddStep("open room", () => + { + multiplayerScreen.OpenNewRoom(new Room + { + Name = { Value = "Test Room" }, + Playlist = + { + new PlaylistItem + { + Beatmap = { Value = new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo }, + Ruleset = { Value = new OsuRuleset().RulesetInfo }, + } + } + }); + }); + + AddStep("create room", () => + { + InputManager.MoveMouseTo(this.ChildrenOfType().Single()); + InputManager.Click(MouseButton.Left); + }); + + AddUntilStep("wait for join", () => client.Room != null); + + AddStep("join other user (ready, host)", () => + { + client.AddUser(new User { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Other" }); + client.TransferHost(MultiplayerTestScene.PLAYER_1_ID); + client.ChangeUserState(MultiplayerTestScene.PLAYER_1_ID, MultiplayerUserState.Ready); + }); + + AddStep("change playlist", () => + { + room.Playlist.Add(new PlaylistItem + { + Beatmap = { Value = new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo }, + Ruleset = { Value = new OsuRuleset().RulesetInfo }, + }); + }); + + AddStep("click spectate button", () => + { + InputManager.MoveMouseTo(this.ChildrenOfType().Single()); + InputManager.Click(MouseButton.Left); + }); + + AddStep("start match externally", () => client.StartMatch()); + + AddAssert("screen not changed", () => multiplayerScreen.IsCurrentScreen()); + } + + private void loadMultiplayer() { AddStep("show", () => { diff --git a/osu.Game/Tests/Visual/Multiplayer/TestMultiplayerClient.cs b/osu.Game/Tests/Visual/Multiplayer/TestMultiplayerClient.cs index de77a15da0..167cf705a7 100644 --- a/osu.Game/Tests/Visual/Multiplayer/TestMultiplayerClient.cs +++ b/osu.Game/Tests/Visual/Multiplayer/TestMultiplayerClient.cs @@ -25,6 +25,8 @@ namespace osu.Game.Tests.Visual.Multiplayer public override IBindable IsConnected => isConnected; private readonly Bindable isConnected = new Bindable(true); + public Room? APIRoom { get; private set; } + public Action? RoomSetupAction; [Resolved] @@ -138,10 +140,16 @@ namespace osu.Game.Tests.Visual.Multiplayer RoomSetupAction?.Invoke(room); RoomSetupAction = null; + APIRoom = apiRoom; + return Task.FromResult(room); } - protected override Task LeaveRoomInternal() => Task.CompletedTask; + protected override Task LeaveRoomInternal() + { + APIRoom = null; + return Task.CompletedTask; + } public override Task TransferHost(int userId) => ((IMultiplayerClient)this).HostChanged(userId); From 7fe8737d9437073a6c6137483d6c9be48cf3c9c4 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 11 May 2021 19:21:44 +0900 Subject: [PATCH 78/88] Add failing tests --- .../Multiplayer/TestSceneMultiplayer.cs | 92 ++++++++++++++----- 1 file changed, 67 insertions(+), 25 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs index 519d76fc93..a81927bec2 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs @@ -49,34 +49,23 @@ namespace osu.Game.Tests.Visual.Multiplayer } [Test] - public void TestMatchStartWhileSpectatingWithNoBeatmap() + public void TestLocalPlayDoesNotStartWhileSpectatingWithNoBeatmap() { loadMultiplayer(); - AddStep("open room", () => + createRoom(new Room { - multiplayerScreen.OpenNewRoom(new Room + Name = { Value = "Test Room" }, + Playlist = { - Name = { Value = "Test Room" }, - Playlist = + new PlaylistItem { - new PlaylistItem - { - Beatmap = { Value = new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo }, - Ruleset = { Value = new OsuRuleset().RulesetInfo }, - } + Beatmap = { Value = beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First(b => b.RulesetID == 0)).BeatmapInfo }, + Ruleset = { Value = new OsuRuleset().RulesetInfo }, } - }); + } }); - AddStep("create room", () => - { - InputManager.MoveMouseTo(this.ChildrenOfType().Single()); - InputManager.Click(MouseButton.Left); - }); - - AddUntilStep("wait for join", () => client.Room != null); - AddStep("join other user (ready, host)", () => { client.AddUser(new User { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Other" }); @@ -84,13 +73,44 @@ namespace osu.Game.Tests.Visual.Multiplayer client.ChangeUserState(MultiplayerTestScene.PLAYER_1_ID, MultiplayerUserState.Ready); }); - AddStep("change playlist", () => + AddStep("delete beatmap", () => beatmaps.Delete(importedSet)); + + AddStep("click spectate button", () => { - room.Playlist.Add(new PlaylistItem + InputManager.MoveMouseTo(this.ChildrenOfType().Single()); + InputManager.Click(MouseButton.Left); + }); + + AddStep("start match externally", () => client.StartMatch()); + + AddAssert("play not started", () => multiplayerScreen.IsCurrentScreen()); + } + + [Test] + public void TestLocalPlayStartsWhileSpectatingWhenBeatmapBecomesAvailable() + { + loadMultiplayer(); + + createRoom(new Room + { + Name = { Value = "Test Room" }, + Playlist = { - Beatmap = { Value = new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo }, - Ruleset = { Value = new OsuRuleset().RulesetInfo }, - }); + new PlaylistItem + { + Beatmap = { Value = beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First(b => b.RulesetID == 0)).BeatmapInfo }, + Ruleset = { Value = new OsuRuleset().RulesetInfo }, + } + } + }); + + AddStep("delete beatmap", () => beatmaps.Delete(importedSet)); + + AddStep("join other user (ready, host)", () => + { + client.AddUser(new User { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Other" }); + client.TransferHost(MultiplayerTestScene.PLAYER_1_ID); + client.ChangeUserState(MultiplayerTestScene.PLAYER_1_ID, MultiplayerUserState.Ready); }); AddStep("click spectate button", () => @@ -101,7 +121,29 @@ namespace osu.Game.Tests.Visual.Multiplayer AddStep("start match externally", () => client.StartMatch()); - AddAssert("screen not changed", () => multiplayerScreen.IsCurrentScreen()); + AddStep("restore beatmap", () => + { + beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).Wait(); + importedSet = beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.All).First(); + }); + + AddUntilStep("play started", () => !multiplayerScreen.IsCurrentScreen()); + } + + private void createRoom(Room room) + { + AddStep("open room", () => + { + multiplayerScreen.OpenNewRoom(room); + }); + + AddStep("create room", () => + { + InputManager.MoveMouseTo(this.ChildrenOfType().Single()); + InputManager.Click(MouseButton.Left); + }); + + AddUntilStep("wait for join", () => client.Room != null); } private void loadMultiplayer() From 9ad1e5067e0494730446a34bc01e0222010e438a Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 11 May 2021 19:22:09 +0900 Subject: [PATCH 79/88] Fix spectate being entered while not having the beatmap --- .../Multiplayer/MultiplayerMatchSubScreen.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs index ab22d40181..fa18b792c3 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/MultiplayerMatchSubScreen.cs @@ -14,6 +14,7 @@ using osu.Framework.Screens; using osu.Framework.Threading; using osu.Game.Beatmaps; using osu.Game.Configuration; +using osu.Game.Online; using osu.Game.Online.Multiplayer; using osu.Game.Online.Rooms; using osu.Game.Overlays; @@ -354,10 +355,17 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer client.ChangeBeatmapAvailability(availability.NewValue); - // while this flow is handled server-side, this covers the edge case of the local user being in a ready state and then deleting the current beatmap. - if (availability.NewValue != Online.Rooms.BeatmapAvailability.LocallyAvailable() - && client.LocalUser?.State == MultiplayerUserState.Ready) - client.ChangeState(MultiplayerUserState.Idle); + if (availability.NewValue.State != DownloadState.LocallyAvailable) + { + // while this flow is handled server-side, this covers the edge case of the local user being in a ready state and then deleting the current beatmap. + if (client.LocalUser?.State == MultiplayerUserState.Ready) + client.ChangeState(MultiplayerUserState.Idle); + } + else + { + if (client.LocalUser?.State == MultiplayerUserState.Spectating && (client.Room?.State == MultiplayerRoomState.WaitingForLoad || client.Room?.State == MultiplayerRoomState.Playing)) + onLoadRequested(); + } } private void onReadyClick() @@ -413,6 +421,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer private void onLoadRequested() { + if (BeatmapAvailability.Value.State != DownloadState.LocallyAvailable) + return; + // In the case of spectating, IMultiplayerClient.LoadRequested can be fired while the game is still spectating a previous session. // For now, we want to game to switch to the new game so need to request exiting from the play screen. if (!ParentScreen.IsCurrentScreen()) From bc4213eea1fd86a2184d1c0ba8d874025e9c3ce6 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 11 May 2021 19:26:58 +0900 Subject: [PATCH 80/88] Add test for changing back to idle on deletion --- .../Multiplayer/TestSceneMultiplayer.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs index a81927bec2..f2331542c6 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs @@ -15,7 +15,6 @@ using osu.Game.Rulesets; using osu.Game.Rulesets.Osu; using osu.Game.Screens.OnlinePlay.Components; using osu.Game.Screens.OnlinePlay.Multiplayer.Match; -using osu.Game.Tests.Beatmaps; using osu.Game.Tests.Resources; using osu.Game.Users; using osuTK.Input; @@ -43,9 +42,37 @@ namespace osu.Game.Tests.Visual.Multiplayer { Dependencies.Cache(rulesets = new RulesetStore(ContextFactory)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, host, Beatmap.Default)); - beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).Wait(); + } + [SetUp] + public void Setup() => Schedule(() => + { + beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).Wait(); importedSet = beatmaps.GetAllUsableBeatmapSetsEnumerable(IncludedDetails.All).First(); + }); + + [Test] + public void TestUserSetToIdleWhenBeatmapDeleted() + { + loadMultiplayer(); + + createRoom(new Room + { + Name = { Value = "Test Room" }, + Playlist = + { + new PlaylistItem + { + Beatmap = { Value = beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First(b => b.RulesetID == 0)).BeatmapInfo }, + Ruleset = { Value = new OsuRuleset().RulesetInfo }, + } + } + }); + + AddStep("set user ready", () => client.ChangeState(MultiplayerUserState.Ready)); + AddStep("delete beatmap", () => beatmaps.Delete(importedSet)); + + AddAssert("user state is idle", () => client.LocalUser?.State == MultiplayerUserState.Idle); } [Test] From f5bc389998283eca1a7e4d0363945f1800840aa7 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 11 May 2021 19:31:32 +0900 Subject: [PATCH 81/88] Fix flaky tests --- .../Visual/Multiplayer/TestSceneMultiplayer.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs index f2331542c6..69fbd56490 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayer.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using System.Linq; using NUnit.Framework; using osu.Framework.Allocation; @@ -14,6 +15,7 @@ using osu.Game.Online.Rooms; using osu.Game.Rulesets; using osu.Game.Rulesets.Osu; using osu.Game.Screens.OnlinePlay.Components; +using osu.Game.Screens.OnlinePlay.Multiplayer; using osu.Game.Screens.OnlinePlay.Multiplayer.Match; using osu.Game.Tests.Resources; using osu.Game.Users; @@ -56,7 +58,7 @@ namespace osu.Game.Tests.Visual.Multiplayer { loadMultiplayer(); - createRoom(new Room + createRoom(() => new Room { Name = { Value = "Test Room" }, Playlist = @@ -80,7 +82,7 @@ namespace osu.Game.Tests.Visual.Multiplayer { loadMultiplayer(); - createRoom(new Room + createRoom(() => new Room { Name = { Value = "Test Room" }, Playlist = @@ -118,7 +120,7 @@ namespace osu.Game.Tests.Visual.Multiplayer { loadMultiplayer(); - createRoom(new Room + createRoom(() => new Room { Name = { Value = "Test Room" }, Playlist = @@ -157,13 +159,16 @@ namespace osu.Game.Tests.Visual.Multiplayer AddUntilStep("play started", () => !multiplayerScreen.IsCurrentScreen()); } - private void createRoom(Room room) + private void createRoom(Func room) { AddStep("open room", () => { - multiplayerScreen.OpenNewRoom(room); + multiplayerScreen.OpenNewRoom(room()); }); + AddUntilStep("wait for room open", () => this.ChildrenOfType().FirstOrDefault()?.IsLoaded == true); + AddWaitStep("wait for transition", 2); + AddStep("create room", () => { InputManager.MoveMouseTo(this.ChildrenOfType().Single()); From 9b09361cc9a5b3ef1fb9cecf34e28f6401ae9def Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 12 May 2021 12:16:41 +0900 Subject: [PATCH 82/88] Add testable spectator streaming client --- .../Spectator/TestSpectatorStreamingClient.cs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs diff --git a/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs new file mode 100644 index 0000000000..c177d3f399 --- /dev/null +++ b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs @@ -0,0 +1,84 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using osu.Framework.Bindables; +using osu.Framework.Utils; +using osu.Game.Online; +using osu.Game.Online.Spectator; +using osu.Game.Replays.Legacy; +using osu.Game.Scoring; + +namespace osu.Game.Tests.Visual.Spectator +{ + public class TestSpectatorStreamingClient : SpectatorStreamingClient + { + public new BindableList PlayingUsers => (BindableList)base.PlayingUsers; + private readonly HashSet watchingUsers = new HashSet(); + + private readonly Dictionary userBeatmapDictionary = new Dictionary(); + private readonly Dictionary userSentStateDictionary = new Dictionary(); + + public TestSpectatorStreamingClient() + : base(new DevelopmentEndpointConfiguration()) + { + } + + public void StartPlay(int userId, int beatmapId) + { + userBeatmapDictionary[userId] = beatmapId; + sendState(userId, beatmapId); + } + + public void EndPlay(int userId, int beatmapId) + { + ((ISpectatorClient)this).UserFinishedPlaying(userId, new SpectatorState + { + BeatmapID = beatmapId, + RulesetID = 0, + }); + + userSentStateDictionary[userId] = false; + } + + public void SendFrames(int userId, int index, int count) + { + var frames = new List(); + + for (int i = index; i < index + count; i++) + { + var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1; + + frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState)); + } + + var bundle = new FrameDataBundle(new ScoreInfo(), frames); + ((ISpectatorClient)this).UserSentFrames(userId, bundle); + + if (!userSentStateDictionary[userId]) + sendState(userId, userBeatmapDictionary[userId]); + } + + public override void WatchUser(int userId) + { + // When newly watching a user, the server sends the playing state immediately. + if (!watchingUsers.Contains(userId) && PlayingUsers.Contains(userId)) + sendState(userId, userBeatmapDictionary[userId]); + + base.WatchUser(userId); + + watchingUsers.Add(userId); + } + + private void sendState(int userId, int beatmapId) + { + ((ISpectatorClient)this).UserBeganPlaying(userId, new SpectatorState + { + BeatmapID = beatmapId, + RulesetID = 0, + }); + + userSentStateDictionary[userId] = true; + } + } +} From 21fc0ba43baea6e0cb115952ab21df06f1d1c003 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 12 May 2021 12:17:42 +0900 Subject: [PATCH 83/88] Combine test spectator streaming client implementations --- .../Visual/Gameplay/TestSceneSpectator.cs | 89 ++----------------- .../TestSceneMultiSpectatorLeaderboard.cs | 70 +-------------- .../TestSceneMultiSpectatorScreen.cs | 75 +--------------- .../TestSceneCurrentlyPlayingDisplay.cs | 4 +- 4 files changed, 11 insertions(+), 227 deletions(-) diff --git a/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs b/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs index 0777571d02..a7ed217b4d 100644 --- a/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs +++ b/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs @@ -1,35 +1,32 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using NUnit.Framework; using osu.Framework.Allocation; -using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Screens; using osu.Framework.Testing; -using osu.Framework.Utils; using osu.Game.Beatmaps; using osu.Game.Database; -using osu.Game.Online; using osu.Game.Online.Spectator; -using osu.Game.Replays.Legacy; using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Osu.Replays; using osu.Game.Rulesets.UI; -using osu.Game.Scoring; using osu.Game.Screens.Play; using osu.Game.Tests.Beatmaps.IO; using osu.Game.Tests.Visual.Multiplayer; +using osu.Game.Tests.Visual.Spectator; using osu.Game.Users; namespace osu.Game.Tests.Visual.Gameplay { public class TestSceneSpectator : ScreenTestScene { + private readonly User streamingUser = new User { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Test user" }; + [Cached(typeof(SpectatorStreamingClient))] private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient(); @@ -215,9 +212,9 @@ namespace osu.Game.Tests.Visual.Gameplay private void waitForPlayer() => AddUntilStep("wait for player", () => Stack.CurrentScreen is Player); - private void start(int? beatmapId = null) => AddStep("start play", () => testSpectatorStreamingClient.StartPlay(beatmapId ?? importedBeatmapId)); + private void start(int? beatmapId = null) => AddStep("start play", () => testSpectatorStreamingClient.StartPlay(streamingUser.Id, beatmapId ?? importedBeatmapId)); - private void finish(int? beatmapId = null) => AddStep("end play", () => testSpectatorStreamingClient.EndPlay(beatmapId ?? importedBeatmapId)); + private void finish(int? beatmapId = null) => AddStep("end play", () => testSpectatorStreamingClient.EndPlay(streamingUser.Id, beatmapId ?? importedBeatmapId)); private void checkPaused(bool state) => AddUntilStep($"game is {(state ? "paused" : "playing")}", () => player.ChildrenOfType().First().IsPaused.Value == state); @@ -226,89 +223,17 @@ namespace osu.Game.Tests.Visual.Gameplay { AddStep("send frames", () => { - testSpectatorStreamingClient.SendFrames(nextFrame, count); + testSpectatorStreamingClient.SendFrames(streamingUser.Id, nextFrame, count); nextFrame += count; }); } private void loadSpectatingScreen() { - AddStep("load screen", () => LoadScreen(spectatorScreen = new SoloSpectator(testSpectatorStreamingClient.StreamingUser))); + AddStep("load screen", () => LoadScreen(spectatorScreen = new SoloSpectator(streamingUser))); AddUntilStep("wait for screen load", () => spectatorScreen.LoadState == LoadState.Loaded); } - public class TestSpectatorStreamingClient : SpectatorStreamingClient - { - public readonly User StreamingUser = new User { Id = MultiplayerTestScene.PLAYER_1_ID, Username = "Test user" }; - - public new BindableList PlayingUsers => (BindableList)base.PlayingUsers; - - private int beatmapId; - - public TestSpectatorStreamingClient() - : base(new DevelopmentEndpointConfiguration()) - { - } - - public void StartPlay(int beatmapId) - { - this.beatmapId = beatmapId; - sendState(beatmapId); - } - - public void EndPlay(int beatmapId) - { - ((ISpectatorClient)this).UserFinishedPlaying(StreamingUser.Id, new SpectatorState - { - BeatmapID = beatmapId, - RulesetID = 0, - }); - - sentState = false; - } - - private bool sentState; - - public void SendFrames(int index, int count) - { - var frames = new List(); - - for (int i = index; i < index + count; i++) - { - var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1; - - frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState)); - } - - var bundle = new FrameDataBundle(new ScoreInfo(), frames); - ((ISpectatorClient)this).UserSentFrames(StreamingUser.Id, bundle); - - if (!sentState) - sendState(beatmapId); - } - - public override void WatchUser(int userId) - { - if (!PlayingUsers.Contains(userId) && sentState) - { - // usually the server would do this. - sendState(beatmapId); - } - - base.WatchUser(userId); - } - - private void sendState(int beatmapId) - { - sentState = true; - ((ISpectatorClient)this).UserBeganPlaying(StreamingUser.Id, new SpectatorState - { - BeatmapID = beatmapId, - RulesetID = 0, - }); - } - } - internal class TestUserLookupCache : UserLookupCache { protected override Task ComputeValueAsync(int lookup, CancellationToken token = default) => Task.FromResult(new User diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs index d3a5daeac6..263adc07e1 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorLeaderboard.cs @@ -11,15 +11,12 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Testing; using osu.Framework.Timing; -using osu.Framework.Utils; using osu.Game.Database; -using osu.Game.Online; using osu.Game.Online.Spectator; -using osu.Game.Replays.Legacy; using osu.Game.Rulesets.Osu.Scoring; -using osu.Game.Scoring; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; using osu.Game.Screens.Play.HUD; +using osu.Game.Tests.Visual.Spectator; using osu.Game.Users; namespace osu.Game.Tests.Visual.Multiplayer @@ -149,71 +146,6 @@ namespace osu.Game.Tests.Visual.Multiplayer private void assertCombo(int userId, int expectedCombo) => AddUntilStep($"player {userId} has {expectedCombo} combo", () => this.ChildrenOfType().Single(s => s.User?.Id == userId).Combo.Value == expectedCombo); - private class TestSpectatorStreamingClient : SpectatorStreamingClient - { - private readonly Dictionary userBeatmapDictionary = new Dictionary(); - private readonly Dictionary userSentStateDictionary = new Dictionary(); - - public TestSpectatorStreamingClient() - : base(new DevelopmentEndpointConfiguration()) - { - } - - public void StartPlay(int userId, int beatmapId) - { - userBeatmapDictionary[userId] = beatmapId; - userSentStateDictionary[userId] = false; - sendState(userId, beatmapId); - } - - public void EndPlay(int userId, int beatmapId) - { - ((ISpectatorClient)this).UserFinishedPlaying(userId, new SpectatorState - { - BeatmapID = beatmapId, - RulesetID = 0, - }); - userSentStateDictionary[userId] = false; - } - - public void SendFrames(int userId, int index, int count) - { - var frames = new List(); - - for (int i = index; i < index + count; i++) - { - var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1; - frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState)); - } - - var bundle = new FrameDataBundle(new ScoreInfo { Combo = index + count }, frames); - ((ISpectatorClient)this).UserSentFrames(userId, bundle); - if (!userSentStateDictionary[userId]) - sendState(userId, userBeatmapDictionary[userId]); - } - - public override void WatchUser(int userId) - { - if (userSentStateDictionary[userId]) - { - // usually the server would do this. - sendState(userId, userBeatmapDictionary[userId]); - } - - base.WatchUser(userId); - } - - private void sendState(int userId, int beatmapId) - { - ((ISpectatorClient)this).UserBeganPlaying(userId, new SpectatorState - { - BeatmapID = beatmapId, - RulesetID = 0, - }); - userSentStateDictionary[userId] = true; - } - } - private class TestUserLookupCache : UserLookupCache { protected override Task ComputeValueAsync(int lookup, CancellationToken token = default) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs index db431c0a82..689c249d05 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiSpectatorScreen.cs @@ -9,16 +9,13 @@ using NUnit.Framework; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Testing; -using osu.Framework.Utils; using osu.Game.Beatmaps; using osu.Game.Database; -using osu.Game.Online; using osu.Game.Online.Spectator; -using osu.Game.Replays.Legacy; -using osu.Game.Scoring; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; using osu.Game.Screens.Play; using osu.Game.Tests.Beatmaps.IO; +using osu.Game.Tests.Visual.Spectator; using osu.Game.Users; namespace osu.Game.Tests.Visual.Multiplayer @@ -301,76 +298,6 @@ namespace osu.Game.Tests.Visual.Multiplayer private PlayerArea getInstance(int userId) => spectatorScreen.ChildrenOfType().Single(p => p.UserId == userId); - public class TestSpectatorStreamingClient : SpectatorStreamingClient - { - private readonly Dictionary userBeatmapDictionary = new Dictionary(); - private readonly Dictionary userSentStateDictionary = new Dictionary(); - - public TestSpectatorStreamingClient() - : base(new DevelopmentEndpointConfiguration()) - { - } - - public void StartPlay(int userId, int beatmapId) - { - userBeatmapDictionary[userId] = beatmapId; - userSentStateDictionary[userId] = false; - - sendState(userId, beatmapId); - } - - public void EndPlay(int userId, int beatmapId) - { - ((ISpectatorClient)this).UserFinishedPlaying(userId, new SpectatorState - { - BeatmapID = beatmapId, - RulesetID = 0, - }); - - userSentStateDictionary[userId] = false; - } - - public void SendFrames(int userId, int index, int count) - { - var frames = new List(); - - for (int i = index; i < index + count; i++) - { - var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1; - - frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState)); - } - - var bundle = new FrameDataBundle(new ScoreInfo { Combo = index + count }, frames); - ((ISpectatorClient)this).UserSentFrames(userId, bundle); - - if (!userSentStateDictionary[userId]) - sendState(userId, userBeatmapDictionary[userId]); - } - - public override void WatchUser(int userId) - { - if (!PlayingUsers.Contains(userId) && userSentStateDictionary.TryGetValue(userId, out var sent) && sent) - { - // usually the server would do this. - sendState(userId, userBeatmapDictionary[userId]); - } - - base.WatchUser(userId); - } - - private void sendState(int userId, int beatmapId) - { - ((ISpectatorClient)this).UserBeganPlaying(userId, new SpectatorState - { - BeatmapID = beatmapId, - RulesetID = 0, - }); - - userSentStateDictionary[userId] = true; - } - } - internal class TestUserLookupCache : UserLookupCache { protected override Task ComputeValueAsync(int lookup, CancellationToken token = default) diff --git a/osu.Game.Tests/Visual/Online/TestSceneCurrentlyPlayingDisplay.cs b/osu.Game.Tests/Visual/Online/TestSceneCurrentlyPlayingDisplay.cs index 1baa07f208..8ae6398003 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneCurrentlyPlayingDisplay.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneCurrentlyPlayingDisplay.cs @@ -12,7 +12,7 @@ using osu.Framework.Testing; using osu.Game.Database; using osu.Game.Online.Spectator; using osu.Game.Overlays.Dashboard; -using osu.Game.Tests.Visual.Gameplay; +using osu.Game.Tests.Visual.Spectator; using osu.Game.Users; namespace osu.Game.Tests.Visual.Online @@ -20,7 +20,7 @@ namespace osu.Game.Tests.Visual.Online public class TestSceneCurrentlyPlayingDisplay : OsuTestScene { [Cached(typeof(SpectatorStreamingClient))] - private TestSceneSpectator.TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSceneSpectator.TestSpectatorStreamingClient(); + private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient(); private CurrentlyPlayingDisplay currentlyPlaying; From ad11818868d50e704d98ec28ec12f7ee2a2df9b8 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 12 May 2021 12:19:36 +0900 Subject: [PATCH 84/88] Remove watched users on stop watching --- osu.Game/Online/Spectator/SpectatorStreamingClient.cs | 2 +- .../Visual/Spectator/TestSpectatorStreamingClient.cs | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs index 378096c7fb..691ad5624a 100644 --- a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs +++ b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs @@ -230,7 +230,7 @@ namespace osu.Game.Online.Spectator connection.SendAsync(nameof(ISpectatorServer.StartWatchingUser), userId); } - public void StopWatchingUser(int userId) + public virtual void StopWatchingUser(int userId) { lock (userLock) { diff --git a/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs index c177d3f399..806d9b45ab 100644 --- a/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs +++ b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs @@ -38,7 +38,8 @@ namespace osu.Game.Tests.Visual.Spectator RulesetID = 0, }); - userSentStateDictionary[userId] = false; + userBeatmapDictionary.Remove(userId); + userSentStateDictionary.Remove(userId); } public void SendFrames(int userId, int index, int count) @@ -66,10 +67,15 @@ namespace osu.Game.Tests.Visual.Spectator sendState(userId, userBeatmapDictionary[userId]); base.WatchUser(userId); - watchingUsers.Add(userId); } + public override void StopWatchingUser(int userId) + { + base.StopWatchingUser(userId); + watchingUsers.Remove(userId); + } + private void sendState(int userId, int beatmapId) { ((ISpectatorClient)this).UserBeganPlaying(userId, new SpectatorState From e0e8f5ab80700db4113b5df45a7d91046cbe57ad Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 12 May 2021 13:06:28 +0900 Subject: [PATCH 85/88] Fix ordering + threading issues --- .../Spectator/TestSpectatorStreamingClient.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs index 806d9b45ab..c2428ce415 100644 --- a/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs +++ b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Collections.Concurrent; using System.Collections.Generic; using osu.Framework.Bindables; using osu.Framework.Utils; @@ -14,7 +15,7 @@ namespace osu.Game.Tests.Visual.Spectator public class TestSpectatorStreamingClient : SpectatorStreamingClient { public new BindableList PlayingUsers => (BindableList)base.PlayingUsers; - private readonly HashSet watchingUsers = new HashSet(); + private readonly ConcurrentDictionary watchingUsers = new ConcurrentDictionary(); private readonly Dictionary userBeatmapDictionary = new Dictionary(); private readonly Dictionary userSentStateDictionary = new Dictionary(); @@ -62,18 +63,17 @@ namespace osu.Game.Tests.Visual.Spectator public override void WatchUser(int userId) { - // When newly watching a user, the server sends the playing state immediately. - if (!watchingUsers.Contains(userId) && PlayingUsers.Contains(userId)) - sendState(userId, userBeatmapDictionary[userId]); - base.WatchUser(userId); - watchingUsers.Add(userId); + + // When newly watching a user, the server sends the playing state immediately. + if (watchingUsers.TryAdd(userId, 0) && PlayingUsers.Contains(userId)) + sendState(userId, userBeatmapDictionary[userId]); } public override void StopWatchingUser(int userId) { base.StopWatchingUser(userId); - watchingUsers.Remove(userId); + watchingUsers.TryRemove(userId, out _); } private void sendState(int userId, int beatmapId) From f4c96b26750a9c1556a7c62b5f60e95b1a95e476 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 12 May 2021 13:10:59 +0900 Subject: [PATCH 86/88] Only update playing user states when users are watched --- osu.Game/Online/Spectator/SpectatorStreamingClient.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs index 691ad5624a..ec6d1bf9d8 100644 --- a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs +++ b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs @@ -142,7 +142,11 @@ namespace osu.Game.Online.Spectator if (!playingUsers.Contains(userId)) playingUsers.Add(userId); - playingUserStates[userId] = state; + // UserBeganPlaying() is called by the server regardless of whether the local user is watching the remote user, and is called a further time when the remote user is watched. + // This may be a temporary thing (see: https://github.com/ppy/osu-server-spectator/blob/2273778e02cfdb4a9c6a934f2a46a8459cb5d29c/osu.Server.Spectator/Hubs/SpectatorHub.cs#L28-L29). + // We don't want the user states to update unless the player is being watched, otherwise calling BindUserBeganPlaying() can lead to double invocations. + if (watchingUsers.Contains(userId)) + playingUserStates[userId] = state; } OnUserBeganPlaying?.Invoke(userId, state); From 273cd18b8aac5d2b8593dac1e48bdd609957e7e1 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 12 May 2021 14:19:36 +0900 Subject: [PATCH 87/88] Use test streaming client in gameplay leaderboard test --- ...TestSceneMultiplayerGameplayLeaderboard.cs | 32 +++---------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerGameplayLeaderboard.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerGameplayLeaderboard.cs index b6c06bb149..6813a6e7dd 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerGameplayLeaderboard.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerGameplayLeaderboard.cs @@ -6,14 +6,12 @@ using System.Collections.Generic; using System.Linq; 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.Configuration; using osu.Game.Database; -using osu.Game.Online; using osu.Game.Online.API; using osu.Game.Online.Spectator; using osu.Game.Replays.Legacy; @@ -22,6 +20,7 @@ using osu.Game.Rulesets.Scoring; using osu.Game.Scoring; using osu.Game.Screens.Play.HUD; using osu.Game.Tests.Visual.Online; +using osu.Game.Tests.Visual.Spectator; namespace osu.Game.Tests.Visual.Multiplayer { @@ -30,7 +29,7 @@ namespace osu.Game.Tests.Visual.Multiplayer private const int users = 16; [Cached(typeof(SpectatorStreamingClient))] - private TestMultiplayerStreaming streamingClient = new TestMultiplayerStreaming(users); + private TestMultiplayerStreaming streamingClient = new TestMultiplayerStreaming(); [Cached(typeof(UserLookupCache))] private UserLookupCache lookupCache = new TestSceneCurrentlyPlayingDisplay.TestUserLookupCache(); @@ -71,7 +70,8 @@ namespace osu.Game.Tests.Visual.Multiplayer var playable = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); - streamingClient.Start(Beatmap.Value.BeatmapInfo.OnlineBeatmapID ?? 0); + for (int i = 0; i < users; i++) + streamingClient.StartPlay(i, Beatmap.Value.BeatmapInfo.OnlineBeatmapID ?? 0); Client.CurrentMatchPlayingUserIds.Clear(); Client.CurrentMatchPlayingUserIds.AddRange(streamingClient.PlayingUsers); @@ -114,30 +114,8 @@ namespace osu.Game.Tests.Visual.Multiplayer AddStep("change to standardised", () => config.SetValue(OsuSetting.ScoreDisplayMode, ScoringMode.Standardised)); } - public class TestMultiplayerStreaming : SpectatorStreamingClient + public class TestMultiplayerStreaming : TestSpectatorStreamingClient { - public new BindableList PlayingUsers => (BindableList)base.PlayingUsers; - - private readonly int totalUsers; - - public TestMultiplayerStreaming(int totalUsers) - : base(new DevelopmentEndpointConfiguration()) - { - 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 lastHeaders = new Dictionary(); public void RandomlyUpdateState() From e1dacde31456e2366226c64398b0dab1a116d3ab Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 12 May 2021 14:22:15 +0900 Subject: [PATCH 88/88] Add combo to test streaming client --- osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs index c2428ce415..cc8437479d 100644 --- a/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs +++ b/osu.Game/Tests/Visual/Spectator/TestSpectatorStreamingClient.cs @@ -54,7 +54,7 @@ namespace osu.Game.Tests.Visual.Spectator frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState)); } - var bundle = new FrameDataBundle(new ScoreInfo(), frames); + var bundle = new FrameDataBundle(new ScoreInfo { Combo = index + count }, frames); ((ISpectatorClient)this).UserSentFrames(userId, bundle); if (!userSentStateDictionary[userId])