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.
|
|
|
|
|
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;
|
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
|
|
|
|
{
|
2020-10-28 17:40:06 +08:00
|
|
|
public class SpectatorPlayer : Player
|
2020-10-27 17:56:28 +08:00
|
|
|
{
|
2021-05-31 18:22:20 +08:00
|
|
|
[Resolved]
|
|
|
|
private SpectatorClient spectatorClient { get; set; }
|
|
|
|
|
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-16 15:16:02 +08:00
|
|
|
public SpectatorPlayer(Score score, PlayerConfiguration configuration = null)
|
|
|
|
: 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()
|
|
|
|
{
|
2021-05-20 14:55:07 +08:00
|
|
|
spectatorClient.OnUserBeganPlaying += userBeganPlaying;
|
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
|
|
|
}
|
|
|
|
|
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-05-31 18:24:22 +08:00
|
|
|
spectatorClient.OnNewFrames += userSentFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void userSentFrames(int userId, FrameDataBundle bundle)
|
|
|
|
{
|
|
|
|
if (userId != score.ScoreInfo.User.Id)
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
IConvertibleReplayFrame convertibleFrame = GameplayRuleset.CreateConvertibleReplayFrame();
|
|
|
|
convertibleFrame.FromLegacy(frame, GameplayBeatmap.PlayableBeatmap);
|
|
|
|
|
|
|
|
var convertedFrame = (ReplayFrame)convertibleFrame;
|
|
|
|
convertedFrame.Time = frame.Time;
|
|
|
|
|
|
|
|
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)
|
|
|
|
NonFrameStableSeek(score.Replay.Frames[0].Time);
|
2021-05-31 18:24:22 +08:00
|
|
|
}
|
|
|
|
|
2021-06-03 16:42:01 +08:00
|
|
|
protected override Score CreateScore() => score;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:03:28 +08:00
|
|
|
public override bool OnExiting(IScreen next)
|
|
|
|
{
|
2021-05-20 14:55:07 +08:00
|
|
|
spectatorClient.OnUserBeganPlaying -= userBeganPlaying;
|
2021-05-31 18:24:22 +08:00
|
|
|
spectatorClient.OnNewFrames -= userSentFrames;
|
|
|
|
|
2020-11-01 23:03:28 +08:00
|
|
|
return base.OnExiting(next);
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:42:04 +08:00
|
|
|
private void userBeganPlaying(int userId, SpectatorState state)
|
|
|
|
{
|
2020-11-02 14:48:21 +08:00
|
|
|
if (userId != score.ScoreInfo.UserID) return;
|
|
|
|
|
|
|
|
Schedule(() =>
|
2020-10-28 17:47:15 +08:00
|
|
|
{
|
2020-11-02 14:48:21 +08:00
|
|
|
if (this.IsCurrentScreen()) this.Exit();
|
|
|
|
});
|
2020-10-28 16:42:04 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 17:05:15 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2021-05-20 14:55:07 +08:00
|
|
|
if (spectatorClient != null)
|
2021-05-31 18:24:22 +08:00
|
|
|
{
|
2021-05-20 14:55:07 +08:00
|
|
|
spectatorClient.OnUserBeganPlaying -= userBeganPlaying;
|
2021-05-31 18:24:22 +08:00
|
|
|
spectatorClient.OnNewFrames -= userSentFrames;
|
|
|
|
}
|
2020-10-28 17:05:15 +08:00
|
|
|
}
|
2020-10-27 17:56:28 +08:00
|
|
|
}
|
|
|
|
}
|