1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-21 23:10:51 +08:00
Files
osu-lazer/osu.Game/Screens/Play/SpectatorPlayer.cs
T
Bartłomiej Dach 6416f59c7b Disallow placing gameplay leaderboard in skins outside player
Closes https://github.com/ppy/osu/issues/33542.

For a diff this simple this took much more hemming and hawing because
things are a bit annoying here from a few angles:

- The only way that is considered idiomatic right now for a skin
  component to not be applicable to a screen is to require a dependency
  from DI that is only provided by applicable screens.
  `DrawableGameplayLeaderboard` has a few of those dependencies, but the
  scope of all the usages makes it so that the only really viable one to
  use here is `IGameplayLeaderboardProvider` itself (see: visual tests,
  and also the usage of multiplayer spectator, where the leaderboard is
  *not* under a player instance).

- The smelly part of this is that the `Player` inheritance hierarchy
  must ensure that *every* non-abstract class has an
  `IGameplayLeaderboardProvider` cached. It is not trivial - if not
  straight up impossible - to force this via some `Player` level
  abstract method, because such a method would need to somehow
  accommodate all possible leaderboard providers. That however also
  means that every possible future `Player` implementor *must inherently
  know* to also cache a leaderboard provider lest it die at runtime. I
  don't love that, but I also don't see better alternatives.

- Speaking of which, I also noticed that solo spectator and playlists
  don't have gameplay leaderboards. At all. Which I don't believe to be
  something that I broke with the leaderboard work - I'm pretty sure
  that was the pre-existing state - however I don't see any reason why
  they *couldn't* receive gameplay leaderboards. I'm not doing that
  here, though, just leaving TODOs for later.
2025-06-09 13:11:29 +02:00

145 lines
4.7 KiB
C#

// 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;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Screens;
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;
using osu.Game.Screens.Select.Leaderboards;
namespace osu.Game.Screens.Play
{
public abstract partial class SpectatorPlayer : Player
{
// TODO: maybe consider giving this proper scores.
// `SoloGameplayLeaderboardProvider` doesn't immediately work because there's no guarantee that `LeaderboardManager` global state matches the currently spectated beatmap.
[Cached(typeof(IGameplayLeaderboardProvider))]
private readonly EmptyGameplayLeaderboardProvider leaderboardProvider = new EmptyGameplayLeaderboardProvider();
[Resolved]
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;
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,
});
}
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);
}
/// <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;
protected override void StartGameplay()
{
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;
bool isFirstBundle = score.Replay.Frames.Count == 0;
foreach (var frame in bundle.Frames)
{
IConvertibleReplayFrame convertibleFrame = GameplayState.Ruleset.CreateConvertibleReplayFrame()!;
convertibleFrame.FromLegacy(frame, GameplayState.Beatmap);
var convertedFrame = (ReplayFrame)convertibleFrame;
convertedFrame.Time = frame.Time;
convertedFrame.Header = frame.Header;
score.Replay.Frames.Add(convertedFrame);
}
if (isFirstBundle && score.Replay.Frames.Count > 0)
SetGameplayStartTime(score.Replay.Frames[0].Time);
}
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);
if (SpectatorClient.IsNotNull())
SpectatorClient.OnNewFrames -= userSentFrames;
}
}
}