mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 12:57:36 +08:00
Merge pull request #13721 from peppy/submit-on-player-exit
Submit scores on player exit
This commit is contained in:
commit
9eb6ae4d45
121
osu.Game.Tests/Visual/Gameplay/TestScenePlayerScoreSubmission.cs
Normal file
121
osu.Game.Tests/Visual/Gameplay/TestScenePlayerScoreSubmission.cs
Normal file
@ -0,0 +1,121 @@
|
||||
// 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.Linq;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Screens;
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Online.Rooms;
|
||||
using osu.Game.Online.Solo;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Rulesets.Objects;
|
||||
using osu.Game.Screens.Ranking;
|
||||
|
||||
namespace osu.Game.Tests.Visual.Gameplay
|
||||
{
|
||||
public class TestScenePlayerScoreSubmission : OsuPlayerTestScene
|
||||
{
|
||||
protected override bool AllowFail => allowFail;
|
||||
|
||||
private bool allowFail;
|
||||
|
||||
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
|
||||
|
||||
protected override bool HasCustomSteps => true;
|
||||
|
||||
protected override TestPlayer CreatePlayer(Ruleset ruleset) => new TestPlayer(false);
|
||||
|
||||
[Test]
|
||||
public void TestNoSubmissionOnResultsWithNoToken()
|
||||
{
|
||||
prepareTokenResponse(false);
|
||||
|
||||
CreateTest(() => allowFail = false);
|
||||
|
||||
AddUntilStep("wait for token request", () => Player.TokenCreationRequested);
|
||||
|
||||
AddUntilStep("wait for track to start running", () => Beatmap.Value.Track.IsRunning);
|
||||
AddStep("seek to completion", () => Player.GameplayClockContainer.Seek(Player.DrawableRuleset.Objects.Last().GetEndTime()));
|
||||
|
||||
AddUntilStep("results displayed", () => Player.GetChildScreen() is ResultsScreen);
|
||||
|
||||
AddAssert("ensure no submission", () => Player.SubmittedScore == null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSubmissionOnResults()
|
||||
{
|
||||
prepareTokenResponse(true);
|
||||
|
||||
CreateTest(() => allowFail = false);
|
||||
|
||||
AddUntilStep("wait for token request", () => Player.TokenCreationRequested);
|
||||
|
||||
AddUntilStep("wait for track to start running", () => Beatmap.Value.Track.IsRunning);
|
||||
AddStep("seek to completion", () => Player.GameplayClockContainer.Seek(Player.DrawableRuleset.Objects.Last().GetEndTime()));
|
||||
|
||||
AddUntilStep("results displayed", () => Player.GetChildScreen() is ResultsScreen);
|
||||
AddAssert("ensure passing submission", () => Player.SubmittedScore?.ScoreInfo.Passed == true);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestNoSubmissionOnExitWithNoToken()
|
||||
{
|
||||
prepareTokenResponse(false);
|
||||
|
||||
CreateTest(() => allowFail = false);
|
||||
|
||||
AddUntilStep("wait for token request", () => Player.TokenCreationRequested);
|
||||
|
||||
AddStep("exit", () => Player.Exit());
|
||||
AddAssert("ensure no submission", () => Player.SubmittedScore == null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSubmissionOnFail()
|
||||
{
|
||||
prepareTokenResponse(true);
|
||||
|
||||
CreateTest(() => allowFail = true);
|
||||
|
||||
AddUntilStep("wait for token request", () => Player.TokenCreationRequested);
|
||||
AddUntilStep("wait for fail", () => Player.HasFailed);
|
||||
AddStep("exit", () => Player.Exit());
|
||||
|
||||
AddAssert("ensure failing submission", () => Player.SubmittedScore?.ScoreInfo.Passed == false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSubmissionOnExit()
|
||||
{
|
||||
prepareTokenResponse(true);
|
||||
|
||||
CreateTest(() => allowFail = false);
|
||||
|
||||
AddUntilStep("wait for token request", () => Player.TokenCreationRequested);
|
||||
AddStep("exit", () => Player.Exit());
|
||||
AddAssert("ensure failing submission", () => Player.SubmittedScore?.ScoreInfo.Passed == false);
|
||||
}
|
||||
|
||||
private void prepareTokenResponse(bool validToken)
|
||||
{
|
||||
AddStep("Prepare test API", () =>
|
||||
{
|
||||
dummyAPI.HandleRequest = request =>
|
||||
{
|
||||
switch (request)
|
||||
{
|
||||
case CreateSoloScoreRequest tokenRequest:
|
||||
if (validToken)
|
||||
tokenRequest.TriggerSuccess(new APIScoreToken { ID = 1234 });
|
||||
else
|
||||
tokenRequest.TriggerFailure(new APIException("something went wrong!", null));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -768,6 +768,7 @@ namespace osu.Game.Screens.Play
|
||||
return false;
|
||||
|
||||
HasFailed = true;
|
||||
Score.ScoreInfo.Passed = false;
|
||||
|
||||
// There is a chance that we could be in a paused state as the ruleset's internal clock (see FrameStabilityContainer)
|
||||
// could process an extra frame after the GameplayClock is stopped.
|
||||
@ -950,6 +951,10 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
screenSuspension?.Expire();
|
||||
|
||||
// if arriving here and the results screen preparation task hasn't run, it's safe to say the user has not completed the beatmap.
|
||||
if (prepareScoreForDisplayTask == null)
|
||||
Score.ScoreInfo.Passed = false;
|
||||
|
||||
// EndPlaying() is typically called from ReplayRecorder.Dispose(). Disposal is currently asynchronous.
|
||||
// To resolve test failures, forcefully end playing synchronously when this screen exits.
|
||||
// Todo: Replace this with a more permanent solution once osu-framework has a synchronous cleanup method.
|
||||
|
@ -12,6 +12,16 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
public class SoloPlayer : SubmittingPlayer
|
||||
{
|
||||
public SoloPlayer()
|
||||
: this(null)
|
||||
{
|
||||
}
|
||||
|
||||
protected SoloPlayer(PlayerConfiguration configuration = null)
|
||||
: base(configuration)
|
||||
{
|
||||
}
|
||||
|
||||
protected override APIRequest<APIScoreToken> CreateTokenRequest()
|
||||
{
|
||||
if (!(Beatmap.Value.BeatmapInfo.OnlineBeatmapID is int beatmapId))
|
||||
@ -27,9 +37,11 @@ namespace osu.Game.Screens.Play
|
||||
|
||||
protected override APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, long token)
|
||||
{
|
||||
Debug.Assert(Beatmap.Value.BeatmapInfo.OnlineBeatmapID != null);
|
||||
var beatmap = score.ScoreInfo.Beatmap;
|
||||
|
||||
int beatmapId = Beatmap.Value.BeatmapInfo.OnlineBeatmapID.Value;
|
||||
Debug.Assert(beatmap.OnlineBeatmapID != null);
|
||||
|
||||
int beatmapId = beatmap.OnlineBeatmapID.Value;
|
||||
|
||||
return new SubmitSoloScoreRequest(beatmapId, token, score.ScoreInfo);
|
||||
}
|
||||
|
@ -27,6 +27,8 @@ namespace osu.Game.Screens.Play
|
||||
[Resolved]
|
||||
private IAPIProvider api { get; set; }
|
||||
|
||||
private TaskCompletionSource<bool> scoreSubmissionSource;
|
||||
|
||||
protected SubmittingPlayer(PlayerConfiguration configuration = null)
|
||||
: base(configuration)
|
||||
{
|
||||
@ -106,27 +108,16 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
await base.PrepareScoreForResultsAsync(score).ConfigureAwait(false);
|
||||
|
||||
// token may be null if the request failed but gameplay was still allowed (see HandleTokenRetrievalFailure).
|
||||
if (token == null)
|
||||
return;
|
||||
await submitScore(score).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
var request = CreateSubmissionRequest(score, token.Value);
|
||||
public override bool OnExiting(IScreen next)
|
||||
{
|
||||
var exiting = base.OnExiting(next);
|
||||
|
||||
request.Success += s =>
|
||||
{
|
||||
score.ScoreInfo.OnlineScoreID = s.ID;
|
||||
tcs.SetResult(true);
|
||||
};
|
||||
submitScore(Score);
|
||||
|
||||
request.Failure += e =>
|
||||
{
|
||||
Logger.Error(e, "Failed to submit score");
|
||||
tcs.SetResult(false);
|
||||
};
|
||||
|
||||
api.Queue(request);
|
||||
await tcs.Task.ConfigureAwait(false);
|
||||
return exiting;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -143,5 +134,33 @@ namespace osu.Game.Screens.Play
|
||||
/// <param name="score">The score to be submitted.</param>
|
||||
/// <param name="token">The submission token.</param>
|
||||
protected abstract APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, long token);
|
||||
|
||||
private Task submitScore(Score score)
|
||||
{
|
||||
// token may be null if the request failed but gameplay was still allowed (see HandleTokenRetrievalFailure).
|
||||
if (token == null)
|
||||
return Task.CompletedTask;
|
||||
|
||||
if (scoreSubmissionSource != null)
|
||||
return scoreSubmissionSource.Task;
|
||||
|
||||
scoreSubmissionSource = new TaskCompletionSource<bool>();
|
||||
var request = CreateSubmissionRequest(score, token.Value);
|
||||
|
||||
request.Success += s =>
|
||||
{
|
||||
score.ScoreInfo.OnlineScoreID = s.ID;
|
||||
scoreSubmissionSource.SetResult(true);
|
||||
};
|
||||
|
||||
request.Failure += e =>
|
||||
{
|
||||
Logger.Error(e, "Failed to submit score");
|
||||
scoreSubmissionSource.SetResult(false);
|
||||
};
|
||||
|
||||
api.Queue(request);
|
||||
return scoreSubmissionSource.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,18 @@
|
||||
// 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.Bindables;
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Online.Rooms;
|
||||
using osu.Game.Rulesets.Judgements;
|
||||
using osu.Game.Rulesets.Mods;
|
||||
using osu.Game.Rulesets.Scoring;
|
||||
using osu.Game.Rulesets.UI;
|
||||
using osu.Game.Scoring;
|
||||
using osu.Game.Screens.Play;
|
||||
|
||||
namespace osu.Game.Tests.Visual
|
||||
@ -16,7 +20,7 @@ namespace osu.Game.Tests.Visual
|
||||
/// <summary>
|
||||
/// A player that exposes many components that would otherwise not be available, for testing purposes.
|
||||
/// </summary>
|
||||
public class TestPlayer : Player
|
||||
public class TestPlayer : SoloPlayer
|
||||
{
|
||||
protected override bool PauseOnFocusLost { get; }
|
||||
|
||||
@ -35,6 +39,10 @@ namespace osu.Game.Tests.Visual
|
||||
|
||||
public new HealthProcessor HealthProcessor => base.HealthProcessor;
|
||||
|
||||
public bool TokenCreationRequested { get; private set; }
|
||||
|
||||
public Score SubmittedScore { get; private set; }
|
||||
|
||||
public new bool PauseCooldownActive => base.PauseCooldownActive;
|
||||
|
||||
public readonly List<JudgementResult> Results = new List<JudgementResult>();
|
||||
@ -49,6 +57,20 @@ namespace osu.Game.Tests.Visual
|
||||
PauseOnFocusLost = pauseOnFocusLost;
|
||||
}
|
||||
|
||||
protected override bool HandleTokenRetrievalFailure(Exception exception) => false;
|
||||
|
||||
protected override APIRequest<APIScoreToken> CreateTokenRequest()
|
||||
{
|
||||
TokenCreationRequested = true;
|
||||
return base.CreateTokenRequest();
|
||||
}
|
||||
|
||||
protected override APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, long token)
|
||||
{
|
||||
SubmittedScore = score;
|
||||
return base.CreateSubmissionRequest(score, token);
|
||||
}
|
||||
|
||||
protected override void PrepareReplay()
|
||||
{
|
||||
// Generally, replay generation is handled by whatever is constructing the player.
|
||||
|
Loading…
Reference in New Issue
Block a user