1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 06:47:24 +08:00

Fix OnUserBeganPlaying not being invoked if already watching

This commit is contained in:
smoogipoo 2021-04-16 14:11:55 +09:00
parent 6a36e326bc
commit 5652490d61
2 changed files with 49 additions and 3 deletions

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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -204,6 +205,29 @@ namespace osu.Game.Tests.Visual.Gameplay
AddAssert("screen didn't change", () => Stack.CurrentScreen is SoloSpectator);
}
[Test]
public void OnUserBeganPlayingCallbackInvokedOnNewAdd()
{
bool callbackInvoked = false;
Action<int, SpectatorState> callbackAction = (_, __) => callbackInvoked = true;
AddStep("bind first event", () => testSpectatorStreamingClient.OnUserBeganPlaying += callbackAction);
start();
AddAssert("callback invoked", () => callbackInvoked);
AddStep("reset", () =>
{
testSpectatorStreamingClient.OnUserBeganPlaying -= callbackAction;
callbackInvoked = false;
});
AddStep("bind event again", () => testSpectatorStreamingClient.OnUserBeganPlaying += callbackAction);
AddAssert("callback invoked", () => callbackInvoked);
// Don't leave the event bound if test run succeeded.
AddStep("reset", () => testSpectatorStreamingClient.OnUserBeganPlaying -= callbackAction);
}
private OsuFramedReplayInputHandler replayHandler =>
(OsuFramedReplayInputHandler)Stack.ChildrenOfType<OsuInputManager>().First().ReplayInputHandler;

View File

@ -60,6 +60,7 @@ namespace osu.Game.Online.Spectator
private IBindable<IReadOnlyList<Mod>> currentMods { get; set; }
private readonly SpectatorState currentState = new SpectatorState();
private readonly Dictionary<int, SpectatorState> currentUserStates = new Dictionary<int, SpectatorState>();
private bool isPlaying;
@ -68,10 +69,25 @@ namespace osu.Game.Online.Spectator
/// </summary>
public event Action<int, FrameDataBundle> OnNewFrames;
private event Action<int, SpectatorState> onUserBeganPlaying;
/// <summary>
/// Called whenever a user starts a play session.
/// Called whenever a user starts a play session, or immediately if the user is being watched and currently in a play session.
/// </summary>
public event Action<int, SpectatorState> OnUserBeganPlaying;
public event Action<int, SpectatorState> OnUserBeganPlaying
{
add
{
onUserBeganPlaying += value;
lock (userLock)
{
foreach (var (userId, state) in currentUserStates)
value?.Invoke(userId, state);
}
}
remove => onUserBeganPlaying -= value;
}
/// <summary>
/// Called whenever a user finishes a play session.
@ -134,7 +150,10 @@ namespace osu.Game.Online.Spectator
if (!playingUsers.Contains(userId))
playingUsers.Add(userId);
OnUserBeganPlaying?.Invoke(userId, state);
lock (userLock)
currentUserStates[userId] = state;
onUserBeganPlaying?.Invoke(userId, state);
return Task.CompletedTask;
}
@ -143,6 +162,9 @@ namespace osu.Game.Online.Spectator
{
playingUsers.Remove(userId);
lock (userLock)
currentUserStates.Remove(userId);
OnUserFinishedPlaying?.Invoke(userId, state);
return Task.CompletedTask;