Files
tbd-station-14/Content.Server/Robotics/Systems/RoboticsConsoleSystem.cs
deltanedas b33730db22 malf killer 9000 (robotics console) (#24855)
* create devicenet frequencies

* create borg transponder and give it to all nt borgs

* add robotics console

* actually implement battery charge display + some fix

* tab

* real explosion

* little safer

* disable destroy button clientside too when on cooldown

* m

* how do i do this when i review things...

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* webedit ops

Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>

* ui updates

* oracle java

* do a thing

* update ui when a borg times out

* maybe fix test

* add IsLocked to LockSystem

* make destroying gib the chassis again, so emagging isnt sus

* use locking

* require using alt click to unlock so normal click is open ui

* the

* use LogType.Action

* take this L

* pocket lint?

* sharer

* pro ops

* robor pushmarkup

* m

* update and make it not use prototype anymore

* frame0

* update yaml

* untroll

* bad

* h

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
2024-05-08 23:36:07 -07:00

147 lines
5.2 KiB
C#

using Content.Server.Administration.Logs;
using Content.Server.DeviceNetwork;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.Radio.EntitySystems;
using Content.Shared.Lock;
using Content.Shared.Database;
using Content.Shared.DeviceNetwork;
using Content.Shared.Robotics;
using Content.Shared.Robotics.Components;
using Content.Shared.Robotics.Systems;
using Robust.Server.GameObjects;
using Robust.Shared.Timing;
using System.Diagnostics.CodeAnalysis;
namespace Content.Server.Research.Systems;
/// <summary>
/// Handles UI and state receiving for the robotics control console.
/// <c>BorgTransponderComponent<c/> broadcasts state from the station's borgs to consoles.
/// </summary>
public sealed class RoboticsConsoleSystem : SharedRoboticsConsoleSystem
{
[Dependency] private readonly DeviceNetworkSystem _deviceNetwork = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly LockSystem _lock = default!;
[Dependency] private readonly RadioSystem _radio = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
// almost never timing out more than 1 per tick so initialize with that capacity
private List<string> _removing = new(1);
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoboticsConsoleComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
Subs.BuiEvents<RoboticsConsoleComponent>(RoboticsConsoleUiKey.Key, subs =>
{
subs.Event<BoundUIOpenedEvent>(OnOpened);
subs.Event<RoboticsConsoleDisableMessage>(OnDisable);
subs.Event<RoboticsConsoleDestroyMessage>(OnDestroy);
// TODO: camera stuff
});
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var now = _timing.CurTime;
var query = EntityQueryEnumerator<RoboticsConsoleComponent>();
while (query.MoveNext(out var uid, out var comp))
{
// remove cyborgs that havent pinged in a while
_removing.Clear();
foreach (var (address, data) in comp.Cyborgs)
{
if (now >= data.Timeout)
_removing.Add(address);
}
// needed to prevent modifying while iterating it
foreach (var address in _removing)
{
comp.Cyborgs.Remove(address);
}
if (_removing.Count > 0)
UpdateUserInterface((uid, comp));
}
}
private void OnPacketReceived(Entity<RoboticsConsoleComponent> ent, ref DeviceNetworkPacketEvent args)
{
var payload = args.Data;
if (!payload.TryGetValue(DeviceNetworkConstants.Command, out string? command))
return;
if (command != DeviceNetworkConstants.CmdUpdatedState)
return;
if (!payload.TryGetValue(RoboticsConsoleConstants.NET_CYBORG_DATA, out CyborgControlData? data))
return;
var real = data.Value;
real.Timeout = _timing.CurTime + ent.Comp.Timeout;
ent.Comp.Cyborgs[args.SenderAddress] = real;
UpdateUserInterface(ent);
}
private void OnOpened(Entity<RoboticsConsoleComponent> ent, ref BoundUIOpenedEvent args)
{
UpdateUserInterface(ent);
}
private void OnDisable(Entity<RoboticsConsoleComponent> ent, ref RoboticsConsoleDisableMessage args)
{
if (_lock.IsLocked(ent.Owner))
return;
if (!ent.Comp.Cyborgs.TryGetValue(args.Address, out var data))
return;
var payload = new NetworkPayload()
{
[DeviceNetworkConstants.Command] = RoboticsConsoleConstants.NET_DISABLE_COMMAND
};
_deviceNetwork.QueuePacket(ent, args.Address, payload);
_adminLogger.Add(LogType.Action, LogImpact.High, $"{ToPrettyString(args.Actor):user} disabled borg {data.Name} with address {args.Address}");
}
private void OnDestroy(Entity<RoboticsConsoleComponent> ent, ref RoboticsConsoleDestroyMessage args)
{
if (_lock.IsLocked(ent.Owner))
return;
var now = _timing.CurTime;
if (now < ent.Comp.NextDestroy)
return;
if (!ent.Comp.Cyborgs.Remove(args.Address, out var data))
return;
var payload = new NetworkPayload()
{
[DeviceNetworkConstants.Command] = RoboticsConsoleConstants.NET_DESTROY_COMMAND
};
_deviceNetwork.QueuePacket(ent, args.Address, payload);
var message = Loc.GetString(ent.Comp.DestroyMessage, ("name", data.Name));
_radio.SendRadioMessage(ent, message, ent.Comp.RadioChannel, ent);
_adminLogger.Add(LogType.Action, LogImpact.Extreme, $"{ToPrettyString(args.Actor):user} destroyed borg {data.Name} with address {args.Address}");
ent.Comp.NextDestroy = now + ent.Comp.DestroyCooldown;
Dirty(ent, ent.Comp);
}
private void UpdateUserInterface(Entity<RoboticsConsoleComponent> ent)
{
var state = new RoboticsConsoleState(ent.Comp.Cyborgs);
_ui.SetUiState(ent.Owner, RoboticsConsoleUiKey.Key, state);
}
}