using System;
using DiscordRPC;
namespace ETDiscordPresence
{
internal class PresenceChanger : IDisposable
{
private readonly DiscordRpcClient client;
private GameWatcher.ServerInfo? currentServerInfo;
internal PresenceChanger()
{
client = new DiscordRpcClient("626868455299219466");
client.OnReady += (sender, args) => ApplyPresence();
client.OnPresenceUpdate += (sender, args) => ApplyPresence();
client.Initialize();
}
internal void SetPresence(GameWatcher.ServerInfo serverInfo)
{
currentServerInfo = serverInfo;
ApplyPresence();
}
internal void RemovePresence()
{
if (IsActivePresence())
{
client.ClearPresence();
}
currentServerInfo = null;
}
private void ApplyPresence()
{
if (!client.IsInitialized)
{
return;
}
if (IsActivePresence() || currentServerInfo == null)
{
return;
}
client.SetPresence(new RichPresence()
{
Details = currentServerInfo.Value.Hostname,
State = currentServerInfo.Value.Address,
Assets = new Assets()
{
LargeImageKey = "logo",
LargeImageText = "Wolfenstein - Enemy Territory",
}
});
}
private bool IsActivePresence()
{
if (currentServerInfo == null)
{
return false;
}
return client.CurrentPresence != null
&& client.CurrentPresence.Details == currentServerInfo.Value.Hostname
&& client.CurrentPresence.State == currentServerInfo.Value.Address;
}
public void Dispose()
{
RemovePresence();
client.Dispose();
}
}
}