1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-18 01:23:05 +08:00
osu-lazer/osu.Game/Screens/Play/SpectatorPlayer.cs

139 lines
4.3 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
2024-02-29 03:03:09 +08:00
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Screens;
2021-10-05 13:48:10 +08:00
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Replays;
using osu.Game.Rulesets.Replays.Types;
using osu.Game.Scoring;
using osu.Game.Screens.Ranking;
namespace osu.Game.Screens.Play
{
2022-11-24 13:32:20 +08:00
public abstract partial class SpectatorPlayer : Player
{
[Resolved]
2024-02-28 15:24:04 +08:00
protected SpectatorClient SpectatorClient { get; private set; } = null!;
private readonly Score score;
protected override bool CheckModsAllowFailure()
{
if (!allowFail)
return false;
return base.CheckModsAllowFailure();
}
private bool allowFail;
2024-02-28 15:24:04 +08:00
protected SpectatorPlayer(Score score, PlayerConfiguration? configuration = null)
: base(configuration)
{
this.score = score;
}
[BackgroundDependencyLoader]
private void load()
{
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,
});
}
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);
}
2023-11-23 09:01:59 +08:00
/// <summary>
/// Should be called when it is apparent that the player being spectated has failed.
/// This will subsequently stop blocking the fail screen from displaying (usually done out of safety).
/// </summary>
public void AllowFail() => allowFail = true;
2021-06-03 16:38:51 +08:00
protected override void StartGameplay()
{
2021-06-03 16:38:51 +08:00
base.StartGameplay();
// Start gameplay along with the very first arrival frame (the latest one).
score.Replay.Frames.Clear();
SpectatorClient.OnNewFrames += userSentFrames;
}
private void userSentFrames(int userId, FrameDataBundle bundle)
{
if (userId != score.ScoreInfo.User.OnlineID)
return;
if (!LoadedBeatmapSuccessfully)
return;
if (!this.IsCurrentScreen())
return;
2021-06-10 20:39:13 +08:00
bool isFirstBundle = score.Replay.Frames.Count == 0;
foreach (var frame in bundle.Frames)
{
2024-02-28 15:24:04 +08:00
IConvertibleReplayFrame convertibleFrame = GameplayState.Ruleset.CreateConvertibleReplayFrame()!;
2021-10-02 01:22:23 +08:00
convertibleFrame.FromLegacy(frame, GameplayState.Beatmap);
var convertedFrame = (ReplayFrame)convertibleFrame;
convertedFrame.Time = frame.Time;
convertedFrame.Header = frame.Header;
score.Replay.Frames.Add(convertedFrame);
}
2021-06-10 20:39:13 +08:00
if (isFirstBundle && score.Replay.Frames.Count > 0)
SetGameplayStartTime(score.Replay.Frames[0].Time);
}
2021-10-05 13:48:10 +08:00
protected override Score CreateScore(IBeatmap beatmap) => score;
protected override ResultsScreen CreateResults(ScoreInfo score)
=> new SpectatorResultsScreen(score);
protected override void PrepareReplay()
{
DrawableRuleset?.SetReplayScore(score);
}
public override bool OnExiting(ScreenExitEvent e)
{
SpectatorClient.OnNewFrames -= userSentFrames;
return base.OnExiting(e);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2024-02-29 03:03:09 +08:00
if (SpectatorClient.IsNotNull())
SpectatorClient.OnNewFrames -= userSentFrames;
}
}
}