1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 15:12:57 +08:00

Add flow for submitting score on exiting SubmittingPlayer

This commit is contained in:
Dean Herbert 2021-07-01 17:02:33 +09:00
parent 397d2491b3
commit 04b874bb00

View File

@ -27,6 +27,8 @@ namespace osu.Game.Screens.Play
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private IAPIProvider api { get; set; }
private TaskCompletionSource<bool> scoreSubmissionSource;
protected SubmittingPlayer(PlayerConfiguration configuration = null) protected SubmittingPlayer(PlayerConfiguration configuration = null)
: base(configuration) : base(configuration)
{ {
@ -102,36 +104,18 @@ namespace osu.Game.Screens.Play
/// <returns>Whether gameplay should be immediately exited as a result. Returning false allows the gameplay session to continue. Defaults to true.</returns> /// <returns>Whether gameplay should be immediately exited as a result. Returning false allows the gameplay session to continue. Defaults to true.</returns>
protected virtual bool HandleTokenRetrievalFailure(Exception exception) => true; protected virtual bool HandleTokenRetrievalFailure(Exception exception) => true;
public override bool OnExiting(IScreen next)
{
return base.OnExiting(next);
}
protected override async Task PrepareScoreForResultsAsync(Score score) protected override async Task PrepareScoreForResultsAsync(Score score)
{ {
await base.PrepareScoreForResultsAsync(score).ConfigureAwait(false); await base.PrepareScoreForResultsAsync(score).ConfigureAwait(false);
// token may be null if the request failed but gameplay was still allowed (see HandleTokenRetrievalFailure). await submitScore(score).ConfigureAwait(false);
if (token == null) }
return;
var tcs = new TaskCompletionSource<bool>(); public override bool OnExiting(IScreen next)
var request = CreateSubmissionRequest(score, token.Value); {
submitScore(Score);
request.Success += s => return base.OnExiting(next);
{
score.ScoreInfo.OnlineScoreID = s.ID;
tcs.SetResult(true);
};
request.Failure += e =>
{
Logger.Error(e, "Failed to submit score");
tcs.SetResult(false);
};
api.Queue(request);
await tcs.Task.ConfigureAwait(false);
} }
/// <summary> /// <summary>
@ -148,5 +132,33 @@ namespace osu.Game.Screens.Play
/// <param name="score">The score to be submitted.</param> /// <param name="score">The score to be submitted.</param>
/// <param name="token">The submission token.</param> /// <param name="token">The submission token.</param>
protected abstract APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, long token); 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;
}
} }
} }