Fixes money being debitted multiple times (#4078)

This commit is contained in:
esguard
2021-05-30 07:19:14 +02:00
committed by GitHub
parent 5fad2123d9
commit 0ef61ba47a
3 changed files with 32 additions and 11 deletions

View File

@@ -84,20 +84,23 @@ namespace Content.Server.Cargo
/// Approves an order in the database. /// Approves an order in the database.
/// </summary> /// </summary>
/// <param name="order">The order to be approved.</param> /// <param name="order">The order to be approved.</param>
public void ApproveOrder(int orderNumber) public bool ApproveOrder(int orderNumber)
{ {
if (CurrentOrderSize == MaxOrderSize) if (CurrentOrderSize == MaxOrderSize)
return; return false;
if (!_orders.TryGetValue(orderNumber, out var order)) if (!_orders.TryGetValue(orderNumber, out var order))
return; return false;
if (order.Approved)
return false;
else if (CurrentOrderSize + order.Amount > MaxOrderSize) 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.PayingAccountId);
order.Amount = MaxOrderSize - CurrentOrderSize; order.Amount = MaxOrderSize - CurrentOrderSize;
} }
order.Approved = true; order.Approved = true;
CurrentOrderSize += order.Amount; CurrentOrderSize += order.Amount;
return true;
} }
/// <summary> /// <summary>

View File

@@ -133,9 +133,13 @@ namespace Content.Server.GameObjects.Components.Cargo
var capacity = _cargoConsoleSystem.GetCapacity(orders.Database.Id); var capacity = _cargoConsoleSystem.GetCapacity(orders.Database.Id);
if (capacity.CurrentCapacity == capacity.MaxCapacity) if (capacity.CurrentCapacity == capacity.MaxCapacity)
break; 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)) if (!_cargoConsoleSystem.ChangeBalance(_bankAccount.Id, (-product.PointCost) * order.Amount))
break; break;
_cargoConsoleSystem.ApproveOrder(orders.Database.Id, msg.OrderNumber);
UpdateUIState(); UpdateUIState();
break; break;
} }

View File

@@ -114,7 +114,25 @@ namespace Content.Server.GameObjects.EntitySystems
{ {
return _databasesDict.TryGetValue(id, out database); return _databasesDict.TryGetValue(id, out database);
} }
/// <summary>
/// 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.
/// </summary>
public bool CheckBalance(int id, int amount)
{
if (!TryGetBankAccount(id, out var account))
{
return false;
}
if (account.Balance + amount < 0)
{
return false;
}
return true;
}
/// <summary> /// <summary>
/// Attempts to change the given account's balance. /// Attempts to change the given account's balance.
/// Returns false if there's no account associated with the given ID /// Returns false if there's no account associated with the given ID
@@ -127,11 +145,6 @@ namespace Content.Server.GameObjects.EntitySystems
return false; return false;
} }
if (account.Balance + amount < 0)
{
return false;
}
account.Balance += amount; account.Balance += amount;
return true; return true;
} }
@@ -158,7 +171,8 @@ namespace Content.Server.GameObjects.EntitySystems
{ {
if (!TryGetOrderDatabase(id, out var database)) if (!TryGetOrderDatabase(id, out var database))
return false; return false;
database.ApproveOrder(orderNumber); if (!database.ApproveOrder(orderNumber))
return false;
SyncComponentsWithId(id); SyncComponentsWithId(id);
return true; return true;
} }