mirror of
https://github.com/ppy/osu.git
synced 2024-12-15 04:13:00 +08:00
Add flow for submitting score on exiting SubmittingPlayer
This commit is contained in:
parent
397d2491b3
commit
04b874bb00
@ -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)
|
||||
{
|
||||
@ -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>
|
||||
protected virtual bool HandleTokenRetrievalFailure(Exception exception) => true;
|
||||
|
||||
public override bool OnExiting(IScreen next)
|
||||
{
|
||||
return base.OnExiting(next);
|
||||
}
|
||||
|
||||
protected override async Task PrepareScoreForResultsAsync(Score score)
|
||||
{
|
||||
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)
|
||||
{
|
||||
submitScore(Score);
|
||||
|
||||
request.Success += s =>
|
||||
{
|
||||
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);
|
||||
return base.OnExiting(next);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -148,5 +132,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user