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:
Radrark
2021-01-09 02:11:09 -03:00
committed by GitHub
parent 79dd7a7d7e
commit 670d277963
6 changed files with 101 additions and 140 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -26,7 +26,6 @@ namespace Content.Server.GameObjects.Components.Cargo
[ComponentReference(typeof(IActivate))] [ComponentReference(typeof(IActivate))]
public class CargoConsoleComponent : SharedCargoConsoleComponent, IActivate public class CargoConsoleComponent : SharedCargoConsoleComponent, IActivate
{ {
[Dependency] private readonly ICargoOrderDataManager _cargoOrderDataManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IMapManager _mapManager = default!;
[ViewVariables] [ViewVariables]
@@ -88,7 +87,7 @@ namespace Content.Server.GameObjects.Components.Cargo
{ {
if (UserInterface != null) if (UserInterface != null)
{ {
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage; UserInterface.OnReceiveMessage -= UserInterfaceOnOnReceiveMessage;
} }
base.OnRemove(); base.OnRemove();
@@ -125,12 +124,12 @@ namespace Content.Server.GameObjects.Components.Cargo
break; 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; break;
} }
case CargoConsoleRemoveOrderMessage msg: case CargoConsoleRemoveOrderMessage msg:
{ {
_cargoOrderDataManager.RemoveOrder(orders.Database.Id, msg.OrderNumber); _cargoConsoleSystem.RemoveOrder(orders.Database.Id, msg.OrderNumber);
break; break;
} }
case CargoConsoleApproveOrderMessage msg: case CargoConsoleApproveOrderMessage msg:
@@ -145,12 +144,12 @@ namespace Content.Server.GameObjects.Components.Cargo
PrototypeManager.TryIndex(order.ProductId, out CargoProductPrototype product); PrototypeManager.TryIndex(order.ProductId, out CargoProductPrototype product);
if (product == null!) if (product == null!)
break; break;
var capacity = _cargoOrderDataManager.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.ChangeBalance(_bankAccount.Id, (-product.PointCost) * order.Amount)) if (!_cargoConsoleSystem.ChangeBalance(_bankAccount.Id, (-product.PointCost) * order.Amount))
break; break;
_cargoOrderDataManager.ApproveOrder(orders.Database.Id, msg.OrderNumber); _cargoConsoleSystem.ApproveOrder(orders.Database.Id, msg.OrderNumber);
UpdateUIState(); UpdateUIState();
break; break;
} }
@@ -186,7 +185,7 @@ namespace Content.Server.GameObjects.Components.Cargo
{ {
if (cargoTelepad.TryGetComponent<CargoTelepadComponent>(out var telepadComponent)) if (cargoTelepad.TryGetComponent<CargoTelepadComponent>(out var telepadComponent))
{ {
var approvedOrders = _cargoOrderDataManager.RemoveAndGetApprovedFrom(orders.Database); var approvedOrders = _cargoConsoleSystem.RemoveAndGetApprovedOrders(orders.Database.Id);
orders.Database.ClearOrderCapacity(); orders.Database.ClearOrderCapacity();
foreach (var order in approvedOrders) foreach (var order in approvedOrders)
{ {
@@ -226,7 +225,7 @@ namespace Content.Server.GameObjects.Components.Cargo
var id = _bankAccount.Id; var id = _bankAccount.Id;
var name = _bankAccount.Name; var name = _bankAccount.Name;
var balance = _bankAccount.Balance; var balance = _bankAccount.Balance;
var capacity = _cargoOrderDataManager.GetCapacity(id); var capacity = _cargoConsoleSystem.GetCapacity(id);
UserInterface?.SetState(new CargoConsoleInterfaceState(_requestOnly, id, name, balance, capacity)); UserInterface?.SetState(new CargoConsoleInterfaceState(_requestOnly, id, name, balance, capacity));
} }
} }

View File

@@ -1,6 +1,8 @@
using Content.Server.Cargo; using Content.Server.Cargo;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Cargo; using Content.Shared.GameObjects.Components.Cargo;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.IoC; using Robust.Shared.IoC;
namespace Content.Server.GameObjects.Components.Cargo namespace Content.Server.GameObjects.Components.Cargo
@@ -8,8 +10,6 @@ namespace Content.Server.GameObjects.Components.Cargo
[RegisterComponent] [RegisterComponent]
public class CargoOrderDatabaseComponent : SharedCargoOrderDatabaseComponent public class CargoOrderDatabaseComponent : SharedCargoOrderDatabaseComponent
{ {
[Dependency] private readonly ICargoOrderDataManager _cargoOrderDataManager = default!;
public CargoOrderDatabase Database { get; set; } public CargoOrderDatabase Database { get; set; }
public bool ConnectedToDatabase => Database != null; public bool ConnectedToDatabase => Database != null;
@@ -17,7 +17,7 @@ namespace Content.Server.GameObjects.Components.Cargo
{ {
base.Initialize(); base.Initialize();
_cargoOrderDataManager.AddComponent(this); Database = EntitySystem.Get<CargoConsoleSystem>().StationOrderDatabase;
} }
public override ComponentState GetComponentState() public override ComponentState GetComponentState()

View File

@@ -1,10 +1,13 @@
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.Cargo;
using Robust.Shared.GameObjects.Systems; 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 namespace Content.Server.GameObjects.EntitySystems
{ {
public class CargoConsoleSystem : EntitySystem public class CargoConsoleSystem : EntitySystem, IResettingEntitySystem
{ {
/// <summary> /// <summary>
/// How much time to wait (in seconds) before increasing bank accounts balance. /// 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. /// Stores all bank accounts.
/// </summary> /// </summary>
private readonly Dictionary<int, CargoBankAccount> _accountsDict = new(); private readonly Dictionary<int, CargoBankAccount> _accountsDict = new();
private readonly Dictionary<int, CargoOrderDatabase> _databasesDict = new();
/// <summary> /// <summary>
/// Used to assign IDs to bank accounts. Incremental counter. /// Used to assign IDs to bank accounts. Incremental counter.
/// </summary> /// </summary>
@@ -36,9 +41,12 @@ namespace Content.Server.GameObjects.EntitySystems
/// </summary> /// </summary>
public CargoBankAccount StationAccount => GetBankAccount(0); public CargoBankAccount StationAccount => GetBankAccount(0);
public CargoOrderDatabase StationOrderDatabase => GetOrderDatabase(0);
public override void Initialize() public override void Initialize()
{ {
CreateBankAccount("Orbital Monitor IV Station", 100000); CreateBankAccount("Orbital Monitor IV Station", 100000);
CreateOrderDatabase(0);
} }
public override void Update(float frameTime) 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> /// <summary>
/// Creates a new bank account. /// Creates a new bank account.
/// </summary> /// </summary>
@@ -66,6 +83,11 @@ namespace Content.Server.GameObjects.EntitySystems
_accountIndex += 1; _accountIndex += 1;
} }
public void CreateOrderDatabase(int id)
{
_databasesDict.Add(id, new CargoOrderDatabase(id));
}
/// <summary> /// <summary>
/// Returns the bank account associated with the given ID. /// Returns the bank account associated with the given ID.
/// </summary> /// </summary>
@@ -74,6 +96,11 @@ namespace Content.Server.GameObjects.EntitySystems
return _accountsDict[id]; return _accountsDict[id];
} }
public CargoOrderDatabase GetOrderDatabase(int id)
{
return _databasesDict[id];
}
/// <summary> /// <summary>
/// Returns whether the account exists, eventually passing the account in the out parameter. /// Returns whether the account exists, eventually passing the account in the out parameter.
/// </summary> /// </summary>
@@ -82,6 +109,11 @@ namespace Content.Server.GameObjects.EntitySystems
return _accountsDict.TryGetValue(id, out account); return _accountsDict.TryGetValue(id, out account);
} }
public bool TryGetOrderDatabase(int id, out CargoOrderDatabase database)
{
return _databasesDict.TryGetValue(id, out database);
}
/// <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
@@ -102,5 +134,58 @@ namespace Content.Server.GameObjects.EntitySystems
account.Balance += amount; account.Balance += amount;
return true; 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();
}
}
} }
} }

View File

@@ -1,4 +1,4 @@
using Content.Server.Administration; using Content.Server.Administration;
using Content.Server.AI.Utility.Considerations; using Content.Server.AI.Utility.Considerations;
using Content.Server.AI.WorldState; using Content.Server.AI.WorldState;
using Content.Server.Cargo; using Content.Server.Cargo;
@@ -38,7 +38,6 @@ namespace Content.Server
IoCManager.Register<IChatManager, ChatManager>(); IoCManager.Register<IChatManager, ChatManager>();
IoCManager.Register<IMoMMILink, MoMMILink>(); IoCManager.Register<IMoMMILink, MoMMILink>();
IoCManager.Register<ISandboxManager, SandboxManager>(); IoCManager.Register<ISandboxManager, SandboxManager>();
IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>();
IoCManager.Register<IModuleManager, ServerModuleManager>(); IoCManager.Register<IModuleManager, ServerModuleManager>();
IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>(); IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>();
IoCManager.Register<IServerDbManager, ServerDbManager>(); IoCManager.Register<IServerDbManager, ServerDbManager>();