Move score submission logic in general out to its own Player type

This commit is contained in:
Dean Herbert 2021-03-23 15:00:02 +09:00
parent 6cb14e91c9
commit 7045fce555
3 changed files with 106 additions and 69 deletions

View File

@ -19,7 +19,6 @@ using osuTK;
namespace osu.Game.Screens.OnlinePlay.Multiplayer
{
// Todo: The "room" part of PlaylistsPlayer should be split out into an abstract player class to be inherited instead.
public class MultiplayerPlayer : PlaylistsPlayer
{
protected override bool PauseOnFocusLost => false;

View File

@ -4,11 +4,8 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Framework.Screens;
using osu.Game.Online.API;
using osu.Game.Online.Rooms;
@ -19,23 +16,12 @@ using osu.Game.Screens.Ranking;
namespace osu.Game.Screens.OnlinePlay.Playlists
{
public class PlaylistsPlayer : Player
public class PlaylistsPlayer : RoomSubmittingPlayer
{
public Action Exited;
[Resolved(typeof(Room), nameof(Room.RoomID))]
protected Bindable<long?> RoomId { get; private set; }
protected readonly PlaylistItem PlaylistItem;
protected long? Token { get; private set; }
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
public PlaylistsPlayer(PlaylistItem playlistItem, PlayerConfiguration configuration = null)
: base(configuration)
{
@ -43,12 +29,8 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
}
[BackgroundDependencyLoader]
private void load()
private void load(IBindable<RulesetInfo> ruleset)
{
Token = null;
bool failed = false;
// Sanity checks to ensure that PlaylistsPlayer matches the settings for the current PlaylistItem
if (Beatmap.Value.BeatmapInfo.OnlineBeatmapID != PlaylistItem.Beatmap.Value.OnlineBeatmapID)
throw new InvalidOperationException("Current Beatmap does not match PlaylistItem's Beatmap");
@ -58,31 +40,12 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
if (!PlaylistItem.RequiredMods.All(m => Mods.Value.Any(m.Equals)))
throw new InvalidOperationException("Current Mods do not match PlaylistItem's RequiredMods");
var req = new CreateRoomScoreRequest(RoomId.Value ?? 0, PlaylistItem.ID, Game.VersionHash);
req.Success += r => Token = r.ID;
req.Failure += e =>
{
failed = true;
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();
});
};
api.Queue(req);
while (!failed && !Token.HasValue)
Thread.Sleep(1000);
}
protected override APIRequest<APIScoreToken> CreateTokenRequestRequest() => new CreateRoomScoreRequest(RoomId.Value ?? 0, PlaylistItem.ID, Game.VersionHash);
public override APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, int token) => new SubmitRoomScoreRequest(token, RoomId.Value ?? 0, PlaylistItem.ID, score.ScoreInfo);
public override bool OnExiting(IScreen next)
{
if (base.OnExiting(next))
@ -106,31 +69,6 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
return score;
}
protected override async Task SubmitScore(Score score)
{
await base.SubmitScore(score).ConfigureAwait(false);
Debug.Assert(Token != null);
var tcs = new TaskCompletionSource<bool>();
var request = new SubmitRoomScoreRequest(Token.Value, RoomId.Value ?? 0, PlaylistItem.ID, score.ScoreInfo);
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);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);

View File

@ -0,0 +1,100 @@
// 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.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Framework.Screens;
using osu.Game.Online.API;
using osu.Game.Online.Rooms;
using osu.Game.Scoring;
namespace osu.Game.Screens.Play
{
public abstract class RoomSubmittingPlayer : SubmittingPlayer
{
[Resolved(typeof(Room), nameof(Room.RoomID))]
protected Bindable<long?> RoomId { get; private set; }
protected RoomSubmittingPlayer(PlayerConfiguration configuration)
: base(configuration)
{
}
}
public abstract class SubmittingPlayer : Player
{
protected long? Token { get; private set; }
[Resolved]
private IAPIProvider api { get; set; }
protected SubmittingPlayer(PlayerConfiguration configuration)
: base(configuration)
{
}
[BackgroundDependencyLoader]
private void load()
{
Token = null;
bool failed = false;
var req = CreateTokenRequestRequest();
req.Success += r => Token = r.ID;
req.Failure += e =>
{
failed = true;
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();
});
};
api.Queue(req);
while (!failed && !Token.HasValue)
Thread.Sleep(1000);
}
protected override async Task SubmitScore(Score score)
{
await base.SubmitScore(score).ConfigureAwait(false);
Debug.Assert(Token != null);
var tcs = new TaskCompletionSource<bool>();
var request = CreateSubmissionRequest(score, Token.Value);
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);
}
protected abstract APIRequest<MultiplayerScore> CreateSubmissionRequest(Score score, long token);
protected abstract APIRequest<APIScoreToken> CreateTokenRequestRequest();
}
}