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;
|
2020-12-22 18:09:59 +08:00
|
|
|
using System.Linq;
|
2020-12-20 23:13:05 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-12-23 05:31:40 +08:00
|
|
|
using JetBrains.Annotations;
|
2020-12-20 23:13:05 +08:00
|
|
|
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-20 23:04:06 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2020-12-20 23:13:05 +08:00
|
|
|
using osu.Game.Online.RealtimeMultiplayer;
|
|
|
|
using osu.Game.Scoring;
|
2020-12-20 23:04:06 +08:00
|
|
|
using osu.Game.Screens.Multi.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
|
|
|
|
|
|
|
namespace osu.Game.Screens.Multi.RealtimeMultiplayer
|
|
|
|
{
|
2020-12-22 13:58:47 +08:00
|
|
|
// Todo: The "room" part of TimeshiftPlayer should be split out into an abstract player class to be inherited instead.
|
2020-12-20 23:04:06 +08:00
|
|
|
public class RealtimePlayer : TimeshiftPlayer
|
|
|
|
{
|
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 13:59:11 +08:00
|
|
|
private readonly ManualResetEventSlim startedEvent = new ManualResetEventSlim();
|
2020-12-20 23:13:05 +08:00
|
|
|
|
2020-12-23 05:31:40 +08:00
|
|
|
[CanBeNull]
|
2020-12-22 18:09:59 +08:00
|
|
|
private MultiplayerGameplayLeaderboard leaderboard;
|
2020-12-23 14:58:50 +08:00
|
|
|
|
2020-12-20 23:04:06 +08:00
|
|
|
public RealtimePlayer(PlaylistItem playlistItem)
|
2020-12-20 23:13:05 +08:00
|
|
|
: base(playlistItem, false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2020-12-20 23:04:06 +08:00
|
|
|
{
|
2020-12-20 23:13:05 +08:00
|
|
|
if (Token == null)
|
|
|
|
return; // Todo: Somehow handle token retrieval failure.
|
|
|
|
|
|
|
|
client.MatchStarted += onMatchStarted;
|
|
|
|
client.ResultsReady += onResultsReady;
|
2020-12-23 14:58:50 +08:00
|
|
|
|
|
|
|
isConnected = client.IsConnected.GetBoundCopy();
|
|
|
|
isConnected.BindValueChanged(connected =>
|
|
|
|
{
|
|
|
|
if (!connected.NewValue)
|
2020-12-23 15:32:58 +08:00
|
|
|
{
|
2020-12-23 14:58:50 +08:00
|
|
|
// messaging to the user about this disconnect will be provided by the RealtimeMatchSubScreen.
|
2020-12-23 15:51:11 +08:00
|
|
|
failAndBail();
|
2020-12-23 15:32:58 +08:00
|
|
|
}
|
2020-12-23 14:58:50 +08:00
|
|
|
}, true);
|
|
|
|
|
2020-12-23 16:10:34 +08:00
|
|
|
client.ChangeState(MultiplayerUserState.Loaded)
|
|
|
|
.ContinueWith(task => failAndBail(task.Exception?.Message ?? "Server error"), TaskContinuationOptions.NotOnRanToCompletion);
|
2020-12-20 23:13:05 +08:00
|
|
|
|
2020-12-22 13:59:11 +08:00
|
|
|
if (!startedEvent.Wait(TimeSpan.FromSeconds(30)))
|
|
|
|
{
|
2020-12-23 15:51:11 +08:00
|
|
|
failAndBail("Failed to start the multiplayer match in time.");
|
2020-12-23 20:31:36 +08:00
|
|
|
return;
|
2020-12-22 13:59:11 +08:00
|
|
|
}
|
2020-12-22 18:09:59 +08:00
|
|
|
|
|
|
|
Debug.Assert(client.Room != null);
|
|
|
|
|
|
|
|
int[] userIds = client.Room.Users.Where(u => u.State >= MultiplayerUserState.WaitingForLoad).Select(u => u.UserID).ToArray();
|
|
|
|
|
|
|
|
// 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);
|
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-23 15:51:11 +08:00
|
|
|
startedEvent.Set();
|
|
|
|
Schedule(PerformImmediateExit);
|
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()
|
|
|
|
{
|
|
|
|
if (leaderboard == null)
|
|
|
|
return;
|
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-22 13:59:11 +08:00
|
|
|
private void onMatchStarted() => startedEvent.Set();
|
2020-12-20 23:13:05 +08:00
|
|
|
|
|
|
|
private void onResultsReady() => resultsReady.SetResult(true);
|
|
|
|
|
|
|
|
protected override async Task SubmitScore(Score score)
|
|
|
|
{
|
|
|
|
await base.SubmitScore(score);
|
|
|
|
|
|
|
|
await client.ChangeState(MultiplayerUserState.FinishedPlay);
|
|
|
|
|
|
|
|
// Await up to 30 seconds for results to become available (3 api request timeouts).
|
|
|
|
// This is arbitrary just to not leave the player in an essentially deadlocked state if any connection issues occur.
|
|
|
|
await Task.WhenAny(resultsReady.Task, Task.Delay(TimeSpan.FromSeconds(30)));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override ResultsScreen CreateResults(ScoreInfo score)
|
|
|
|
{
|
|
|
|
Debug.Assert(RoomId.Value != null);
|
|
|
|
return new RealtimeResultsScreen(score, RoomId.Value.Value, PlaylistItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|