using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Content.Server._White.Discord.GameTicking;
using Content.Server.Maps;
using Content.Shared.GameTicking;
using Content.Shared._White;
using Robust.Shared;
using Robust.Shared.Configuration;
namespace Content.Server._White.Discord;
///
/// Listen game events and send notifications to Discord
///
public sealed class RoundNotificationsSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _config = default!;
[Dependency] private readonly IGameMapManager _gameMapManager = default!;
private ISawmill _sawmill = default!;
private readonly HttpClient _httpClient = new();
private string _webhookUrl = string.Empty;
private string _roleId = string.Empty;
private string _serverName = string.Empty;
private bool _roundStartOnly;
private const int EmbedColor = 0x6600FF;
private const int EmbedColorRestart = 0x00eb1f;
///
public override void Initialize()
{
SubscribeLocalEvent(OnRoundRestart);
SubscribeLocalEvent(OnRoundStarted);
SubscribeLocalEvent(OnRoundEnded);
_config.OnValueChanged(WhiteCVars.DiscordRoundWebhook, value => _webhookUrl = value, true);
_config.OnValueChanged(WhiteCVars.DiscordRoundRoleId, value => _roleId = value, true);
_config.OnValueChanged(WhiteCVars.DiscordRoundStartOnly, value => _roundStartOnly = value, true);
_config.OnValueChanged(CVars.GameHostName, value => _serverName = value, true);
_sawmill = IoCManager.Resolve().GetSawmill("notifications");
}
private void OnRoundRestart(RoundRestartCleanupEvent e)
{
if (string.IsNullOrEmpty(_webhookUrl))
return;
var serverName = _serverName[..Math.Min(_serverName.Length, 1500)];
var payload = new WebhookPayload()
{
Embeds = new List