Skip to content

Commit

Permalink
Merge pull request #18785 from cdwcgt/Save-Score-Failed
Browse files Browse the repository at this point in the history
Add ability to save failed score
  • Loading branch information
frenzibyte authored Jul 15, 2022
2 parents 4ee9bb3 + e6236ba commit 279bdcb
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 11 deletions.
31 changes: 27 additions & 4 deletions osu.Game.Tests/Visual/Gameplay/TestScenePlayerLocalScoreImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
using osu.Framework.Audio;
using osu.Framework.Extensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics.Containers;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.Play;
using osu.Game.Screens.Ranking;
using osu.Game.Tests.Resources;

Expand Down Expand Up @@ -58,14 +62,35 @@ public override void SetUpSteps()

protected override bool HasCustomSteps => true;

protected override bool AllowFail => false;
protected override bool AllowFail => allowFail;

private bool allowFail;

[SetUp]
public void SetUp()
{
allowFail = false;
customRuleset = null;
}

[Test]
public void TestSaveFailedReplay()
{
AddStep("allow fail", () => allowFail = true);

CreateTest();

AddUntilStep("fail screen displayed", () => Player.ChildrenOfType<FailOverlay>().First().State.Value == Visibility.Visible);
AddUntilStep("score not in database", () => Realm.Run(r => r.Find<ScoreInfo>(Player.Score.ScoreInfo.ID) == null));
AddStep("click save button", () => Player.ChildrenOfType<SaveFailedScoreButton>().First().ChildrenOfType<OsuClickableContainer>().First().TriggerClick());
AddUntilStep("score not in database", () => Realm.Run(r => r.Find<ScoreInfo>(Player.Score.ScoreInfo.ID) != null));
}

[Test]
public void TestLastPlayedUpdated()
{
DateTimeOffset? getLastPlayed() => Realm.Run(r => r.Find<BeatmapInfo>(Beatmap.Value.BeatmapInfo.ID)?.LastPlayed);

AddStep("set no custom ruleset", () => customRuleset = null);
AddAssert("last played is null", () => getLastPlayed() == null);

CreateTest();
Expand All @@ -77,8 +102,6 @@ public void TestLastPlayedUpdated()
[Test]
public void TestScoreStoredLocally()
{
AddStep("set no custom ruleset", () => customRuleset = null);

CreateTest();

AddUntilStep("wait for track to start running", () => Beatmap.Value.Track.IsRunning);
Expand Down
2 changes: 0 additions & 2 deletions osu.Game/Online/DownloadState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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.

#nullable disable

namespace osu.Game.Online
{
public enum DownloadState
Expand Down
44 changes: 44 additions & 0 deletions osu.Game/Screens/Play/FailOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@

#nullable disable

using System;
using System.Threading.Tasks;
using osu.Game.Scoring;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;

namespace osu.Game.Screens.Play
{
public class FailOverlay : GameplayMenuOverlay
{
public Func<Task<ScoreInfo>> SaveReplay;

public override string Header => "failed";
public override string Description => "you're dead, try again?";

Expand All @@ -19,6 +30,39 @@ private void load(OsuColour colours)
{
AddButton("Retry", colours.YellowDark, () => OnRetry?.Invoke());
AddButton("Quit", new Color4(170, 27, 39, 255), () => OnQuit?.Invoke());
// from #10339 maybe this is a better visual effect
Add(new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Height = TwoLayerButton.SIZE_EXTENDED.Y,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex("#333")
},
new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Spacing = new Vector2(5),
Padding = new MarginPadding(10),
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
new SaveFailedScoreButton(SaveReplay)
{
Width = 300
},
}
}
}
});
}
}
}
13 changes: 9 additions & 4 deletions osu.Game/Screens/Play/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ private void load(AudioManager audio, OsuConfigManager config, OsuGameBase game,
},
FailOverlay = new FailOverlay
{
SaveReplay = () =>
{
Score.ScoreInfo.Passed = false;
Score.ScoreInfo.Rank = ScoreRank.F;
return prepareAndImportScore();
},
OnRetry = Restart,
OnQuit = () => PerformExit(true),
},
Expand Down Expand Up @@ -720,7 +726,7 @@ private void scoreCompletionChanged(ValueChangedEvent<bool> completed)
if (!Configuration.ShowResults)
return;

prepareScoreForDisplayTask ??= Task.Run(prepareScoreForResults);
prepareScoreForDisplayTask ??= Task.Run(prepareAndImportScore);

bool storyboardHasOutro = DimmableStoryboard.ContentDisplayed && !DimmableStoryboard.HasStoryboardEnded.Value;

Expand All @@ -739,7 +745,7 @@ private void scoreCompletionChanged(ValueChangedEvent<bool> completed)
/// Asynchronously run score preparation operations (database import, online submission etc.).
/// </summary>
/// <returns>The final score.</returns>
private async Task<ScoreInfo> prepareScoreForResults()
private async Task<ScoreInfo> prepareAndImportScore()
{
var scoreCopy = Score.DeepClone();

Expand Down Expand Up @@ -1024,8 +1030,7 @@ public override bool OnExiting(ScreenExitEvent e)
if (prepareScoreForDisplayTask == null)
{
Score.ScoreInfo.Passed = false;
// potentially should be ScoreRank.F instead? this is the best alternative for now.
Score.ScoreInfo.Rank = ScoreRank.D;
Score.ScoreInfo.Rank = ScoreRank.F;
}

// EndPlaying() is typically called from ReplayRecorder.Dispose(). Disposal is currently asynchronous.
Expand Down
11 changes: 10 additions & 1 deletion osu.Game/Screens/Play/ReplayPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ public class ReplayPlayer : Player, IKeyBindingHandler<GlobalAction>
{
private readonly Func<IBeatmap, IReadOnlyList<Mod>, Score> createScore;

private readonly bool replayIsFailedScore;

// Disallow replays from failing. (see https://github.com/ppy/osu/issues/6108)
protected override bool CheckModsAllowFailure() => false;
protected override bool CheckModsAllowFailure()
{
if (!replayIsFailedScore)
return false;

return base.CheckModsAllowFailure();
}

public ReplayPlayer(Score score, PlayerConfiguration configuration = null)
: this((_, _) => score, configuration)
{
replayIsFailedScore = score.ScoreInfo.Rank == ScoreRank.F;
}

public ReplayPlayer(Func<IBeatmap, IReadOnlyList<Mod>, Score> createScore, PlayerConfiguration configuration = null)
Expand Down
92 changes: 92 additions & 0 deletions osu.Game/Screens/Play/SaveFailedScoreButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Database;
using osu.Game.Scoring;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osuTK;

namespace osu.Game.Screens.Play
{
public class SaveFailedScoreButton : CompositeDrawable
{
private readonly Bindable<DownloadState> state = new Bindable<DownloadState>();

private readonly Func<Task<ScoreInfo>> importFailedScore;

private ScoreInfo? importedScore;

private DownloadButton button = null!;

public SaveFailedScoreButton(Func<Task<ScoreInfo>> importFailedScore)
{
Size = new Vector2(50, 30);

this.importFailedScore = importFailedScore;
}

[BackgroundDependencyLoader]
private void load(OsuGame? game, Player? player, RealmAccess realm)
{
InternalChild = button = new DownloadButton
{
RelativeSizeAxes = Axes.Both,
State = { BindTarget = state },
Action = () =>
{
switch (state.Value)
{
case DownloadState.LocallyAvailable:
game?.PresentScore(importedScore, ScorePresentType.Gameplay);
break;

case DownloadState.NotDownloaded:
state.Value = DownloadState.Importing;
Task.Run(importFailedScore).ContinueWith(t =>
{
importedScore = realm.Run(r => r.Find<ScoreInfo>(t.GetResultSafely().ID)?.Detach());
Schedule(() => state.Value = importedScore != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded);
});
break;
}
}
};

if (player != null)
{
importedScore = realm.Run(r => r.Find<ScoreInfo>(player.Score.ScoreInfo.ID)?.Detach());
if (importedScore != null)
state.Value = DownloadState.LocallyAvailable;
}

state.BindValueChanged(state =>
{
switch (state.NewValue)
{
case DownloadState.LocallyAvailable:
button.TooltipText = @"watch replay";
button.Enabled.Value = true;
break;

case DownloadState.Importing:
button.TooltipText = @"importing score";
button.Enabled.Value = false;
break;

default:
button.TooltipText = @"save score";
button.Enabled.Value = true;
break;
}
}, true);
}
}
}

0 comments on commit 279bdcb

Please sign in to comment.