2021-03-16 15:50:20 +01:00
|
|
|
using System.Collections.Generic;
|
2022-01-09 23:47:01 +11:00
|
|
|
using System.Linq;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Coordinates.Helpers;
|
|
|
|
|
using Content.Server.Power.Components;
|
|
|
|
|
using Content.Server.UserInterface;
|
|
|
|
|
using Content.Shared.Cargo;
|
|
|
|
|
using Content.Shared.Cargo.Components;
|
2021-07-10 17:35:33 +02:00
|
|
|
using Content.Shared.Sound;
|
2021-03-16 15:50:20 +01:00
|
|
|
using Robust.Server.GameObjects;
|
2021-05-31 23:45:28 -05:00
|
|
|
using Robust.Shared.Audio;
|
2019-11-21 16:37:15 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Map;
|
2021-03-16 15:50:20 +01:00
|
|
|
using Robust.Shared.Maths;
|
2021-05-31 23:45:28 -05:00
|
|
|
using Robust.Shared.Player;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-03-16 15:50:20 +01:00
|
|
|
using Robust.Shared.ViewVariables;
|
2019-11-21 16:37:15 -08:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Cargo.Components
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class CargoConsoleComponent : SharedCargoConsoleComponent
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
2020-12-18 20:14:26 -06:00
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2021-12-08 17:04:21 +01:00
|
|
|
[Dependency] private readonly IEntityManager _entMan = default!;
|
2019-11-21 16:37:15 -08:00
|
|
|
|
2020-07-08 01:41:20 +02:00
|
|
|
private CargoBankAccount? _bankAccount;
|
2020-06-12 18:31:57 +02:00
|
|
|
|
2019-11-21 16:37:15 -08:00
|
|
|
[ViewVariables]
|
2020-07-08 01:41:20 +02:00
|
|
|
public CargoBankAccount? BankAccount
|
2020-06-12 18:31:57 +02:00
|
|
|
{
|
|
|
|
|
get => _bankAccount;
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
if (_bankAccount == value)
|
2020-07-08 01:41:20 +02:00
|
|
|
{
|
2020-06-12 18:31:57 +02:00
|
|
|
return;
|
2020-07-08 01:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-12 18:31:57 +02:00
|
|
|
if (_bankAccount != null)
|
|
|
|
|
{
|
2020-07-02 07:45:40 -05:00
|
|
|
_bankAccount.OnBalanceChange -= UpdateUIState;
|
2020-06-12 18:31:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_bankAccount = value;
|
2020-07-08 01:41:20 +02:00
|
|
|
|
2020-06-12 18:31:57 +02:00
|
|
|
if (value != null)
|
|
|
|
|
{
|
2020-07-08 01:41:20 +02:00
|
|
|
value.OnBalanceChange += UpdateUIState;
|
2020-06-12 18:31:57 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-02 07:45:40 -05:00
|
|
|
UpdateUIState();
|
2020-06-12 18:31:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("requestOnly")]
|
2019-11-21 16:37:15 -08:00
|
|
|
private bool _requestOnly = false;
|
|
|
|
|
|
2021-07-10 17:35:33 +02:00
|
|
|
[DataField("errorSound")]
|
|
|
|
|
private SoundSpecifier _errorSound = new SoundPathSpecifier("/Audio/Effects/error.ogg");
|
|
|
|
|
|
2021-12-08 17:04:21 +01:00
|
|
|
private bool Powered => !_entMan.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
|
2022-02-15 15:01:45 +11:00
|
|
|
private CargoSystem _cargoConsoleSystem = default!;
|
2020-03-22 19:34:38 -04:00
|
|
|
|
2020-08-24 20:47:17 +02:00
|
|
|
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(CargoConsoleUiKey.Key);
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2020-12-04 13:26:54 +01:00
|
|
|
Owner.EnsureComponentWarn(out GalacticMarketComponent _);
|
|
|
|
|
Owner.EnsureComponentWarn(out CargoOrderDatabaseComponent _);
|
2020-08-22 22:29:20 +02:00
|
|
|
|
|
|
|
|
if (UserInterface != null)
|
|
|
|
|
{
|
|
|
|
|
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 15:01:45 +11:00
|
|
|
_cargoConsoleSystem = EntitySystem.Get<CargoSystem>();
|
2020-06-12 18:31:57 +02:00
|
|
|
BankAccount = _cargoConsoleSystem.StationAccount;
|
2019-11-21 16:37:15 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void OnRemove()
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
|
|
|
|
if (UserInterface != null)
|
|
|
|
|
{
|
2021-01-09 02:11:09 -03:00
|
|
|
UserInterface.OnReceiveMessage -= UserInterfaceOnOnReceiveMessage;
|
2020-08-22 22:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.OnRemove();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 16:37:15 -08:00
|
|
|
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
|
|
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
if (!_entMan.TryGetComponent(Owner, out CargoOrderDatabaseComponent? orders))
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 16:37:15 -08:00
|
|
|
var message = serverMsg.Message;
|
2021-03-16 15:50:20 +01:00
|
|
|
if (orders.Database == null)
|
2019-11-21 16:37:15 -08:00
|
|
|
return;
|
2020-03-22 19:34:38 -04:00
|
|
|
if (!Powered)
|
|
|
|
|
return;
|
2019-11-21 16:37:15 -08:00
|
|
|
switch (message)
|
|
|
|
|
{
|
|
|
|
|
case CargoConsoleAddOrderMessage msg:
|
2021-03-16 15:50:20 +01:00
|
|
|
{
|
|
|
|
|
if (msg.Amount <= 0 || _bankAccount == null)
|
2020-07-08 01:41:20 +02:00
|
|
|
{
|
2019-11-21 16:37:15 -08:00
|
|
|
break;
|
2020-07-08 01:41:20 +02:00
|
|
|
}
|
2021-03-16 15:50:20 +01:00
|
|
|
|
2021-05-31 23:45:28 -05:00
|
|
|
if (!_cargoConsoleSystem.AddOrder(orders.Database.Id, msg.Requester, msg.Reason, msg.ProductId,
|
2021-07-31 19:52:33 +02:00
|
|
|
msg.Amount, _bankAccount.Id))
|
2021-05-31 23:45:28 -05:00
|
|
|
{
|
2021-12-14 10:22:16 +13:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), _errorSound.GetSound(), Owner, AudioParams.Default);
|
2021-05-31 23:45:28 -05:00
|
|
|
}
|
2021-03-16 15:50:20 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2019-11-21 16:37:15 -08:00
|
|
|
case CargoConsoleRemoveOrderMessage msg:
|
2021-03-16 15:50:20 +01:00
|
|
|
{
|
|
|
|
|
_cargoConsoleSystem.RemoveOrder(orders.Database.Id, msg.OrderNumber);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case CargoConsoleApproveOrderMessage msg:
|
|
|
|
|
{
|
|
|
|
|
if (_requestOnly ||
|
|
|
|
|
!orders.Database.TryGetOrder(msg.OrderNumber, out var order) ||
|
|
|
|
|
_bankAccount == null)
|
2020-07-08 01:41:20 +02:00
|
|
|
{
|
2019-11-21 16:37:15 -08:00
|
|
|
break;
|
2020-07-08 01:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
if (msg.Session.AttachedEntity is not {Valid: true} player)
|
2021-11-11 02:15:23 +13:00
|
|
|
break;
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
PrototypeManager.TryIndex(order.ProductId, out CargoProductPrototype? product);
|
|
|
|
|
if (product == null!)
|
2019-11-21 16:37:15 -08:00
|
|
|
break;
|
2021-03-16 15:50:20 +01:00
|
|
|
var capacity = _cargoConsoleSystem.GetCapacity(orders.Database.Id);
|
2021-05-31 23:45:28 -05:00
|
|
|
if (
|
2021-07-10 17:35:33 +02:00
|
|
|
(capacity.CurrentCapacity == capacity.MaxCapacity
|
2021-05-31 23:45:28 -05:00
|
|
|
|| capacity.CurrentCapacity + order.Amount > capacity.MaxCapacity
|
|
|
|
|
|| !_cargoConsoleSystem.CheckBalance(_bankAccount.Id, (-product.PointCost) * order.Amount)
|
2021-12-05 18:09:01 +01:00
|
|
|
|| !_cargoConsoleSystem.ApproveOrder(Owner, player, orders.Database.Id, msg.OrderNumber)
|
2021-07-10 17:35:33 +02:00
|
|
|
|| !_cargoConsoleSystem.ChangeBalance(_bankAccount.Id, (-product.PointCost) * order.Amount))
|
2021-05-31 23:45:28 -05:00
|
|
|
)
|
|
|
|
|
{
|
2021-12-14 10:22:16 +13:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), _errorSound.GetSound(), Owner, AudioParams.Default);
|
2021-03-16 15:50:20 +01:00
|
|
|
break;
|
2021-05-31 23:45:28 -05:00
|
|
|
}
|
2021-11-11 02:15:23 +13:00
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
UpdateUIState();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-11-21 16:37:15 -08:00
|
|
|
case CargoConsoleShuttleMessage _:
|
2021-03-16 15:50:20 +01:00
|
|
|
{
|
2022-01-09 23:47:01 +11:00
|
|
|
// Jesus fucking christ Glass
|
2021-03-16 15:50:20 +01:00
|
|
|
//var approvedOrders = _cargoOrderDataManager.RemoveAndGetApprovedFrom(orders.Database);
|
|
|
|
|
//orders.Database.ClearOrderCapacity();
|
|
|
|
|
|
|
|
|
|
// TODO replace with shuttle code
|
|
|
|
|
// TEMPORARY loop for spawning stuff on telepad (looks for a telepad adjacent to the console)
|
2021-12-05 18:09:01 +01:00
|
|
|
EntityUid? cargoTelepad = null;
|
2021-12-08 17:04:21 +01:00
|
|
|
var indices = _entMan.GetComponent<TransformComponent>(Owner).Coordinates.ToVector2i(_entMan, _mapManager);
|
2021-03-16 15:50:20 +01:00
|
|
|
var offsets = new Vector2i[] { new Vector2i(0, 1), new Vector2i(1, 1), new Vector2i(1, 0), new Vector2i(1, -1),
|
|
|
|
|
new Vector2i(0, -1), new Vector2i(-1, -1), new Vector2i(-1, 0), new Vector2i(-1, 1), };
|
2020-12-18 20:14:26 -06:00
|
|
|
|
2022-03-03 21:18:35 +11:00
|
|
|
var lookup = EntitySystem.Get<EntityLookupSystem>();
|
2022-01-09 23:47:01 +11:00
|
|
|
var gridId = _entMan.GetComponent<TransformComponent>(Owner).GridID;
|
|
|
|
|
|
|
|
|
|
// TODO: Should use anchoring.
|
|
|
|
|
foreach (var entity in lookup.GetEntitiesIntersecting(gridId, offsets.Select(o => o + indices)))
|
2021-03-16 15:50:20 +01:00
|
|
|
{
|
2022-01-09 23:47:01 +11:00
|
|
|
if (_entMan.HasComponent<CargoTelepadComponent>(entity) && _entMan.TryGetComponent<ApcPowerReceiverComponent?>(entity, out var powerReceiver) && powerReceiver.Powered)
|
2020-12-18 20:14:26 -06:00
|
|
|
{
|
2022-01-09 23:47:01 +11:00
|
|
|
cargoTelepad = entity;
|
|
|
|
|
break;
|
2020-12-18 20:14:26 -06:00
|
|
|
}
|
2021-03-16 15:50:20 +01:00
|
|
|
}
|
2022-01-09 23:47:01 +11:00
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
if (cargoTelepad != null)
|
|
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
if (_entMan.TryGetComponent<CargoTelepadComponent?>(cargoTelepad.Value, out var telepadComponent))
|
2020-12-18 20:14:26 -06:00
|
|
|
{
|
2021-03-16 15:50:20 +01:00
|
|
|
var approvedOrders = _cargoConsoleSystem.RemoveAndGetApprovedOrders(orders.Database.Id);
|
|
|
|
|
orders.Database.ClearOrderCapacity();
|
|
|
|
|
foreach (var order in approvedOrders)
|
2020-12-18 20:14:26 -06:00
|
|
|
{
|
2022-02-15 15:01:45 +11:00
|
|
|
_cargoConsoleSystem.QueueTeleport(telepadComponent, order);
|
2020-12-18 20:14:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-21 16:37:15 -08:00
|
|
|
}
|
2021-03-16 15:50:20 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2019-11-21 16:37:15 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-02 07:45:40 -05:00
|
|
|
private void UpdateUIState()
|
2019-11-21 16:37:15 -08:00
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
if (_bankAccount == null || !_entMan.EntityExists(Owner))
|
2020-07-02 07:45:40 -05:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var id = _bankAccount.Id;
|
|
|
|
|
var name = _bankAccount.Name;
|
|
|
|
|
var balance = _bankAccount.Balance;
|
2021-01-09 02:11:09 -03:00
|
|
|
var capacity = _cargoConsoleSystem.GetCapacity(id);
|
2020-08-22 22:29:20 +02:00
|
|
|
UserInterface?.SetState(new CargoConsoleInterfaceState(_requestOnly, id, name, balance, capacity));
|
2019-11-21 16:37:15 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|