1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-13 16:47:46 +08:00

Extend metadata client with user presence-observing capabilities

This commit is contained in:
Bartłomiej Dach 2023-12-06 18:24:31 +01:00
parent 602550b9c2
commit 41c33f74f2
No known key found for this signature in database
6 changed files with 239 additions and 5 deletions

View File

@ -2,11 +2,23 @@
// See the LICENCE file in the repository root for full licence text.
using System.Threading.Tasks;
using osu.Game.Users;
namespace osu.Game.Online.Metadata
{
public interface IMetadataClient
/// <summary>
/// Interface for metadata-related remote procedure calls to be executed on the client side.
/// </summary>
public interface IMetadataClient : IStatefulUserHubClient
{
/// <summary>
/// Delivers the set of requested <see cref="BeatmapUpdates"/> to the client.
/// </summary>
Task BeatmapSetsUpdated(BeatmapUpdates updates);
/// <summary>
/// Delivers an update of the <see cref="UserPresence"/> of the user with the supplied <paramref name="userId"/>.
/// </summary>
Task UserPresenceUpdated(int userId, UserPresence? status);
}
}

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Threading.Tasks;
using osu.Game.Users;
namespace osu.Game.Online.Metadata
{
@ -17,5 +18,25 @@ namespace osu.Game.Online.Metadata
/// <param name="queueId">The last processed queue ID.</param>
/// <returns></returns>
Task<BeatmapUpdates> GetChangesSince(int queueId);
/// <summary>
/// Signals to the server that the current user's <see cref="UserActivity"/> has changed.
/// </summary>
Task UpdateActivity(UserActivity? activity);
/// <summary>
/// Signals to the server that the current user's <see cref="UserStatus"/> has changed.
/// </summary>
Task UpdateStatus(UserStatus? status);
/// <summary>
/// Signals to the server that the current user would like to begin receiving updates on other users' online presence.
/// </summary>
Task BeginWatchingUserPresence();
/// <summary>
/// Signals to the server that the current user would like to stop receiving updates on other users' online presence.
/// </summary>
Task EndWatchingUserPresence();
}
}

View File

@ -4,22 +4,71 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Users;
namespace osu.Game.Online.Metadata
{
public abstract partial class MetadataClient : Component, IMetadataClient, IMetadataServer
{
public abstract Task BeatmapSetsUpdated(BeatmapUpdates updates);
public abstract IBindable<bool> IsConnected { get; }
#region Beatmap metadata updates
public abstract Task<BeatmapUpdates> GetChangesSince(int queueId);
public Action<int[]>? ChangedBeatmapSetsArrived;
public abstract Task BeatmapSetsUpdated(BeatmapUpdates updates);
public event Action<int[]>? ChangedBeatmapSetsArrived;
protected Task ProcessChanges(int[] beatmapSetIDs)
{
ChangedBeatmapSetsArrived?.Invoke(beatmapSetIDs.Distinct().ToArray());
return Task.CompletedTask;
}
#endregion
#region User presence updates
/// <summary>
/// Whether the client is currently receiving user presence updates from the server.
/// </summary>
public abstract IBindable<bool> IsWatchingUserPresence { get; }
/// <summary>
/// Dictionary keyed by user ID containing all of the <see cref="UserPresence"/> information about currently online users received from the server.
/// </summary>
public abstract IBindableDictionary<int, UserPresence> UserStates { get; }
/// <inheritdoc/>
public abstract Task UpdateActivity(UserActivity? activity);
/// <inheritdoc/>
public abstract Task UpdateStatus(UserStatus? status);
/// <inheritdoc/>
public abstract Task BeginWatchingUserPresence();
/// <inheritdoc/>
public abstract Task EndWatchingUserPresence();
/// <inheritdoc/>
public abstract Task UserPresenceUpdated(int userId, UserPresence? presence);
#endregion
#region Disconnection handling
public event Action? Disconnecting;
public virtual Task DisconnectRequested()
{
Schedule(() => Disconnecting?.Invoke());
return Task.CompletedTask;
}
#endregion
}
}

View File

@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using osu.Framework.Allocation;
@ -10,17 +11,32 @@ using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Game.Configuration;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Users;
namespace osu.Game.Online.Metadata
{
public partial class OnlineMetadataClient : MetadataClient
{
public override IBindable<bool> IsConnected { get; } = new Bindable<bool>();
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
private readonly BindableBool isWatchingUserPresence = new BindableBool();
// ReSharper disable once InconsistentlySynchronizedField
public override IBindableDictionary<int, UserPresence> UserStates => userStates;
private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>();
private readonly string endpoint;
private IHubClientConnector? connector;
private Bindable<int> lastQueueId = null!;
private IBindable<APIUser> localUser = null!;
private IBindable<UserActivity?> userActivity = null!;
private IBindable<UserStatus?>? userStatus;
private HubConnection? connection => connector?.CurrentConnection;
public OnlineMetadataClient(EndpointConfiguration endpoints)
@ -33,7 +49,7 @@ namespace osu.Game.Online.Metadata
{
// Importantly, we are intentionally not using MessagePack here to correctly support derived class serialization.
// More information on the limitations / reasoning can be found in osu-server-spectator's initialisation code.
connector = api.GetHubConnector(nameof(OnlineMetadataClient), endpoint);
connector = api.GetHubConnector(nameof(OnlineMetadataClient), endpoint, false);
if (connector != null)
{
@ -42,12 +58,37 @@ namespace osu.Game.Online.Metadata
// this is kind of SILLY
// https://github.com/dotnet/aspnetcore/issues/15198
connection.On<BeatmapUpdates>(nameof(IMetadataClient.BeatmapSetsUpdated), ((IMetadataClient)this).BeatmapSetsUpdated);
connection.On<int, UserPresence?>(nameof(IMetadataClient.UserPresenceUpdated), ((IMetadataClient)this).UserPresenceUpdated);
};
connector.IsConnected.BindValueChanged(isConnectedChanged, true);
IsConnected.BindTo(connector.IsConnected);
IsConnected.BindValueChanged(isConnectedChanged, true);
}
lastQueueId = config.GetBindable<int>(OsuSetting.LastProcessedMetadataId);
localUser = api.LocalUser.GetBoundCopy();
userActivity = api.Activity.GetBoundCopy()!;
}
protected override void LoadComplete()
{
base.LoadComplete();
localUser.BindValueChanged(_ =>
{
if (localUser.Value is not GuestUser)
{
userStatus = localUser.Value.Status.GetBoundCopy();
userStatus.BindValueChanged(status => UpdateStatus(status.NewValue), true);
}
else
userStatus = null;
}, true);
userActivity.BindValueChanged(activity =>
{
if (localUser.Value is not GuestUser)
UpdateActivity(activity.NewValue);
}, true);
}
private bool catchingUp;
@ -55,7 +96,17 @@ namespace osu.Game.Online.Metadata
private void isConnectedChanged(ValueChangedEvent<bool> connected)
{
if (!connected.NewValue)
{
isWatchingUserPresence.Value = false;
userStates.Clear();
return;
}
if (localUser.Value is not GuestUser)
{
UpdateActivity(userActivity.Value);
UpdateStatus(userStatus?.Value);
}
if (lastQueueId.Value >= 0)
{
@ -116,6 +167,71 @@ namespace osu.Game.Online.Metadata
return connection.InvokeAsync<BeatmapUpdates>(nameof(IMetadataServer.GetChangesSince), queueId);
}
public override Task UpdateActivity(UserActivity? activity)
{
if (connector?.IsConnected.Value != true)
return Task.FromCanceled(new CancellationToken(true));
Debug.Assert(connection != null);
return connection.InvokeAsync(nameof(IMetadataServer.UpdateActivity), activity);
}
public override Task UpdateStatus(UserStatus? status)
{
if (connector?.IsConnected.Value != true)
return Task.FromCanceled(new CancellationToken(true));
Debug.Assert(connection != null);
return connection.InvokeAsync(nameof(IMetadataServer.UpdateStatus), status);
}
public override Task UserPresenceUpdated(int userId, UserPresence? presence)
{
lock (userStates)
{
if (presence != null)
userStates[userId] = presence.Value;
else
userStates.Remove(userId);
}
return Task.CompletedTask;
}
public override async Task BeginWatchingUserPresence()
{
if (connector?.IsConnected.Value != true)
throw new OperationCanceledException();
Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.BeginWatchingUserPresence)).ConfigureAwait(false);
isWatchingUserPresence.Value = true;
}
public override async Task EndWatchingUserPresence()
{
try
{
if (connector?.IsConnected.Value != true)
throw new OperationCanceledException();
// must happen synchronously before any remote calls to avoid misordering.
userStates.Clear();
Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence)).ConfigureAwait(false);
}
finally
{
isWatchingUserPresence.Value = false;
}
}
public override async Task DisconnectRequested()
{
await base.DisconnectRequested().ConfigureAwait(false);
await EndWatchingUserPresence().ConfigureAwait(false);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);

View File

@ -9,6 +9,7 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Screens;
using osu.Game.Online.API;
using osu.Game.Online.Metadata;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Spectator;
using osu.Game.Overlays;
@ -30,6 +31,9 @@ namespace osu.Game.Online
[Resolved]
private SpectatorClient spectatorClient { get; set; } = null!;
[Resolved]
private MetadataClient metadataClient { get; set; } = null!;
[Resolved]
private INotificationOverlay? notificationOverlay { get; set; }
@ -56,6 +60,7 @@ namespace osu.Game.Online
multiplayerClient.Disconnecting += notifyAboutForcedDisconnection;
spectatorClient.Disconnecting += notifyAboutForcedDisconnection;
metadataClient.Disconnecting += notifyAboutForcedDisconnection;
}
protected override void LoadComplete()
@ -131,6 +136,9 @@ namespace osu.Game.Online
if (multiplayerClient.IsNotNull())
multiplayerClient.Disconnecting -= notifyAboutForcedDisconnection;
if (metadataClient.IsNotNull())
metadataClient.Disconnecting -= notifyAboutForcedDisconnection;
}
}
}

View File

@ -0,0 +1,28 @@
// 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 MessagePack;
namespace osu.Game.Users
{
/// <summary>
/// Structure containing all relevant information about a user's online presence.
/// </summary>
[Serializable]
[MessagePackObject]
public struct UserPresence
{
/// <summary>
/// The user's current activity.
/// </summary>
[Key(0)]
public UserActivity? Activity { get; set; }
/// <summary>
/// The user's current status.
/// </summary>
[Key(1)]
public UserStatus? Status { get; set; }
}
}