BorgSystem.Ui.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Linq;
  2. using Content.Shared.UserInterface;
  3. using Content.Shared.Database;
  4. using Content.Shared.NameIdentifier;
  5. using Content.Shared.PowerCell.Components;
  6. using Content.Shared.Preferences;
  7. using Content.Shared.Silicons.Borgs;
  8. using Content.Shared.Silicons.Borgs.Components;
  9. namespace Content.Server.Silicons.Borgs;
  10. /// <inheritdoc/>
  11. public sealed partial class BorgSystem
  12. {
  13. public void InitializeUI()
  14. {
  15. SubscribeLocalEvent<BorgChassisComponent, BeforeActivatableUIOpenEvent>(OnBeforeBorgUiOpen);
  16. SubscribeLocalEvent<BorgChassisComponent, BorgEjectBrainBuiMessage>(OnEjectBrainBuiMessage);
  17. SubscribeLocalEvent<BorgChassisComponent, BorgEjectBatteryBuiMessage>(OnEjectBatteryBuiMessage);
  18. SubscribeLocalEvent<BorgChassisComponent, BorgSetNameBuiMessage>(OnSetNameBuiMessage);
  19. SubscribeLocalEvent<BorgChassisComponent, BorgRemoveModuleBuiMessage>(OnRemoveModuleBuiMessage);
  20. }
  21. private void OnBeforeBorgUiOpen(EntityUid uid, BorgChassisComponent component, BeforeActivatableUIOpenEvent args)
  22. {
  23. UpdateUI(uid, component);
  24. }
  25. private void OnEjectBrainBuiMessage(EntityUid uid, BorgChassisComponent component, BorgEjectBrainBuiMessage args)
  26. {
  27. if (component.BrainEntity is not { } brain)
  28. return;
  29. _adminLog.Add(LogType.Action, LogImpact.Medium,
  30. $"{ToPrettyString(args.Actor):player} removed brain {ToPrettyString(brain)} from borg {ToPrettyString(uid)}");
  31. _container.Remove(brain, component.BrainContainer);
  32. _hands.TryPickupAnyHand(args.Actor, brain);
  33. UpdateUI(uid, component);
  34. }
  35. private void OnEjectBatteryBuiMessage(EntityUid uid, BorgChassisComponent component, BorgEjectBatteryBuiMessage args)
  36. {
  37. if (!TryComp<PowerCellSlotComponent>(uid, out var slotComp) ||
  38. !Container.TryGetContainer(uid, slotComp.CellSlotId, out var container) ||
  39. !container.ContainedEntities.Any())
  40. {
  41. return;
  42. }
  43. var ents = Container.EmptyContainer(container);
  44. _hands.TryPickupAnyHand(args.Actor, ents.First());
  45. }
  46. private void OnSetNameBuiMessage(EntityUid uid, BorgChassisComponent component, BorgSetNameBuiMessage args)
  47. {
  48. if (args.Name.Length > HumanoidCharacterProfile.MaxNameLength ||
  49. args.Name.Length == 0 ||
  50. string.IsNullOrWhiteSpace(args.Name) ||
  51. string.IsNullOrEmpty(args.Name))
  52. {
  53. return;
  54. }
  55. var name = args.Name.Trim();
  56. if (TryComp<NameIdentifierComponent>(uid, out var identifier))
  57. name = $"{name} {identifier.FullIdentifier}";
  58. var metaData = MetaData(uid);
  59. // don't change the name if the value doesn't actually change
  60. if (metaData.EntityName.Equals(name, StringComparison.InvariantCulture))
  61. return;
  62. _adminLog.Add(LogType.Action, LogImpact.High, $"{ToPrettyString(args.Actor):player} set borg \"{ToPrettyString(uid)}\"'s name to: {name}");
  63. _metaData.SetEntityName(uid, name, metaData);
  64. }
  65. private void OnRemoveModuleBuiMessage(EntityUid uid, BorgChassisComponent component, BorgRemoveModuleBuiMessage args)
  66. {
  67. var module = GetEntity(args.Module);
  68. if (!component.ModuleContainer.Contains(module))
  69. return;
  70. if (!CanRemoveModule((uid, component), (module, Comp<BorgModuleComponent>(module)), args.Actor))
  71. return;
  72. _adminLog.Add(LogType.Action, LogImpact.Medium,
  73. $"{ToPrettyString(args.Actor):player} removed module {ToPrettyString(module)} from borg {ToPrettyString(uid)}");
  74. _container.Remove(module, component.ModuleContainer);
  75. _hands.TryPickupAnyHand(args.Actor, module);
  76. UpdateUI(uid, component);
  77. }
  78. public void UpdateUI(EntityUid uid, BorgChassisComponent? component = null)
  79. {
  80. if (!Resolve(uid, ref component))
  81. return;
  82. var chargePercent = 0f;
  83. var hasBattery = false;
  84. if (_powerCell.TryGetBatteryFromSlot(uid, out var battery))
  85. {
  86. hasBattery = true;
  87. chargePercent = battery.CurrentCharge / battery.MaxCharge;
  88. }
  89. var state = new BorgBuiState(chargePercent, hasBattery);
  90. _ui.SetUiState(uid, BorgUiKey.Key, state);
  91. }
  92. }