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;
|
2020-10-26 19:59:46 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-10-26 18:47:39 +08:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Allocation;
|
2020-10-26 19:59:46 +08:00
|
|
|
using osu.Framework.Bindables;
|
2020-10-26 18:47:39 +08:00
|
|
|
using osu.Framework.Graphics;
|
2020-10-27 18:23:35 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-10-26 19:59:46 +08:00
|
|
|
using osu.Framework.Screens;
|
|
|
|
using osu.Game.Beatmaps;
|
2020-10-27 18:23:35 +08:00
|
|
|
using osu.Game.Graphics;
|
2020-10-26 18:47:39 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2020-10-27 18:23:35 +08:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
2020-10-26 19:59:46 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
2020-10-27 18:23:35 +08:00
|
|
|
using osu.Game.Overlays.BeatmapListing.Panels;
|
2020-10-26 19:59:46 +08:00
|
|
|
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-26 18:47:39 +08:00
|
|
|
using osu.Game.Users;
|
2020-10-27 18:23:35 +08:00
|
|
|
using osuTK;
|
2020-10-26 18:47:39 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
|
|
|
public class Spectator : OsuScreen
|
|
|
|
{
|
|
|
|
private readonly User targetUser;
|
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<WorkingBeatmap> beatmap { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private Bindable<RulesetInfo> ruleset { get; set; }
|
|
|
|
|
2020-10-28 15:33:52 +08:00
|
|
|
private Ruleset rulesetInstance;
|
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<IReadOnlyList<Mod>> mods { get; set; }
|
|
|
|
|
2020-10-27 18:23:35 +08:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
[Resolved]
|
|
|
|
private SpectatorStreamingClient spectatorStreaming { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private BeatmapManager beatmaps { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private RulesetStore rulesets { get; set; }
|
|
|
|
|
|
|
|
private Replay replay;
|
|
|
|
|
2020-10-27 18:23:35 +08:00
|
|
|
private Container beatmapPanelContainer;
|
|
|
|
|
|
|
|
private SpectatorState state;
|
|
|
|
|
|
|
|
private IBindable<WeakReference<BeatmapSetInfo>> managerUpdated;
|
|
|
|
|
2020-10-26 18:47:39 +08:00
|
|
|
public Spectator([NotNull] User targetUser)
|
|
|
|
{
|
|
|
|
this.targetUser = targetUser ?? throw new ArgumentNullException(nameof(targetUser));
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
2020-10-27 18:23:35 +08:00
|
|
|
new FillFlowContainer
|
2020-10-26 18:47:39 +08:00
|
|
|
{
|
2020-10-27 18:23:35 +08:00
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
2020-10-26 18:47:39 +08:00
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
2020-10-27 18:23:35 +08:00
|
|
|
Spacing = new Vector2(15),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = "Currently spectating",
|
|
|
|
Font = OsuFont.Default.With(size: 30),
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
},
|
|
|
|
new UserGridPanel(targetUser)
|
|
|
|
{
|
|
|
|
Width = 290,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
},
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = "playing",
|
|
|
|
Font = OsuFont.Default.With(size: 30),
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
},
|
|
|
|
beatmapPanelContainer = new Container
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
},
|
|
|
|
}
|
2020-10-26 18:47:39 +08:00
|
|
|
},
|
|
|
|
};
|
2020-10-27 13:57:00 +08:00
|
|
|
}
|
2020-10-26 19:59:46 +08:00
|
|
|
|
2020-10-27 13:57:00 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2020-10-27 16:11:07 +08:00
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
spectatorStreaming.OnUserBeganPlaying += userBeganPlaying;
|
|
|
|
spectatorStreaming.OnUserFinishedPlaying += userFinishedPlaying;
|
|
|
|
spectatorStreaming.OnNewFrames += userSentFrames;
|
|
|
|
|
|
|
|
spectatorStreaming.WatchUser((int)targetUser.Id);
|
2020-10-27 18:23:35 +08:00
|
|
|
|
|
|
|
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))
|
|
|
|
attemptStart();
|
2020-10-26 19:59:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void userSentFrames(int userId, FrameDataBundle data)
|
|
|
|
{
|
|
|
|
if (userId != targetUser.Id)
|
|
|
|
return;
|
|
|
|
|
2020-10-27 13:57:00 +08:00
|
|
|
// 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;
|
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
foreach (var frame in data.Frames)
|
|
|
|
{
|
|
|
|
IConvertibleReplayFrame convertibleFrame = rulesetInstance.CreateConvertibleReplayFrame();
|
2020-10-28 15:33:52 +08:00
|
|
|
convertibleFrame.FromLegacy(frame, beatmap.Value.Beatmap);
|
2020-10-26 19:59:46 +08:00
|
|
|
|
|
|
|
var convertedFrame = (ReplayFrame)convertibleFrame;
|
|
|
|
convertedFrame.Time = frame.Time;
|
|
|
|
|
|
|
|
replay.Frames.Add(convertedFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void userBeganPlaying(int userId, SpectatorState state)
|
|
|
|
{
|
|
|
|
if (userId != targetUser.Id)
|
|
|
|
return;
|
|
|
|
|
2020-10-27 15:27:15 +08:00
|
|
|
replay ??= new Replay { HasReceivedAllFrames = false };
|
2020-10-26 19:59:46 +08:00
|
|
|
|
2020-10-27 18:23:35 +08:00
|
|
|
this.state = state;
|
|
|
|
|
|
|
|
attemptStart();
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:11:14 +08:00
|
|
|
private void userFinishedPlaying(int userId, SpectatorState state)
|
|
|
|
{
|
|
|
|
if (userId != targetUser.Id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (replay == null) return;
|
|
|
|
|
|
|
|
replay.HasReceivedAllFrames = true;
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:23:35 +08:00
|
|
|
private void attemptStart()
|
|
|
|
{
|
2020-10-26 19:59:46 +08:00
|
|
|
var resolvedRuleset = rulesets.AvailableRulesets.FirstOrDefault(r => r.ID == state.RulesetID)?.CreateInstance();
|
|
|
|
|
|
|
|
// ruleset not available
|
|
|
|
if (resolvedRuleset == null)
|
|
|
|
return;
|
|
|
|
|
2020-10-27 18:23:35 +08:00
|
|
|
if (state.BeatmapID == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.MakeCurrent();
|
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
var resolvedBeatmap = beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == state.BeatmapID);
|
|
|
|
|
|
|
|
if (resolvedBeatmap == null)
|
2020-10-27 18:23:35 +08:00
|
|
|
{
|
|
|
|
showBeatmapPanel(state.BeatmapID.Value);
|
2020-10-26 19:59:46 +08:00
|
|
|
return;
|
2020-10-27 18:23:35 +08:00
|
|
|
}
|
2020-10-26 19:59:46 +08:00
|
|
|
|
|
|
|
var scoreInfo = new ScoreInfo
|
|
|
|
{
|
|
|
|
Beatmap = resolvedBeatmap,
|
|
|
|
Mods = state.Mods.Select(m => m.ToMod(resolvedRuleset)).ToArray(),
|
|
|
|
Ruleset = resolvedRuleset.RulesetInfo,
|
|
|
|
};
|
|
|
|
|
|
|
|
ruleset.Value = resolvedRuleset.RulesetInfo;
|
2020-10-28 15:33:52 +08:00
|
|
|
rulesetInstance = resolvedRuleset;
|
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
beatmap.Value = beatmaps.GetWorkingBeatmap(resolvedBeatmap);
|
|
|
|
|
2020-10-27 17:56:28 +08:00
|
|
|
this.Push(new SpectatorPlayerLoader(new Score
|
2020-10-26 19:59:46 +08:00
|
|
|
{
|
|
|
|
ScoreInfo = scoreInfo,
|
|
|
|
Replay = replay,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:23:35 +08:00
|
|
|
private void showBeatmapPanel(int beatmapId)
|
|
|
|
{
|
|
|
|
var req = new GetBeatmapSetRequest(beatmapId, BeatmapSetLookupType.BeatmapId);
|
|
|
|
req.Success += res => Schedule(() =>
|
|
|
|
{
|
|
|
|
beatmapPanelContainer.Child = new GridBeatmapPanel(res.ToBeatmapSet(rulesets));
|
|
|
|
});
|
|
|
|
|
|
|
|
api.Queue(req);
|
|
|
|
}
|
|
|
|
|
2020-10-26 19:59:46 +08:00
|
|
|
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);
|
|
|
|
}
|
2020-10-27 18:23:35 +08:00
|
|
|
|
2020-10-28 06:29:07 +08:00
|
|
|
managerUpdated?.UnbindAll();
|
2020-10-26 18:47:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|