2020-12-20 23:04:06 +08:00
|
|
|
// 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.
|
|
|
|
|
2020-12-20 23:13:05 +08:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Allocation;
|
2020-12-23 14:58:50 +08:00
|
|
|
using osu.Framework.Bindables;
|
2020-12-22 13:59:11 +08:00
|
|
|
using osu.Framework.Logging;
|
2020-12-24 14:32:55 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2020-12-20 23:04:06 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2020-12-25 12:38:11 +08:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-20 23:13:05 +08:00
|
|
|
using osu.Game.Scoring;
|
2020-12-23 16:39:08 +08:00
|
|
|
using osu.Game.Screens.Play;
|
2020-12-22 18:09:59 +08:00
|
|
|
using osu.Game.Screens.Play.HUD;
|
2020-12-20 23:13:05 +08:00
|
|
|
using osu.Game.Screens.Ranking;
|
2020-12-22 18:09:59 +08:00
|
|
|
using osuTK;
|
2020-12-20 23:04:06 +08:00
|
|
|
|
2020-12-25 23:50:00 +08:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer
|
2020-12-20 23:04:06 +08:00
|
|
|
{
|
2021-03-23 14:33:31 +08:00
|
|
|
public class MultiplayerPlayer : RoomSubmittingPlayer
|
2020-12-20 23:04:06 +08:00
|
|
|
{
|
2020-12-20 23:13:05 +08:00
|
|
|
protected override bool PauseOnFocusLost => false;
|
|
|
|
|
|
|
|
// Disallow fails in multiplayer for now.
|
|
|
|
protected override bool CheckModsAllowFailure() => false;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private StatefulMultiplayerClient client { get; set; }
|
|
|
|
|
2020-12-23 18:57:43 +08:00
|
|
|
private IBindable<bool> isConnected;
|
|
|
|
|
2020-12-20 23:13:05 +08:00
|
|
|
private readonly TaskCompletionSource<bool> resultsReady = new TaskCompletionSource<bool>();
|
|
|
|
|
2020-12-22 18:09:59 +08:00
|
|
|
private MultiplayerGameplayLeaderboard leaderboard;
|
2020-12-23 14:58:50 +08:00
|
|
|
|
2020-12-24 09:38:53 +08:00
|
|
|
private readonly int[] userIds;
|
|
|
|
|
2020-12-24 14:32:55 +08:00
|
|
|
private LoadingLayer loadingDisplay;
|
|
|
|
|
2020-12-24 09:38:53 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a multiplayer player.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistItem">The playlist item to be played.</param>
|
|
|
|
/// <param name="userIds">The users which are participating in this game.</param>
|
2020-12-25 12:38:11 +08:00
|
|
|
public MultiplayerPlayer(PlaylistItem playlistItem, int[] userIds)
|
2020-12-23 16:39:08 +08:00
|
|
|
: base(playlistItem, new PlayerConfiguration
|
|
|
|
{
|
|
|
|
AllowPause = false,
|
|
|
|
AllowRestart = false,
|
2020-12-24 15:29:51 +08:00
|
|
|
AllowSkippingIntro = false,
|
2020-12-23 16:39:08 +08:00
|
|
|
})
|
2020-12-20 23:13:05 +08:00
|
|
|
{
|
2020-12-24 09:38:53 +08:00
|
|
|
this.userIds = userIds;
|
2020-12-20 23:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2020-12-20 23:04:06 +08:00
|
|
|
{
|
2020-12-24 21:32:30 +08:00
|
|
|
// todo: this should be implemented via a custom HUD implementation, and correctly masked to the main content area.
|
|
|
|
LoadComponentAsync(leaderboard = new MultiplayerGameplayLeaderboard(ScoreProcessor, userIds), HUDOverlay.Add);
|
|
|
|
|
2021-01-04 21:42:39 +08:00
|
|
|
HUDOverlay.Add(loadingDisplay = new LoadingLayer(true) { Depth = float.MaxValue });
|
2021-03-23 15:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadAsyncComplete()
|
|
|
|
{
|
|
|
|
base.LoadAsyncComplete();
|
2020-12-24 21:32:30 +08:00
|
|
|
|
2021-03-24 12:20:44 +08:00
|
|
|
if (!ValidForResume)
|
|
|
|
return; // token retrieval may have failed.
|
2020-12-20 23:13:05 +08:00
|
|
|
|
|
|
|
client.MatchStarted += onMatchStarted;
|
|
|
|
client.ResultsReady += onResultsReady;
|
|
|
|
|
2020-12-24 14:32:55 +08:00
|
|
|
ScoreProcessor.HasCompleted.BindValueChanged(completed =>
|
|
|
|
{
|
|
|
|
// wait for server to tell us that results are ready (see SubmitScore implementation)
|
|
|
|
loadingDisplay.Show();
|
|
|
|
});
|
|
|
|
|
2020-12-23 14:58:50 +08:00
|
|
|
isConnected = client.IsConnected.GetBoundCopy();
|
2021-01-12 18:04:16 +08:00
|
|
|
isConnected.BindValueChanged(connected => Schedule(() =>
|
2020-12-22 13:59:11 +08:00
|
|
|
{
|
2020-12-23 14:58:50 +08:00
|
|
|
if (!connected.NewValue)
|
2020-12-22 13:59:11 +08:00
|
|
|
{
|
2020-12-25 12:38:11 +08:00
|
|
|
// messaging to the user about this disconnect will be provided by the MultiplayerMatchSubScreen.
|
2020-12-23 15:51:11 +08:00
|
|
|
failAndBail();
|
2020-12-23 15:32:58 +08:00
|
|
|
}
|
2021-01-12 18:04:16 +08:00
|
|
|
}), true);
|
2020-12-23 14:58:50 +08:00
|
|
|
|
2020-12-22 18:09:59 +08:00
|
|
|
Debug.Assert(client.Room != null);
|
2020-12-24 14:32:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:51:34 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
((IBindable<bool>)leaderboard.Expanded).BindTo(IsBreakTime);
|
|
|
|
}
|
|
|
|
|
2020-12-24 14:32:55 +08:00
|
|
|
protected override void StartGameplay()
|
|
|
|
{
|
|
|
|
// block base call, but let the server know we are ready to start.
|
|
|
|
loadingDisplay.Show();
|
|
|
|
|
|
|
|
client.ChangeState(MultiplayerUserState.Loaded).ContinueWith(task => failAndBail(task.Exception?.Message ?? "Server error"), TaskContinuationOptions.NotOnRanToCompletion);
|
2020-12-23 15:51:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void failAndBail(string message = null)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(message))
|
|
|
|
Logger.Log(message, LoggingTarget.Runtime, LogLevel.Important);
|
2020-12-22 13:59:11 +08:00
|
|
|
|
2020-12-24 00:37:49 +08:00
|
|
|
Schedule(() => PerformExit(false));
|
2020-12-20 23:13:05 +08:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:09:59 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
2020-12-23 05:31:40 +08:00
|
|
|
adjustLeaderboardPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void adjustLeaderboardPosition()
|
|
|
|
{
|
2020-12-22 18:09:59 +08:00
|
|
|
const float padding = 44; // enough margin to avoid the hit error display.
|
|
|
|
|
|
|
|
leaderboard.Position = new Vector2(
|
|
|
|
padding,
|
|
|
|
padding + HUDOverlay.TopScoringElementsHeight);
|
2020-12-20 23:13:05 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 14:32:55 +08:00
|
|
|
private void onMatchStarted() => Scheduler.Add(() =>
|
|
|
|
{
|
|
|
|
loadingDisplay.Hide();
|
|
|
|
base.StartGameplay();
|
|
|
|
});
|
2020-12-20 23:13:05 +08:00
|
|
|
|
|
|
|
private void onResultsReady() => resultsReady.SetResult(true);
|
|
|
|
|
2021-03-23 14:45:22 +08:00
|
|
|
protected override async Task PrepareScoreForResultsAsync(Score score)
|
2020-12-20 23:13:05 +08:00
|
|
|
{
|
2021-03-23 14:45:22 +08:00
|
|
|
await base.PrepareScoreForResultsAsync(score).ConfigureAwait(false);
|
2020-12-20 23:13:05 +08:00
|
|
|
|
2021-03-08 11:57:16 +08:00
|
|
|
await client.ChangeState(MultiplayerUserState.FinishedPlay).ConfigureAwait(false);
|
2020-12-20 23:13:05 +08:00
|
|
|
|
2020-12-24 18:33:49 +08:00
|
|
|
// Await up to 60 seconds for results to become available (6 api request timeouts).
|
2020-12-20 23:13:05 +08:00
|
|
|
// This is arbitrary just to not leave the player in an essentially deadlocked state if any connection issues occur.
|
2021-03-08 11:57:16 +08:00
|
|
|
await Task.WhenAny(resultsReady.Task, Task.Delay(TimeSpan.FromSeconds(60))).ConfigureAwait(false);
|
2020-12-20 23:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override ResultsScreen CreateResults(ScoreInfo score)
|
|
|
|
{
|
|
|
|
Debug.Assert(RoomId.Value != null);
|
2020-12-25 12:38:11 +08:00
|
|
|
return new MultiplayerResultsScreen(score, RoomId.Value.Value, PlaylistItem);
|
2020-12-20 23:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
if (client != null)
|
|
|
|
{
|
|
|
|
client.MatchStarted -= onMatchStarted;
|
|
|
|
client.ResultsReady -= onResultsReady;
|
|
|
|
}
|
2020-12-20 23:04:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|