1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-20 02:39:53 +08:00
Files
osu-lazer/osu.Game/Screens/Edit/GameplayTest/EditorPlayer.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

246 lines
9.1 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 System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Input.Bindings;
using osu.Game.Overlays;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Scoring;
using osu.Game.Screens.Play;
using osu.Game.Screens.Ranking;
using osu.Game.Screens.Select.Leaderboards;
using osu.Game.Users;
namespace osu.Game.Screens.Edit.GameplayTest
{
public partial class EditorPlayer : Player, IKeyBindingHandler<GlobalAction>
{
private readonly Editor editor;
private readonly EditorState editorState;
protected override UserActivity InitialActivity => new UserActivity.TestingBeatmap(Beatmap.Value.BeatmapInfo);
[Resolved]
private MusicController musicController { get; set; } = null!;
[Cached(typeof(IGameplayLeaderboardProvider))]
private EmptyGameplayLeaderboardProvider leaderboardProvider = new EmptyGameplayLeaderboardProvider();
public EditorPlayer(Editor editor)
: base(new PlayerConfiguration { ShowResults = false })
{
this.editor = editor;
editorState = editor.GetState();
}
protected override GameplayClockContainer CreateGameplayClockContainer(WorkingBeatmap beatmap, double gameplayStart)
{
var masterGameplayClockContainer = new MasterGameplayClockContainer(beatmap, gameplayStart);
// Only reset the time to the current point if the editor is later than the normal start time (and the first object).
// This allows more sane test playing from the start of the beatmap (ie. correctly adding lead-in time).
if (editorState.Time > gameplayStart && editorState.Time > DrawableRuleset.Objects.FirstOrDefault()?.StartTime)
masterGameplayClockContainer.Reset(editorState.Time);
return masterGameplayClockContainer;
}
protected override void LoadComplete()
{
base.LoadComplete();
markPreviousObjectsHit();
markVisibleDrawableObjectsHit();
ScoreProcessor.HasCompleted.BindValueChanged(completed =>
{
if (completed.NewValue)
{
Scheduler.AddDelayed(() =>
{
if (this.IsCurrentScreen())
this.Exit();
}, RESULTS_DISPLAY_DELAY);
}
});
}
private void markPreviousObjectsHit()
{
foreach (var hitObject in enumerateHitObjects(DrawableRuleset.Objects, editorState.Time))
{
var judgement = hitObject.Judgement;
// this is very dodgy because there's no guarantee that `JudgementResult` is the correct result type for the object.
// however, instantiating the correct one is difficult here, because `JudgementResult`s are constructed by DHOs
// and because of pooling we don't *have* a DHO to use here.
// this basically mostly attempts to fill holes in `ScoreProcessor` tallies
// so that gameplay can actually complete at the end of the map when entering gameplay test midway through it, and not much else.
var result = new JudgementResult(hitObject, judgement)
{
Type = judgement.MaxResult,
GameplayRate = GameplayClockContainer.GetTrueGameplayRate(),
};
HealthProcessor.ApplyResult(result);
ScoreProcessor.ApplyResult(result);
}
static IEnumerable<HitObject> enumerateHitObjects(IEnumerable<HitObject> hitObjects, double cutoffTime)
{
foreach (var hitObject in hitObjects)
{
foreach (var nested in enumerateHitObjects(hitObject.NestedHitObjects, cutoffTime))
{
if (nested.GetEndTime() < cutoffTime)
yield return nested;
}
if (hitObject.GetEndTime() < cutoffTime)
yield return hitObject;
}
}
}
private void markVisibleDrawableObjectsHit()
{
if (!DrawableRuleset.Playfield.IsLoaded)
{
Schedule(markVisibleDrawableObjectsHit);
return;
}
foreach (var drawableObject in enumerateDrawableObjects(DrawableRuleset.Playfield.AllHitObjects, editorState.Time))
{
if (drawableObject.Entry == null)
continue;
var result = drawableObject.CreateResult(drawableObject.HitObject.Judgement);
result.Type = result.Judgement.MaxResult;
drawableObject.Entry.Result = result;
}
static IEnumerable<DrawableHitObject> enumerateDrawableObjects(IEnumerable<DrawableHitObject> drawableObjects, double cutoffTime)
{
foreach (var drawableObject in drawableObjects)
{
foreach (var nested in enumerateDrawableObjects(drawableObject.NestedHitObjects, cutoffTime))
{
if (nested.HitObject.GetEndTime() < cutoffTime)
yield return nested;
}
if (drawableObject.HitObject.GetEndTime() < cutoffTime)
yield return drawableObject;
}
}
}
protected override void PrepareReplay()
{
// don't record replays.
}
protected override bool CheckModsAllowFailure() => false; // never fail.
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat)
return false;
switch (e.Action)
{
case GlobalAction.EditorTestPlayToggleAutoplay:
toggleAutoplay();
return true;
case GlobalAction.EditorTestPlayToggleQuickPause:
toggleQuickPause();
return true;
case GlobalAction.EditorTestPlayQuickExitToInitialTime:
quickExit(false);
return true;
case GlobalAction.EditorTestPlayQuickExitToCurrentTime:
quickExit(true);
return true;
default:
return false;
}
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
private void toggleAutoplay()
{
if (DrawableRuleset.ReplayScore == null)
{
var autoplay = Ruleset.Value.CreateInstance().GetAutoplayMod();
if (autoplay == null)
return;
var score = autoplay.CreateScoreFromReplayData(GameplayState.Beatmap, [autoplay]);
// remove past frames to prevent replay frame handler from seeking back to start in an attempt to play back the entirety of the replay.
score.Replay.Frames.RemoveAll(f => f.Time <= GameplayClockContainer.CurrentTime);
DrawableRuleset.SetReplayScore(score);
// Without this schedule, the `GlobalCursorDisplay.Update()` machinery will fade the gameplay cursor out, but we still want it to show.
Schedule(() => DrawableRuleset.Cursor?.Show());
}
else
DrawableRuleset.SetReplayScore(null);
}
private void toggleQuickPause()
{
if (GameplayClockContainer.IsPaused.Value)
GameplayClockContainer.Start();
else
GameplayClockContainer.Stop();
}
private void quickExit(bool useCurrentTime)
{
if (useCurrentTime)
editorState.Time = GameplayClockContainer.CurrentTime;
editor.RestoreState(editorState);
this.Exit();
}
public override void OnEntering(ScreenTransitionEvent e)
{
base.OnEntering(e);
// finish alpha transforms on entering to avoid gameplay starting in a half-hidden state.
// the finish calls are purposefully not propagated to children to avoid messing up their state.
FinishTransforms();
GameplayClockContainer.FinishTransforms(false, nameof(Alpha));
}
public override bool OnExiting(ScreenExitEvent e)
{
musicController.Stop();
editor.RestoreState(editorState);
return base.OnExiting(e);
}
protected override ResultsScreen CreateResults(ScoreInfo score) => throw new NotSupportedException();
}
}