1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 00:42:55 +08:00

Merge pull request #31524 from smoogipoo/user-panel-status

Display up-to-date online status in user panels
This commit is contained in:
Dean Herbert 2025-02-05 19:22:27 +09:00 committed by GitHub
commit ea725e2caf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 391 additions and 268 deletions

View File

@ -0,0 +1,52 @@
// 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 NUnit.Framework;
using osu.Framework.Testing;
using osu.Game.Tests.Visual;
using osu.Game.Tests.Visual.Metadata;
namespace osu.Game.Tests.Online
{
[TestFixture]
[HeadlessTest]
public partial class TestSceneMetadataClient : OsuTestScene
{
private TestMetadataClient client = null!;
[SetUp]
public void Setup() => Schedule(() =>
{
Child = client = new TestMetadataClient();
});
[Test]
public void TestWatchingMultipleTimesInvokesServerMethodsOnce()
{
int countBegin = 0;
int countEnd = 0;
IDisposable token1 = null!;
IDisposable token2 = null!;
AddStep("setup", () =>
{
client.OnBeginWatchingUserPresence += () => countBegin++;
client.OnEndWatchingUserPresence += () => countEnd++;
});
AddStep("begin watching presence (1)", () => token1 = client.BeginWatchingUserPresence());
AddAssert("server method invoked once", () => countBegin, () => Is.EqualTo(1));
AddStep("begin watching presence (2)", () => token2 = client.BeginWatchingUserPresence());
AddAssert("server method not invoked a second time", () => countBegin, () => Is.EqualTo(1));
AddStep("end watching presence (1)", () => token1.Dispose());
AddAssert("server method not invoked", () => countEnd, () => Is.EqualTo(0));
AddStep("end watching presence (2)", () => token2.Dispose());
AddAssert("server method invoked once", () => countEnd, () => Is.EqualTo(1));
}
}
}

View File

@ -65,7 +65,9 @@ namespace osu.Game.Tests.Visual.Online
[Test] [Test]
public void TestBasicDisplay() public void TestBasicDisplay()
{ {
AddStep("Begin watching user presence", () => metadataClient.BeginWatchingUserPresence()); IDisposable token = null!;
AddStep("Begin watching user presence", () => token = metadataClient.BeginWatchingUserPresence());
AddStep("Add online user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, new UserPresence { Status = UserStatus.Online, Activity = new UserActivity.ChoosingBeatmap() })); 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); AddUntilStep("Panel loaded", () => currentlyOnline.ChildrenOfType<UserGridPanel>().FirstOrDefault()?.User.Id == 2);
AddAssert("Spectate button disabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.False); AddAssert("Spectate button disabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.False);
@ -78,14 +80,16 @@ namespace osu.Game.Tests.Visual.Online
AddStep("Remove playing user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, null)); AddStep("Remove playing user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, null));
AddUntilStep("Panel no longer present", () => !currentlyOnline.ChildrenOfType<UserGridPanel>().Any()); AddUntilStep("Panel no longer present", () => !currentlyOnline.ChildrenOfType<UserGridPanel>().Any());
AddStep("End watching user presence", () => metadataClient.EndWatchingUserPresence()); AddStep("End watching user presence", () => token.Dispose());
} }
[Test] [Test]
public void TestUserWasPlayingBeforeWatchingUserPresence() public void TestUserWasPlayingBeforeWatchingUserPresence()
{ {
IDisposable token = null!;
AddStep("User began playing", () => spectatorClient.SendStartPlay(streamingUser.Id, 0)); AddStep("User began playing", () => spectatorClient.SendStartPlay(streamingUser.Id, 0));
AddStep("Begin watching user presence", () => metadataClient.BeginWatchingUserPresence()); AddStep("Begin watching user presence", () => token = metadataClient.BeginWatchingUserPresence());
AddStep("Add online user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, new UserPresence { Status = UserStatus.Online, Activity = new UserActivity.ChoosingBeatmap() })); 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); AddUntilStep("Panel loaded", () => currentlyOnline.ChildrenOfType<UserGridPanel>().FirstOrDefault()?.User.Id == 2);
AddAssert("Spectate button enabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.True); AddAssert("Spectate button enabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.True);
@ -93,7 +97,7 @@ namespace osu.Game.Tests.Visual.Online
AddStep("User finished playing", () => spectatorClient.SendEndPlay(streamingUser.Id)); AddStep("User finished playing", () => spectatorClient.SendEndPlay(streamingUser.Id));
AddAssert("Spectate button disabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.False); AddAssert("Spectate button disabled", () => currentlyOnline.ChildrenOfType<PurpleRoundedButton>().First().Enabled.Value, () => Is.False);
AddStep("Remove playing user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, null)); AddStep("Remove playing user", () => metadataClient.UserPresenceUpdated(streamingUser.Id, null));
AddStep("End watching user presence", () => metadataClient.EndWatchingUserPresence()); AddStep("End watching user presence", () => token.Dispose());
} }
internal partial class TestUserLookupCache : UserLookupCache internal partial class TestUserLookupCache : UserLookupCache

View File

@ -4,17 +4,18 @@
using System; using System;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Online; using osu.Game.Online;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Metadata;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Tests.Beatmaps; using osu.Game.Tests.Beatmaps;
using osu.Game.Tests.Visual.Metadata;
using osu.Game.Users; using osu.Game.Users;
using osuTK; using osuTK;
@ -23,32 +24,32 @@ namespace osu.Game.Tests.Visual.Online
[TestFixture] [TestFixture]
public partial class TestSceneUserPanel : OsuTestScene public partial class TestSceneUserPanel : OsuTestScene
{ {
private readonly Bindable<UserActivity?> activity = new Bindable<UserActivity?>();
private readonly Bindable<UserStatus?> status = new Bindable<UserStatus?>();
private UserGridPanel boundPanel1 = null!;
private TestUserListPanel boundPanel2 = null!;
[Cached] [Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple); private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
[Cached(typeof(LocalUserStatisticsProvider))]
private readonly TestUserStatisticsProvider statisticsProvider = new TestUserStatisticsProvider();
[Resolved] [Resolved]
private IRulesetStore rulesetStore { get; set; } = null!; private IRulesetStore rulesetStore { get; set; } = null!;
private TestUserStatisticsProvider statisticsProvider = null!;
private TestMetadataClient metadataClient = null!;
private TestUserListPanel panel = null!;
[SetUp] [SetUp]
public void SetUp() => Schedule(() => public void SetUp() => Schedule(() =>
{ {
activity.Value = null; Child = new DependencyProvidingContainer
status.Value = null; {
RelativeSizeAxes = Axes.Both,
Remove(statisticsProvider, false); CachedDependencies =
Clear(); [
Add(statisticsProvider); (typeof(LocalUserStatisticsProvider), statisticsProvider = new TestUserStatisticsProvider()),
(typeof(MetadataClient), metadataClient = new TestMetadataClient())
Add(new FillFlowContainer ],
Children = new Drawable[]
{
statisticsProvider,
metadataClient,
new FillFlowContainer
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
@ -78,7 +79,7 @@ namespace osu.Game.Tests.Visual.Online
CoverUrl = @"https://assets.ppy.sh/user-cover-presets/1/df28696b58541a9e67f6755918951d542d93bdf1da41720fcca2fd2c1ea8cf51.jpeg", CoverUrl = @"https://assets.ppy.sh/user-cover-presets/1/df28696b58541a9e67f6755918951d542d93bdf1da41720fcca2fd2c1ea8cf51.jpeg",
IsOnline = true IsOnline = true
}) { Width = 300 }, }) { Width = 300 },
boundPanel1 = new UserGridPanel(new APIUser new UserGridPanel(new APIUser
{ {
Username = @"peppy", Username = @"peppy",
Id = 2, Id = 2,
@ -87,13 +88,12 @@ namespace osu.Game.Tests.Visual.Online
IsSupporter = true, IsSupporter = true,
SupportLevel = 3, SupportLevel = 3,
}) { Width = 300 }, }) { Width = 300 },
boundPanel2 = new TestUserListPanel(new APIUser panel = new TestUserListPanel(new APIUser
{ {
Username = @"Evast", Username = @"peppy",
Id = 8195163, Id = 2,
CountryCode = CountryCode.BY, CountryCode = CountryCode.AU,
CoverUrl = @"https://assets.ppy.sh/user-profile-covers/8195163/4a8e2ad5a02a2642b631438cfa6c6bd7e2f9db289be881cb27df18331f64144c.jpeg", CoverUrl = @"https://assets.ppy.sh/user-profile-covers/8195163/4a8e2ad5a02a2642b631438cfa6c6bd7e2f9db289be881cb27df18331f64144c.jpeg",
IsOnline = false,
LastVisit = DateTimeOffset.Now LastVisit = DateTimeOffset.Now
}), }),
new UserRankPanel(new APIUser new UserRankPanel(new APIUser
@ -112,55 +112,54 @@ namespace osu.Game.Tests.Visual.Online
CountryCode = CountryCode.AU, CountryCode = CountryCode.AU,
CoverUrl = @"https://assets.ppy.sh/user-profile-covers/8195163/4a8e2ad5a02a2642b631438cfa6c6bd7e2f9db289be881cb27df18331f64144c.jpeg", CoverUrl = @"https://assets.ppy.sh/user-profile-covers/8195163/4a8e2ad5a02a2642b631438cfa6c6bd7e2f9db289be881cb27df18331f64144c.jpeg",
Statistics = new UserStatistics { GlobalRank = null, CountryRank = null } Statistics = new UserStatistics { GlobalRank = null, CountryRank = null }
}) { Width = 300 } }) { Width = 300 },
new UserGridPanel(API.LocalUser.Value)
{
Width = 300
} }
}); }
}
}
};
boundPanel1.Status.BindTo(status); metadataClient.BeginWatchingUserPresence();
boundPanel1.Activity.BindTo(activity);
boundPanel2.Status.BindTo(status);
boundPanel2.Activity.BindTo(activity);
}); });
[Test] [Test]
public void TestUserStatus() public void TestUserStatus()
{ {
AddStep("online", () => status.Value = UserStatus.Online); AddStep("online", () => setPresence(UserStatus.Online, null));
AddStep("do not disturb", () => status.Value = UserStatus.DoNotDisturb); AddStep("do not disturb", () => setPresence(UserStatus.DoNotDisturb, null));
AddStep("offline", () => status.Value = UserStatus.Offline); AddStep("offline", () => setPresence(UserStatus.Offline, null));
AddStep("null status", () => status.Value = null);
} }
[Test] [Test]
public void TestUserActivity() public void TestUserActivity()
{ {
AddStep("set online status", () => status.Value = UserStatus.Online); AddStep("idle", () => setPresence(UserStatus.Online, null));
AddStep("watching replay", () => setPresence(UserStatus.Online, new UserActivity.WatchingReplay(createScore(@"nats"))));
AddStep("idle", () => activity.Value = null); AddStep("spectating user", () => setPresence(UserStatus.Online, new UserActivity.SpectatingUser(createScore(@"mrekk"))));
AddStep("watching replay", () => activity.Value = new UserActivity.WatchingReplay(createScore(@"nats"))); AddStep("solo (osu!)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(0)));
AddStep("spectating user", () => activity.Value = new UserActivity.SpectatingUser(createScore(@"mrekk"))); AddStep("solo (osu!taiko)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(1)));
AddStep("solo (osu!)", () => activity.Value = soloGameStatusForRuleset(0)); AddStep("solo (osu!catch)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(2)));
AddStep("solo (osu!taiko)", () => activity.Value = soloGameStatusForRuleset(1)); AddStep("solo (osu!mania)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(3)));
AddStep("solo (osu!catch)", () => activity.Value = soloGameStatusForRuleset(2)); AddStep("choosing", () => setPresence(UserStatus.Online, new UserActivity.ChoosingBeatmap()));
AddStep("solo (osu!mania)", () => activity.Value = soloGameStatusForRuleset(3)); AddStep("editing beatmap", () => setPresence(UserStatus.Online, new UserActivity.EditingBeatmap(new BeatmapInfo())));
AddStep("choosing", () => activity.Value = new UserActivity.ChoosingBeatmap()); AddStep("modding beatmap", () => setPresence(UserStatus.Online, new UserActivity.ModdingBeatmap(new BeatmapInfo())));
AddStep("editing beatmap", () => activity.Value = new UserActivity.EditingBeatmap(new BeatmapInfo())); AddStep("testing beatmap", () => setPresence(UserStatus.Online, new UserActivity.TestingBeatmap(new BeatmapInfo())));
AddStep("modding beatmap", () => activity.Value = new UserActivity.ModdingBeatmap(new BeatmapInfo()));
AddStep("testing beatmap", () => activity.Value = new UserActivity.TestingBeatmap(new BeatmapInfo()));
} }
[Test] [Test]
public void TestUserActivityChange() public void TestUserActivityChange()
{ {
AddAssert("visit message is visible", () => boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is visible", () => panel.LastVisitMessage.IsPresent);
AddStep("set online status", () => status.Value = UserStatus.Online); AddStep("set online status", () => setPresence(UserStatus.Online, null));
AddAssert("visit message is not visible", () => !boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is not visible", () => !panel.LastVisitMessage.IsPresent);
AddStep("set choosing activity", () => activity.Value = new UserActivity.ChoosingBeatmap()); AddStep("set choosing activity", () => setPresence(UserStatus.Online, new UserActivity.ChoosingBeatmap()));
AddStep("set offline status", () => status.Value = UserStatus.Offline); AddStep("set offline status", () => setPresence(UserStatus.Offline, null));
AddAssert("visit message is visible", () => boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is visible", () => panel.LastVisitMessage.IsPresent);
AddStep("set online status", () => status.Value = UserStatus.Online); AddStep("set online status", () => setPresence(UserStatus.Online, null));
AddAssert("visit message is not visible", () => !boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is not visible", () => !panel.LastVisitMessage.IsPresent);
} }
[Test] [Test]
@ -185,6 +184,31 @@ namespace osu.Game.Tests.Visual.Online
AddStep("set statistics to empty", () => statisticsProvider.UpdateStatistics(new UserStatistics(), Ruleset.Value)); AddStep("set statistics to empty", () => statisticsProvider.UpdateStatistics(new UserStatistics(), Ruleset.Value));
} }
[Test]
public void TestLocalUserActivity()
{
AddStep("idle", () => setPresence(UserStatus.Online, null, API.LocalUser.Value.OnlineID));
AddStep("watching replay", () => setPresence(UserStatus.Online, new UserActivity.WatchingReplay(createScore(@"nats")), API.LocalUser.Value.OnlineID));
AddStep("spectating user", () => setPresence(UserStatus.Online, new UserActivity.SpectatingUser(createScore(@"mrekk")), API.LocalUser.Value.OnlineID));
AddStep("solo (osu!)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(0), API.LocalUser.Value.OnlineID));
AddStep("solo (osu!taiko)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(1), API.LocalUser.Value.OnlineID));
AddStep("solo (osu!catch)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(2), API.LocalUser.Value.OnlineID));
AddStep("solo (osu!mania)", () => setPresence(UserStatus.Online, soloGameStatusForRuleset(3), API.LocalUser.Value.OnlineID));
AddStep("choosing", () => setPresence(UserStatus.Online, new UserActivity.ChoosingBeatmap(), API.LocalUser.Value.OnlineID));
AddStep("editing beatmap", () => setPresence(UserStatus.Online, new UserActivity.EditingBeatmap(new BeatmapInfo()), API.LocalUser.Value.OnlineID));
AddStep("modding beatmap", () => setPresence(UserStatus.Online, new UserActivity.ModdingBeatmap(new BeatmapInfo()), API.LocalUser.Value.OnlineID));
AddStep("testing beatmap", () => setPresence(UserStatus.Online, new UserActivity.TestingBeatmap(new BeatmapInfo()), API.LocalUser.Value.OnlineID));
AddStep("set offline status", () => setPresence(UserStatus.Offline, null, API.LocalUser.Value.OnlineID));
}
private void setPresence(UserStatus status, UserActivity? activity, int? userId = null)
{
if (status == UserStatus.Offline)
metadataClient.UserPresenceUpdated(userId ?? panel.User.OnlineID, null);
else
metadataClient.UserPresenceUpdated(userId ?? panel.User.OnlineID, new UserPresence { Status = status, Activity = activity });
}
private UserActivity soloGameStatusForRuleset(int rulesetId) => new UserActivity.InSoloGame(new BeatmapInfo(), rulesetStore.GetRuleset(rulesetId)!); private UserActivity soloGameStatusForRuleset(int rulesetId) => new UserActivity.InSoloGame(new BeatmapInfo(), rulesetStore.GetRuleset(rulesetId)!);
private ScoreInfo createScore(string name) => new ScoreInfo(new TestBeatmap(Ruleset.Value).BeatmapInfo) private ScoreInfo createScore(string name) => new ScoreInfo(new TestBeatmap(Ruleset.Value).BeatmapInfo)

View File

@ -95,6 +95,9 @@ namespace osu.Game.Configuration
/// </summary> /// </summary>
DailyChallengeIntroPlayed, DailyChallengeIntroPlayed,
/// <summary>
/// The activity for the current user to broadcast to other players.
/// </summary>
UserOnlineActivity, UserOnlineActivity,
} }
} }

View File

@ -3,9 +3,13 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
using osu.Game.Users; using osu.Game.Users;
namespace osu.Game.Online.Metadata namespace osu.Game.Online.Metadata
@ -14,6 +18,9 @@ namespace osu.Game.Online.Metadata
{ {
public abstract IBindable<bool> IsConnected { get; } public abstract IBindable<bool> IsConnected { get; }
[Resolved]
private IAPIProvider api { get; set; } = null!;
#region Beatmap metadata updates #region Beatmap metadata updates
public abstract Task<BeatmapUpdates> GetChangesSince(int queueId); public abstract Task<BeatmapUpdates> GetChangesSince(int queueId);
@ -32,11 +39,6 @@ namespace osu.Game.Online.Metadata
#region User presence updates #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> /// <summary>
/// The <see cref="UserPresence"/> information about the current user. /// The <see cref="UserPresence"/> information about the current user.
/// </summary> /// </summary>
@ -52,31 +54,91 @@ namespace osu.Game.Online.Metadata
/// </summary> /// </summary>
public abstract IBindableDictionary<int, UserPresence> FriendPresences { get; } public abstract IBindableDictionary<int, UserPresence> FriendPresences { get; }
/// <inheritdoc/> /// <summary>
/// Attempts to retrieve the presence of a user.
/// </summary>
/// <param name="userId">The user ID.</param>
/// <returns>The user presence, or null if not available or the user's offline.</returns>
public UserPresence? GetPresence(int userId)
{
if (userId == api.LocalUser.Value.OnlineID)
return LocalUserPresence;
if (FriendPresences.TryGetValue(userId, out UserPresence presence))
return presence;
if (UserPresences.TryGetValue(userId, out presence))
return presence;
return null;
}
public abstract Task UpdateActivity(UserActivity? activity); public abstract Task UpdateActivity(UserActivity? activity);
/// <inheritdoc/>
public abstract Task UpdateStatus(UserStatus? status); public abstract Task UpdateStatus(UserStatus? status);
/// <inheritdoc/> private int userPresenceWatchCount;
public abstract Task BeginWatchingUserPresence();
/// <inheritdoc/> protected bool IsWatchingUserPresence
public abstract Task EndWatchingUserPresence(); => Interlocked.CompareExchange(ref userPresenceWatchCount, userPresenceWatchCount, userPresenceWatchCount) > 0;
/// <summary>
/// Signals to the server that we want to begin receiving status updates for all users.
/// </summary>
/// <returns>An <see cref="IDisposable"/> which will end the session when disposed.</returns>
public IDisposable BeginWatchingUserPresence() => new UserPresenceWatchToken(this);
Task IMetadataServer.BeginWatchingUserPresence()
{
if (Interlocked.Increment(ref userPresenceWatchCount) == 1)
return BeginWatchingUserPresenceInternal();
return Task.CompletedTask;
}
Task IMetadataServer.EndWatchingUserPresence()
{
if (Interlocked.Decrement(ref userPresenceWatchCount) == 0)
return EndWatchingUserPresenceInternal();
return Task.CompletedTask;
}
protected abstract Task BeginWatchingUserPresenceInternal();
protected abstract Task EndWatchingUserPresenceInternal();
/// <inheritdoc/>
public abstract Task UserPresenceUpdated(int userId, UserPresence? presence); public abstract Task UserPresenceUpdated(int userId, UserPresence? presence);
/// <inheritdoc/>
public abstract Task FriendPresenceUpdated(int userId, UserPresence? presence); public abstract Task FriendPresenceUpdated(int userId, UserPresence? presence);
private class UserPresenceWatchToken : IDisposable
{
private readonly IMetadataServer server;
private bool isDisposed;
public UserPresenceWatchToken(IMetadataServer server)
{
this.server = server;
server.BeginWatchingUserPresence().FireAndForget();
}
public void Dispose()
{
if (isDisposed)
return;
server.EndWatchingUserPresence().FireAndForget();
isDisposed = true;
}
}
#endregion #endregion
#region Daily Challenge #region Daily Challenge
public abstract IBindable<DailyChallengeInfo?> DailyChallengeInfo { get; } public abstract IBindable<DailyChallengeInfo?> DailyChallengeInfo { get; }
/// <inheritdoc/>
public abstract Task DailyChallengeUpdated(DailyChallengeInfo? info); public abstract Task DailyChallengeUpdated(DailyChallengeInfo? info);
#endregion #endregion

View File

@ -20,9 +20,6 @@ namespace osu.Game.Online.Metadata
{ {
public override IBindable<bool> IsConnected { get; } = new Bindable<bool>(); public override IBindable<bool> IsConnected { get; } = new Bindable<bool>();
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
private readonly BindableBool isWatchingUserPresence = new BindableBool();
public override UserPresence LocalUserPresence => localUserPresence; public override UserPresence LocalUserPresence => localUserPresence;
private UserPresence localUserPresence; private UserPresence localUserPresence;
@ -109,15 +106,18 @@ namespace osu.Game.Online.Metadata
{ {
Schedule(() => Schedule(() =>
{ {
isWatchingUserPresence.Value = false;
userPresences.Clear(); userPresences.Clear();
friendPresences.Clear(); friendPresences.Clear();
dailyChallengeInfo.Value = null; dailyChallengeInfo.Value = null;
localUserPresence = default; localUserPresence = default;
}); });
return; return;
} }
if (IsWatchingUserPresence)
BeginWatchingUserPresenceInternal();
if (localUser.Value is not GuestUser) if (localUser.Value is not GuestUser)
{ {
UpdateActivity(userActivity.Value); UpdateActivity(userActivity.Value);
@ -201,6 +201,31 @@ namespace osu.Game.Online.Metadata
return connection.InvokeAsync(nameof(IMetadataServer.UpdateStatus), status); return connection.InvokeAsync(nameof(IMetadataServer.UpdateStatus), status);
} }
protected override Task BeginWatchingUserPresenceInternal()
{
if (connector?.IsConnected.Value != true)
return Task.CompletedTask;
Logger.Log($@"{nameof(OnlineMetadataClient)} began watching user presence", LoggingTarget.Network);
Debug.Assert(connection != null);
return connection.InvokeAsync(nameof(IMetadataServer.BeginWatchingUserPresence));
}
protected override Task EndWatchingUserPresenceInternal()
{
if (connector?.IsConnected.Value != true)
return Task.CompletedTask;
Logger.Log($@"{nameof(OnlineMetadataClient)} stopped watching user presence", LoggingTarget.Network);
// must be scheduled before any remote calls to avoid mis-ordering.
Schedule(() => userPresences.Clear());
Debug.Assert(connection != null);
return connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence));
}
public override Task UserPresenceUpdated(int userId, UserPresence? presence) public override Task UserPresenceUpdated(int userId, UserPresence? presence)
{ {
Schedule(() => Schedule(() =>
@ -237,36 +262,6 @@ namespace osu.Game.Online.Metadata
return Task.CompletedTask; 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);
Schedule(() => isWatchingUserPresence.Value = true);
Logger.Log($@"{nameof(OnlineMetadataClient)} began watching user presence", LoggingTarget.Network);
}
public override async Task EndWatchingUserPresence()
{
try
{
if (connector?.IsConnected.Value != true)
throw new OperationCanceledException();
// must be scheduled before any remote calls to avoid mis-ordering.
Schedule(() => userPresences.Clear());
Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence)).ConfigureAwait(false);
Logger.Log($@"{nameof(OnlineMetadataClient)} stopped watching user presence", LoggingTarget.Network);
}
finally
{
Schedule(() => isWatchingUserPresence.Value = false);
}
}
public override Task DailyChallengeUpdated(DailyChallengeInfo? info) public override Task DailyChallengeUpdated(DailyChallengeInfo? info)
{ {
Schedule(() => dailyChallengeInfo.Value = info); Schedule(() => dailyChallengeInfo.Value = info);

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Diagnostics; using System.Diagnostics;
@ -40,17 +38,20 @@ namespace osu.Game.Overlays.Dashboard
private readonly IBindableDictionary<int, UserPresence> onlineUserPresences = new BindableDictionary<int, UserPresence>(); private readonly IBindableDictionary<int, UserPresence> onlineUserPresences = new BindableDictionary<int, UserPresence>();
private readonly Dictionary<int, OnlineUserPanel> userPanels = new Dictionary<int, OnlineUserPanel>(); private readonly Dictionary<int, OnlineUserPanel> userPanels = new Dictionary<int, OnlineUserPanel>();
private SearchContainer<OnlineUserPanel> userFlow; private SearchContainer<OnlineUserPanel> userFlow = null!;
private BasicSearchTextBox searchTextBox; private BasicSearchTextBox searchTextBox = null!;
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private IAPIProvider api { get; set; } = null!;
[Resolved] [Resolved]
private SpectatorClient spectatorClient { get; set; } private SpectatorClient spectatorClient { get; set; } = null!;
[Resolved] [Resolved]
private MetadataClient metadataClient { get; set; } private MetadataClient metadataClient { get; set; } = null!;
[Resolved]
private UserLookupCache users { get; set; } = null!;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider) private void load(OverlayColourProvider colourProvider)
@ -99,9 +100,6 @@ namespace osu.Game.Overlays.Dashboard
searchTextBox.Current.ValueChanged += text => userFlow.SearchTerm = text.NewValue; searchTextBox.Current.ValueChanged += text => userFlow.SearchTerm = text.NewValue;
} }
[Resolved]
private UserLookupCache users { get; set; }
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
@ -120,7 +118,7 @@ namespace osu.Game.Overlays.Dashboard
searchTextBox.TakeFocus(); searchTextBox.TakeFocus();
} }
private void onUserPresenceUpdated(object sender, NotifyDictionaryChangedEventArgs<int, UserPresence> e) => Schedule(() => private void onUserPresenceUpdated(object? sender, NotifyDictionaryChangedEventArgs<int, UserPresence> e) => Schedule(() =>
{ {
switch (e.Action) switch (e.Action)
{ {
@ -133,36 +131,9 @@ namespace osu.Game.Overlays.Dashboard
users.GetUserAsync(userId).ContinueWith(task => users.GetUserAsync(userId).ContinueWith(task =>
{ {
APIUser user = task.GetResultSafely(); if (task.GetResultSafely() is APIUser user)
Schedule(() => userFlow.Add(userPanels[userId] = createUserPanel(user)));
if (user == null)
return;
Schedule(() =>
{
userFlow.Add(userPanels[userId] = createUserPanel(user).With(p =>
{
var presence = onlineUserPresences.GetValueOrDefault(userId);
p.Status.Value = presence.Status;
p.Activity.Value = presence.Activity;
}));
}); });
});
}
break;
case NotifyDictionaryChangedAction.Replace:
Debug.Assert(e.NewItems != null);
foreach (var kvp in e.NewItems)
{
if (userPanels.TryGetValue(kvp.Key, out var panel))
{
panel.Activity.Value = kvp.Value.Activity;
panel.Status.Value = kvp.Value.Status;
}
} }
break; break;
@ -181,7 +152,7 @@ namespace osu.Game.Overlays.Dashboard
} }
}); });
private void onPlayingUsersChanged(object sender, NotifyCollectionChangedEventArgs e) private void onPlayingUsersChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
switch (e.Action) switch (e.Action)
{ {
@ -221,15 +192,12 @@ namespace osu.Game.Overlays.Dashboard
{ {
public readonly APIUser User; public readonly APIUser User;
public readonly Bindable<UserStatus?> Status = new Bindable<UserStatus?>();
public readonly Bindable<UserActivity> Activity = new Bindable<UserActivity>();
public BindableBool CanSpectate { get; } = new BindableBool(); public BindableBool CanSpectate { get; } = new BindableBool();
public IEnumerable<LocalisableString> FilterTerms { get; } public IEnumerable<LocalisableString> FilterTerms { get; }
[Resolved(canBeNull: true)] [Resolved]
private IPerformFromScreenRunner performer { get; set; } private IPerformFromScreenRunner? performer { get; set; }
public bool FilteringActive { set; get; } public bool FilteringActive { set; get; }
@ -270,10 +238,7 @@ namespace osu.Game.Overlays.Dashboard
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre, Origin = Anchor.TopCentre
// this is SHOCKING
Activity = { BindTarget = Activity },
Status = { BindTarget = Status },
}, },
new PurpleRoundedButton new PurpleRoundedButton
{ {

View File

@ -6,7 +6,6 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Online.Metadata; using osu.Game.Online.Metadata;
using osu.Game.Online.Multiplayer;
using osu.Game.Overlays.Dashboard; using osu.Game.Overlays.Dashboard;
using osu.Game.Overlays.Dashboard.Friends; using osu.Game.Overlays.Dashboard.Friends;
@ -18,6 +17,7 @@ namespace osu.Game.Overlays
private MetadataClient metadataClient { get; set; } = null!; private MetadataClient metadataClient { get; set; } = null!;
private IBindable<bool> metadataConnected = null!; private IBindable<bool> metadataConnected = null!;
private IDisposable? userPresenceWatchToken;
public DashboardOverlay() public DashboardOverlay()
: base(OverlayColourScheme.Purple) : base(OverlayColourScheme.Purple)
@ -61,9 +61,12 @@ namespace osu.Game.Overlays
return; return;
if (State.Value == Visibility.Visible) if (State.Value == Visibility.Visible)
metadataClient.BeginWatchingUserPresence().FireAndForget(); userPresenceWatchToken ??= metadataClient.BeginWatchingUserPresence();
else else
metadataClient.EndWatchingUserPresence().FireAndForget(); {
userPresenceWatchToken?.Dispose();
userPresenceWatchToken = null;
}
} }
} }
} }

View File

@ -12,6 +12,7 @@ using osu.Framework.Extensions.ObjectExtensions;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Metadata;
using osu.Game.Online.Spectator; using osu.Game.Online.Spectator;
using osu.Game.Replays; using osu.Game.Replays;
using osu.Game.Rulesets; using osu.Game.Rulesets;
@ -38,6 +39,9 @@ namespace osu.Game.Screens.Spectate
[Resolved] [Resolved]
private SpectatorClient spectatorClient { get; set; } = null!; private SpectatorClient spectatorClient { get; set; } = null!;
[Resolved]
private MetadataClient metadataClient { get; set; } = null!;
[Resolved] [Resolved]
private UserLookupCache userLookupCache { get; set; } = null!; private UserLookupCache userLookupCache { get; set; } = null!;
@ -50,6 +54,7 @@ namespace osu.Game.Screens.Spectate
private readonly Dictionary<int, SpectatorGameplayState> gameplayStates = new Dictionary<int, SpectatorGameplayState>(); private readonly Dictionary<int, SpectatorGameplayState> gameplayStates = new Dictionary<int, SpectatorGameplayState>();
private IDisposable? realmSubscription; private IDisposable? realmSubscription;
private IDisposable? userWatchToken;
/// <summary> /// <summary>
/// Creates a new <see cref="SpectatorScreen"/>. /// Creates a new <see cref="SpectatorScreen"/>.
@ -64,6 +69,8 @@ namespace osu.Game.Screens.Spectate
{ {
base.LoadComplete(); base.LoadComplete();
userWatchToken = metadataClient.BeginWatchingUserPresence();
userLookupCache.GetUsersAsync(users.ToArray()).ContinueWith(task => Schedule(() => userLookupCache.GetUsersAsync(users.ToArray()).ContinueWith(task => Schedule(() =>
{ {
var foundUsers = task.GetResultSafely(); var foundUsers = task.GetResultSafely();
@ -282,6 +289,7 @@ namespace osu.Game.Screens.Spectate
} }
realmSubscription?.Dispose(); realmSubscription?.Dispose();
userWatchToken?.Dispose();
} }
} }
} }

View File

@ -16,9 +16,6 @@ namespace osu.Game.Tests.Visual.Metadata
public override IBindable<bool> IsConnected => isConnected; public override IBindable<bool> IsConnected => isConnected;
private readonly BindableBool isConnected = new BindableBool(true); private readonly BindableBool isConnected = new BindableBool(true);
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
private readonly BindableBool isWatchingUserPresence = new BindableBool();
public override UserPresence LocalUserPresence => localUserPresence; public override UserPresence LocalUserPresence => localUserPresence;
private UserPresence localUserPresence; private UserPresence localUserPresence;
@ -34,15 +31,18 @@ namespace osu.Game.Tests.Visual.Metadata
[Resolved] [Resolved]
private IAPIProvider api { get; set; } = null!; private IAPIProvider api { get; set; } = null!;
public override Task BeginWatchingUserPresence() public event Action? OnBeginWatchingUserPresence;
public event Action? OnEndWatchingUserPresence;
protected override Task BeginWatchingUserPresenceInternal()
{ {
isWatchingUserPresence.Value = true; OnBeginWatchingUserPresence?.Invoke();
return Task.CompletedTask; return Task.CompletedTask;
} }
public override Task EndWatchingUserPresence() protected override Task EndWatchingUserPresenceInternal()
{ {
isWatchingUserPresence.Value = false; OnEndWatchingUserPresence?.Invoke();
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -50,7 +50,7 @@ namespace osu.Game.Tests.Visual.Metadata
{ {
localUserPresence = localUserPresence with { Activity = activity }; localUserPresence = localUserPresence with { Activity = activity };
if (isWatchingUserPresence.Value) if (IsWatchingUserPresence)
{ {
if (userPresences.ContainsKey(api.LocalUser.Value.Id)) if (userPresences.ContainsKey(api.LocalUser.Value.Id))
userPresences[api.LocalUser.Value.Id] = localUserPresence; userPresences[api.LocalUser.Value.Id] = localUserPresence;
@ -63,7 +63,7 @@ namespace osu.Game.Tests.Visual.Metadata
{ {
localUserPresence = localUserPresence with { Status = status }; localUserPresence = localUserPresence with { Status = status };
if (isWatchingUserPresence.Value) if (IsWatchingUserPresence)
{ {
if (userPresences.ContainsKey(api.LocalUser.Value.Id)) if (userPresences.ContainsKey(api.LocalUser.Value.Id))
userPresences[api.LocalUser.Value.Id] = localUserPresence; userPresences[api.LocalUser.Value.Id] = localUserPresence;
@ -74,7 +74,7 @@ namespace osu.Game.Tests.Visual.Metadata
public override Task UserPresenceUpdated(int userId, UserPresence? presence) public override Task UserPresenceUpdated(int userId, UserPresence? presence)
{ {
if (isWatchingUserPresence.Value) if (IsWatchingUserPresence)
{ {
if (presence?.Status != null) if (presence?.Status != null)
{ {

View File

@ -1,10 +1,8 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -15,43 +13,51 @@ using osu.Game.Users.Drawables;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Metadata;
namespace osu.Game.Users namespace osu.Game.Users
{ {
public abstract partial class ExtendedUserPanel : UserPanel public abstract partial class ExtendedUserPanel : UserPanel
{ {
public readonly Bindable<UserStatus?> Status = new Bindable<UserStatus?>(); protected TextFlowContainer LastVisitMessage { get; private set; } = null!;
public readonly IBindable<UserActivity> Activity = new Bindable<UserActivity>(); private StatusIcon statusIcon = null!;
private StatusText statusMessage = null!;
protected TextFlowContainer LastVisitMessage { get; private set; } [Resolved]
private MetadataClient? metadata { get; set; }
private StatusIcon statusIcon; private UserStatus? lastStatus;
private StatusText statusMessage; private UserActivity? lastActivity;
private DateTimeOffset? lastVisit;
protected ExtendedUserPanel(APIUser user) protected ExtendedUserPanel(APIUser user)
: base(user) : base(user)
{ {
lastVisit = user.LastVisit;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
BorderColour = ColourProvider?.Light1 ?? Colours.GreyVioletLighter; BorderColour = ColourProvider?.Light1 ?? Colours.GreyVioletLighter;
Status.ValueChanged += status => displayStatus(status.NewValue, Activity.Value);
Activity.ValueChanged += activity => displayStatus(Status.Value, activity.NewValue);
} }
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
Status.TriggerChange(); updatePresence();
// Colour should be applied immediately on first load. // Colour should be applied immediately on first load.
statusIcon.FinishTransforms(); statusIcon.FinishTransforms();
} }
protected override void Update()
{
base.Update();
updatePresence();
}
protected Container CreateStatusIcon() => statusIcon = new StatusIcon(); protected Container CreateStatusIcon() => statusIcon = new StatusIcon();
protected FillFlowContainer CreateStatusMessage(bool rightAlignedChildren) protected FillFlowContainer CreateStatusMessage(bool rightAlignedChildren)
@ -70,15 +76,6 @@ namespace osu.Game.Users
text.Origin = alignment; text.Origin = alignment;
text.AutoSizeAxes = Axes.Both; text.AutoSizeAxes = Axes.Both;
text.Alpha = 0; text.Alpha = 0;
if (User.LastVisit.HasValue)
{
text.AddText(@"Last seen ");
text.AddText(new DrawableDate(User.LastVisit.Value, italic: false)
{
Shadow = false
});
}
})); }));
statusContainer.Add(statusMessage = new StatusText statusContainer.Add(statusMessage = new StatusText
@ -91,37 +88,47 @@ namespace osu.Game.Users
return statusContainer; return statusContainer;
} }
private void displayStatus(UserStatus? status, UserActivity activity = null) private void updatePresence()
{ {
if (status != null) UserPresence? presence = metadata?.GetPresence(User.OnlineID);
UserStatus status = presence?.Status ?? UserStatus.Offline;
UserActivity? activity = presence?.Activity;
if (status == lastStatus && activity == lastActivity)
return;
if (status == UserStatus.Offline && lastVisit != null)
{ {
LastVisitMessage.FadeTo(status == UserStatus.Offline && User.LastVisit.HasValue ? 1 : 0); LastVisitMessage.FadeTo(1);
LastVisitMessage.Clear();
LastVisitMessage.AddText(@"Last seen ");
LastVisitMessage.AddText(new DrawableDate(lastVisit.Value, italic: false)
{
Shadow = false
});
}
else
LastVisitMessage.FadeTo(0);
// Set status message based on activity (if we have one) and status is not offline // Set status message based on activity (if we have one) and status is not offline
if (activity != null && status != UserStatus.Offline) if (activity != null && status != UserStatus.Offline)
{ {
statusMessage.Text = activity.GetStatus(); statusMessage.Text = activity.GetStatus();
statusMessage.TooltipText = activity.GetDetails(); statusMessage.TooltipText = activity.GetDetails() ?? string.Empty;
statusIcon.FadeColour(activity.GetAppropriateColour(Colours), 500, Easing.OutQuint); statusIcon.FadeColour(activity.GetAppropriateColour(Colours), 500, Easing.OutQuint);
return;
} }
// Otherwise use only status // Otherwise use only status
else
{
statusMessage.Text = status.GetLocalisableDescription(); statusMessage.Text = status.GetLocalisableDescription();
statusMessage.TooltipText = string.Empty; statusMessage.TooltipText = string.Empty;
statusIcon.FadeColour(status.Value.GetAppropriateColour(Colours), 500, Easing.OutQuint); statusIcon.FadeColour(status.GetAppropriateColour(Colours), 500, Easing.OutQuint);
return;
} }
// Fallback to web status if local one is null lastStatus = status;
if (User.IsOnline) lastActivity = activity;
{ lastVisit = status != UserStatus.Offline ? DateTimeOffset.Now : lastVisit;
Status.Value = UserStatus.Online;
return;
}
Status.Value = UserStatus.Offline;
} }
protected override bool OnHover(HoverEvent e) protected override bool OnHover(HoverEvent e)