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(UpdateUserInterface); SubscribeLocalEvent(UpdateUserInterface); SubscribeLocalEvent(UpdateUserInterface); Subs.BuiEvents(GeneralStationRecordConsoleKey.Key, subs => { subs.Event(UpdateUserInterface); subs.Event(OnKeySelected); subs.Event(OnFiltersChanged); subs.Event(OnRecordDelete); }); } private void OnRecordDelete(Entity 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(Entity 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 ent, ref SelectStationRecord msg) { ent.Comp.ActiveKey = msg.SelectedKey; UpdateUserInterface(ent); } private void OnFiltersChanged(Entity 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 ent) { var (uid, console) = ent; var owningStation = _station.GetOwningStation(uid); if (!TryComp(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(key, out var record, stationRecords); GeneralStationRecordConsoleState newState = new(id, record, listing, console.Filter, ent.Comp.CanDeleteEntries); _ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, newState); } }