1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Add initial classes for spectator support

This commit is contained in:
Dean Herbert 2020-10-21 19:05:20 +09:00
parent da573c7487
commit b39a4da6bc
7 changed files with 139 additions and 1 deletions

View File

@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -13,7 +15,9 @@ using osu.Framework.Input.StateChanges;
using osu.Framework.Testing;
using osu.Framework.Threading;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.Spectator;
using osu.Game.Replays;
using osu.Game.Replays.Legacy;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Replays;
using osu.Game.Rulesets.UI;
@ -260,13 +264,27 @@ namespace osu.Game.Tests.Visual.Gameplay
internal class TestReplayRecorder : ReplayRecorder<TestAction>
{
private readonly SpectatorClient client;
public TestReplayRecorder(Replay target)
: base(target)
{
var connection = new HubConnectionBuilder()
.WithUrl("http://localhost:5009/spectator")
.AddMessagePackProtocol()
// .ConfigureLogging(logging => { logging.AddConsole(); })
.Build();
connection.StartAsync().Wait();
client = new SpectatorClient(connection);
}
protected override ReplayFrame HandleFrame(Vector2 mousePosition, List<TestAction> actions, ReplayFrame previousFrame)
=> new TestReplayFrame(Time.Current, mousePosition, actions.ToArray());
{
client.SendFrames(new FrameDataBundle(new[] { new LegacyReplayFrame(Time.Current, mousePosition.X, mousePosition.Y, ReplayButtonState.None) }));
return new TestReplayFrame(Time.Current, mousePosition, actions.ToArray());
}
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using osu.Game.Replays.Legacy;
namespace osu.Game.Online.Spectator
{
[Serializable]
public class FrameDataBundle
{
public IEnumerable<LegacyReplayFrame> Frames { get; set; }
public FrameDataBundle(IEnumerable<LegacyReplayFrame> frames)
{
Frames = frames;
}
}
}

View File

@ -0,0 +1,14 @@
using System.Threading.Tasks;
using osu.Game.Online.Spectator;
namespace osu.Server.Spectator.Hubs
{
public interface ISpectatorClient
{
Task UserBeganPlaying(string userId, int beatmapId);
Task UserFinishedPlaying(string userId, int beatmapId);
Task UserSentFrames(string userId, FrameDataBundle data);
}
}

View File

@ -0,0 +1,14 @@
using System.Threading.Tasks;
namespace osu.Game.Online.Spectator
{
public interface ISpectatorServer
{
Task BeginPlaySession(int beatmapId);
Task SendFrameData(FrameDataBundle data);
Task EndPlaySession(int beatmapId);
Task StartWatchingUser(string userId);
Task EndWatchingUser(string userId);
}
}

View File

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using osu.Server.Spectator.Hubs;
namespace osu.Game.Online.Spectator
{
public class SpectatorClient : ISpectatorClient
{
private readonly HubConnection connection;
private readonly List<string> watchingUsers = new List<string>();
public SpectatorClient(HubConnection connection)
{
this.connection = connection;
// this is kind of SILLY
// https://github.com/dotnet/aspnetcore/issues/15198
connection.On<string, int>(nameof(ISpectatorClient.UserBeganPlaying), ((ISpectatorClient)this).UserBeganPlaying);
connection.On<string, FrameDataBundle>(nameof(ISpectatorClient.UserSentFrames), ((ISpectatorClient)this).UserSentFrames);
connection.On<string, int>(nameof(ISpectatorClient.UserFinishedPlaying), ((ISpectatorClient)this).UserFinishedPlaying);
}
Task ISpectatorClient.UserBeganPlaying(string userId, int beatmapId)
{
if (connection.ConnectionId != userId)
{
if (watchingUsers.Contains(userId))
{
Console.WriteLine($"{connection.ConnectionId} received began playing for already watched user {userId}");
}
else
{
Console.WriteLine($"{connection.ConnectionId} requesting watch other user {userId}");
WatchUser(userId);
watchingUsers.Add(userId);
}
}
else
{
Console.WriteLine($"{connection.ConnectionId} Received user playing event for self {beatmapId}");
}
return Task.CompletedTask;
}
Task ISpectatorClient.UserFinishedPlaying(string userId, int beatmapId)
{
Console.WriteLine($"{connection.ConnectionId} Received user finished event {beatmapId}");
return Task.CompletedTask;
}
Task ISpectatorClient.UserSentFrames(string userId, FrameDataBundle data)
{
Console.WriteLine($"{connection.ConnectionId} Received frames from {userId}: {data.Frames.First().ToString()}");
return Task.CompletedTask;
}
public Task BeginPlaying(int beatmapId) => connection.SendAsync(nameof(ISpectatorServer.BeginPlaySession), beatmapId);
public Task SendFrames(FrameDataBundle data) => connection.SendAsync(nameof(ISpectatorServer.SendFrameData), data);
public Task EndPlaying(int beatmapId) => connection.SendAsync(nameof(ISpectatorServer.EndPlaySession), beatmapId);
private Task WatchUser(string userId) => connection.SendAsync(nameof(ISpectatorServer.StartWatchingUser), userId);
}
}

View File

@ -1,6 +1,7 @@
// 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 MessagePack;
using osu.Game.Rulesets.Replays;
using osuTK;
@ -8,6 +9,7 @@ namespace osu.Game.Replays.Legacy
{
public class LegacyReplayFrame : ReplayFrame
{
[IgnoreMember]
public Vector2 Position => new Vector2(MouseX ?? 0, MouseY ?? 0);
public float? MouseX;

View File

@ -21,6 +21,9 @@
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="DiffPlex" Version="1.6.3" />
<PackageReference Include="Humanizer" Version="2.8.26" />
<PackageReference Include="MessagePack" Version="1.7.3.7" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.9" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.2.6" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />