2020-12-20 15:04:06 +00: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 15:13:05 +00:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
2021-08-09 09:13:28 +00:00
|
|
|
using System.Linq;
|
2020-12-20 15:13:05 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Allocation;
|
2020-12-23 06:58:50 +00:00
|
|
|
using osu.Framework.Bindables;
|
2021-08-09 09:13:28 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-12-22 05:59:11 +00:00
|
|
|
using osu.Framework.Logging;
|
2020-12-24 06:32:55 +00:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2020-12-20 15:04:06 +00:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2020-12-25 04:38:11 +00:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-20 15:13:05 +00:00
|
|
|
using osu.Game.Scoring;
|
2020-12-23 08:39:08 +00:00
|
|
|
using osu.Game.Screens.Play;
|
2020-12-22 10:09:59 +00:00
|
|
|
using osu.Game.Screens.Play.HUD;
|
2020-12-20 15:13:05 +00:00
|
|
|
using osu.Game.Screens.Ranking;
|
2021-08-22 01:54:07 +00:00
|
|
|
using osu.Game.Users;
|
2020-12-22 10:09:59 +00:00
|
|
|
using osuTK;
|
2020-12-20 15:04:06 +00:00
|
|
|
|
2020-12-25 15:50:00 +00:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer
|
2020-12-20 15:04:06 +00:00
|
|
|
{
|
2021-03-23 06:33:31 +00:00
|
|
|
public class MultiplayerPlayer : RoomSubmittingPlayer
|
2020-12-20 15:04:06 +00:00
|
|
|
{
|
2020-12-20 15:13:05 +00:00
|
|
|
protected override bool PauseOnFocusLost => false;
|
|
|
|
|
|
|
|
// Disallow fails in multiplayer for now.
|
|
|
|
protected override bool CheckModsAllowFailure() => false;
|
|
|
|
|
2021-08-22 01:54:07 +00:00
|
|
|
protected override UserActivity InitialActivity => new UserActivity.InMultiplayerGame(Beatmap.Value.BeatmapInfo, Ruleset.Value);
|
|
|
|
|
2020-12-20 15:13:05 +00:00
|
|
|
[Resolved]
|
2021-05-20 06:39:45 +00:00
|
|
|
private MultiplayerClient client { get; set; }
|
2020-12-20 15:13:05 +00:00
|
|
|
|
2020-12-23 10:57:43 +00:00
|
|
|
private IBindable<bool> isConnected;
|
|
|
|
|
2020-12-20 15:13:05 +00:00
|
|
|
private readonly TaskCompletionSource<bool> resultsReady = new TaskCompletionSource<bool>();
|
|
|
|
|
2020-12-22 10:09:59 +00:00
|
|
|
private MultiplayerGameplayLeaderboard leaderboard;
|
2020-12-23 06:58:50 +00:00
|
|
|
|
2021-08-10 09:39:20 +00:00
|
|
|
private readonly MultiplayerRoomUser[] users;
|
2020-12-24 01:38:53 +00:00
|
|
|
|
2020-12-24 06:32:55 +00:00
|
|
|
private LoadingLayer loadingDisplay;
|
2021-08-09 09:13:28 +00:00
|
|
|
private FillFlowContainer leaderboardFlow;
|
2020-12-24 06:32:55 +00:00
|
|
|
|
2020-12-24 01:38:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a multiplayer player.
|
|
|
|
/// </summary>
|
2021-08-24 04:22:06 +00:00
|
|
|
/// <param name="room">The room.</param>
|
2020-12-24 01:38:53 +00:00
|
|
|
/// <param name="playlistItem">The playlist item to be played.</param>
|
2021-08-10 09:39:20 +00:00
|
|
|
/// <param name="users">The users which are participating in this game.</param>
|
2021-08-24 04:22:06 +00:00
|
|
|
public MultiplayerPlayer(Room room, PlaylistItem playlistItem, MultiplayerRoomUser[] users)
|
|
|
|
: base(room, playlistItem, new PlayerConfiguration
|
2020-12-23 08:39:08 +00:00
|
|
|
{
|
|
|
|
AllowPause = false,
|
|
|
|
AllowRestart = false,
|
2021-04-16 04:59:10 +00:00
|
|
|
AllowSkipping = false,
|
2020-12-23 08:39:08 +00:00
|
|
|
})
|
2020-12-20 15:13:05 +00:00
|
|
|
{
|
2021-08-10 09:39:20 +00:00
|
|
|
this.users = users;
|
2020-12-20 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2020-12-20 15:04:06 +00:00
|
|
|
{
|
2021-08-05 10:01:07 +00:00
|
|
|
if (!LoadedBeatmapSuccessfully)
|
|
|
|
return;
|
|
|
|
|
2021-08-09 09:13:28 +00:00
|
|
|
HUDOverlay.Add(leaderboardFlow = new FillFlowContainer
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
2021-08-17 05:57:48 +00:00
|
|
|
Spacing = new Vector2(5)
|
2021-08-09 09:13:28 +00:00
|
|
|
});
|
|
|
|
|
2020-12-24 13:32:30 +00:00
|
|
|
// todo: this should be implemented via a custom HUD implementation, and correctly masked to the main content area.
|
2022-03-08 03:57:59 +00:00
|
|
|
LoadComponentAsync(leaderboard = new MultiplayerGameplayLeaderboard(GameplayState.Ruleset.RulesetInfo, ScoreProcessor, users), l =>
|
2021-08-09 09:13:28 +00:00
|
|
|
{
|
|
|
|
if (!LoadedBeatmapSuccessfully)
|
|
|
|
return;
|
|
|
|
|
|
|
|
((IBindable<bool>)leaderboard.Expanded).BindTo(HUDOverlay.ShowHud);
|
|
|
|
|
2021-08-17 05:57:48 +00:00
|
|
|
leaderboardFlow.Insert(0, l);
|
2021-08-09 09:13:28 +00:00
|
|
|
|
|
|
|
if (leaderboard.TeamScores.Count >= 2)
|
|
|
|
{
|
|
|
|
LoadComponentAsync(new GameplayMatchScoreDisplay
|
|
|
|
{
|
|
|
|
Team1Score = { BindTarget = leaderboard.TeamScores.First().Value },
|
|
|
|
Team2Score = { BindTarget = leaderboard.TeamScores.Last().Value },
|
|
|
|
Expanded = { BindTarget = HUDOverlay.ShowHud },
|
2021-08-17 05:57:48 +00:00
|
|
|
}, scoreDisplay => leaderboardFlow.Insert(1, scoreDisplay));
|
2021-08-09 09:13:28 +00:00
|
|
|
}
|
|
|
|
});
|
2020-12-24 13:32:30 +00:00
|
|
|
|
2021-08-24 04:22:06 +00:00
|
|
|
LoadComponentAsync(new GameplayChatDisplay(Room)
|
2021-08-17 05:57:48 +00:00
|
|
|
{
|
|
|
|
Expanded = { BindTarget = HUDOverlay.ShowHud },
|
|
|
|
}, chat => leaderboardFlow.Insert(2, chat));
|
|
|
|
|
2021-01-04 13:42:39 +00:00
|
|
|
HUDOverlay.Add(loadingDisplay = new LoadingLayer(true) { Depth = float.MaxValue });
|
2021-03-23 07:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadAsyncComplete()
|
|
|
|
{
|
|
|
|
base.LoadAsyncComplete();
|
2020-12-24 13:32:30 +00:00
|
|
|
|
2021-08-05 10:01:07 +00:00
|
|
|
if (!LoadedBeatmapSuccessfully)
|
|
|
|
return;
|
|
|
|
|
2021-03-24 04:20:44 +00:00
|
|
|
if (!ValidForResume)
|
|
|
|
return; // token retrieval may have failed.
|
2020-12-20 15:13:05 +00:00
|
|
|
|
|
|
|
client.MatchStarted += onMatchStarted;
|
|
|
|
client.ResultsReady += onResultsReady;
|
|
|
|
|
2020-12-24 06:32:55 +00:00
|
|
|
ScoreProcessor.HasCompleted.BindValueChanged(completed =>
|
|
|
|
{
|
|
|
|
// wait for server to tell us that results are ready (see SubmitScore implementation)
|
|
|
|
loadingDisplay.Show();
|
|
|
|
});
|
|
|
|
|
2020-12-23 06:58:50 +00:00
|
|
|
isConnected = client.IsConnected.GetBoundCopy();
|
2021-01-12 10:04:16 +00:00
|
|
|
isConnected.BindValueChanged(connected => Schedule(() =>
|
2020-12-22 05:59:11 +00:00
|
|
|
{
|
2020-12-23 06:58:50 +00:00
|
|
|
if (!connected.NewValue)
|
2020-12-22 05:59:11 +00:00
|
|
|
{
|
2020-12-25 04:38:11 +00:00
|
|
|
// messaging to the user about this disconnect will be provided by the MultiplayerMatchSubScreen.
|
2020-12-23 07:51:11 +00:00
|
|
|
failAndBail();
|
2020-12-23 07:32:58 +00:00
|
|
|
}
|
2021-01-12 10:04:16 +00:00
|
|
|
}), true);
|
2020-12-23 06:58:50 +00:00
|
|
|
|
2020-12-22 10:09:59 +00:00
|
|
|
Debug.Assert(client.Room != null);
|
2020-12-24 06:32:55 +00: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 07:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void failAndBail(string message = null)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(message))
|
|
|
|
Logger.Log(message, LoggingTarget.Runtime, LogLevel.Important);
|
2020-12-22 05:59:11 +00:00
|
|
|
|
2020-12-23 16:37:49 +00:00
|
|
|
Schedule(() => PerformExit(false));
|
2020-12-20 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:09:59 +00:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
2021-08-05 10:01:07 +00:00
|
|
|
|
|
|
|
if (!LoadedBeatmapSuccessfully)
|
|
|
|
return;
|
|
|
|
|
2020-12-22 21:31:40 +00:00
|
|
|
adjustLeaderboardPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void adjustLeaderboardPosition()
|
|
|
|
{
|
2020-12-22 10:09:59 +00:00
|
|
|
const float padding = 44; // enough margin to avoid the hit error display.
|
|
|
|
|
2021-08-09 09:13:28 +00:00
|
|
|
leaderboardFlow.Position = new Vector2(padding, padding + HUDOverlay.TopScoringElementsHeight);
|
2020-12-20 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 06:32:55 +00:00
|
|
|
private void onMatchStarted() => Scheduler.Add(() =>
|
|
|
|
{
|
|
|
|
loadingDisplay.Hide();
|
|
|
|
base.StartGameplay();
|
|
|
|
});
|
2020-12-20 15:13:05 +00:00
|
|
|
|
|
|
|
private void onResultsReady() => resultsReady.SetResult(true);
|
|
|
|
|
2021-03-23 06:45:22 +00:00
|
|
|
protected override async Task PrepareScoreForResultsAsync(Score score)
|
2020-12-20 15:13:05 +00:00
|
|
|
{
|
2021-03-23 06:45:22 +00:00
|
|
|
await base.PrepareScoreForResultsAsync(score).ConfigureAwait(false);
|
2020-12-20 15:13:05 +00:00
|
|
|
|
2021-03-08 03:57:16 +00:00
|
|
|
await client.ChangeState(MultiplayerUserState.FinishedPlay).ConfigureAwait(false);
|
2020-12-20 15:13:05 +00:00
|
|
|
|
2020-12-24 10:33:49 +00:00
|
|
|
// Await up to 60 seconds for results to become available (6 api request timeouts).
|
2020-12-20 15:13:05 +00:00
|
|
|
// This is arbitrary just to not leave the player in an essentially deadlocked state if any connection issues occur.
|
2021-03-08 03:57:16 +00:00
|
|
|
await Task.WhenAny(resultsReady.Task, Task.Delay(TimeSpan.FromSeconds(60))).ConfigureAwait(false);
|
2020-12-20 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override ResultsScreen CreateResults(ScoreInfo score)
|
|
|
|
{
|
2021-08-24 05:25:29 +00:00
|
|
|
Debug.Assert(Room.RoomID.Value != null);
|
|
|
|
|
2021-08-12 18:01:31 +00:00
|
|
|
return leaderboard.TeamScores.Count == 2
|
2021-08-24 05:25:29 +00:00
|
|
|
? new MultiplayerTeamResultsScreen(score, Room.RoomID.Value.Value, PlaylistItem, leaderboard.TeamScores)
|
|
|
|
: new MultiplayerResultsScreen(score, Room.RoomID.Value.Value, PlaylistItem);
|
2020-12-20 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
if (client != null)
|
|
|
|
{
|
|
|
|
client.MatchStarted -= onMatchStarted;
|
|
|
|
client.ResultsReady -= onResultsReady;
|
|
|
|
}
|
2020-12-20 15:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|