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,104 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.Components.Cargo;
|
||||
using Content.Shared.Prototypes.Cargo;
|
||||
|
||||
namespace Content.Server.Cargo
|
||||
{
|
||||
public class CargoOrderDataManager : ICargoOrderDataManager
|
||||
{
|
||||
private readonly Dictionary<int, CargoOrderDatabase> _accounts = new();
|
||||
private readonly List<CargoOrderDatabaseComponent> _components = new();
|
||||
|
||||
public CargoOrderDataManager()
|
||||
{
|
||||
CreateAccount(0);
|
||||
}
|
||||
|
||||
public void CreateAccount(int id)
|
||||
{
|
||||
_accounts.Add(id, new CargoOrderDatabase(id));
|
||||
}
|
||||
|
||||
public bool TryGetAccount(int id, out CargoOrderDatabase account)
|
||||
{
|
||||
if (_accounts.TryGetValue(id, out var _account))
|
||||
{
|
||||
account = _account;
|
||||
return true;
|
||||
}
|
||||
account = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an order to the database.
|
||||
/// </summary>
|
||||
/// <param name="requester">The person who requested the item.</param>
|
||||
/// <param name="reason">The reason the product was requested.</param>
|
||||
/// <param name="productId">The ID of the product requested.</param>
|
||||
/// <param name="amount">The amount of the products requested.</param>
|
||||
/// <param name="payingAccountId">The ID of the bank account paying for the order.</param>
|
||||
/// <param name="approved">Whether the order will be bought when the orders are processed.</param>
|
||||
public virtual void AddOrder(int id, string requester, string reason, string productId, int amount, int payingAccountId)
|
||||
{
|
||||
if (amount < 1 || !TryGetAccount(id, out var account))
|
||||
return;
|
||||
account.AddOrder(requester, reason, productId, amount, payingAccountId);
|
||||
SyncComponentsWithId(id);
|
||||
}
|
||||
|
||||
public void RemoveOrder(int id, int orderNumber)
|
||||
{
|
||||
if (!TryGetAccount(id, out var account))
|
||||
return;
|
||||
account.RemoveOrder(orderNumber);
|
||||
SyncComponentsWithId(id);
|
||||
}
|
||||
|
||||
public void ApproveOrder(int id, int orderNumber)
|
||||
{
|
||||
if (!TryGetAccount(id, out var account))
|
||||
return;
|
||||
account.ApproveOrder(orderNumber);
|
||||
SyncComponentsWithId(id);
|
||||
}
|
||||
|
||||
private void SyncComponentsWithId(int id)
|
||||
{
|
||||
foreach (var component in _components)
|
||||
{
|
||||
if (!component.ConnectedToDatabase || component.Database.Id != id)
|
||||
continue;
|
||||
component.Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
public List<CargoOrderData> RemoveAndGetApprovedFrom(CargoOrderDatabase database)
|
||||
{
|
||||
var approvedOrders = database.SpliceApproved();
|
||||
SyncComponentsWithId(database.Id);
|
||||
return approvedOrders;
|
||||
}
|
||||
|
||||
public void AddComponent(CargoOrderDatabaseComponent component)
|
||||
{
|
||||
if (_components.Contains(component))
|
||||
return;
|
||||
_components.Add(component);
|
||||
component.Database = _accounts[0];
|
||||
}
|
||||
|
||||
public List<CargoOrderData> GetOrdersFromAccount(int accountId)
|
||||
{
|
||||
if (!TryGetAccount(accountId, out var account))
|
||||
return null;
|
||||
return account.GetOrders();
|
||||
}
|
||||
|
||||
public (int CurrentCapacity, int MaxCapacity) GetCapacity(int id)
|
||||
{
|
||||
TryGetAccount(id, out var account);
|
||||
return (account.CurrentOrderSize, account.MaxOrderSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.Components.Cargo;
|
||||
using Content.Shared.Prototypes.Cargo;
|
||||
|
||||
namespace Content.Server.Cargo
|
||||
{
|
||||
public interface ICargoOrderDataManager
|
||||
{
|
||||
bool TryGetAccount(int id, out CargoOrderDatabase account);
|
||||
void AddOrder(int id, string requester, string reason, string productId, int amount, int payingAccountId);
|
||||
void RemoveOrder(int id, int orderNumber);
|
||||
void ApproveOrder(int id, int orderNumber);
|
||||
void AddComponent(CargoOrderDatabaseComponent component);
|
||||
List<CargoOrderData> GetOrdersFromAccount(int accountId);
|
||||
List<CargoOrderData> RemoveAndGetApprovedFrom(CargoOrderDatabase database);
|
||||
(int CurrentCapacity, int MaxCapacity) GetCapacity(int id);
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,6 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class CargoConsoleComponent : SharedCargoConsoleComponent, IActivate
|
||||
{
|
||||
[Dependency] private readonly ICargoOrderDataManager _cargoOrderDataManager = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
|
||||
[ViewVariables]
|
||||
@@ -88,7 +87,7 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
{
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
||||
UserInterface.OnReceiveMessage -= UserInterfaceOnOnReceiveMessage;
|
||||
}
|
||||
|
||||
base.OnRemove();
|
||||
@@ -125,12 +124,12 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
break;
|
||||
}
|
||||
|
||||
_cargoOrderDataManager.AddOrder(orders.Database.Id, msg.Requester, msg.Reason, msg.ProductId, msg.Amount, _bankAccount.Id);
|
||||
_cargoConsoleSystem.AddOrder(orders.Database.Id, msg.Requester, msg.Reason, msg.ProductId, msg.Amount, _bankAccount.Id);
|
||||
break;
|
||||
}
|
||||
case CargoConsoleRemoveOrderMessage msg:
|
||||
{
|
||||
_cargoOrderDataManager.RemoveOrder(orders.Database.Id, msg.OrderNumber);
|
||||
_cargoConsoleSystem.RemoveOrder(orders.Database.Id, msg.OrderNumber);
|
||||
break;
|
||||
}
|
||||
case CargoConsoleApproveOrderMessage msg:
|
||||
@@ -145,12 +144,12 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
PrototypeManager.TryIndex(order.ProductId, out CargoProductPrototype product);
|
||||
if (product == null!)
|
||||
break;
|
||||
var capacity = _cargoOrderDataManager.GetCapacity(orders.Database.Id);
|
||||
var capacity = _cargoConsoleSystem.GetCapacity(orders.Database.Id);
|
||||
if (capacity.CurrentCapacity == capacity.MaxCapacity)
|
||||
break;
|
||||
if (!_cargoConsoleSystem.ChangeBalance(_bankAccount.Id, (-product.PointCost) * order.Amount))
|
||||
break;
|
||||
_cargoOrderDataManager.ApproveOrder(orders.Database.Id, msg.OrderNumber);
|
||||
_cargoConsoleSystem.ApproveOrder(orders.Database.Id, msg.OrderNumber);
|
||||
UpdateUIState();
|
||||
break;
|
||||
}
|
||||
@@ -165,7 +164,7 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
var indices = Owner.Transform.Coordinates.ToVector2i(Owner.EntityManager, _mapManager);
|
||||
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), };
|
||||
var adjacentEntities = new List<IEnumerable<IEntity>>(); //Probably better than IEnumerable.concat
|
||||
var adjacentEntities = new List<IEnumerable<IEntity>>(); //Probably better than IEnumerable.concat
|
||||
foreach (var offset in offsets)
|
||||
{
|
||||
adjacentEntities.Add((indices+offset).GetEntitiesInTileFast(Owner.Transform.GridID));
|
||||
@@ -186,7 +185,7 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
{
|
||||
if (cargoTelepad.TryGetComponent<CargoTelepadComponent>(out var telepadComponent))
|
||||
{
|
||||
var approvedOrders = _cargoOrderDataManager.RemoveAndGetApprovedFrom(orders.Database);
|
||||
var approvedOrders = _cargoConsoleSystem.RemoveAndGetApprovedOrders(orders.Database.Id);
|
||||
orders.Database.ClearOrderCapacity();
|
||||
foreach (var order in approvedOrders)
|
||||
{
|
||||
@@ -226,7 +225,7 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
var id = _bankAccount.Id;
|
||||
var name = _bankAccount.Name;
|
||||
var balance = _bankAccount.Balance;
|
||||
var capacity = _cargoOrderDataManager.GetCapacity(id);
|
||||
var capacity = _cargoConsoleSystem.GetCapacity(id);
|
||||
UserInterface?.SetState(new CargoConsoleInterfaceState(_requestOnly, id, name, balance, capacity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Content.Server.Cargo;
|
||||
using Content.Server.Cargo;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Shared.GameObjects.Components.Cargo;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Cargo
|
||||
@@ -8,8 +10,6 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
[RegisterComponent]
|
||||
public class CargoOrderDatabaseComponent : SharedCargoOrderDatabaseComponent
|
||||
{
|
||||
[Dependency] private readonly ICargoOrderDataManager _cargoOrderDataManager = default!;
|
||||
|
||||
public CargoOrderDatabase Database { get; set; }
|
||||
public bool ConnectedToDatabase => Database != null;
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Content.Server.GameObjects.Components.Cargo
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_cargoOrderDataManager.AddComponent(this);
|
||||
Database = EntitySystem.Get<CargoConsoleSystem>().StationOrderDatabase;
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState()
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Content.Server.Administration;
|
||||
using Content.Server.Administration;
|
||||
using Content.Server.AI.Utility.Considerations;
|
||||
using Content.Server.AI.WorldState;
|
||||
using Content.Server.Cargo;
|
||||
@@ -38,7 +38,6 @@ namespace Content.Server
|
||||
IoCManager.Register<IChatManager, ChatManager>();
|
||||
IoCManager.Register<IMoMMILink, MoMMILink>();
|
||||
IoCManager.Register<ISandboxManager, SandboxManager>();
|
||||
IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>();
|
||||
IoCManager.Register<IModuleManager, ServerModuleManager>();
|
||||
IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>();
|
||||
IoCManager.Register<IServerDbManager, ServerDbManager>();
|
||||
|
||||
Reference in New Issue
Block a user