1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-27 18:32:56 +08:00

Move spectator begin/end playing to SubmittingPlayer

This commit is contained in:
Dan Balasescu 2022-07-28 20:44:02 +09:00
parent 1da9830145
commit ce694123eb
4 changed files with 37 additions and 52 deletions

View File

@ -27,7 +27,6 @@ using osu.Game.Rulesets.Replays;
using osu.Game.Rulesets.Replays.Types; using osu.Game.Rulesets.Replays.Types;
using osu.Game.Rulesets.UI; using osu.Game.Rulesets.UI;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Screens.Play;
using osu.Game.Tests.Gameplay; using osu.Game.Tests.Gameplay;
using osu.Game.Tests.Mods; using osu.Game.Tests.Mods;
using osu.Game.Tests.Visual.Spectator; using osu.Game.Tests.Visual.Spectator;
@ -41,16 +40,12 @@ namespace osu.Game.Tests.Visual.Gameplay
private TestRulesetInputManager playbackManager; private TestRulesetInputManager playbackManager;
private TestRulesetInputManager recordingManager; private TestRulesetInputManager recordingManager;
private Replay replay; private Score recordingScore;
private Replay playbackReplay;
private TestSpectatorClient spectatorClient; private TestSpectatorClient spectatorClient;
private ManualClock manualClock; private ManualClock manualClock;
private TestReplayRecorder recorder; private TestReplayRecorder recorder;
private OsuSpriteText latencyDisplay; private OsuSpriteText latencyDisplay;
private TestFramedReplayInputHandler replayHandler; private TestFramedReplayInputHandler replayHandler;
[SetUpSteps] [SetUpSteps]
@ -58,7 +53,16 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
AddStep("Setup containers", () => AddStep("Setup containers", () =>
{ {
replay = new Replay(); recordingScore = new Score
{
ScoreInfo =
{
BeatmapInfo = new BeatmapInfo(),
Ruleset = new OsuRuleset().RulesetInfo,
}
};
playbackReplay = new Replay();
manualClock = new ManualClock(); manualClock = new ManualClock();
Child = new DependencyProvidingContainer Child = new DependencyProvidingContainer
@ -67,7 +71,6 @@ namespace osu.Game.Tests.Visual.Gameplay
CachedDependencies = new[] CachedDependencies = new[]
{ {
(typeof(SpectatorClient), (object)(spectatorClient = new TestSpectatorClient())), (typeof(SpectatorClient), (object)(spectatorClient = new TestSpectatorClient())),
(typeof(GameplayState), TestGameplayState.Create(new OsuRuleset()))
}, },
Children = new Drawable[] Children = new Drawable[]
{ {
@ -81,7 +84,7 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
recordingManager = new TestRulesetInputManager(TestCustomisableModRuleset.CreateTestRulesetInfo(), 0, SimultaneousBindingMode.Unique) recordingManager = new TestRulesetInputManager(TestCustomisableModRuleset.CreateTestRulesetInfo(), 0, SimultaneousBindingMode.Unique)
{ {
Recorder = recorder = new TestReplayRecorder Recorder = recorder = new TestReplayRecorder(recordingScore)
{ {
ScreenSpaceToGamefield = pos => recordingManager.ToLocalSpace(pos), ScreenSpaceToGamefield = pos => recordingManager.ToLocalSpace(pos),
}, },
@ -112,7 +115,7 @@ namespace osu.Game.Tests.Visual.Gameplay
playbackManager = new TestRulesetInputManager(TestCustomisableModRuleset.CreateTestRulesetInfo(), 0, SimultaneousBindingMode.Unique) playbackManager = new TestRulesetInputManager(TestCustomisableModRuleset.CreateTestRulesetInfo(), 0, SimultaneousBindingMode.Unique)
{ {
Clock = new FramedClock(manualClock), Clock = new FramedClock(manualClock),
ReplayInputHandler = replayHandler = new TestFramedReplayInputHandler(replay) ReplayInputHandler = replayHandler = new TestFramedReplayInputHandler(playbackReplay)
{ {
GamefieldToScreenSpace = pos => playbackManager.ToScreenSpace(pos), GamefieldToScreenSpace = pos => playbackManager.ToScreenSpace(pos),
}, },
@ -144,6 +147,7 @@ namespace osu.Game.Tests.Visual.Gameplay
} }
}; };
spectatorClient.BeginPlaying(TestGameplayState.Create(new OsuRuleset()), recordingScore);
spectatorClient.OnNewFrames += onNewFrames; spectatorClient.OnNewFrames += onNewFrames;
}); });
} }
@ -151,15 +155,15 @@ namespace osu.Game.Tests.Visual.Gameplay
[Test] [Test]
public void TestBasic() public void TestBasic()
{ {
AddUntilStep("received frames", () => replay.Frames.Count > 50); AddUntilStep("received frames", () => playbackReplay.Frames.Count > 50);
AddStep("stop sending frames", () => recorder.Expire()); AddStep("stop sending frames", () => recorder.Expire());
AddUntilStep("wait for all frames received", () => replay.Frames.Count == recorder.SentFrames.Count); AddUntilStep("wait for all frames received", () => playbackReplay.Frames.Count == recorder.SentFrames.Count);
} }
[Test] [Test]
public void TestWithSendFailure() public void TestWithSendFailure()
{ {
AddUntilStep("received frames", () => replay.Frames.Count > 50); AddUntilStep("received frames", () => playbackReplay.Frames.Count > 50);
int framesReceivedSoFar = 0; int framesReceivedSoFar = 0;
int frameSendAttemptsSoFar = 0; int frameSendAttemptsSoFar = 0;
@ -172,21 +176,21 @@ namespace osu.Game.Tests.Visual.Gameplay
AddUntilStep("wait for next send attempt", () => AddUntilStep("wait for next send attempt", () =>
{ {
framesReceivedSoFar = replay.Frames.Count; framesReceivedSoFar = playbackReplay.Frames.Count;
return spectatorClient.FrameSendAttempts > frameSendAttemptsSoFar + 1; return spectatorClient.FrameSendAttempts > frameSendAttemptsSoFar + 1;
}); });
AddUntilStep("wait for more send attempts", () => spectatorClient.FrameSendAttempts > frameSendAttemptsSoFar + 10); AddUntilStep("wait for more send attempts", () => spectatorClient.FrameSendAttempts > frameSendAttemptsSoFar + 10);
AddAssert("frames did not increase", () => framesReceivedSoFar == replay.Frames.Count); AddAssert("frames did not increase", () => framesReceivedSoFar == playbackReplay.Frames.Count);
AddStep("stop failing sends", () => spectatorClient.ShouldFailSendingFrames = false); AddStep("stop failing sends", () => spectatorClient.ShouldFailSendingFrames = false);
AddUntilStep("wait for next frames", () => framesReceivedSoFar < replay.Frames.Count); AddUntilStep("wait for next frames", () => framesReceivedSoFar < playbackReplay.Frames.Count);
AddStep("stop sending frames", () => recorder.Expire()); AddStep("stop sending frames", () => recorder.Expire());
AddUntilStep("wait for all frames received", () => replay.Frames.Count == recorder.SentFrames.Count); AddUntilStep("wait for all frames received", () => playbackReplay.Frames.Count == recorder.SentFrames.Count);
AddAssert("ensure frames were received in the correct sequence", () => replay.Frames.Select(f => f.Time).SequenceEqual(recorder.SentFrames.Select(f => f.Time))); AddAssert("ensure frames were received in the correct sequence", () => playbackReplay.Frames.Select(f => f.Time).SequenceEqual(recorder.SentFrames.Select(f => f.Time)));
} }
private void onNewFrames(int userId, FrameDataBundle frames) private void onNewFrames(int userId, FrameDataBundle frames)
@ -195,10 +199,10 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
var frame = new TestReplayFrame(); var frame = new TestReplayFrame();
frame.FromLegacy(legacyFrame, null); frame.FromLegacy(legacyFrame, null);
replay.Frames.Add(frame); playbackReplay.Frames.Add(frame);
} }
Logger.Log($"Received {frames.Frames.Count} new frames (total {replay.Frames.Count} of {recorder.SentFrames.Count})"); Logger.Log($"Received {frames.Frames.Count} new frames (total {playbackReplay.Frames.Count} of {recorder.SentFrames.Count})");
} }
private double latency = SpectatorClient.TIME_BETWEEN_SENDS; private double latency = SpectatorClient.TIME_BETWEEN_SENDS;
@ -219,7 +223,7 @@ namespace osu.Game.Tests.Visual.Gameplay
if (!replayHandler.HasFrames) if (!replayHandler.HasFrames)
return; return;
var lastFrame = replay.Frames.LastOrDefault(); var lastFrame = playbackReplay.Frames.LastOrDefault();
// this isn't perfect as we basically can't be aware of the rate-of-send here (the streamer is not sending data when not being moved). // this isn't perfect as we basically can't be aware of the rate-of-send here (the streamer is not sending data when not being moved).
// in gameplay playback, the case where NextFrame is null would pause gameplay and handle this correctly; it's strictly a test limitation / best effort implementation. // in gameplay playback, the case where NextFrame is null would pause gameplay and handle this correctly; it's strictly a test limitation / best effort implementation.
@ -360,15 +364,8 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
public List<ReplayFrame> SentFrames = new List<ReplayFrame>(); public List<ReplayFrame> SentFrames = new List<ReplayFrame>();
public TestReplayRecorder() public TestReplayRecorder(Score score)
: base(new Score : base(score)
{
ScoreInfo =
{
BeatmapInfo = new BeatmapInfo(),
Ruleset = new OsuRuleset().RulesetInfo,
}
})
{ {
} }

View File

@ -14,7 +14,6 @@ using osu.Framework.Input.Events;
using osu.Game.Online.Spectator; using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Replays; using osu.Game.Rulesets.Replays;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Screens.Play;
using osuTK; using osuTK;
namespace osu.Game.Rulesets.UI namespace osu.Game.Rulesets.UI
@ -33,9 +32,6 @@ namespace osu.Game.Rulesets.UI
[Resolved] [Resolved]
private SpectatorClient spectatorClient { get; set; } private SpectatorClient spectatorClient { get; set; }
[Resolved]
private GameplayState gameplayState { get; set; }
protected ReplayRecorder(Score target) protected ReplayRecorder(Score target)
{ {
this.target = target; this.target = target;
@ -48,15 +44,7 @@ namespace osu.Game.Rulesets.UI
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
inputManager = GetContainingInputManager(); inputManager = GetContainingInputManager();
spectatorClient.BeginPlaying(gameplayState, target);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
spectatorClient?.EndPlaying(gameplayState);
} }
protected override void Update() protected override void Update()

View File

@ -26,7 +26,6 @@ using osu.Game.Extensions;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.IO.Archives; using osu.Game.IO.Archives;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.Spectator;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Mods;
@ -101,9 +100,6 @@ namespace osu.Game.Screens.Play
[Resolved] [Resolved]
private MusicController musicController { get; set; } private MusicController musicController { get; set; }
[Resolved]
private SpectatorClient spectatorClient { get; set; }
public GameplayState GameplayState { get; private set; } public GameplayState GameplayState { get; private set; }
private Ruleset ruleset; private Ruleset ruleset;
@ -1030,11 +1026,6 @@ namespace osu.Game.Screens.Play
// if arriving here and the results screen preparation task hasn't run, it's safe to say the user has not completed the beatmap. // if arriving here and the results screen preparation task hasn't run, it's safe to say the user has not completed the beatmap.
if (prepareScoreForDisplayTask == null) if (prepareScoreForDisplayTask == null)
ScoreProcessor.FailScore(Score.ScoreInfo); ScoreProcessor.FailScore(Score.ScoreInfo);
// EndPlaying() is typically called from ReplayRecorder.Dispose(). Disposal is currently asynchronous.
// To resolve test failures, forcefully end playing synchronously when this screen exits.
// Todo: Replace this with a more permanent solution once osu-framework has a synchronous cleanup method.
spectatorClient.EndPlaying(GameplayState);
} }
// GameplayClockContainer performs seeks / start / stop operations on the beatmap's track. // GameplayClockContainer performs seeks / start / stop operations on the beatmap's track.

View File

@ -15,6 +15,7 @@ using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Scoring; using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring; using osu.Game.Scoring;
@ -33,6 +34,9 @@ namespace osu.Game.Screens.Play
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private IAPIProvider api { get; set; }
[Resolved]
private SpectatorClient spectatorClient { get; set; }
private TaskCompletionSource<bool> scoreSubmissionSource; private TaskCompletionSource<bool> scoreSubmissionSource;
protected SubmittingPlayer(PlayerConfiguration configuration = null) protected SubmittingPlayer(PlayerConfiguration configuration = null)
@ -134,6 +138,8 @@ namespace osu.Game.Screens.Play
if (realmBeatmap != null) if (realmBeatmap != null)
realmBeatmap.LastPlayed = DateTimeOffset.Now; realmBeatmap.LastPlayed = DateTimeOffset.Now;
}); });
spectatorClient.BeginPlaying(GameplayState, Score);
} }
public override bool OnExiting(ScreenExitEvent e) public override bool OnExiting(ScreenExitEvent e)
@ -141,7 +147,10 @@ namespace osu.Game.Screens.Play
bool exiting = base.OnExiting(e); bool exiting = base.OnExiting(e);
if (LoadedBeatmapSuccessfully) if (LoadedBeatmapSuccessfully)
{
submitScore(Score.DeepClone()); submitScore(Score.DeepClone());
spectatorClient.EndPlaying(GameplayState);
}
return exiting; return exiting;
} }