From 242b847516cdf53f4f2c54fdd7097b9ec862f9a5 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 23 Mar 2021 16:41:36 +0900 Subject: [PATCH] Add flow for allowing gameplay to continue even when an error occurs with token retrieval --- osu.Game/Screens/Play/RoomSubmittingPlayer.cs | 2 +- osu.Game/Screens/Play/SubmittingPlayer.cs | 71 +++++++++++++------ 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/osu.Game/Screens/Play/RoomSubmittingPlayer.cs b/osu.Game/Screens/Play/RoomSubmittingPlayer.cs index c695e99874..6ef39e4b75 100644 --- a/osu.Game/Screens/Play/RoomSubmittingPlayer.cs +++ b/osu.Game/Screens/Play/RoomSubmittingPlayer.cs @@ -27,6 +27,6 @@ namespace osu.Game.Screens.Play protected override APIRequest CreateTokenRequestRequest() => new CreateRoomScoreRequest(RoomId.Value ?? 0, PlaylistItem.ID, Game.VersionHash); - public override APIRequest CreateSubmissionRequest(Score score, int token) => new SubmitRoomScoreRequest(token, RoomId.Value ?? 0, PlaylistItem.ID, score.ScoreInfo); + protected override APIRequest CreateSubmissionRequest(Score score, long token) => new SubmitRoomScoreRequest(token, RoomId.Value ?? 0, PlaylistItem.ID, score.ScoreInfo); } } diff --git a/osu.Game/Screens/Play/SubmittingPlayer.cs b/osu.Game/Screens/Play/SubmittingPlayer.cs index 356f70f4bf..55f4ba4b9b 100644 --- a/osu.Game/Screens/Play/SubmittingPlayer.cs +++ b/osu.Game/Screens/Play/SubmittingPlayer.cs @@ -1,8 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Diagnostics; -using System.Threading; +using System; using System.Threading.Tasks; using osu.Framework.Allocation; using osu.Framework.Logging; @@ -30,40 +29,70 @@ namespace osu.Game.Screens.Play protected override void LoadAsyncComplete() { - base.LoadAsyncComplete(); - // Token request construction should happen post-load to allow derived classes to potentially prepare DI backings that are used to create the request. - bool failed = false; + var tcs = new TaskCompletionSource(); + + if (!api.IsLoggedIn) + { + fail(new InvalidOperationException("API is not online.")); + return; + } var req = CreateTokenRequestRequest(); - req.Success += r => Token = r.ID; - req.Failure += e => + + if (req == null) { - failed = true; + fail(new InvalidOperationException("Request could not be constructed.")); + return; + } - if (string.IsNullOrEmpty(e.Message)) - Logger.Error(e, "Failed to retrieve a score submission token."); - else - Logger.Log($"You are not able to submit a score: {e.Message}", level: LogLevel.Important); - - Schedule(() => - { - ValidForResume = false; - this.Exit(); - }); + req.Success += r => + { + Token = r.ID; + tcs.SetResult(true); }; + req.Failure += fail; api.Queue(req); - while (!failed && !Token.HasValue) - Thread.Sleep(1000); + tcs.Task.Wait(); + + void fail(Exception exception) + { + if (HandleTokenRetrievalFailure(exception)) + { + if (string.IsNullOrEmpty(exception.Message)) + Logger.Error(exception, "Failed to retrieve a score submission token."); + else + Logger.Log($"You are not able to submit a score: {exception.Message}", level: LogLevel.Important); + + Schedule(() => + { + ValidForResume = false; + this.Exit(); + }); + } + + tcs.SetResult(false); + } + + base.LoadAsyncComplete(); } + /// + /// Called when a token could not be retrieved for submission. + /// + /// The error causing the failure. + /// Whether gameplay should be immediately exited as a result. Returning false allows the gameplay session to continue. Defaults to true. + protected virtual bool HandleTokenRetrievalFailure(Exception exception) => true; + protected override async Task PrepareScoreForResultsAsync(Score score) { await base.PrepareScoreForResultsAsync(score).ConfigureAwait(false); - Debug.Assert(Token != null); + // token may be null if the request failed but gameplay was still allowed (see HandleTokenRetrievalFailure). + if (Token == null) + return; var tcs = new TaskCompletionSource(); var request = CreateSubmissionRequest(score, Token.Value);