122 lines
4.8 KiB
C#
122 lines
4.8 KiB
C#
using System.Linq;
|
||
using Content.Server.Station.Systems;
|
||
using Content.Server._Sunrise.Messenger;
|
||
using Content.Server.StationRecords.Components;
|
||
using Content.Shared.StationRecords;
|
||
using Robust.Server.GameObjects;
|
||
|
||
namespace Content.Server.StationRecords.Systems;
|
||
|
||
public sealed partial class GeneralStationRecordConsoleSystem : EntitySystem
|
||
{
|
||
[Dependency] private readonly UserInterfaceSystem _ui = default!;
|
||
[Dependency] private readonly StationSystem _station = default!;
|
||
[Dependency] private readonly StationRecordsSystem _stationRecords = default!;
|
||
[Dependency] private readonly MessengerServerSystem _messenger = 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>(OnOpened);
|
||
subs.Event<SelectStationRecord>(OnKeySelected);
|
||
subs.Event<SetStationRecordFilter>(OnFiltersChanged);
|
||
subs.Event<DeleteStationRecord>(OnRecordDelete);
|
||
});
|
||
|
||
// Sunrise added
|
||
InitializeSunrise();
|
||
}
|
||
|
||
private void OnRecordDelete(Entity<GeneralStationRecordConsoleComponent> ent, ref DeleteStationRecord args)
|
||
{
|
||
if (!ent.Comp.CanDeleteEntries)
|
||
return;
|
||
|
||
var owning = _station.GetOwningStation(ent.Owner);
|
||
|
||
// Sunrise added start
|
||
if (owning == null)
|
||
return;
|
||
|
||
// Дополнительная серверная проверка на случай педиков с читами
|
||
if (!HasAccess(ent, args.Actor))
|
||
return;
|
||
|
||
if (!_stationRecords.TryGetRecord<GeneralStationRecord>(new StationRecordKey(args.Id, owning.Value), out var record))
|
||
return;
|
||
|
||
var message = Loc.GetString("station-record-deleted", ("name", record.Name));
|
||
var popup = Loc.GetString("station-record-deleted-successfully");
|
||
|
||
DoFeedback(ent, message, popup);
|
||
// Sunrise added end
|
||
|
||
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);
|
||
|
||
// Sunrise edit
|
||
GeneralStationRecordConsoleState newState = new(id, record, listing, console.Filter, ent.Comp.CanDeleteEntries, ent.Comp.CanRedactSensitiveData, ent.Comp.HasAccess);
|
||
_ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, newState);
|
||
}
|
||
}
|