2020-10-27 17:56:28 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2022-07-12 09:41:19 +08:00
|
|
|
using System.Diagnostics;
|
2020-10-28 16:42:04 +08:00
|
|
|
using osu.Framework.Allocation;
|
2020-10-28 18:39:38 +08:00
|
|
|
using osu.Framework.Graphics;
|
2020-10-28 16:42:04 +08:00
|
|
|
using osu.Framework.Screens;
|
2021-10-05 13:48:10 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2020-10-28 18:39:38 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2020-10-28 16:42:04 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
2021-05-31 18:24:22 +08:00
|
|
|
using osu.Game.Rulesets.Replays;
|
|
|
|
using osu.Game.Rulesets.Replays.Types;
|
2020-10-27 17:56:28 +08:00
|
|
|
using osu.Game.Scoring;
|
2020-10-28 17:05:15 +08:00
|
|
|
using osu.Game.Screens.Ranking;
|
2020-10-27 17:56:28 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
2021-08-27 18:16:08 +08:00
|
|
|
public abstract class SpectatorPlayer : Player
|
2020-10-27 17:56:28 +08:00
|
|
|
{
|
2021-05-31 18:22:20 +08:00
|
|
|
[Resolved]
|
2021-08-27 18:16:08 +08:00
|
|
|
protected SpectatorClient SpectatorClient { get; private set; }
|
2021-05-31 18:22:20 +08:00
|
|
|
|
2020-10-28 17:40:06 +08:00
|
|
|
private readonly Score score;
|
|
|
|
|
|
|
|
protected override bool CheckModsAllowFailure() => false; // todo: better support starting mid-way through beatmap
|
|
|
|
|
2021-08-27 18:16:08 +08:00
|
|
|
protected SpectatorPlayer(Score score, PlayerConfiguration configuration = null)
|
2021-08-16 15:16:02 +08:00
|
|
|
: base(configuration)
|
2020-10-27 17:56:28 +08:00
|
|
|
{
|
2020-10-28 17:40:06 +08:00
|
|
|
this.score = score;
|
2020-10-27 17:56:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 16:42:04 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2020-10-28 18:39:38 +08:00
|
|
|
AddInternal(new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = $"Watching {score.ScoreInfo.User.Username} playing live!",
|
|
|
|
Font = OsuFont.Default.With(size: 30),
|
|
|
|
Y = 100,
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
});
|
2020-10-28 16:42:04 +08:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:23:41 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
DrawableRuleset.FrameStableClock.WaitingOnFrames.BindValueChanged(waiting =>
|
|
|
|
{
|
|
|
|
if (GameplayClockContainer is MasterGameplayClockContainer master)
|
|
|
|
{
|
|
|
|
if (master.UserPlaybackRate.Value > 1 && waiting.NewValue)
|
|
|
|
master.UserPlaybackRate.Value = 1;
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2021-06-03 16:38:51 +08:00
|
|
|
protected override void StartGameplay()
|
2021-05-31 18:24:22 +08:00
|
|
|
{
|
2021-06-03 16:38:51 +08:00
|
|
|
base.StartGameplay();
|
2021-05-31 18:24:22 +08:00
|
|
|
|
2021-06-10 20:38:20 +08:00
|
|
|
// Start gameplay along with the very first arrival frame (the latest one).
|
|
|
|
score.Replay.Frames.Clear();
|
2021-08-27 18:16:08 +08:00
|
|
|
SpectatorClient.OnNewFrames += userSentFrames;
|
2021-05-31 18:24:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void userSentFrames(int userId, FrameDataBundle bundle)
|
|
|
|
{
|
2021-12-10 14:28:41 +08:00
|
|
|
if (userId != score.ScoreInfo.User.OnlineID)
|
2021-05-31 18:24:22 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!LoadedBeatmapSuccessfully)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!this.IsCurrentScreen())
|
|
|
|
return;
|
|
|
|
|
2021-06-10 20:39:13 +08:00
|
|
|
bool isFirstBundle = score.Replay.Frames.Count == 0;
|
|
|
|
|
2021-05-31 18:24:22 +08:00
|
|
|
foreach (var frame in bundle.Frames)
|
|
|
|
{
|
2022-09-10 11:07:23 +08:00
|
|
|
IConvertibleReplayFrame convertibleFrame = GameplayState.Ruleset.CreateConvertibleReplayFrame();
|
|
|
|
Debug.Assert(convertibleFrame != null);
|
2021-10-02 01:22:23 +08:00
|
|
|
convertibleFrame.FromLegacy(frame, GameplayState.Beatmap);
|
2021-05-31 18:24:22 +08:00
|
|
|
|
|
|
|
var convertedFrame = (ReplayFrame)convertibleFrame;
|
|
|
|
convertedFrame.Time = frame.Time;
|
2022-01-31 17:32:17 +08:00
|
|
|
convertedFrame.Header = frame.Header;
|
2021-05-31 18:24:22 +08:00
|
|
|
|
|
|
|
score.Replay.Frames.Add(convertedFrame);
|
|
|
|
}
|
2021-06-03 16:27:21 +08:00
|
|
|
|
2021-06-10 20:39:13 +08:00
|
|
|
if (isFirstBundle && score.Replay.Frames.Count > 0)
|
2022-03-17 19:54:42 +08:00
|
|
|
SetGameplayStartTime(score.Replay.Frames[0].Time);
|
2021-05-31 18:24:22 +08:00
|
|
|
}
|
|
|
|
|
2021-10-05 13:48:10 +08:00
|
|
|
protected override Score CreateScore(IBeatmap beatmap) => score;
|
2021-06-03 16:42:01 +08:00
|
|
|
|
2021-05-31 18:22:20 +08:00
|
|
|
protected override ResultsScreen CreateResults(ScoreInfo score)
|
2021-06-03 16:42:01 +08:00
|
|
|
=> new SpectatorResultsScreen(score);
|
2021-05-31 18:22:20 +08:00
|
|
|
|
2020-10-28 17:40:06 +08:00
|
|
|
protected override void PrepareReplay()
|
|
|
|
{
|
|
|
|
DrawableRuleset?.SetReplayScore(score);
|
|
|
|
}
|
|
|
|
|
2022-04-21 23:52:44 +08:00
|
|
|
public override bool OnExiting(ScreenExitEvent e)
|
2020-11-01 23:03:28 +08:00
|
|
|
{
|
2021-08-27 18:16:08 +08:00
|
|
|
SpectatorClient.OnNewFrames -= userSentFrames;
|
2021-05-31 18:24:22 +08:00
|
|
|
|
2022-04-21 23:52:44 +08:00
|
|
|
return base.OnExiting(e);
|
2020-11-01 23:03:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 17:05:15 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2021-08-27 18:16:08 +08:00
|
|
|
if (SpectatorClient != null)
|
|
|
|
SpectatorClient.OnNewFrames -= userSentFrames;
|
2020-10-28 17:05:15 +08:00
|
|
|
}
|
2020-10-27 17:56:28 +08:00
|
|
|
}
|
|
|
|
}
|