mirror of
https://github.com/ppy/osu.git
synced 2025-01-14 20:25:39 +08:00
Add basic implementation of spectator screen
This commit is contained in:
parent
154ccf1b49
commit
ac4671c594
@ -2,10 +2,22 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Screens;
|
||||
using osu.Game.Beatmaps;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Online.Spectator;
|
||||
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;
|
||||
using osu.Game.Users;
|
||||
|
||||
namespace osu.Game.Screens.Play
|
||||
@ -14,6 +26,26 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
private readonly User targetUser;
|
||||
|
||||
[Resolved]
|
||||
private Bindable<WorkingBeatmap> beatmap { get; set; }
|
||||
|
||||
[Resolved]
|
||||
private Bindable<RulesetInfo> ruleset { get; set; }
|
||||
|
||||
[Resolved]
|
||||
private Bindable<IReadOnlyList<Mod>> mods { get; set; }
|
||||
|
||||
[Resolved]
|
||||
private SpectatorStreamingClient spectatorStreaming { get; set; }
|
||||
|
||||
[Resolved]
|
||||
private BeatmapManager beatmaps { get; set; }
|
||||
|
||||
[Resolved]
|
||||
private RulesetStore rulesets { get; set; }
|
||||
|
||||
private Replay replay;
|
||||
|
||||
public Spectator([NotNull] User targetUser)
|
||||
{
|
||||
this.targetUser = targetUser ?? throw new ArgumentNullException(nameof(targetUser));
|
||||
@ -31,6 +63,87 @@ namespace osu.Game.Screens.Play
|
||||
Origin = Anchor.Centre,
|
||||
},
|
||||
};
|
||||
|
||||
spectatorStreaming.OnUserBeganPlaying += userBeganPlaying;
|
||||
spectatorStreaming.OnUserFinishedPlaying += userFinishedPlaying;
|
||||
spectatorStreaming.OnNewFrames += userSentFrames;
|
||||
|
||||
spectatorStreaming.WatchUser((int)targetUser.Id);
|
||||
}
|
||||
|
||||
private void userSentFrames(int userId, FrameDataBundle data)
|
||||
{
|
||||
if (userId != targetUser.Id)
|
||||
return;
|
||||
|
||||
var rulesetInstance = ruleset.Value.CreateInstance();
|
||||
|
||||
foreach (var frame in data.Frames)
|
||||
{
|
||||
IConvertibleReplayFrame convertibleFrame = rulesetInstance.CreateConvertibleReplayFrame();
|
||||
convertibleFrame.FromLegacy(frame, beatmap.Value.Beatmap, null);
|
||||
|
||||
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();
|
||||
|
||||
var resolvedRuleset = rulesets.AvailableRulesets.FirstOrDefault(r => r.ID == state.RulesetID)?.CreateInstance();
|
||||
|
||||
// ruleset not available
|
||||
if (resolvedRuleset == null)
|
||||
return;
|
||||
|
||||
var resolvedBeatmap = beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == state.BeatmapID);
|
||||
|
||||
if (resolvedBeatmap == null)
|
||||
return;
|
||||
|
||||
var scoreInfo = new ScoreInfo
|
||||
{
|
||||
Beatmap = resolvedBeatmap,
|
||||
Mods = state.Mods.Select(m => m.ToMod(resolvedRuleset)).ToArray(),
|
||||
Ruleset = resolvedRuleset.RulesetInfo,
|
||||
};
|
||||
|
||||
this.MakeCurrent();
|
||||
|
||||
ruleset.Value = resolvedRuleset.RulesetInfo;
|
||||
beatmap.Value = beatmaps.GetWorkingBeatmap(resolvedBeatmap);
|
||||
|
||||
this.Push(new ReplayPlayerLoader(new Score
|
||||
{
|
||||
ScoreInfo = scoreInfo,
|
||||
Replay = replay,
|
||||
}));
|
||||
}
|
||||
|
||||
private void userFinishedPlaying(int userId, SpectatorState state)
|
||||
{
|
||||
// todo: handle this in some way?
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user