Skip to content

Commit

Permalink
Update DiscordRichPresence to use new statistics provider component
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Oct 25, 2024
1 parent fdeb8b9 commit 663b769
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions osu.Desktop/DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using osu.Game;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Multiplayer;
Expand Down Expand Up @@ -47,6 +48,9 @@ internal partial class DiscordRichPresence : Component
[Resolved]
private MultiplayerClient multiplayerClient { get; set; } = null!;

[Resolved]
private LocalUserStatisticsProvider statisticsProvider { get; set; } = null!;

[Resolved]
private OsuConfigManager config { get; set; } = null!;

Expand All @@ -65,6 +69,7 @@ internal partial class DiscordRichPresence : Component
};

private IBindable<APIUser>? user;
private IBindable<UserStatistics?>? localStatistics;

[BackgroundDependencyLoader]
private void load()
Expand Down Expand Up @@ -117,6 +122,10 @@ protected override void LoadComplete()
status.BindValueChanged(_ => schedulePresenceUpdate());
activity.BindValueChanged(_ => schedulePresenceUpdate());
privacyMode.BindValueChanged(_ => schedulePresenceUpdate());

localStatistics = statisticsProvider.Statistics.GetBoundCopy();
localStatistics.BindValueChanged(_ => schedulePresenceUpdate());

multiplayerClient.RoomUpdated += onRoomUpdated;
}

Expand Down Expand Up @@ -158,7 +167,7 @@ private void schedulePresenceUpdate()

private void updatePresence(bool hideIdentifiableInformation)
{
if (user == null)
if (user == null || localStatistics == null)
return;

// user activity
Expand Down Expand Up @@ -228,12 +237,7 @@ private void updatePresence(bool hideIdentifiableInformation)
if (privacyMode.Value == DiscordRichPresenceMode.Limited)
presence.Assets.LargeImageText = string.Empty;
else
{
if (user.Value.RulesetsStatistics != null && user.Value.RulesetsStatistics.TryGetValue(ruleset.Value.ShortName, out UserStatistics? statistics))
presence.Assets.LargeImageText = $"{user.Value.Username}" + (statistics.GlobalRank > 0 ? $" (rank #{statistics.GlobalRank:N0})" : string.Empty);
else
presence.Assets.LargeImageText = $"{user.Value.Username}" + (user.Value.Statistics?.GlobalRank > 0 ? $" (rank #{user.Value.Statistics.GlobalRank:N0})" : string.Empty);
}
presence.Assets.LargeImageText = $"{user.Value.Username}" + (localStatistics.Value?.GlobalRank > 0 ? $" (rank #{localStatistics.Value?.GlobalRank:N0})" : string.Empty);

// small image
presence.Assets.SmallImageKey = ruleset.Value.IsLegacyRuleset() ? $"mode_{ruleset.Value.OnlineID}" : "mode_custom";
Expand Down

0 comments on commit 663b769

Please sign in to comment.