1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-31 05:32:57 +08:00

Separate the local user state

This commit is contained in:
Dan Balasescu 2025-01-17 15:59:25 +09:00
parent 5425d62186
commit a51938f4e9
No known key found for this signature in database
3 changed files with 38 additions and 13 deletions

View File

@ -37,6 +37,11 @@ namespace osu.Game.Online.Metadata
/// </summary> /// </summary>
public abstract IBindable<bool> IsWatchingUserPresence { get; } public abstract IBindable<bool> IsWatchingUserPresence { get; }
/// <summary>
/// The <see cref="UserPresence"/> information about the current user.
/// </summary>
public abstract UserPresence LocalUserState { get; }
/// <summary> /// <summary>
/// Dictionary keyed by user ID containing all of the <see cref="UserPresence"/> information about currently online users received from the server. /// Dictionary keyed by user ID containing all of the <see cref="UserPresence"/> information about currently online users received from the server.
/// </summary> /// </summary>

View File

@ -23,6 +23,9 @@ namespace osu.Game.Online.Metadata
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence; public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
private readonly BindableBool isWatchingUserPresence = new BindableBool(); private readonly BindableBool isWatchingUserPresence = new BindableBool();
public override UserPresence LocalUserState => localUserState;
private UserPresence localUserState;
public override IBindableDictionary<int, UserPresence> UserStates => userStates; public override IBindableDictionary<int, UserPresence> UserStates => userStates;
private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>(); private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>();
@ -110,6 +113,7 @@ namespace osu.Game.Online.Metadata
userStates.Clear(); userStates.Clear();
friendStates.Clear(); friendStates.Clear();
dailyChallengeInfo.Value = null; dailyChallengeInfo.Value = null;
localUserState = default;
}); });
return; return;
} }
@ -202,9 +206,19 @@ namespace osu.Game.Online.Metadata
Schedule(() => Schedule(() =>
{ {
if (presence?.Status != null) if (presence?.Status != null)
{
if (userId == api.LocalUser.Value.OnlineID)
localUserState = presence.Value;
else
userStates[userId] = presence.Value; userStates[userId] = presence.Value;
}
else
{
if (userId == api.LocalUser.Value.OnlineID)
localUserState = default;
else else
userStates.Remove(userId); userStates.Remove(userId);
}
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -242,14 +256,7 @@ namespace osu.Game.Online.Metadata
throw new OperationCanceledException(); throw new OperationCanceledException();
// must be scheduled before any remote calls to avoid mis-ordering. // must be scheduled before any remote calls to avoid mis-ordering.
Schedule(() => Schedule(() => userStates.Clear());
{
bool hadLocalUserState = userStates.TryGetValue(api.LocalUser.Value.OnlineID, out var presence);
userStates.Clear();
if (hadLocalUserState)
userStates[api.LocalUser.Value.OnlineID] = presence;
});
Debug.Assert(connection != null); Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence)).ConfigureAwait(false); await connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence)).ConfigureAwait(false);
Logger.Log($@"{nameof(OnlineMetadataClient)} stopped watching user presence", LoggingTarget.Network); Logger.Log($@"{nameof(OnlineMetadataClient)} stopped watching user presence", LoggingTarget.Network);

View File

@ -19,6 +19,9 @@ namespace osu.Game.Tests.Visual.Metadata
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence; public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
private readonly BindableBool isWatchingUserPresence = new BindableBool(); private readonly BindableBool isWatchingUserPresence = new BindableBool();
public override UserPresence LocalUserState => localUserState;
private UserPresence localUserState;
public override IBindableDictionary<int, UserPresence> UserStates => userStates; public override IBindableDictionary<int, UserPresence> UserStates => userStates;
private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>(); private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>();
@ -71,11 +74,21 @@ namespace osu.Game.Tests.Visual.Metadata
{ {
if (isWatchingUserPresence.Value) if (isWatchingUserPresence.Value)
{ {
if (presence.HasValue) if (presence?.Status != null)
{
if (userId == api.LocalUser.Value.OnlineID)
localUserState = presence.Value;
else
userStates[userId] = presence.Value; userStates[userId] = presence.Value;
}
else
{
if (userId == api.LocalUser.Value.OnlineID)
localUserState = default;
else else
userStates.Remove(userId); userStates.Remove(userId);
} }
}
return Task.CompletedTask; return Task.CompletedTask;
} }