diff --git a/Content.Server/Cargo/CargoOrderDataManager.cs b/Content.Server/Cargo/CargoOrderDataManager.cs deleted file mode 100644 index 8665b597ed..0000000000 --- a/Content.Server/Cargo/CargoOrderDataManager.cs +++ /dev/null @@ -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 _accounts = new(); - private readonly List _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; - } - - /// - /// Adds an order to the database. - /// - /// The person who requested the item. - /// The reason the product was requested. - /// The ID of the product requested. - /// The amount of the products requested. - /// The ID of the bank account paying for the order. - /// Whether the order will be bought when the orders are processed. - 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 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 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); - } - } -} diff --git a/Content.Server/Cargo/ICargoOrderDataManager.cs b/Content.Server/Cargo/ICargoOrderDataManager.cs deleted file mode 100644 index 118a4e1f9d..0000000000 --- a/Content.Server/Cargo/ICargoOrderDataManager.cs +++ /dev/null @@ -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 GetOrdersFromAccount(int accountId); - List RemoveAndGetApprovedFrom(CargoOrderDatabase database); - (int CurrentCapacity, int MaxCapacity) GetCapacity(int id); - } -} diff --git a/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs b/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs index b48ebd83bb..db36c2a884 100644 --- a/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs +++ b/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs @@ -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>(); //Probably better than IEnumerable.concat + var adjacentEntities = new List>(); //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(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)); } } diff --git a/Content.Server/GameObjects/Components/Cargo/CargoOrderDatabaseComponent.cs b/Content.Server/GameObjects/Components/Cargo/CargoOrderDatabaseComponent.cs index 09fff675c2..54c5c7e091 100644 --- a/Content.Server/GameObjects/Components/Cargo/CargoOrderDatabaseComponent.cs +++ b/Content.Server/GameObjects/Components/Cargo/CargoOrderDatabaseComponent.cs @@ -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().StationOrderDatabase; } public override ComponentState GetComponentState() diff --git a/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs b/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs index a12412a57d..ad6ffad73c 100644 --- a/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs @@ -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 { /// /// 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. /// private readonly Dictionary _accountsDict = new(); + + private readonly Dictionary _databasesDict = new(); /// /// Used to assign IDs to bank accounts. Incremental counter. /// @@ -36,9 +41,12 @@ namespace Content.Server.GameObjects.EntitySystems /// 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(); + } + /// /// Creates a new bank account. /// @@ -66,6 +83,11 @@ namespace Content.Server.GameObjects.EntitySystems _accountIndex += 1; } + public void CreateOrderDatabase(int id) + { + _databasesDict.Add(id, new CargoOrderDatabase(id)); + } + /// /// Returns the bank account associated with the given ID. /// @@ -74,6 +96,11 @@ namespace Content.Server.GameObjects.EntitySystems return _accountsDict[id]; } + public CargoOrderDatabase GetOrderDatabase(int id) + { + return _databasesDict[id]; + } + /// /// Returns whether the account exists, eventually passing the account in the out parameter. /// @@ -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); + } + /// /// 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 RemoveAndGetApprovedOrders(int id) + { + if (!TryGetOrderDatabase(id, out var database)) + return new List(); + 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()) + { + if (!comp.ConnectedToDatabase || comp.Database.Id != id) + continue; + comp.Dirty(); + } + } } } diff --git a/Content.Server/ServerContentIoC.cs b/Content.Server/ServerContentIoC.cs index 09a1f2d17c..89469b58ce 100644 --- a/Content.Server/ServerContentIoC.cs +++ b/Content.Server/ServerContentIoC.cs @@ -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(); IoCManager.Register(); IoCManager.Register(); - IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); IoCManager.Register();