From 7817681894de8c01280bc22241e5dd83751803bd Mon Sep 17 00:00:00 2001 From: DamianX Date: Fri, 12 Jun 2020 18:31:57 +0200 Subject: [PATCH] Refactored GalacticBankManager (#1089) --- Content.Server/Cargo/CargoBankAccount.cs | 15 ++- Content.Server/Cargo/GalacticBankManager.cs | 109 ------------------ Content.Server/Cargo/ICargoBankAccount.cs | 1 + Content.Server/Cargo/IGalacticBankManager.cs | 20 ---- Content.Server/EntryPoint.cs | 5 +- .../Components/Cargo/CargoConsoleComponent.cs | 52 ++++++--- .../EntitySystems/CargoConsoleSystem.cs | 106 +++++++++++++++++ Content.Server/ServerContentIoC.cs | 5 +- 8 files changed, 161 insertions(+), 152 deletions(-) delete mode 100644 Content.Server/Cargo/GalacticBankManager.cs delete mode 100644 Content.Server/Cargo/IGalacticBankManager.cs create mode 100644 Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs diff --git a/Content.Server/Cargo/CargoBankAccount.cs b/Content.Server/Cargo/CargoBankAccount.cs index a7244f4ed7..477d148791 100644 --- a/Content.Server/Cargo/CargoBankAccount.cs +++ b/Content.Server/Cargo/CargoBankAccount.cs @@ -8,7 +8,20 @@ namespace Content.Server.Cargo public string Name { get; } - public int Balance { get; set; } + private int _balance; + public int Balance + { + get => _balance; + set + { + if (_balance == value) + return; + _balance = value; + OnBalanceChange?.Invoke(); + } + } + + public event Action OnBalanceChange; public CargoBankAccount(int id, string name, int balance) { diff --git a/Content.Server/Cargo/GalacticBankManager.cs b/Content.Server/Cargo/GalacticBankManager.cs deleted file mode 100644 index 1fbcb81ddb..0000000000 --- a/Content.Server/Cargo/GalacticBankManager.cs +++ /dev/null @@ -1,109 +0,0 @@ -using Content.Server.GameObjects.Components.Cargo; -using Robust.Shared.Timing; -using System; -using System.Collections.Generic; - -namespace Content.Server.Cargo -{ - public class GalacticBankManager : IGalacticBankManager - { - private readonly float DELAY = 10f; - private readonly int POINT_INCREASE = 10; - - private int _index = 0; - private float _timer = 10f; - private readonly Dictionary _accounts = new Dictionary(); - private readonly List _components = new List(); - - public GalacticBankManager() - { - CreateBankAccount("Orbital Monitor IV Station", 100000); - } - - public IEnumerable GetAllBankAccounts() - { - return _accounts.Values; - } - - public void Shutdown() - { - throw new System.NotImplementedException(); - } - - public void CreateBankAccount(string name, int balance = 0) - { - var account = new CargoBankAccount(_index, name, balance); - _accounts.Add(_index, account); - _index += 1; - } - - public CargoBankAccount GetBankAccount(int id) - { - return _accounts[id]; - } - - public bool TryGetBankAccount(int id, out CargoBankAccount account) - { - if (_accounts.TryGetValue(id, out var _account)) - { - account = _account; - return true; - } - account = null; - return false; - } - - public void Update(FrameEventArgs frameEventArgs) - { - _timer += frameEventArgs.DeltaSeconds; - if (_timer < DELAY) - return; - _timer -= DELAY; - foreach (var account in GetAllBankAccounts()) - { - account.Balance += POINT_INCREASE; - } - SyncComponents(); - } - - private void SyncComponents() - { - foreach (var component in _components) - { - var account = GetBankAccount(component.BankId); - if (account == null) - continue; - component.SetState(account.Id, account.Name, account.Balance); - } - } - - private void SyncComponentsWithId(int id) - { - var account = GetBankAccount(id); - foreach (var component in _components) - { - if (component.BankId != id) - continue; - component.SetState(account.Id, account.Name, account.Balance); - } - } - - public void AddComponent(CargoConsoleComponent component) - { - if (_components.Contains(component)) - return; - _components.Add(component); - } - - public bool ChangeBalance(int id, int n) - { - if (!TryGetBankAccount(id, out var account)) - return false; - if (account.Balance + n < 0) - return false; - account.Balance += n; - SyncComponentsWithId(account.Id); - return true; - } - } -} diff --git a/Content.Server/Cargo/ICargoBankAccount.cs b/Content.Server/Cargo/ICargoBankAccount.cs index 998f846c99..c8ee438a99 100644 --- a/Content.Server/Cargo/ICargoBankAccount.cs +++ b/Content.Server/Cargo/ICargoBankAccount.cs @@ -7,5 +7,6 @@ namespace Content.Server.Cargo int Id { get; } string Name { get; } int Balance { get; } + public event Action OnBalanceChange; } } diff --git a/Content.Server/Cargo/IGalacticBankManager.cs b/Content.Server/Cargo/IGalacticBankManager.cs deleted file mode 100644 index a6853f04ff..0000000000 --- a/Content.Server/Cargo/IGalacticBankManager.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Content.Server.GameObjects.Components.Cargo; -using Robust.Shared.Timing; -using System.Collections.Generic; - -namespace Content.Server.Cargo -{ - public interface IGalacticBankManager - { - IEnumerable GetAllBankAccounts(); - - void Shutdown(); - void Update(FrameEventArgs frameEventArgs); - - void CreateBankAccount(string name, int balance); - CargoBankAccount GetBankAccount(int id); - void AddComponent(CargoConsoleComponent cargoConsoleComponent); - bool TryGetBankAccount(int id, out CargoBankAccount account); - bool ChangeBalance(int id, int n); - } -} diff --git a/Content.Server/EntryPoint.cs b/Content.Server/EntryPoint.cs index 90a76589e4..5329f8b588 100644 --- a/Content.Server/EntryPoint.cs +++ b/Content.Server/EntryPoint.cs @@ -1,9 +1,7 @@ -using Content.Server.Cargo; -using Content.Server.Interfaces; +using Content.Server.Interfaces; using Content.Server.Interfaces.Chat; using Content.Server.Interfaces.GameTicking; using Content.Server.Interfaces.PDA; -using Content.Server.Preferences; using Content.Server.Sandbox; using Content.Shared.Kitchen; using Robust.Server.Interfaces.Player; @@ -98,7 +96,6 @@ namespace Content.Server case ModUpdateLevel.PreEngine: { _gameTicker.Update(frameEventArgs); - IoCManager.Resolve().Update(frameEventArgs); break; } } diff --git a/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs b/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs index 9104580303..4e0d8d7124 100644 --- a/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs +++ b/Content.Server/GameObjects/Components/Cargo/CargoConsoleComponent.cs @@ -1,21 +1,16 @@ using Content.Server.Cargo; +using Content.Server.GameObjects.Components.Power; using Content.Server.GameObjects.EntitySystems; using Content.Shared.GameObjects.Components.Cargo; -using Content.Server.GameObjects.Components.Power; using Content.Shared.Prototypes.Cargo; +using JetBrains.Annotations; using Robust.Server.GameObjects.Components.UserInterface; using Robust.Server.Interfaces.GameObjects; using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects.Systems; using Robust.Shared.IoC; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Content.Server.Utility; -using Robust.Shared.Map; namespace Content.Server.GameObjects.Components.Cargo { @@ -24,7 +19,6 @@ namespace Content.Server.GameObjects.Components.Cargo public class CargoConsoleComponent : SharedCargoConsoleComponent, IActivate { #pragma warning disable 649 - [Dependency] private readonly IGalacticBankManager _galacticBankManager; [Dependency] private readonly ICargoOrderDataManager _cargoOrderDataManager; #pragma warning restore 649 @@ -37,13 +31,43 @@ namespace Content.Server.GameObjects.Components.Cargo public GalacticMarketComponent Market { get; private set; } [ViewVariables] public CargoOrderDatabaseComponent Orders { get; private set; } + + private CargoBankAccount _bankAccount; + [ViewVariables] - public int BankId { get; private set; } + [CanBeNull] + public CargoBankAccount BankAccount + { + get => _bankAccount; + private set + { + if (_bankAccount == value) + return; + if (_bankAccount != null) + { + _bankAccount.OnBalanceChange -= OnBankAccountChange; + } + + _bankAccount = value; + if (value != null) + { + _bankAccount.OnBalanceChange += OnBankAccountChange; + } + + OnBankAccountChange(); + } + } + + private void OnBankAccountChange() + { + SetState(_bankAccount.Id, _bankAccount.Name, _bankAccount.Balance); + } private bool _requestOnly = false; private PowerDeviceComponent _powerDevice; private bool Powered => _powerDevice.Powered; + private CargoConsoleSystem _cargoConsoleSystem; public override void Initialize() { @@ -53,8 +77,8 @@ namespace Content.Server.GameObjects.Components.Cargo _userInterface = Owner.GetComponent().GetBoundUserInterface(CargoConsoleUiKey.Key); _userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage; _powerDevice = Owner.GetComponent(); - _galacticBankManager.AddComponent(this); - BankId = 0; + _cargoConsoleSystem = EntitySystem.Get(); + BankAccount = _cargoConsoleSystem.StationAccount; } /// @@ -80,7 +104,7 @@ namespace Content.Server.GameObjects.Components.Cargo { if (msg.Amount <= 0) break; - _cargoOrderDataManager.AddOrder(Orders.Database.Id, msg.Requester, msg.Reason, msg.ProductId, msg.Amount, BankId); + _cargoOrderDataManager.AddOrder(Orders.Database.Id, msg.Requester, msg.Reason, msg.ProductId, msg.Amount, _bankAccount.Id); break; } case CargoConsoleRemoveOrderMessage msg: @@ -96,7 +120,7 @@ namespace Content.Server.GameObjects.Components.Cargo _prototypeManager.TryIndex(order.ProductId, out CargoProductPrototype product); if (product == null) break; - if (!_galacticBankManager.ChangeBalance(BankId, (-product.PointCost) * order.Amount)) + if (!_cargoConsoleSystem.ChangeBalance(_bankAccount.Id, (-product.PointCost) * order.Amount)) break; _cargoOrderDataManager.ApproveOrder(Orders.Database.Id, msg.OrderNumber); break; diff --git a/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs b/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs new file mode 100644 index 0000000000..49350c41df --- /dev/null +++ b/Content.Server/GameObjects/EntitySystems/CargoConsoleSystem.cs @@ -0,0 +1,106 @@ +using System.Collections.Generic; +using Content.Server.Cargo; +using Robust.Shared.GameObjects.Systems; + +namespace Content.Server.GameObjects.EntitySystems +{ + public class CargoConsoleSystem : EntitySystem + { + /// + /// How much time to wait (in seconds) before increasing bank accounts balance. + /// + private const float Delay = 10f; + /// + /// How many points to give to every bank account every seconds. + /// + private const int PointIncrease = 10; + + /// + /// Keeps track of how much time has elapsed since last balance increase. + /// + private float _timer; + /// + /// Stores all bank accounts. + /// + private readonly Dictionary _accountsDict = new Dictionary(); + /// + /// Used to assign IDs to bank accounts. Incremental counter. + /// + private int _accountIndex = 0; + /// + /// Enumeration of all bank accounts. + /// + public IEnumerable BankAccounts => _accountsDict.Values; + /// + /// The station's bank account. + /// + public CargoBankAccount StationAccount => GetBankAccount(0); + + public override void Initialize() + { + CreateBankAccount("Orbital Monitor IV Station", 100000); + } + + public override void Update(float frameTime) + { + _timer += frameTime; + if (_timer < Delay) + { + return; + } + + _timer -= Delay; + foreach (var account in BankAccounts) + { + account.Balance += PointIncrease; + } + } + + /// + /// Creates a new bank account. + /// + public void CreateBankAccount(string name, int balance) + { + var account = new CargoBankAccount(_accountIndex, name, balance); + _accountsDict.Add(_accountIndex, account); + _accountIndex += 1; + } + + /// + /// Returns the bank account associated with the given ID. + /// + public CargoBankAccount GetBankAccount(int id) + { + return _accountsDict[id]; + } + + /// + /// Returns whether the account exists, eventually passing the account in the out parameter. + /// + public bool TryGetBankAccount(int id, out CargoBankAccount account) + { + return _accountsDict.TryGetValue(id, out account); + } + + /// + /// Attempts to change the given account's balance. + /// Returns false if there's no account associated with the given ID + /// or if the balance would end up being negative. + /// + public bool ChangeBalance(int id, int amount) + { + if (!TryGetBankAccount(id, out var account)) + { + return false; + } + + if (account.Balance + amount < 0) + { + return false; + } + + account.Balance += amount; + return true; + } + } +} diff --git a/Content.Server/ServerContentIoC.cs b/Content.Server/ServerContentIoC.cs index 54b61d71c7..e9f4f9215c 100644 --- a/Content.Server/ServerContentIoC.cs +++ b/Content.Server/ServerContentIoC.cs @@ -9,10 +9,8 @@ using Content.Server.PDA; using Content.Server.Preferences; using Content.Server.Sandbox; using Content.Server.Utility; -using Content.Shared.Chemistry; -using Content.Shared.Kitchen; using Content.Shared.Interfaces; -using Content.Shared.Interfaces.Chemistry; +using Content.Shared.Kitchen; using Robust.Shared.IoC; namespace Content.Server @@ -27,7 +25,6 @@ namespace Content.Server IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); - IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); IoCManager.Register();