2019-04-13 19:06:53 +08:00
// 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 DiscordRPC ;
using osu.Framework.Allocation ;
using osu.Framework.Bindables ;
using osu.Framework.Graphics ;
using osu.Framework.Logging ;
using osu.Game.Online.API ;
using osu.Game.Rulesets ;
using osu.Game.Users ;
2019-05-15 01:13:21 +08:00
using static osu . Game . Users . UserActivity ;
2019-04-13 19:06:53 +08:00
using User = osu . Game . Users . User ;
namespace osu.Desktop
{
internal class DiscordRichPresenceClient : Component
{
2019-04-15 04:18:57 +08:00
private const string client_id = "563024054391537674" ;
2019-04-13 19:06:53 +08:00
private Bindable < User > user ;
private readonly DiscordRpcClient client = new DiscordRpcClient ( client_id ) ;
[BackgroundDependencyLoader]
private void load ( IAPIProvider provider )
{
user = provider . LocalUser . GetBoundCopy ( ) ;
user . ValueChanged + = usr = >
{
2019-05-15 01:13:21 +08:00
usr . NewValue . Activity . ValueChanged + = activity = > updateStatus ( user . Value . Status . Value , activity . NewValue ) ;
usr . NewValue . Status . ValueChanged + = status = > updateStatus ( status . NewValue , user . Value . Activity . Value ) ;
2019-04-13 19:06:53 +08:00
} ;
2019-05-15 01:13:21 +08:00
user . TriggerChange ( ) ;
2019-04-13 19:06:53 +08:00
2019-05-15 01:13:21 +08:00
enableLogging ( ) ;
client . Initialize ( ) ;
}
private void enableLogging ( )
{
2019-04-13 19:06:53 +08:00
client . OnReady + = ( _ , __ ) = > Logger . Log ( "Discord RPC Client ready." , LoggingTarget . Network , LogLevel . Debug ) ;
client . OnError + = ( _ , e ) = > Logger . Log ( $"An error occurred with Discord RPC Client : {e.Message}" , LoggingTarget . Network , LogLevel . Debug ) ;
client . OnConnectionFailed + = ( _ , e ) = > Logger . Log ( "Discord RPC Client failed to initialize : is discord running ?" , LoggingTarget . Network , LogLevel . Debug ) ;
client . OnPresenceUpdate + = ( _ , __ ) = > Logger . Log ( "Updated Discord Rich Presence" , LoggingTarget . Network , LogLevel . Debug ) ;
}
2019-05-15 01:13:21 +08:00
private void updateStatus ( UserStatus st , UserActivity a )
2019-04-13 19:06:53 +08:00
{
2019-05-15 01:13:21 +08:00
var presence = defaultPresence ( st is UserStatusOnline ? a ? . Status : st . Message ) ;
if ( ! ( st is UserStatusOnline ) ) //don't update the presence any further if the current user status is DND / Offline & simply return with the default presence
{
client . SetPresence ( presence ) ;
return ;
}
2019-04-13 19:06:53 +08:00
2019-05-15 01:13:21 +08:00
switch ( a )
2019-04-13 19:06:53 +08:00
{
2019-05-15 01:13:21 +08:00
case UserActivitySoloGame game :
2019-04-13 19:06:53 +08:00
presence . State = $"{game.Beatmap.Metadata.Artist} - {game.Beatmap.Metadata.Title} [{game.Beatmap.Version}]" ;
2019-04-13 19:25:52 +08:00
setPresenceGamemode ( game . Ruleset , presence ) ;
2019-04-13 19:06:53 +08:00
break ;
2019-05-15 01:13:21 +08:00
case UserActivityEditing editing :
2019-04-15 04:18:57 +08:00
presence . State = $"{editing.Beatmap.Metadata.Artist} - {editing.Beatmap.Metadata.Title} " + ( ! string . IsNullOrEmpty ( editing . Beatmap . Version ) ? $"[{editing.Beatmap.Version}]" : "" ) ;
presence . Assets . SmallImageKey = "edit" ;
presence . Assets . SmallImageText = "editing" ;
2019-04-13 19:06:53 +08:00
break ;
}
client . SetPresence ( presence ) ;
}
private void setPresenceGamemode ( RulesetInfo ruleset , RichPresence presence )
{
switch ( ruleset . ID )
{
case 0 :
2019-05-17 00:36:54 +08:00
presence . Assets . SmallImageKey = ruleset . ShortName ;
2019-04-13 19:06:53 +08:00
break ;
case 1 :
2019-05-17 00:36:54 +08:00
presence . Assets . SmallImageKey = ruleset . ShortName ;
2019-04-13 19:06:53 +08:00
break ;
case 2 :
2019-05-17 00:36:54 +08:00
presence . Assets . SmallImageKey = ruleset . ShortName ;
2019-04-13 19:06:53 +08:00
break ;
case 3 :
2019-05-17 00:36:54 +08:00
presence . Assets . SmallImageKey = ruleset . ShortName ;
2019-04-13 19:06:53 +08:00
break ;
2019-04-15 04:18:57 +08:00
default :
presence . Assets . SmallImageKey = "unknown" ;
break ;
2019-04-13 19:06:53 +08:00
}
presence . Assets . SmallImageText = ruleset . ShortName ;
}
private RichPresence defaultPresence ( string status ) = > new RichPresence
{
Details = status ,
Assets = new Assets
{
LargeImageKey = "lazer" ,
2019-04-15 03:12:10 +08:00
LargeImageText = user . Value . Username
2019-04-13 19:06:53 +08:00
}
} ;
2019-05-15 01:37:43 +08:00
protected override void Dispose ( bool isDisposing )
{
client . Dispose ( ) ;
base . Dispose ( isDisposing ) ;
}
2019-04-13 19:06:53 +08:00
}
}