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

Merge pull request #25694 from bdach/online-presence

Display other users' online presence state in dashboard overlay
This commit is contained in:
Dean Herbert 2023-12-07 19:13:51 +09:00 committed by GitHub
commit 2df15698b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 480 additions and 39 deletions

View File

@ -10,43 +10,50 @@ using osu.Framework.Graphics;
using osu.Framework.Testing;
using osu.Game.Database;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Metadata;
using osu.Game.Online.Spectator;
using osu.Game.Overlays;
using osu.Game.Overlays.Dashboard;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Tests.Visual.Metadata;
using osu.Game.Tests.Visual.Spectator;
using osu.Game.Users;
namespace osu.Game.Tests.Visual.Online
{
public partial class TestSceneCurrentlyPlayingDisplay : OsuTestScene
public partial class TestSceneCurrentlyOnlineDisplay : OsuTestScene
{
private readonly APIUser streamingUser = new APIUser { Id = 2, Username = "Test user" };
private TestSpectatorClient spectatorClient = null!;
private CurrentlyPlayingDisplay currentlyPlaying = null!;
private TestMetadataClient metadataClient = null!;
private CurrentlyOnlineDisplay currentlyOnline = null!;
[SetUpSteps]
public void SetUpSteps()
{
AddStep("add streaming client", () =>
AddStep("set up components", () =>
{
spectatorClient = new TestSpectatorClient();
metadataClient = new TestMetadataClient();
var lookupCache = new TestUserLookupCache();
Children = new Drawable[]
{
lookupCache,
spectatorClient,
metadataClient,
new DependencyProvidingContainer
{
RelativeSizeAxes = Axes.Both,
CachedDependencies = new (Type, object)[]
{
(typeof(SpectatorClient), spectatorClient),
(typeof(MetadataClient), metadataClient),
(typeof(UserLookupCache), lookupCache),
(typeof(OverlayColourProvider), new OverlayColourProvider(OverlayColourScheme.Purple)),
},
Child = currentlyPlaying = new CurrentlyPlayingDisplay
Child = currentlyOnline = new CurrentlyOnlineDisplay
{
RelativeSizeAxes = Axes.Both,
}
@ -58,10 +65,20 @@ namespace osu.Game.Tests.Visual.Online
[Test]
public void TestBasicDisplay()
{
AddStep("Add playing user", () => spectatorClient.SendStartPlay(streamingUser.Id, 0));
AddUntilStep("Panel loaded", () => currentlyPlaying.ChildrenOfType<UserGridPanel>().FirstOrDefault()?.User.Id == 2);
AddStep("Remove playing user", () => spectatorClient.SendEndPlay(streamingUser.Id));
AddUntilStep("Panel no longer present", () => !currentlyPlaying.ChildrenOfType<UserGridPanel>().Any());
AddStep("Begin watching user presence", () => metadataClient.BeginWatchingUserPresence());
AddStep("Add online user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, new UserPresence { Status = UserStatus.Online, Activity = new UserActivity.ChoosingBeatmap() }));
AddUntilStep("Panel loaded", () => currentlyOnline.ChildrenOfType<UserGridPanel>().FirstOrDefault()?.User.Id == 2);
AddAssert("Spectate button disabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.False);
AddStep("User began playing", () => spectatorClient.SendStartPlay(streamingUser.Id, 0));
AddAssert("Spectate button enabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.True);
AddStep("User finished playing", () => spectatorClient.SendEndPlay(streamingUser.Id));
AddAssert("Spectate button disabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.False);
AddStep("Remove playing user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, null));
AddUntilStep("Panel no longer present", () => !currentlyOnline.ChildrenOfType<UserGridPanel>().Any());
AddStep("End watching user presence", () => metadataClient.EndWatchingUserPresence());
}
internal partial class TestUserLookupCache : UserLookupCache

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

@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
@ -20,6 +19,7 @@ using osu.Game.Database;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Metadata;
using osu.Game.Online.Spectator;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Screens;
@ -30,19 +30,27 @@ using osuTK;
namespace osu.Game.Overlays.Dashboard
{
internal partial class CurrentlyPlayingDisplay : CompositeDrawable
internal partial class CurrentlyOnlineDisplay : CompositeDrawable
{
private const float search_textbox_height = 40;
private const float padding = 10;
private readonly IBindableList<int> playingUsers = new BindableList<int>();
private readonly IBindableDictionary<int, UserPresence> onlineUsers = new BindableDictionary<int, UserPresence>();
private readonly Dictionary<int, OnlineUserPanel> userPanels = new Dictionary<int, OnlineUserPanel>();
private SearchContainer<PlayingUserPanel> userFlow;
private SearchContainer<OnlineUserPanel> userFlow;
private BasicSearchTextBox searchTextBox;
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private SpectatorClient spectatorClient { get; set; }
[Resolved]
private MetadataClient metadataClient { get; set; }
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
@ -72,7 +80,7 @@ namespace osu.Game.Overlays.Dashboard
PlaceholderText = HomeStrings.SearchPlaceholder,
},
},
userFlow = new SearchContainer<PlayingUserPanel>
userFlow = new SearchContainer<OnlineUserPanel>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
@ -97,6 +105,9 @@ namespace osu.Game.Overlays.Dashboard
{
base.LoadComplete();
onlineUsers.BindTo(metadataClient.UserStates);
onlineUsers.BindCollectionChanged(onUserUpdated, true);
playingUsers.BindTo(spectatorClient.PlayingUsers);
playingUsers.BindCollectionChanged(onPlayingUsersChanged, true);
}
@ -108,15 +119,20 @@ namespace osu.Game.Overlays.Dashboard
searchTextBox.TakeFocus();
}
private void onPlayingUsersChanged(object sender, NotifyCollectionChangedEventArgs e) => Schedule(() =>
private void onUserUpdated(object sender, NotifyDictionaryChangedEventArgs<int, UserPresence> e) => Schedule(() =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
case NotifyDictionaryChangedAction.Add:
Debug.Assert(e.NewItems != null);
foreach (int userId in e.NewItems)
foreach (var kvp in e.NewItems)
{
int userId = kvp.Key;
if (userId == api.LocalUser.Value.Id)
continue;
users.GetUserAsync(userId).ContinueWith(task =>
{
APIUser user = task.GetResultSafely();
@ -126,40 +142,90 @@ namespace osu.Game.Overlays.Dashboard
Schedule(() =>
{
// user may no longer be playing.
if (!playingUsers.Contains(user.Id))
return;
// explicitly refetch the user's status.
// things may have changed in between the time of scheduling and the time of actual execution.
if (onlineUsers.TryGetValue(userId, out var updatedStatus))
{
user.Activity.Value = updatedStatus.Activity;
user.Status.Value = updatedStatus.Status;
}
// TODO: remove this once online state is being updated more correctly.
user.IsOnline = true;
userFlow.Add(createUserPanel(user));
userFlow.Add(userPanels[userId] = createUserPanel(user));
});
});
}
break;
case NotifyDictionaryChangedAction.Replace:
Debug.Assert(e.NewItems != null);
foreach (var kvp in e.NewItems)
{
if (userPanels.TryGetValue(kvp.Key, out var panel))
{
panel.User.Activity.Value = kvp.Value.Activity;
panel.User.Status.Value = kvp.Value.Status;
}
}
break;
case NotifyDictionaryChangedAction.Remove:
Debug.Assert(e.OldItems != null);
foreach (var kvp in e.OldItems)
{
int userId = kvp.Key;
if (userPanels.Remove(userId, out var userPanel))
userPanel.Expire();
}
break;
}
});
private void onPlayingUsersChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Debug.Assert(e.NewItems != null);
foreach (int userId in e.NewItems)
{
if (userPanels.TryGetValue(userId, out var panel))
panel.CanSpectate.Value = userId != api.LocalUser.Value.Id;
}
break;
case NotifyCollectionChangedAction.Remove:
Debug.Assert(e.OldItems != null);
foreach (int userId in e.OldItems)
userFlow.FirstOrDefault(card => card.User.Id == userId)?.Expire();
{
if (userPanels.TryGetValue(userId, out var panel))
panel.CanSpectate.Value = false;
}
break;
}
});
}
private PlayingUserPanel createUserPanel(APIUser user) =>
new PlayingUserPanel(user).With(panel =>
private OnlineUserPanel createUserPanel(APIUser user) =>
new OnlineUserPanel(user).With(panel =>
{
panel.Anchor = Anchor.TopCentre;
panel.Origin = Anchor.TopCentre;
});
public partial class PlayingUserPanel : CompositeDrawable, IFilterable
public partial class OnlineUserPanel : CompositeDrawable, IFilterable
{
public readonly APIUser User;
public BindableBool CanSpectate { get; } = new BindableBool();
public IEnumerable<LocalisableString> FilterTerms { get; }
[Resolved(canBeNull: true)]
@ -178,7 +244,7 @@ namespace osu.Game.Overlays.Dashboard
}
}
public PlayingUserPanel(APIUser user)
public OnlineUserPanel(APIUser user)
{
User = user;
@ -188,7 +254,7 @@ namespace osu.Game.Overlays.Dashboard
}
[BackgroundDependencyLoader]
private void load(IAPIProvider api)
private void load()
{
InternalChildren = new Drawable[]
{
@ -205,6 +271,9 @@ namespace osu.Game.Overlays.Dashboard
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
// this is SHOCKING
Activity = { BindTarget = User.Activity },
Status = { BindTarget = User.Status },
},
new PurpleRoundedButton
{
@ -213,7 +282,7 @@ namespace osu.Game.Overlays.Dashboard
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Action = () => performer?.PerformFromScreen(s => s.Push(new SoloSpectatorScreen(User))),
Enabled = { Value = User.Id != api.LocalUser.Value.Id }
Enabled = { BindTarget = CanSpectate }
}
}
},

View File

@ -29,7 +29,7 @@ namespace osu.Game.Overlays.Dashboard
[LocalisableDescription(typeof(FriendsStrings), nameof(FriendsStrings.TitleCompact))]
Friends,
[Description("Currently Playing")]
[Description("Currently online")]
CurrentlyPlaying
}
}

View File

@ -2,6 +2,11 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.Metadata;
using osu.Game.Online.Multiplayer;
using osu.Game.Overlays.Dashboard;
using osu.Game.Overlays.Dashboard.Friends;
@ -9,6 +14,11 @@ namespace osu.Game.Overlays
{
public partial class DashboardOverlay : TabbableOnlineOverlay<DashboardOverlayHeader, DashboardOverlayTabs>
{
[Resolved]
private MetadataClient metadataClient { get; set; } = null!;
private IBindable<bool> metadataConnected = null!;
public DashboardOverlay()
: base(OverlayColourScheme.Purple)
{
@ -27,12 +37,33 @@ namespace osu.Game.Overlays
break;
case DashboardOverlayTabs.CurrentlyPlaying:
LoadDisplay(new CurrentlyPlayingDisplay());
LoadDisplay(new CurrentlyOnlineDisplay());
break;
default:
throw new NotImplementedException($"Display for {tab} tab is not implemented");
}
}
protected override void LoadComplete()
{
base.LoadComplete();
metadataConnected = metadataClient.IsConnected.GetBoundCopy();
metadataConnected.BindValueChanged(_ => updateUserPresenceState());
State.BindValueChanged(_ => updateUserPresenceState());
updateUserPresenceState();
}
private void updateUserPresenceState()
{
if (!metadataConnected.Value)
return;
if (State.Value == Visibility.Visible)
metadataClient.BeginWatchingUserPresence().FireAndForget();
else
metadataClient.EndWatchingUserPresence().FireAndForget();
}
}
}

View File

@ -0,0 +1,81 @@
// 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.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Online.API;
using osu.Game.Online.Metadata;
using osu.Game.Users;
namespace osu.Game.Tests.Visual.Metadata
{
public partial class TestMetadataClient : MetadataClient
{
public override IBindable<bool> IsConnected => new BindableBool(true);
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
private readonly BindableBool isWatchingUserPresence = new BindableBool();
public override IBindableDictionary<int, UserPresence> UserStates => userStates;
private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>();
[Resolved]
private IAPIProvider api { get; set; } = null!;
public override Task BeginWatchingUserPresence()
{
isWatchingUserPresence.Value = true;
return Task.CompletedTask;
}
public override Task EndWatchingUserPresence()
{
isWatchingUserPresence.Value = false;
return Task.CompletedTask;
}
public override Task UpdateActivity(UserActivity? activity)
{
if (isWatchingUserPresence.Value)
{
userStates.TryGetValue(api.LocalUser.Value.Id, out var localUserPresence);
localUserPresence = localUserPresence with { Activity = activity };
userStates[api.LocalUser.Value.Id] = localUserPresence;
}
return Task.CompletedTask;
}
public override Task UpdateStatus(UserStatus? status)
{
if (isWatchingUserPresence.Value)
{
userStates.TryGetValue(api.LocalUser.Value.Id, out var localUserPresence);
localUserPresence = localUserPresence with { Status = status };
userStates[api.LocalUser.Value.Id] = localUserPresence;
}
return Task.CompletedTask;
}
public override Task UserPresenceUpdated(int userId, UserPresence? presence)
{
if (isWatchingUserPresence.Value)
{
if (presence.HasValue)
userStates[userId] = presence.Value;
else
userStates.Remove(userId);
}
return Task.CompletedTask;
}
public override Task<BeatmapUpdates> GetChangesSince(int queueId)
=> Task.FromResult(new BeatmapUpdates(Array.Empty<int>(), queueId));
public override Task BeatmapSetsUpdated(BeatmapUpdates updates) => Task.CompletedTask;
}
}

View File

@ -9,10 +9,12 @@ using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Users.Drawables;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Users
@ -26,7 +28,7 @@ namespace osu.Game.Users
protected TextFlowContainer LastVisitMessage { get; private set; }
private StatusIcon statusIcon;
private OsuSpriteText statusMessage;
private StatusText statusMessage;
protected ExtendedUserPanel(APIUser user)
: base(user)
@ -88,7 +90,7 @@ namespace osu.Game.Users
}
}));
statusContainer.Add(statusMessage = new OsuSpriteText
statusContainer.Add(statusMessage = new StatusText
{
Anchor = alignment,
Origin = alignment,
@ -108,12 +110,14 @@ namespace osu.Game.Users
if (activity != null && status != UserStatus.Offline)
{
statusMessage.Text = activity.GetStatus();
statusMessage.TooltipText = activity.GetDetails();
statusIcon.FadeColour(activity.GetAppropriateColour(Colours), 500, Easing.OutQuint);
return;
}
// Otherwise use only status
statusMessage.Text = status.GetLocalisableDescription();
statusMessage.TooltipText = string.Empty;
statusIcon.FadeColour(status.Value.GetAppropriateColour(Colours), 500, Easing.OutQuint);
return;
@ -140,5 +144,10 @@ namespace osu.Game.Users
BorderThickness = 0;
base.OnHoverLost(e);
}
private partial class StatusText : OsuSpriteText, IHasTooltip
{
public LocalisableString TooltipText { get; set; }
}
}
}

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; }
}
}