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.
|
|
|
|
|
2022-02-24 01:18:35 +08:00
|
|
|
using System;
|
2021-05-12 11:16:41 +08:00
|
|
|
using System.Collections.Generic;
|
2021-05-20 16:41:46 +08:00
|
|
|
using System.Diagnostics;
|
2022-09-08 12:17:27 +08:00
|
|
|
using System.Linq;
|
2021-05-20 16:20:30 +08:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Allocation;
|
2021-05-12 11:16:41 +08:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Utils;
|
2021-05-20 16:20:30 +08:00
|
|
|
using osu.Game.Online.API;
|
2021-05-12 11:16:41 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
|
|
|
using osu.Game.Replays.Legacy;
|
2022-01-28 21:26:05 +08:00
|
|
|
using osu.Game.Rulesets.Replays;
|
2021-05-12 11:16:41 +08:00
|
|
|
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
|
|
|
{
|
2021-06-10 21:41:38 +08:00
|
|
|
/// <summary>
|
2022-02-24 01:18:35 +08:00
|
|
|
/// Maximum number of frames sent per bundle via <see cref="SendFramesFromUser"/>.
|
2021-06-10 21:41:38 +08:00
|
|
|
/// </summary>
|
|
|
|
public const int FRAME_BUNDLE_SIZE = 10;
|
|
|
|
|
2022-02-24 01:18:35 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to force send operations to fail (simulating a network issue).
|
|
|
|
/// </summary>
|
|
|
|
public bool ShouldFailSendingFrames { get; set; }
|
|
|
|
|
2022-02-24 01:18:42 +08:00
|
|
|
public int FrameSendAttempts { get; private set; }
|
|
|
|
|
2021-05-20 16:20:30 +08:00
|
|
|
public override IBindable<bool> IsConnected { get; } = new Bindable<bool>(true);
|
|
|
|
|
2022-01-31 14:12:08 +08:00
|
|
|
public IReadOnlyDictionary<int, ReplayFrame> LastReceivedUserFrames => lastReceivedUserFrames;
|
|
|
|
|
|
|
|
private readonly Dictionary<int, ReplayFrame> lastReceivedUserFrames = new Dictionary<int, ReplayFrame>();
|
|
|
|
|
2021-05-12 11:16:41 +08:00
|
|
|
private readonly Dictionary<int, int> userBeatmapDictionary = new Dictionary<int, int>();
|
2022-09-07 19:00:24 +08:00
|
|
|
private readonly Dictionary<int, APIMod[]> userModsDictionary = new Dictionary<int, APIMod[]>();
|
2021-06-10 21:41:38 +08:00
|
|
|
private readonly Dictionary<int, int> userNextFrameDictionary = new Dictionary<int, int>();
|
2021-05-12 11:16:41 +08:00
|
|
|
|
2021-05-20 16:20:30 +08:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; } = null!;
|
2021-05-12 11:16:41 +08:00
|
|
|
|
2022-01-28 21:26:05 +08:00
|
|
|
public TestSpectatorClient()
|
|
|
|
{
|
2022-02-02 22:00:43 +08:00
|
|
|
OnNewFrames += (i, bundle) => lastReceivedUserFrames[i] = bundle.Frames[^1];
|
2022-01-28 21:26:05 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:41:46 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Starts play for an arbitrary user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to start play for.</param>
|
|
|
|
/// <param name="beatmapId">The playing beatmap id.</param>
|
2022-09-07 19:00:24 +08:00
|
|
|
/// <param name="mods">The mods the user has applied.</param>
|
|
|
|
public void SendStartPlay(int userId, int beatmapId, APIMod[]? mods = null)
|
2021-05-12 11:16:41 +08:00
|
|
|
{
|
|
|
|
userBeatmapDictionary[userId] = beatmapId;
|
2022-09-07 19:00:24 +08:00
|
|
|
userModsDictionary[userId] = mods ?? Array.Empty<APIMod>();
|
2021-06-10 21:41:38 +08:00
|
|
|
userNextFrameDictionary[userId] = 0;
|
2021-05-20 16:41:46 +08:00
|
|
|
sendPlayingState(userId);
|
2021-05-12 11:16:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:41:46 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Ends play for an arbitrary user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to end play for.</param>
|
2022-02-02 22:00:43 +08:00
|
|
|
/// <param name="state">The spectator state to end play with.</param>
|
2022-02-22 16:56:07 +08:00
|
|
|
public void SendEndPlay(int userId, SpectatedUserState state = SpectatedUserState.Quit)
|
2021-05-12 11:16:41 +08:00
|
|
|
{
|
2022-02-02 22:00:43 +08:00
|
|
|
if (!userBeatmapDictionary.ContainsKey(userId))
|
2021-05-20 16:41:46 +08:00
|
|
|
return;
|
|
|
|
|
2021-05-12 11:16:41 +08:00
|
|
|
((ISpectatorClient)this).UserFinishedPlaying(userId, new SpectatorState
|
|
|
|
{
|
2021-05-20 16:41:46 +08:00
|
|
|
BeatmapID = userBeatmapDictionary[userId],
|
2021-05-12 11:16:41 +08:00
|
|
|
RulesetID = 0,
|
2022-09-07 19:00:24 +08:00
|
|
|
Mods = userModsDictionary[userId],
|
2022-02-02 22:00:43 +08:00
|
|
|
State = state
|
2021-05-12 11:16:41 +08:00
|
|
|
});
|
2022-02-02 22:00:43 +08:00
|
|
|
|
|
|
|
userBeatmapDictionary.Remove(userId);
|
2022-09-07 19:00:24 +08:00
|
|
|
userModsDictionary.Remove(userId);
|
2021-05-12 11:16:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:41:46 +08:00
|
|
|
/// <summary>
|
2021-06-10 21:41:38 +08:00
|
|
|
/// Sends frames for an arbitrary user, in bundles containing 10 frames each.
|
2022-02-24 01:18:35 +08:00
|
|
|
/// This bypasses the standard queueing mechanism completely and should only be used to test cases where multiple users need to be sending data.
|
|
|
|
/// Importantly, <see cref="ShouldFailSendingFrames"/> will have no effect.
|
2021-05-20 16:41:46 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to send frames for.</param>
|
2021-06-10 21:41:38 +08:00
|
|
|
/// <param name="count">The total number of frames to send.</param>
|
2022-03-17 18:23:49 +08:00
|
|
|
/// <param name="startTime">The time to start gameplay frames from.</param>
|
|
|
|
public void SendFramesFromUser(int userId, int count, double startTime = 0)
|
2021-05-12 11:16:41 +08:00
|
|
|
{
|
|
|
|
var frames = new List<LegacyReplayFrame>();
|
|
|
|
|
2021-06-10 21:41:38 +08:00
|
|
|
int currentFrameIndex = userNextFrameDictionary[userId];
|
|
|
|
int lastFrameIndex = currentFrameIndex + count - 1;
|
|
|
|
|
|
|
|
for (; currentFrameIndex <= lastFrameIndex; currentFrameIndex++)
|
2021-05-12 11:16:41 +08:00
|
|
|
{
|
2021-06-10 21:41:38 +08:00
|
|
|
// This is done in the next frame so that currentFrameIndex is updated to the correct value.
|
|
|
|
if (frames.Count == FRAME_BUNDLE_SIZE)
|
|
|
|
flush();
|
2021-05-12 11:16:41 +08:00
|
|
|
|
2021-06-10 21:41:38 +08:00
|
|
|
var buttonState = currentFrameIndex == lastFrameIndex ? ReplayButtonState.None : ReplayButtonState.Left1;
|
2022-03-17 18:23:49 +08:00
|
|
|
frames.Add(new LegacyReplayFrame(currentFrameIndex * 100 + startTime, RNG.Next(0, 512), RNG.Next(0, 512), buttonState));
|
2021-05-12 11:16:41 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 21:41:38 +08:00
|
|
|
flush();
|
|
|
|
|
|
|
|
userNextFrameDictionary[userId] = currentFrameIndex;
|
|
|
|
|
|
|
|
void flush()
|
|
|
|
{
|
|
|
|
if (frames.Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var bundle = new FrameDataBundle(new ScoreInfo { Combo = currentFrameIndex }, frames.ToArray());
|
|
|
|
((ISpectatorClient)this).UserSentFrames(userId, bundle);
|
|
|
|
|
|
|
|
frames.Clear();
|
|
|
|
}
|
2021-05-12 11:16:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:41:46 +08:00
|
|
|
protected override Task BeginPlayingInternal(SpectatorState state)
|
|
|
|
{
|
|
|
|
// Track the local user's playing beatmap ID.
|
|
|
|
Debug.Assert(state.BeatmapID != null);
|
|
|
|
userBeatmapDictionary[api.LocalUser.Value.Id] = state.BeatmapID.Value;
|
2022-09-08 12:17:27 +08:00
|
|
|
userModsDictionary[api.LocalUser.Value.Id] = state.Mods.ToArray();
|
2021-05-20 16:41:46 +08:00
|
|
|
|
|
|
|
return ((ISpectatorClient)this).UserBeganPlaying(api.LocalUser.Value.Id, state);
|
|
|
|
}
|
2021-05-12 12:06:28 +08:00
|
|
|
|
2022-02-24 01:18:35 +08:00
|
|
|
protected override Task SendFramesInternal(FrameDataBundle bundle)
|
|
|
|
{
|
2022-02-24 01:18:42 +08:00
|
|
|
FrameSendAttempts++;
|
|
|
|
|
2022-02-24 01:18:35 +08:00
|
|
|
if (ShouldFailSendingFrames)
|
2022-06-28 00:43:09 +08:00
|
|
|
return Task.FromException(new InvalidOperationException($"Intentional fail via {nameof(ShouldFailSendingFrames)}"));
|
2022-02-24 01:18:35 +08:00
|
|
|
|
|
|
|
return ((ISpectatorClient)this).UserSentFrames(api.LocalUser.Value.Id, bundle);
|
|
|
|
}
|
2021-05-20 16:20:30 +08:00
|
|
|
|
|
|
|
protected override Task EndPlayingInternal(SpectatorState state) => ((ISpectatorClient)this).UserFinishedPlaying(api.LocalUser.Value.Id, state);
|
|
|
|
|
|
|
|
protected override Task WatchUserInternal(int userId)
|
|
|
|
{
|
2021-05-12 11:16:41 +08:00
|
|
|
// When newly watching a user, the server sends the playing state immediately.
|
2022-02-02 22:00:43 +08:00
|
|
|
if (userBeatmapDictionary.ContainsKey(userId))
|
2021-05-20 16:41:46 +08:00
|
|
|
sendPlayingState(userId);
|
2021-05-20 16:20:30 +08:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2021-05-12 11:16:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:41:46 +08:00
|
|
|
protected override Task StopWatchingUserInternal(int userId) => Task.CompletedTask;
|
2021-05-12 11:19:36 +08:00
|
|
|
|
2021-05-20 16:41:46 +08:00
|
|
|
private void sendPlayingState(int userId)
|
2021-05-12 11:16:41 +08:00
|
|
|
{
|
|
|
|
((ISpectatorClient)this).UserBeganPlaying(userId, new SpectatorState
|
|
|
|
{
|
2021-05-20 16:41:46 +08:00
|
|
|
BeatmapID = userBeatmapDictionary[userId],
|
2021-05-12 11:16:41 +08:00
|
|
|
RulesetID = 0,
|
2022-09-07 19:00:24 +08:00
|
|
|
Mods = userModsDictionary[userId],
|
2022-02-09 11:09:04 +08:00
|
|
|
State = SpectatedUserState.Playing
|
2021-05-12 11:16:41 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|