2021-04-08 21:07:00 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-04-08 21:07:00 +08:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2021-04-22 21:59:47 +08:00
|
|
|
using JetBrains.Annotations;
|
2021-04-08 21:07:00 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-06-11 18:15:53 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-04-08 21:07:00 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-04-09 17:41:48 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-08-11 17:16:25 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2021-08-25 16:30:13 +08:00
|
|
|
using osu.Game.Graphics;
|
2021-04-26 21:23:44 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2022-02-25 15:03:28 +08:00
|
|
|
using osu.Game.Online.Rooms;
|
2021-04-08 21:07:00 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
2021-04-14 19:39:14 +08:00
|
|
|
using osu.Game.Screens.Play;
|
2021-08-09 18:05:23 +08:00
|
|
|
using osu.Game.Screens.Play.HUD;
|
2021-04-08 21:07:00 +08:00
|
|
|
using osu.Game.Screens.Spectate;
|
2022-02-25 15:03:46 +08:00
|
|
|
using osu.Game.Users;
|
2022-02-25 15:03:28 +08:00
|
|
|
using osuTK;
|
2021-04-08 21:07:00 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
|
|
|
{
|
2021-04-22 22:52:22 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A <see cref="SpectatorScreen"/> that spectates multiple users in a match.
|
|
|
|
/// </summary>
|
2021-04-22 22:39:02 +08:00
|
|
|
public class MultiSpectatorScreen : SpectatorScreen
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
|
|
|
// Isolates beatmap/ruleset to this screen.
|
|
|
|
public override bool DisallowExternalBeatmapRulesetChanges => true;
|
|
|
|
|
2021-06-08 21:48:07 +08:00
|
|
|
// We are managing our own adjustments. For now, this happens inside the Player instances themselves.
|
2021-09-16 15:08:09 +08:00
|
|
|
public override bool? AllowTrackAdjustments => false;
|
2021-06-08 21:48:07 +08:00
|
|
|
|
2021-04-22 22:52:22 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether all spectating players have finished loading.
|
|
|
|
/// </summary>
|
2021-04-08 21:07:00 +08:00
|
|
|
public bool AllPlayersLoaded => instances.All(p => p?.PlayerLoaded == true);
|
|
|
|
|
2022-02-25 15:03:46 +08:00
|
|
|
protected override UserActivity InitialActivity => new UserActivity.SpectatingMultiplayerGame(Beatmap.Value.BeatmapInfo, Ruleset.Value);
|
|
|
|
|
2021-08-25 16:30:13 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
2021-04-26 21:23:44 +08:00
|
|
|
[Resolved]
|
2021-05-20 14:39:45 +08:00
|
|
|
private MultiplayerClient multiplayerClient { get; set; }
|
2021-04-26 21:23:44 +08:00
|
|
|
|
2021-04-22 22:52:22 +08:00
|
|
|
private readonly PlayerArea[] instances;
|
2021-04-15 18:12:52 +08:00
|
|
|
private MasterGameplayClockContainer masterClockContainer;
|
2021-04-16 21:47:52 +08:00
|
|
|
private ISyncManager syncManager;
|
2021-04-08 21:07:00 +08:00
|
|
|
private PlayerGrid grid;
|
2021-04-22 22:39:02 +08:00
|
|
|
private MultiSpectatorLeaderboard leaderboard;
|
2021-04-22 22:52:22 +08:00
|
|
|
private PlayerArea currentAudioSource;
|
2021-06-29 21:45:18 +08:00
|
|
|
private bool canStartMasterClock;
|
2021-04-08 21:07:00 +08:00
|
|
|
|
2022-02-25 15:03:28 +08:00
|
|
|
private readonly Room room;
|
2021-08-10 17:39:20 +08:00
|
|
|
private readonly MultiplayerRoomUser[] users;
|
|
|
|
|
2021-04-22 22:52:22 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new <see cref="MultiSpectatorScreen"/>.
|
|
|
|
/// </summary>
|
2022-02-25 15:03:28 +08:00
|
|
|
/// <param name="room">The room.</param>
|
2021-08-10 17:39:20 +08:00
|
|
|
/// <param name="users">The players to spectate.</param>
|
2022-02-25 15:03:28 +08:00
|
|
|
public MultiSpectatorScreen(Room room, MultiplayerRoomUser[] users)
|
2021-08-10 17:39:20 +08:00
|
|
|
: base(users.Select(u => u.UserID).ToArray())
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
2022-02-25 15:03:28 +08:00
|
|
|
this.room = room;
|
2021-08-10 17:39:20 +08:00
|
|
|
this.users = users;
|
|
|
|
|
|
|
|
instances = new PlayerArea[Users.Count];
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2022-02-25 15:03:28 +08:00
|
|
|
FillFlowContainer leaderboardFlow;
|
2021-08-09 18:05:23 +08:00
|
|
|
Container scoreDisplayContainer;
|
|
|
|
|
2021-08-11 17:16:25 +08:00
|
|
|
masterClockContainer = CreateMasterGameplayClockContainer(Beatmap.Value);
|
2021-04-09 17:41:48 +08:00
|
|
|
|
2021-04-21 22:21:03 +08:00
|
|
|
InternalChildren = new[]
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
2021-04-21 22:21:03 +08:00
|
|
|
(Drawable)(syncManager = new CatchUpSyncManager(masterClockContainer)),
|
|
|
|
masterClockContainer.WithChild(new GridContainer
|
2021-04-09 17:41:48 +08:00
|
|
|
{
|
2021-04-14 19:39:14 +08:00
|
|
|
RelativeSizeAxes = Axes.Both,
|
2021-08-09 18:05:23 +08:00
|
|
|
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
|
2021-04-14 19:39:14 +08:00
|
|
|
Content = new[]
|
2021-04-09 17:41:48 +08:00
|
|
|
{
|
2021-04-14 19:39:14 +08:00
|
|
|
new Drawable[]
|
2021-04-09 17:41:48 +08:00
|
|
|
{
|
2021-08-09 18:05:23 +08:00
|
|
|
scoreDisplayContainer = new Container
|
2021-04-14 19:39:14 +08:00
|
|
|
{
|
2021-08-09 18:05:23 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y
|
2021-04-14 19:39:14 +08:00
|
|
|
},
|
2021-08-09 18:05:23 +08:00
|
|
|
},
|
|
|
|
new Drawable[]
|
|
|
|
{
|
|
|
|
new GridContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
ColumnDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
|
|
|
|
Content = new[]
|
|
|
|
{
|
|
|
|
new Drawable[]
|
|
|
|
{
|
2022-02-25 15:03:28 +08:00
|
|
|
leaderboardFlow = new FillFlowContainer
|
2021-08-09 18:05:23 +08:00
|
|
|
{
|
2022-02-25 15:03:28 +08:00
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Spacing = new Vector2(5)
|
2021-08-09 18:05:23 +08:00
|
|
|
},
|
|
|
|
grid = new PlayerGrid { RelativeSizeAxes = Axes.Both }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 19:39:14 +08:00
|
|
|
}
|
2021-04-09 17:41:48 +08:00
|
|
|
}
|
2021-04-21 22:21:03 +08:00
|
|
|
})
|
2021-04-15 18:12:52 +08:00
|
|
|
};
|
|
|
|
|
2021-08-10 17:39:20 +08:00
|
|
|
for (int i = 0; i < Users.Count; i++)
|
2022-08-22 18:14:06 +08:00
|
|
|
grid.Add(instances[i] = new PlayerArea(Users[i], syncManager.AddClock()));
|
2021-04-16 11:25:29 +08:00
|
|
|
|
2022-05-30 18:18:38 +08:00
|
|
|
LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(users)
|
2021-05-03 13:48:04 +08:00
|
|
|
{
|
|
|
|
Expanded = { Value = true },
|
2022-06-24 20:25:23 +08:00
|
|
|
}, _ =>
|
2021-06-29 16:29:25 +08:00
|
|
|
{
|
|
|
|
foreach (var instance in instances)
|
|
|
|
leaderboard.AddClock(instance.UserId, instance.GameplayClock);
|
|
|
|
|
2022-02-25 15:03:28 +08:00
|
|
|
leaderboardFlow.Insert(0, leaderboard);
|
2021-08-09 18:05:23 +08:00
|
|
|
|
2021-08-11 13:44:13 +08:00
|
|
|
if (leaderboard.TeamScores.Count == 2)
|
2021-08-09 18:05:23 +08:00
|
|
|
{
|
|
|
|
LoadComponentAsync(new MatchScoreDisplay
|
|
|
|
{
|
|
|
|
Team1Score = { BindTarget = leaderboard.TeamScores.First().Value },
|
|
|
|
Team2Score = { BindTarget = leaderboard.TeamScores.Last().Value },
|
|
|
|
}, scoreDisplayContainer.Add);
|
|
|
|
}
|
2021-06-29 16:29:25 +08:00
|
|
|
});
|
2022-02-25 15:03:28 +08:00
|
|
|
|
|
|
|
LoadComponentAsync(new GameplayChatDisplay(room)
|
|
|
|
{
|
|
|
|
Expanded = { Value = true },
|
|
|
|
}, chat => leaderboardFlow.Insert(1, chat));
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:12:52 +08:00
|
|
|
protected override void LoadComplete()
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
2021-04-15 18:12:52 +08:00
|
|
|
base.LoadComplete();
|
2021-04-14 19:39:14 +08:00
|
|
|
|
2021-04-21 16:13:01 +08:00
|
|
|
masterClockContainer.Reset();
|
2021-06-29 21:45:18 +08:00
|
|
|
|
|
|
|
syncManager.ReadyToStart += onReadyToStart;
|
|
|
|
syncManager.MasterState.BindValueChanged(onMasterStateChanged, true);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-22 21:59:47 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
if (!isCandidateAudioSource(currentAudioSource?.GameplayClock))
|
|
|
|
{
|
|
|
|
currentAudioSource = instances.Where(i => isCandidateAudioSource(i.GameplayClock))
|
2021-04-26 16:19:44 +08:00
|
|
|
.OrderBy(i => Math.Abs(i.GameplayClock.CurrentTime - syncManager.MasterClock.CurrentTime))
|
2021-04-22 21:59:47 +08:00
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
foreach (var instance in instances)
|
2021-04-26 18:01:30 +08:00
|
|
|
instance.Mute = instance != currentAudioSource;
|
2021-04-22 21:59:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:19:44 +08:00
|
|
|
private bool isCandidateAudioSource([CanBeNull] ISpectatorPlayerClock clock)
|
2021-04-22 21:59:47 +08:00
|
|
|
=> clock?.IsRunning == true && !clock.IsCatchingUp && !clock.WaitingOnFrames.Value;
|
|
|
|
|
2021-06-11 15:25:45 +08:00
|
|
|
private void onReadyToStart()
|
|
|
|
{
|
2021-06-11 17:39:50 +08:00
|
|
|
// Seek the master clock to the gameplay time.
|
|
|
|
// This is chosen as the first available frame in the players' replays, which matches the seek by each individual SpectatorPlayer.
|
2021-10-27 12:04:41 +08:00
|
|
|
double startTime = instances.Where(i => i.Score != null)
|
|
|
|
.SelectMany(i => i.Score.Replay.Frames)
|
|
|
|
.Select(f => f.Time)
|
|
|
|
.DefaultIfEmpty(0)
|
|
|
|
.Min();
|
2021-06-11 15:25:45 +08:00
|
|
|
|
2022-04-13 12:49:58 +08:00
|
|
|
masterClockContainer.StartTime = startTime;
|
|
|
|
masterClockContainer.Reset(true);
|
2021-06-29 21:45:18 +08:00
|
|
|
|
|
|
|
// Although the clock has been started, this flag is set to allow for later synchronisation state changes to also be able to start it.
|
|
|
|
canStartMasterClock = true;
|
2021-06-11 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 18:15:53 +08:00
|
|
|
private void onMasterStateChanged(ValueChangedEvent<MasterClockState> state)
|
|
|
|
{
|
2021-06-11 18:23:25 +08:00
|
|
|
switch (state.NewValue)
|
|
|
|
{
|
|
|
|
case MasterClockState.Synchronised:
|
2021-06-29 21:45:18 +08:00
|
|
|
if (canStartMasterClock)
|
|
|
|
masterClockContainer.Start();
|
|
|
|
|
2021-06-11 18:23:25 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MasterClockState.TooFarAhead:
|
|
|
|
masterClockContainer.Stop();
|
|
|
|
break;
|
|
|
|
}
|
2021-06-11 18:15:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-09 11:20:07 +08:00
|
|
|
protected override void OnNewPlayingUserState(int userId, SpectatorState spectatorState)
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-02 01:08:56 +08:00
|
|
|
protected override void StartGameplay(int userId, SpectatorGameplayState spectatorGameplayState)
|
|
|
|
=> instances.Single(i => i.UserId == userId).LoadScore(spectatorGameplayState.Score);
|
2021-04-08 21:07:00 +08:00
|
|
|
|
2022-02-03 20:50:15 +08:00
|
|
|
protected override void EndGameplay(int userId, SpectatorState state)
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
2022-02-08 20:20:33 +08:00
|
|
|
// Allowed passed/failed users to complete their remaining replay frames.
|
|
|
|
// The failed state isn't really possible in multiplayer (yet?) but is added here just for safety in case it starts being used.
|
2022-02-09 11:09:04 +08:00
|
|
|
if (state.State == SpectatedUserState.Passed || state.State == SpectatedUserState.Failed)
|
2022-02-03 20:50:15 +08:00
|
|
|
return;
|
|
|
|
|
2022-08-06 10:12:18 +08:00
|
|
|
// we could also potentially receive EndGameplay with "Playing" state, at which point we can only early-return and hope it's a passing player.
|
|
|
|
// todo: this shouldn't exist, but it's here as a hotfix for an issue with multi-spectator screen not proceeding to results screen.
|
|
|
|
// see: https://github.com/ppy/osu/issues/19593
|
|
|
|
if (state.State == SpectatedUserState.Playing)
|
|
|
|
return;
|
|
|
|
|
2021-04-26 20:25:34 +08:00
|
|
|
RemoveUser(userId);
|
2021-08-25 16:30:13 +08:00
|
|
|
|
|
|
|
var instance = instances.Single(i => i.UserId == userId);
|
|
|
|
|
|
|
|
instance.FadeColour(colours.Gray4, 400, Easing.OutQuint);
|
2022-08-22 18:14:06 +08:00
|
|
|
syncManager.RemoveClock(instance.GameplayClock);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 20:55:38 +08:00
|
|
|
public override bool OnBackButton()
|
|
|
|
{
|
2022-02-08 10:29:39 +08:00
|
|
|
if (multiplayerClient.Room == null)
|
|
|
|
return base.OnBackButton();
|
2021-12-15 16:37:39 +08:00
|
|
|
|
|
|
|
// On a manual exit, set the player back to idle unless gameplay has finished.
|
|
|
|
if (multiplayerClient.Room.State != MultiplayerRoomState.Open)
|
|
|
|
multiplayerClient.ChangeState(MultiplayerUserState.Idle);
|
|
|
|
|
2021-04-26 20:55:38 +08:00
|
|
|
return base.OnBackButton();
|
|
|
|
}
|
2021-08-11 17:16:25 +08:00
|
|
|
|
|
|
|
protected virtual MasterGameplayClockContainer CreateMasterGameplayClockContainer(WorkingBeatmap beatmap) => new MasterGameplayClockContainer(beatmap, 0);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
}
|