2021-05-12 11:16:41 +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.
|
|
|
|
|
2021-05-12 12:06:28 +08:00
|
|
|
using System.Collections.Concurrent;
|
2021-05-12 11:16:41 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Utils;
|
|
|
|
using osu.Game.Online;
|
|
|
|
using osu.Game.Online.Spectator;
|
|
|
|
using osu.Game.Replays.Legacy;
|
|
|
|
using osu.Game.Scoring;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Spectator
|
|
|
|
{
|
2021-05-20 14:55:07 +08:00
|
|
|
public class TestSpectatorClient : SpectatorClient
|
2021-05-12 11:16:41 +08:00
|
|
|
{
|
|
|
|
public new BindableList<int> PlayingUsers => (BindableList<int>)base.PlayingUsers;
|
2021-05-12 12:06:28 +08:00
|
|
|
private readonly ConcurrentDictionary<int, byte> watchingUsers = new ConcurrentDictionary<int, byte>();
|
2021-05-12 11:16:41 +08:00
|
|
|
|
|
|
|
private readonly Dictionary<int, int> userBeatmapDictionary = new Dictionary<int, int>();
|
|
|
|
private readonly Dictionary<int, bool> userSentStateDictionary = new Dictionary<int, bool>();
|
|
|
|
|
2021-05-20 14:55:07 +08:00
|
|
|
public TestSpectatorClient()
|
2021-05-12 11:16:41 +08:00
|
|
|
: base(new DevelopmentEndpointConfiguration())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartPlay(int userId, int beatmapId)
|
|
|
|
{
|
|
|
|
userBeatmapDictionary[userId] = beatmapId;
|
|
|
|
sendState(userId, beatmapId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void EndPlay(int userId, int beatmapId)
|
|
|
|
{
|
|
|
|
((ISpectatorClient)this).UserFinishedPlaying(userId, new SpectatorState
|
|
|
|
{
|
|
|
|
BeatmapID = beatmapId,
|
|
|
|
RulesetID = 0,
|
|
|
|
});
|
|
|
|
|
2021-05-12 11:19:36 +08:00
|
|
|
userBeatmapDictionary.Remove(userId);
|
|
|
|
userSentStateDictionary.Remove(userId);
|
2021-05-12 11:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SendFrames(int userId, int index, int count)
|
|
|
|
{
|
|
|
|
var frames = new List<LegacyReplayFrame>();
|
|
|
|
|
|
|
|
for (int i = index; i < index + count; i++)
|
|
|
|
{
|
|
|
|
var buttonState = i == index + count - 1 ? ReplayButtonState.None : ReplayButtonState.Left1;
|
|
|
|
|
|
|
|
frames.Add(new LegacyReplayFrame(i * 100, RNG.Next(0, 512), RNG.Next(0, 512), buttonState));
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:22:15 +08:00
|
|
|
var bundle = new FrameDataBundle(new ScoreInfo { Combo = index + count }, frames);
|
2021-05-12 11:16:41 +08:00
|
|
|
((ISpectatorClient)this).UserSentFrames(userId, bundle);
|
|
|
|
|
|
|
|
if (!userSentStateDictionary[userId])
|
|
|
|
sendState(userId, userBeatmapDictionary[userId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void WatchUser(int userId)
|
|
|
|
{
|
2021-05-12 12:06:28 +08:00
|
|
|
base.WatchUser(userId);
|
|
|
|
|
2021-05-12 11:16:41 +08:00
|
|
|
// When newly watching a user, the server sends the playing state immediately.
|
2021-05-12 12:06:28 +08:00
|
|
|
if (watchingUsers.TryAdd(userId, 0) && PlayingUsers.Contains(userId))
|
2021-05-12 11:16:41 +08:00
|
|
|
sendState(userId, userBeatmapDictionary[userId]);
|
|
|
|
}
|
|
|
|
|
2021-05-12 11:19:36 +08:00
|
|
|
public override void StopWatchingUser(int userId)
|
|
|
|
{
|
|
|
|
base.StopWatchingUser(userId);
|
2021-05-12 12:06:28 +08:00
|
|
|
watchingUsers.TryRemove(userId, out _);
|
2021-05-12 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
2021-05-12 11:16:41 +08:00
|
|
|
private void sendState(int userId, int beatmapId)
|
|
|
|
{
|
|
|
|
((ISpectatorClient)this).UserBeganPlaying(userId, new SpectatorState
|
|
|
|
{
|
|
|
|
BeatmapID = beatmapId,
|
|
|
|
RulesetID = 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
userSentStateDictionary[userId] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|