Refactors the CargoOrderDataManager into the CargoConsoleSystem (#2858)
* Update submodule * Refactor CargoOrderDataManager into CargoConsoleSystem * Fix OnRemove event Co-authored-by: Radrark <null>
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Cargo;
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Content.Shared.Prototypes.Cargo;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Server.Cargo;
|
||||
using Content.Server.GameObjects.Components.Cargo;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
public class CargoConsoleSystem : EntitySystem
|
||||
public class CargoConsoleSystem : EntitySystem, IResettingEntitySystem
|
||||
{
|
||||
/// <summary>
|
||||
/// How much time to wait (in seconds) before increasing bank accounts balance.
|
||||
@@ -23,6 +26,8 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
/// Stores all bank accounts.
|
||||
/// </summary>
|
||||
private readonly Dictionary<int, CargoBankAccount> _accountsDict = new();
|
||||
|
||||
private readonly Dictionary<int, CargoOrderDatabase> _databasesDict = new();
|
||||
/// <summary>
|
||||
/// Used to assign IDs to bank accounts. Incremental counter.
|
||||
/// </summary>
|
||||
@@ -36,9 +41,12 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
/// </summary>
|
||||
public CargoBankAccount StationAccount => GetBankAccount(0);
|
||||
|
||||
public CargoOrderDatabase StationOrderDatabase => GetOrderDatabase(0);
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
CreateBankAccount("Orbital Monitor IV Station", 100000);
|
||||
CreateOrderDatabase(0);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
@@ -56,6 +64,15 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_accountsDict.Clear();
|
||||
_databasesDict.Clear();
|
||||
_timer = 0;
|
||||
_accountIndex = 0;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bank account.
|
||||
/// </summary>
|
||||
@@ -66,6 +83,11 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
_accountIndex += 1;
|
||||
}
|
||||
|
||||
public void CreateOrderDatabase(int id)
|
||||
{
|
||||
_databasesDict.Add(id, new CargoOrderDatabase(id));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the bank account associated with the given ID.
|
||||
/// </summary>
|
||||
@@ -74,6 +96,11 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
return _accountsDict[id];
|
||||
}
|
||||
|
||||
public CargoOrderDatabase GetOrderDatabase(int id)
|
||||
{
|
||||
return _databasesDict[id];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the account exists, eventually passing the account in the out parameter.
|
||||
/// </summary>
|
||||
@@ -82,6 +109,11 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
return _accountsDict.TryGetValue(id, out account);
|
||||
}
|
||||
|
||||
public bool TryGetOrderDatabase(int id, out CargoOrderDatabase database)
|
||||
{
|
||||
return _databasesDict.TryGetValue(id, out database);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to change the given account's balance.
|
||||
/// Returns false if there's no account associated with the given ID
|
||||
@@ -102,5 +134,58 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
account.Balance += amount;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool AddOrder(int id, string requester, string reason, string productId, int amount, int payingAccountId)
|
||||
{
|
||||
if (amount < 1 || !TryGetOrderDatabase(id, out var database))
|
||||
return false;
|
||||
database.AddOrder(requester, reason, productId, amount, payingAccountId);
|
||||
SyncComponentsWithId(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool RemoveOrder(int id, int orderNumber)
|
||||
{
|
||||
if (!TryGetOrderDatabase(id, out var database))
|
||||
return false;
|
||||
database.RemoveOrder(orderNumber);
|
||||
SyncComponentsWithId(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ApproveOrder(int id, int orderNumber)
|
||||
{
|
||||
if (!TryGetOrderDatabase(id, out var database))
|
||||
return false;
|
||||
database.ApproveOrder(orderNumber);
|
||||
SyncComponentsWithId(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<CargoOrderData> RemoveAndGetApprovedOrders(int id)
|
||||
{
|
||||
if (!TryGetOrderDatabase(id, out var database))
|
||||
return new List<CargoOrderData>();
|
||||
var approvedOrders = database.SpliceApproved();
|
||||
SyncComponentsWithId(id);
|
||||
return approvedOrders;
|
||||
}
|
||||
|
||||
public (int CurrentCapacity, int MaxCapacity) GetCapacity(int id)
|
||||
{
|
||||
if (!TryGetOrderDatabase(id, out var database))
|
||||
return (0,0);
|
||||
return (database.CurrentOrderSize, database.MaxOrderSize);
|
||||
}
|
||||
|
||||
private void SyncComponentsWithId(int id)
|
||||
{
|
||||
foreach (var comp in ComponentManager.EntityQuery<CargoOrderDatabaseComponent>())
|
||||
{
|
||||
if (!comp.ConnectedToDatabase || comp.Database.Id != id)
|
||||
continue;
|
||||
comp.Dirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user