diff --git a/Content.Server/Cargo/CargoOrderDatabase.cs b/Content.Server/Cargo/CargoOrderDatabase.cs index 2e32641a48..3afee6bc7c 100644 --- a/Content.Server/Cargo/CargoOrderDatabase.cs +++ b/Content.Server/Cargo/CargoOrderDatabase.cs @@ -84,20 +84,23 @@ namespace Content.Server.Cargo /// Approves an order in the database. /// /// The order to be approved. - public void ApproveOrder(int orderNumber) + public bool ApproveOrder(int orderNumber) { if (CurrentOrderSize == MaxOrderSize) - return; + return false; if (!_orders.TryGetValue(orderNumber, out var order)) - return; + return false; + if (order.Approved) + return false; else if (CurrentOrderSize + order.Amount > MaxOrderSize) { - AddOrder(order.Requester, Loc.GetString("{0} (Overflow)", order.Reason.Replace(" (Overflow)","")), order.ProductId, + AddOrder(order.Requester, Loc.GetString("{0} (Overflow)", order.Reason.Replace(" (Overflow)", "")), order.ProductId, order.Amount - MaxOrderSize - CurrentOrderSize, order.PayingAccountId); order.Amount = MaxOrderSize - CurrentOrderSize; } order.Approved = true; CurrentOrderSize += order.Amount; + return true; } /// diff --git a/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs b/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs index 9f8999f492..999bb293c9 100644 --- a/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs +++ b/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs @@ -133,9 +133,13 @@ namespace Content.Server.GameObjects.Components.Cargo var capacity = _cargoConsoleSystem.GetCapacity(orders.Database.Id); if (capacity.CurrentCapacity == capacity.MaxCapacity) break; + if (!_cargoConsoleSystem.CheckBalance(_bankAccount.Id, (-product.PointCost) * order.Amount)) + break; + if (!_cargoConsoleSystem.ApproveOrder(orders.Database.Id, msg.OrderNumber)) + break; if (!_cargoConsoleSystem.ChangeBalance(_bankAccount.Id, (-product.PointCost) * order.Amount)) break; - _cargoConsoleSystem.ApproveOrder(orders.Database.Id, msg.OrderNumber); + UpdateUIState(); break; } diff --git a/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs b/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs index 005f894dda..eb2f5db375 100644 --- a/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs @@ -114,7 +114,25 @@ namespace Content.Server.GameObjects.EntitySystems { return _databasesDict.TryGetValue(id, out database); } + /// + /// Verifies if there is enough money in the account's balance to pay the amount. + /// Returns false if there's no account associated with the given ID + /// or if the balance would end up being negative. + /// + public bool CheckBalance(int id, int amount) + { + if (!TryGetBankAccount(id, out var account)) + { + return false; + } + if (account.Balance + amount < 0) + { + return false; + } + + return true; + } /// /// Attempts to change the given account's balance. /// Returns false if there's no account associated with the given ID @@ -127,11 +145,6 @@ namespace Content.Server.GameObjects.EntitySystems return false; } - if (account.Balance + amount < 0) - { - return false; - } - account.Balance += amount; return true; } @@ -158,7 +171,8 @@ namespace Content.Server.GameObjects.EntitySystems { if (!TryGetOrderDatabase(id, out var database)) return false; - database.ApproveOrder(orderNumber); + if (!database.ApproveOrder(orderNumber)) + return false; SyncComponentsWithId(id); return true; }