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.
|
|
|
|
|
|
|
|
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;
|
|
|
|
using osu.Framework.Graphics;
|
2021-04-09 17:41:48 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-04-08 21:07:00 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
2021-04-15 18:32:55 +08:00
|
|
|
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync;
|
2021-04-14 19:39:14 +08:00
|
|
|
using osu.Game.Screens.Play;
|
2021-04-08 21:07:00 +08:00
|
|
|
using osu.Game.Screens.Spectate;
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
|
2021-04-26 20:25:34 +08:00
|
|
|
private readonly int[] userIds;
|
|
|
|
|
2021-04-08 21:07:00 +08:00
|
|
|
[Resolved]
|
|
|
|
private SpectatorStreamingClient spectatorClient { get; set; }
|
|
|
|
|
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-04-08 21:07:00 +08:00
|
|
|
|
2021-04-22 22:52:22 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new <see cref="MultiSpectatorScreen"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userIds">The players to spectate.</param>
|
2021-04-22 22:39:02 +08:00
|
|
|
public MultiSpectatorScreen(int[] userIds)
|
2021-04-23 18:09:54 +08:00
|
|
|
: base(userIds.Take(PlayerGrid.MAX_PLAYERS).ToArray())
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
2021-04-26 20:25:34 +08:00
|
|
|
this.userIds = GetUserIds().ToArray();
|
|
|
|
instances = new PlayerArea[this.userIds.Length];
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2021-04-09 17:41:48 +08:00
|
|
|
Container leaderboardContainer;
|
2021-04-21 22:21:03 +08:00
|
|
|
masterClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0);
|
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,
|
|
|
|
ColumnDimensions = new[]
|
|
|
|
{
|
|
|
|
new Dimension(GridSizeMode.AutoSize)
|
|
|
|
},
|
|
|
|
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-04-14 19:39:14 +08:00
|
|
|
leaderboardContainer = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
AutoSizeAxes = Axes.X
|
|
|
|
},
|
|
|
|
grid = new PlayerGrid { RelativeSizeAxes = Axes.Both }
|
|
|
|
}
|
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-04-26 20:25:34 +08:00
|
|
|
for (int i = 0; i < userIds.Length; i++)
|
2021-04-26 16:30:27 +08:00
|
|
|
{
|
2021-04-26 20:25:34 +08:00
|
|
|
grid.Add(instances[i] = new PlayerArea(userIds[i], masterClockContainer.GameplayClock));
|
2021-04-26 16:30:27 +08:00
|
|
|
syncManager.AddPlayerClock(instances[i].GameplayClock);
|
|
|
|
}
|
2021-04-16 11:25:29 +08:00
|
|
|
|
2021-04-09 17:41:48 +08:00
|
|
|
// Todo: This is not quite correct - it should be per-user to adjust for other mod combinations.
|
|
|
|
var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value);
|
|
|
|
var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor();
|
|
|
|
scoreProcessor.ApplyBeatmap(playableBeatmap);
|
|
|
|
|
2021-04-26 20:25:34 +08:00
|
|
|
LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, userIds) { Expanded = { Value = true } }, leaderboardContainer.Add);
|
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-15 18:12:52 +08:00
|
|
|
masterClockContainer.Stop();
|
2021-04-21 16:13:01 +08:00
|
|
|
masterClockContainer.Reset();
|
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-04-08 21:07:00 +08:00
|
|
|
protected override void OnUserStateChanged(int userId, SpectatorState spectatorState)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void StartGameplay(int userId, GameplayState gameplayState)
|
|
|
|
{
|
2021-04-16 12:28:32 +08:00
|
|
|
var instance = instances[getIndexForUser(userId)];
|
|
|
|
instance.LoadScore(gameplayState.Score);
|
2021-04-08 21:07:00 +08:00
|
|
|
|
2021-04-26 16:19:44 +08:00
|
|
|
syncManager.AddPlayerClock(instance.GameplayClock);
|
2021-04-16 11:25:29 +08:00
|
|
|
leaderboard.AddClock(instance.UserId, instance.GameplayClock);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void EndGameplay(int userId)
|
|
|
|
{
|
2021-04-26 20:25:34 +08:00
|
|
|
RemoveUser(userId);
|
2021-04-09 17:41:48 +08:00
|
|
|
leaderboard.RemoveClock(userId);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 20:55:38 +08:00
|
|
|
public override bool OnBackButton()
|
|
|
|
{
|
|
|
|
// On a manual exit, set the player state back to idle.
|
|
|
|
multiplayerClient.ChangeState(MultiplayerUserState.Idle);
|
|
|
|
return base.OnBackButton();
|
|
|
|
}
|
|
|
|
|
2021-04-26 20:25:34 +08:00
|
|
|
private int getIndexForUser(int userId) => Array.IndexOf(userIds, userId);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
}
|