GeneralStationRecordConsoleSystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Linq;
  2. using Content.Server.Station.Systems;
  3. using Content.Server.StationRecords.Components;
  4. using Content.Shared.StationRecords;
  5. using Robust.Server.GameObjects;
  6. namespace Content.Server.StationRecords.Systems;
  7. public sealed class GeneralStationRecordConsoleSystem : EntitySystem
  8. {
  9. [Dependency] private readonly UserInterfaceSystem _ui = default!;
  10. [Dependency] private readonly StationSystem _station = default!;
  11. [Dependency] private readonly StationRecordsSystem _stationRecords = default!;
  12. public override void Initialize()
  13. {
  14. SubscribeLocalEvent<GeneralStationRecordConsoleComponent, RecordModifiedEvent>(UpdateUserInterface);
  15. SubscribeLocalEvent<GeneralStationRecordConsoleComponent, AfterGeneralRecordCreatedEvent>(UpdateUserInterface);
  16. SubscribeLocalEvent<GeneralStationRecordConsoleComponent, RecordRemovedEvent>(UpdateUserInterface);
  17. Subs.BuiEvents<GeneralStationRecordConsoleComponent>(GeneralStationRecordConsoleKey.Key, subs =>
  18. {
  19. subs.Event<BoundUIOpenedEvent>(UpdateUserInterface);
  20. subs.Event<SelectStationRecord>(OnKeySelected);
  21. subs.Event<SetStationRecordFilter>(OnFiltersChanged);
  22. subs.Event<DeleteStationRecord>(OnRecordDelete);
  23. });
  24. }
  25. private void OnRecordDelete(Entity<GeneralStationRecordConsoleComponent> ent, ref DeleteStationRecord args)
  26. {
  27. if (!ent.Comp.CanDeleteEntries)
  28. return;
  29. var owning = _station.GetOwningStation(ent.Owner);
  30. if (owning != null)
  31. _stationRecords.RemoveRecord(new StationRecordKey(args.Id, owning.Value));
  32. UpdateUserInterface(ent); // Apparently an event does not get raised for this.
  33. }
  34. private void UpdateUserInterface<T>(Entity<GeneralStationRecordConsoleComponent> ent, ref T args)
  35. {
  36. UpdateUserInterface(ent);
  37. }
  38. // TODO: instead of copy paste shitcode for each record console, have a shared records console comp they all use
  39. // then have this somehow play nicely with creating ui state
  40. // if that gets done put it in StationRecordsSystem console helpers section :)
  41. private void OnKeySelected(Entity<GeneralStationRecordConsoleComponent> ent, ref SelectStationRecord msg)
  42. {
  43. ent.Comp.ActiveKey = msg.SelectedKey;
  44. UpdateUserInterface(ent);
  45. }
  46. private void OnFiltersChanged(Entity<GeneralStationRecordConsoleComponent> ent, ref SetStationRecordFilter msg)
  47. {
  48. if (ent.Comp.Filter == null ||
  49. ent.Comp.Filter.Type != msg.Type || ent.Comp.Filter.Value != msg.Value)
  50. {
  51. ent.Comp.Filter = new StationRecordsFilter(msg.Type, msg.Value);
  52. UpdateUserInterface(ent);
  53. }
  54. }
  55. private void UpdateUserInterface(Entity<GeneralStationRecordConsoleComponent> ent)
  56. {
  57. var (uid, console) = ent;
  58. var owningStation = _station.GetOwningStation(uid);
  59. if (!TryComp<StationRecordsComponent>(owningStation, out var stationRecords))
  60. {
  61. _ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, new GeneralStationRecordConsoleState());
  62. return;
  63. }
  64. var listing = _stationRecords.BuildListing((owningStation.Value, stationRecords), console.Filter);
  65. switch (listing.Count)
  66. {
  67. case 0:
  68. _ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, new GeneralStationRecordConsoleState());
  69. return;
  70. default:
  71. if (console.ActiveKey == null)
  72. console.ActiveKey = listing.Keys.First();
  73. break;
  74. }
  75. if (console.ActiveKey is not { } id)
  76. return;
  77. var key = new StationRecordKey(id, owningStation.Value);
  78. _stationRecords.TryGetRecord<GeneralStationRecord>(key, out var record, stationRecords);
  79. GeneralStationRecordConsoleState newState = new(id, record, listing, console.Filter, ent.Comp.CanDeleteEntries);
  80. _ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, newState);
  81. }
  82. }