Refactored GalacticBankManager (#1089)

This commit is contained in:
DamianX
2020-06-12 18:31:57 +02:00
committed by GitHub
parent 48d9ffa78b
commit 7817681894
8 changed files with 161 additions and 152 deletions

View File

@@ -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)
{

View File

@@ -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<int, CargoBankAccount> _accounts = new Dictionary<int, CargoBankAccount>();
private readonly List<CargoConsoleComponent> _components = new List<CargoConsoleComponent>();
public GalacticBankManager()
{
CreateBankAccount("Orbital Monitor IV Station", 100000);
}
public IEnumerable<CargoBankAccount> 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;
}
}
}

View File

@@ -7,5 +7,6 @@ namespace Content.Server.Cargo
int Id { get; }
string Name { get; }
int Balance { get; }
public event Action OnBalanceChange;
}
}

View File

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

View File

@@ -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<IGalacticBankManager>().Update(frameEventArgs);
break;
}
}

View File

@@ -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<ServerUserInterfaceComponent>().GetBoundUserInterface(CargoConsoleUiKey.Key);
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
_powerDevice = Owner.GetComponent<PowerDeviceComponent>();
_galacticBankManager.AddComponent(this);
BankId = 0;
_cargoConsoleSystem = EntitySystem.Get<CargoConsoleSystem>();
BankAccount = _cargoConsoleSystem.StationAccount;
}
/// <summary>
@@ -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;

View File

@@ -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
{
/// <summary>
/// How much time to wait (in seconds) before increasing bank accounts balance.
/// </summary>
private const float Delay = 10f;
/// <summary>
/// How many points to give to every bank account every <see cref="Delay"/> seconds.
/// </summary>
private const int PointIncrease = 10;
/// <summary>
/// Keeps track of how much time has elapsed since last balance increase.
/// </summary>
private float _timer;
/// <summary>
/// Stores all bank accounts.
/// </summary>
private readonly Dictionary<int, CargoBankAccount> _accountsDict = new Dictionary<int, CargoBankAccount>();
/// <summary>
/// Used to assign IDs to bank accounts. Incremental counter.
/// </summary>
private int _accountIndex = 0;
/// <summary>
/// Enumeration of all bank accounts.
/// </summary>
public IEnumerable<CargoBankAccount> BankAccounts => _accountsDict.Values;
/// <summary>
/// The station's bank account.
/// </summary>
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;
}
}
/// <summary>
/// Creates a new bank account.
/// </summary>
public void CreateBankAccount(string name, int balance)
{
var account = new CargoBankAccount(_accountIndex, name, balance);
_accountsDict.Add(_accountIndex, account);
_accountIndex += 1;
}
/// <summary>
/// Returns the bank account associated with the given ID.
/// </summary>
public CargoBankAccount GetBankAccount(int id)
{
return _accountsDict[id];
}
/// <summary>
/// Returns whether the account exists, eventually passing the account in the out parameter.
/// </summary>
public bool TryGetBankAccount(int id, out CargoBankAccount account)
{
return _accountsDict.TryGetValue(id, out account);
}
/// <summary>
/// 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.
/// </summary>
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;
}
}
}

View File

@@ -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<IChatManager, ChatManager>();
IoCManager.Register<IMoMMILink, MoMMILink>();
IoCManager.Register<ISandboxManager, SandboxManager>();
IoCManager.Register<IGalacticBankManager, GalacticBankManager>();
IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>();
IoCManager.Register<IModuleManager, ServerModuleManager>();
IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>();