uplink locking/unlocking, minor pda refactor (#15842)

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-05-01 06:30:08 +00:00
committed by GitHub
parent 904d61baee
commit 44f9c098ec
13 changed files with 134 additions and 49 deletions

View File

@@ -6,6 +6,8 @@ using Content.Server.Light.EntitySystems;
using Content.Server.Light.Events;
using Content.Server.PDA.Ringer;
using Content.Server.Station.Systems;
using Content.Server.Store.Components;
using Content.Server.Store.Systems;
using Content.Server.UserInterface;
using Content.Shared.PDA;
using Robust.Server.GameObjects;
@@ -18,12 +20,13 @@ namespace Content.Server.PDA
{
public sealed class PDASystem : SharedPDASystem
{
[Dependency] private readonly CartridgeLoaderSystem _cartridgeLoader = default!;
[Dependency] private readonly InstrumentSystem _instrument = default!;
[Dependency] private readonly RingerSystem _ringer = default!;
[Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly StoreSystem _store = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
[Dependency] private readonly UnpoweredFlashlightSystem _unpoweredFlashlight = default!;
[Dependency] private readonly RingerSystem _ringerSystem = default!;
[Dependency] private readonly InstrumentSystem _instrumentSystem = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
[Dependency] private readonly CartridgeLoaderSystem _cartridgeLoaderSystem = default!;
public override void Initialize()
{
@@ -40,43 +43,46 @@ namespace Content.Server.PDA
if (!TryComp(uid, out ServerUserInterfaceComponent? uiComponent))
return;
UpdateStationName(pda);
UpdateStationName(uid, pda);
if (_uiSystem.TryGetUi(uid, PDAUiKey.Key, out var ui, uiComponent))
if (_ui.TryGetUi(uid, PDAUiKey.Key, out var ui, uiComponent))
ui.OnReceiveMessage += (msg) => OnUIMessage(pda, msg);
}
protected override void OnItemInserted(EntityUid uid, PDAComponent pda, EntInsertedIntoContainerMessage args)
{
base.OnItemInserted(uid, pda, args);
UpdatePDAUserInterface(pda);
UpdatePdaUi(uid, pda);
}
protected override void OnItemRemoved(EntityUid uid, PDAComponent pda, EntRemovedFromContainerMessage args)
{
base.OnItemRemoved(uid, pda, args);
UpdatePDAUserInterface(pda);
UpdatePdaUi(uid, pda);
}
private void OnLightToggle(EntityUid uid, PDAComponent pda, LightToggleEvent args)
{
pda.FlashlightOn = args.IsOn;
UpdatePDAUserInterface(pda);
UpdatePdaUi(uid, pda);
}
public void SetOwner(PDAComponent pda, string ownerName)
public void SetOwner(EntityUid uid, PDAComponent pda, string ownerName)
{
pda.OwnerName = ownerName;
UpdatePDAUserInterface(pda);
UpdatePdaUi(uid, pda);
}
private void OnGridChanged(EntityUid uid, PDAComponent pda, GridModifiedEvent args)
{
UpdateStationName(pda);
UpdatePDAUserInterface(pda);
UpdateStationName(uid, pda);
UpdatePdaUi(uid, pda);
}
private void UpdatePDAUserInterface(PDAComponent pda)
/// <summary>
/// Send new UI state to clients, call if you modify something like uplink.
/// </summary>
public void UpdatePdaUi(EntityUid uid, PDAComponent pda)
{
var ownerInfo = new PDAIdInfoText
{
@@ -85,49 +91,62 @@ namespace Content.Server.PDA
JobTitle = pda.ContainedID?.JobTitle
};
if (!_uiSystem.TryGetUi(pda.Owner, PDAUiKey.Key, out var ui))
if (!_ui.TryGetUi(uid, PDAUiKey.Key, out var ui))
return;
var address = GetDeviceNetAddress(pda.Owner);
var hasInstrument = HasComp<InstrumentComponent>(pda.Owner);
var state = new PDAUpdateState(pda.FlashlightOn, pda.PenSlot.HasItem, ownerInfo, pda.StationName, false, hasInstrument, address);
var address = GetDeviceNetAddress(uid);
var hasInstrument = HasComp<InstrumentComponent>(uid);
var showUplink = HasComp<StoreComponent>(uid) && IsUnlocked(uid);
_cartridgeLoaderSystem?.UpdateUiState(pda.Owner, state);
var state = new PDAUpdateState(pda.FlashlightOn, pda.PenSlot.HasItem, ownerInfo, pda.StationName, showUplink, hasInstrument, address);
_cartridgeLoader?.UpdateUiState(uid, state);
}
private void OnUIMessage(PDAComponent pda, ServerBoundUserInterfaceMessage msg)
{
var pdaEnt = pda.Owner;
var uid = pda.Owner;
// todo: move this to entity events
switch (msg.Message)
{
case PDARequestUpdateInterfaceMessage _:
UpdatePDAUserInterface(pda);
UpdatePdaUi(uid, pda);
break;
case PDAToggleFlashlightMessage _:
{
if (EntityManager.TryGetComponent(pdaEnt, out UnpoweredFlashlightComponent? flashlight))
_unpoweredFlashlight.ToggleLight(pdaEnt, flashlight);
if (TryComp<UnpoweredFlashlightComponent>(uid, out var flashlight))
_unpoweredFlashlight.ToggleLight(uid, flashlight);
break;
}
case PDAShowRingtoneMessage _:
{
if (EntityManager.TryGetComponent(pdaEnt, out RingerComponent? ringer))
_ringerSystem.ToggleRingerUI(ringer, msg.Session);
if (TryComp<RingerComponent>(uid, out var ringer))
_ringer.ToggleRingerUI(ringer, msg.Session);
break;
}
case PDAShowMusicMessage _:
{
if (TryComp(pdaEnt, out InstrumentComponent? instrument))
_instrumentSystem.ToggleInstrumentUi(pdaEnt, msg.Session, instrument);
if (TryComp<InstrumentComponent>(uid, out var instrument))
_instrument.ToggleInstrumentUi(uid, msg.Session, instrument);
break;
}
case PDAShowUplinkMessage _:
{
// check if its locked again to prevent malicious clients opening locked uplinks
if (TryComp<StoreComponent>(uid, out var store) && IsUnlocked(uid))
_store.ToggleUi(msg.Session.AttachedEntity!.Value, uid, store);
break;
}
}
}
private void UpdateStationName(PDAComponent pda)
private bool IsUnlocked(EntityUid uid)
{
var station = _stationSystem.GetOwningStation(pda.Owner);
return TryComp<RingerUplinkComponent>(uid, out var uplink) ? uplink.Unlocked : true;
}
private void UpdateStationName(EntityUid uid, PDAComponent pda)
{
var station = _station.GetOwningStation(uid);
pda.StationName = station is null ? null : Name(station.Value);
}