Files
tbd-station-14/Content.Server/StationRecords/Systems/GeneralStationRecordConsoleSystem.cs
nikthechampiongr c8b55e5e44 Record deletion (#27883)
* Allow for Station Records interface for aghosts to delete records

* Fix record consoles not working when there are more than 2 crew members.

HOW DID NOONE NOTICE THIS SOONER???

* Stop being unconventional
2024-05-12 17:07:54 +02:00

100 lines
4.0 KiB
C#

using System.Linq;
using Content.Server.Station.Systems;
using Content.Server.StationRecords.Components;
using Content.Shared.StationRecords;
using Robust.Server.GameObjects;
namespace Content.Server.StationRecords.Systems;
public sealed class GeneralStationRecordConsoleSystem : EntitySystem
{
[Dependency] private readonly UserInterfaceSystem _ui = default!;
[Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly StationRecordsSystem _stationRecords = default!;
public override void Initialize()
{
SubscribeLocalEvent<GeneralStationRecordConsoleComponent, RecordModifiedEvent>(UpdateUserInterface);
SubscribeLocalEvent<GeneralStationRecordConsoleComponent, AfterGeneralRecordCreatedEvent>(UpdateUserInterface);
SubscribeLocalEvent<GeneralStationRecordConsoleComponent, RecordRemovedEvent>(UpdateUserInterface);
Subs.BuiEvents<GeneralStationRecordConsoleComponent>(GeneralStationRecordConsoleKey.Key, subs =>
{
subs.Event<BoundUIOpenedEvent>(UpdateUserInterface);
subs.Event<SelectStationRecord>(OnKeySelected);
subs.Event<SetStationRecordFilter>(OnFiltersChanged);
subs.Event<DeleteStationRecord>(OnRecordDelete);
});
}
private void OnRecordDelete(Entity<GeneralStationRecordConsoleComponent> ent, ref DeleteStationRecord args)
{
if (!ent.Comp.CanDeleteEntries)
return;
var owning = _station.GetOwningStation(ent.Owner);
if (owning != null)
_stationRecords.RemoveRecord(new StationRecordKey(args.Id, owning.Value));
UpdateUserInterface(ent); // Apparently an event does not get raised for this.
}
private void UpdateUserInterface<T>(Entity<GeneralStationRecordConsoleComponent> ent, ref T args)
{
UpdateUserInterface(ent);
}
// TODO: instead of copy paste shitcode for each record console, have a shared records console comp they all use
// then have this somehow play nicely with creating ui state
// if that gets done put it in StationRecordsSystem console helpers section :)
private void OnKeySelected(Entity<GeneralStationRecordConsoleComponent> ent, ref SelectStationRecord msg)
{
ent.Comp.ActiveKey = msg.SelectedKey;
UpdateUserInterface(ent);
}
private void OnFiltersChanged(Entity<GeneralStationRecordConsoleComponent> ent, ref SetStationRecordFilter msg)
{
if (ent.Comp.Filter == null ||
ent.Comp.Filter.Type != msg.Type || ent.Comp.Filter.Value != msg.Value)
{
ent.Comp.Filter = new StationRecordsFilter(msg.Type, msg.Value);
UpdateUserInterface(ent);
}
}
private void UpdateUserInterface(Entity<GeneralStationRecordConsoleComponent> ent)
{
var (uid, console) = ent;
var owningStation = _station.GetOwningStation(uid);
if (!TryComp<StationRecordsComponent>(owningStation, out var stationRecords))
{
_ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, new GeneralStationRecordConsoleState());
return;
}
var listing = _stationRecords.BuildListing((owningStation.Value, stationRecords), console.Filter);
switch (listing.Count)
{
case 0:
_ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, new GeneralStationRecordConsoleState());
return;
default:
if (console.ActiveKey == null)
console.ActiveKey = listing.Keys.First();
break;
}
if (console.ActiveKey is not { } id)
return;
var key = new StationRecordKey(id, owningStation.Value);
_stationRecords.TryGetRecord<GeneralStationRecord>(key, out var record, stationRecords);
GeneralStationRecordConsoleState newState = new(id, record, listing, console.Filter, ent.Comp.CanDeleteEntries);
_ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, newState);
}
}