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;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
public class MultiplayerSpectator : SpectatorScreen
|
|
|
|
{
|
|
|
|
// Isolates beatmap/ruleset to this screen.
|
|
|
|
public override bool DisallowExternalBeatmapRulesetChanges => true;
|
|
|
|
|
|
|
|
public bool AllPlayersLoaded => instances.All(p => p?.PlayerLoaded == true);
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private SpectatorStreamingClient spectatorClient { get; set; }
|
|
|
|
|
|
|
|
private readonly PlayerInstance[] instances;
|
2021-04-15 18:12:52 +08:00
|
|
|
private MasterGameplayClockContainer masterClockContainer;
|
2021-04-15 18:32:55 +08:00
|
|
|
private ISpectatorSyncManager syncManager;
|
2021-04-08 21:07:00 +08:00
|
|
|
private PlayerGrid grid;
|
2021-04-09 17:41:48 +08:00
|
|
|
private MultiplayerSpectatorLeaderboard leaderboard;
|
2021-04-08 21:07:00 +08:00
|
|
|
|
2021-04-08 21:14:26 +08:00
|
|
|
public MultiplayerSpectator(int[] userIds)
|
|
|
|
: base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray())
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
2021-04-16 11:25:29 +08:00
|
|
|
instances = new PlayerInstance[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-15 18:12:52 +08:00
|
|
|
masterClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0)
|
2021-04-08 21:07:00 +08:00
|
|
|
{
|
2021-04-14 19:39:14 +08:00
|
|
|
Child = 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-08 21:07:00 +08:00
|
|
|
};
|
2021-04-09 17:41:48 +08:00
|
|
|
|
2021-04-15 18:12:52 +08:00
|
|
|
InternalChildren = new[]
|
|
|
|
{
|
2021-04-15 18:32:55 +08:00
|
|
|
(Drawable)(syncManager = new SpectatorCatchUpSyncManager(masterClockContainer)),
|
2021-04-15 18:12:52 +08:00
|
|
|
masterClockContainer
|
|
|
|
};
|
|
|
|
|
2021-04-16 11:25:29 +08:00
|
|
|
for (int i = 0; i < UserIds.Length; i++)
|
|
|
|
grid.Add(instances[i] = new PlayerInstance(UserIds[i], new SpectatorCatchUpSlaveClock(masterClockContainer.GameplayClock)));
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
LoadComponentAsync(leaderboard = new MultiplayerSpectatorLeaderboard(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();
|
|
|
|
masterClockContainer.Restart();
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnUserStateChanged(int userId, SpectatorState spectatorState)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void StartGameplay(int userId, GameplayState gameplayState)
|
|
|
|
{
|
|
|
|
int userIndex = getIndexForUser(userId);
|
|
|
|
|
2021-04-16 11:25:29 +08:00
|
|
|
var instance = instances[userIndex];
|
|
|
|
syncManager.RemoveSlave(instance.GameplayClock);
|
|
|
|
leaderboard.RemoveClock(instance.UserId);
|
2021-04-08 21:07:00 +08:00
|
|
|
|
2021-04-16 11:25:29 +08:00
|
|
|
instance.LoadPlayer(gameplayState.Score);
|
|
|
|
syncManager.AddSlave(instance.GameplayClock);
|
|
|
|
leaderboard.AddClock(instance.UserId, instance.GameplayClock);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void EndGameplay(int userId)
|
|
|
|
{
|
|
|
|
spectatorClient.StopWatchingUser(userId);
|
2021-04-09 17:41:48 +08:00
|
|
|
leaderboard.RemoveClock(userId);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 21:14:26 +08:00
|
|
|
private int getIndexForUser(int userId) => Array.IndexOf(UserIds, userId);
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
}
|