1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-07 13:47:33 +08:00
osu-lazer/osu.Game/Screens/Play/Spectator.cs

312 lines
10 KiB
C#
Raw Normal View History

2020-10-26 18:47:39 +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.Collections.Generic;
using System.Linq;
2020-10-26 18:47:39 +08:00
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-10-26 18:47:39 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-10-28 17:35:20 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
2020-10-26 18:47:39 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Spectator;
using osu.Game.Overlays.BeatmapListing.Panels;
using osu.Game.Replays;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Replays;
using osu.Game.Rulesets.Replays.Types;
using osu.Game.Scoring;
2020-10-28 17:35:20 +08:00
using osu.Game.Screens.Multi.Match.Components;
2020-10-26 18:47:39 +08:00
using osu.Game.Users;
using osuTK;
2020-10-26 18:47:39 +08:00
namespace osu.Game.Screens.Play
{
public class Spectator : OsuScreen
{
private readonly User targetUser;
[Resolved]
private Bindable<WorkingBeatmap> beatmap { get; set; }
[Resolved]
private Bindable<RulesetInfo> ruleset { get; set; }
private Ruleset rulesetInstance;
[Resolved]
private Bindable<IReadOnlyList<Mod>> mods { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private SpectatorStreamingClient spectatorStreaming { get; set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
[Resolved]
private RulesetStore rulesets { get; set; }
private Replay replay;
private Container beatmapPanelContainer;
private SpectatorState state;
private IBindable<WeakReference<BeatmapSetInfo>> managerUpdated;
private TriangleButton watchButton;
2020-10-26 18:47:39 +08:00
public Spectator([NotNull] User targetUser)
{
this.targetUser = targetUser ?? throw new ArgumentNullException(nameof(targetUser));
}
[BackgroundDependencyLoader]
2020-10-28 17:35:20 +08:00
private void load(OsuColour colours)
2020-10-26 18:47:39 +08:00
{
2020-10-28 17:35:20 +08:00
InternalChild = new Container
2020-10-26 18:47:39 +08:00
{
2020-10-28 17:35:20 +08:00
Masking = true,
CornerRadius = 20,
AutoSizeAxes = Axes.Both,
AutoSizeDuration = 500,
AutoSizeEasing = Easing.OutQuint,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Children = new Drawable[]
2020-10-26 18:47:39 +08:00
{
2020-10-28 17:35:20 +08:00
new Box
{
2020-10-28 17:35:20 +08:00
Colour = colours.GreySeafoamDark,
RelativeSizeAxes = Axes.Both,
},
new FillFlowContainer
{
Margin = new MarginPadding(20),
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Spacing = new Vector2(15),
Children = new Drawable[]
{
2020-10-28 17:35:20 +08:00
new OsuSpriteText
{
Text = "Spectator Mode",
Font = OsuFont.Default.With(size: 30),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Spacing = new Vector2(15),
Children = new Drawable[]
{
new UserGridPanel(targetUser)
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Height = 145,
Width = 290,
},
new SpriteIcon
{
Size = new Vector2(40),
Icon = FontAwesome.Solid.ArrowRight,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
beatmapPanelContainer = new Container
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
}
},
watchButton = new PurpleTriangleButton
{
Text = "Start Watching",
Width = 250,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Action = attemptStart
}
}
}
2020-10-28 17:35:20 +08:00
}
2020-10-26 18:47:39 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-10-27 16:11:07 +08:00
spectatorStreaming.OnUserBeganPlaying += userBeganPlaying;
spectatorStreaming.OnUserFinishedPlaying += userFinishedPlaying;
spectatorStreaming.OnNewFrames += userSentFrames;
spectatorStreaming.WatchUser((int)targetUser.Id);
managerUpdated = beatmaps.ItemUpdated.GetBoundCopy();
managerUpdated.BindValueChanged(beatmapUpdated);
}
private void beatmapUpdated(ValueChangedEvent<WeakReference<BeatmapSetInfo>> beatmap)
{
if (beatmap.NewValue.TryGetTarget(out var beatmapSet) && beatmapSet.Beatmaps.Any(b => b.OnlineBeatmapID == state.BeatmapID))
2020-10-28 17:35:20 +08:00
Schedule(attemptStart);
}
private void userSentFrames(int userId, FrameDataBundle data)
{
if (userId != targetUser.Id)
return;
// this should never happen as the server sends the user's state on watching,
// but is here as a safety measure.
if (replay == null)
return;
foreach (var frame in data.Frames)
{
IConvertibleReplayFrame convertibleFrame = rulesetInstance.CreateConvertibleReplayFrame();
convertibleFrame.FromLegacy(frame, beatmap.Value.Beatmap);
var convertedFrame = (ReplayFrame)convertibleFrame;
convertedFrame.Time = frame.Time;
replay.Frames.Add(convertedFrame);
}
}
private void userBeganPlaying(int userId, SpectatorState state)
{
if (userId != targetUser.Id)
return;
replay ??= new Replay { HasReceivedAllFrames = false };
this.state = state;
Schedule(attemptStart);
}
private void userFinishedPlaying(int userId, SpectatorState state)
{
if (userId != targetUser.Id)
return;
if (replay != null)
{
replay.HasReceivedAllFrames = true;
replay = null;
}
2020-10-28 17:35:20 +08:00
clearDisplay();
}
2020-10-28 17:35:20 +08:00
private void clearDisplay()
{
watchButton.Enabled.Value = false;
2020-10-28 17:35:20 +08:00
beatmapPanelContainer.Clear();
}
2020-10-28 17:35:20 +08:00
private void attemptStart()
{
clearDisplay();
showBeatmapPanel(state);
var resolvedRuleset = rulesets.AvailableRulesets.FirstOrDefault(r => r.ID == state.RulesetID)?.CreateInstance();
// ruleset not available
if (resolvedRuleset == null)
return;
if (state.BeatmapID == null)
return;
var resolvedBeatmap = beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == state.BeatmapID);
if (resolvedBeatmap == null)
return;
2020-10-28 17:35:20 +08:00
if (replay == null)
return;
var scoreInfo = new ScoreInfo
{
Beatmap = resolvedBeatmap,
User = targetUser,
Mods = state.Mods.Select(m => m.ToMod(resolvedRuleset)).ToArray(),
Ruleset = resolvedRuleset.RulesetInfo,
};
ruleset.Value = resolvedRuleset.RulesetInfo;
rulesetInstance = resolvedRuleset;
beatmap.Value = beatmaps.GetWorkingBeatmap(resolvedBeatmap);
watchButton.Enabled.Value = true;
this.Push(new SpectatorPlayerLoader(new Score
{
ScoreInfo = scoreInfo,
Replay = replay,
}));
}
private void showBeatmapPanel(SpectatorState state)
{
2020-10-28 17:35:20 +08:00
if (state?.BeatmapID == null)
{
beatmapPanelContainer.Clear();
return;
}
var req = new GetBeatmapSetRequest(state.BeatmapID.Value, BeatmapSetLookupType.BeatmapId);
req.Success += res => Schedule(() =>
{
if (state != this.state)
return;
beatmapPanelContainer.Child = new GridBeatmapPanel(res.ToBeatmapSet(rulesets));
});
api.Queue(req);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (spectatorStreaming != null)
{
spectatorStreaming.OnUserBeganPlaying -= userBeganPlaying;
spectatorStreaming.OnUserFinishedPlaying -= userFinishedPlaying;
spectatorStreaming.OnNewFrames -= userSentFrames;
spectatorStreaming.StopWatchingUser((int)targetUser.Id);
}
managerUpdated?.UnbindAll();
2020-10-26 18:47:39 +08:00
}
}
}