using Content.Server.Chat;
using Content.Server.Chat.Systems;
using Content.Server.Communications;
using Content.Server.Station.Systems;
using Content.Shared.GameTicking;
using Robust.Shared.Random;
namespace Content.Server.Nuke
{
///
/// Nuclear code is generated once per round
/// One code works for all nukes
///
public sealed class NukeCodeSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
private const int CodeLength = 6;
public string Code { get; private set; } = default!;
public override void Initialize()
{
base.Initialize();
GenerateNewCode();
SubscribeLocalEvent(OnRestart);
}
private void OnRestart(RoundRestartCleanupEvent ev)
{
GenerateNewCode();
}
///
/// Checks if code is equal to current bombs code
///
public bool IsCodeValid(string code)
{
return code == Code;
}
///
/// Generate a new nuclear bomb code. Replacing old one.
///
public void GenerateNewCode()
{
var ret = "";
for (var i = 0; i < CodeLength; i++)
{
var c = (char) _random.Next('0', '9' + 1);
ret += c;
}
Code = ret;
}
///
/// Send a nuclear code to all communication consoles
///
/// True if at least one console received codes
public bool SendNukeCodes()
{
// todo: this should probably be handled by fax system
var wasSent = false;
var consoles = EntityManager.EntityQuery();
foreach (var console in consoles)
{
if (!EntityManager.TryGetComponent((console).Owner, out TransformComponent? transform))
continue;
var consolePos = transform.MapPosition;
EntityManager.SpawnEntity("NukeCodePaper", consolePos);
wasSent = true;
}
// TODO: Allow selecting a station for nuke codes
if (wasSent)
{
var msg = Loc.GetString("nuke-component-announcement-send-codes");
_chatSystem.DispatchGlobalAnnouncement(msg, colorOverride: Color.Red);
}
return wasSent;
}
}
}