Add bank toolshed commands (#40614)

* add bank commands

* Follow convention

* use protoId

* make logic better

* Move stuff to shared

* Make things dirty

* Move UpdateBankAccount to shared as well

* nullable + resolve

* Fix commands

* make things less legible

* typo

* same typo

* im dumb

* I don't know how to spell

* replace select with foreach

Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com>

* replace select with foreach again

---------

Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com>
This commit is contained in:
Samuka-C
2025-10-15 12:10:25 -03:00
committed by GitHub
parent 68f9d748a2
commit 71bbe926a0
4 changed files with 249 additions and 50 deletions

View File

@@ -1,5 +1,6 @@
using Content.Shared.Cargo.Components;
using Content.Shared.Cargo.Prototypes;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
@@ -55,6 +56,151 @@ public abstract class SharedCargoSystem : EntitySystem
}
return distribution;
}
/// <summary>
/// Returns information about the given bank account.
/// </summary>
/// <param name="station">Station to get bank account info from.</param>
/// <param name="accountPrototypeId">Bank account prototype ID to get info for.</param>
/// <param name="money">The amount of money in the account</param>
/// <returns>Whether or not the bank account exists.</returns>
public bool TryGetAccount(Entity<StationBankAccountComponent?> station, ProtoId<CargoAccountPrototype> accountPrototypeId, out int money)
{
money = 0;
if (!Resolve(station, ref station.Comp))
return false;
return station.Comp.Accounts.TryGetValue(accountPrototypeId, out money);
}
/// <summary>
/// Returns a readonly dictionary of all accounts and their money info.
/// </summary>
/// <param name="station">Station to get bank account info from.</param>
/// <returns>Whether or not the bank account exists.</returns>
public IReadOnlyDictionary<ProtoId<CargoAccountPrototype>, int> GetAccounts(Entity<StationBankAccountComponent?> station)
{
if (!Resolve(station, ref station.Comp))
return new Dictionary<ProtoId<CargoAccountPrototype>, int>();
return station.Comp.Accounts;
}
/// <summary>
/// Attempts to adjust the money of a certain bank account.
/// </summary>
/// <param name="station">Station where the bank account is from</param>
/// <param name="accountPrototypeId">the id of the bank account</param>
/// <param name="money">how much money to set the account to</param>
/// <param name="createAccount">Whether or not it should create the account if it doesn't exist.</param>
/// <param name="dirty">Whether to mark the bank account component as dirty.</param>
/// <returns>Whether or not setting the value succeeded.</returns>
public bool TryAdjustBankAccount(
Entity<StationBankAccountComponent?> station,
ProtoId<CargoAccountPrototype> accountPrototypeId,
int money,
bool createAccount = false,
bool dirty = true)
{
if (!Resolve(station, ref station.Comp))
return false;
var accounts = station.Comp.Accounts;
if (!accounts.ContainsKey(accountPrototypeId) && !createAccount)
return false;
accounts[accountPrototypeId] += money;
var ev = new BankBalanceUpdatedEvent(station, station.Comp.Accounts);
RaiseLocalEvent(station, ref ev, true);
if (!dirty)
return true;
Dirty(station);
return true;
}
/// <summary>
/// Attempts to set the money of a certain bank account.
/// </summary>
/// <param name="station">Station where the bank account is from</param>
/// <param name="accountPrototypeId">the id of the bank account</param>
/// <param name="money">how much money to set the account to</param>
/// <param name="createAccount">Whether or not it should create the account if it doesn't exist.</param>
/// <param name="dirty">Whether to mark the bank account component as dirty.</param>
/// <returns>Whether or not setting the value succeeded.</returns>
public bool TrySetBankAccount(
Entity<StationBankAccountComponent?> station,
ProtoId<CargoAccountPrototype> accountPrototypeId,
int money,
bool createAccount = false,
bool dirty = true)
{
if (!Resolve(station, ref station.Comp))
return false;
var accounts = station.Comp.Accounts;
if (!accounts.ContainsKey(accountPrototypeId) && !createAccount)
return false;
accounts[accountPrototypeId] = money;
var ev = new BankBalanceUpdatedEvent(station, station.Comp.Accounts);
RaiseLocalEvent(station, ref ev, true);
if (!dirty)
return true;
Dirty(station);
return true;
}
public void UpdateBankAccount(
Entity<StationBankAccountComponent?> ent,
int balanceAdded,
ProtoId<CargoAccountPrototype> account,
bool dirty = true)
{
UpdateBankAccount(
ent,
balanceAdded,
new Dictionary<ProtoId<CargoAccountPrototype>, double> { {account, 1} },
dirty: dirty);
}
/// <summary>
/// Adds or removes funds from the <see cref="StationBankAccountComponent"/>.
/// </summary>
/// <param name="ent">The station.</param>
/// <param name="balanceAdded">The amount of funds to add or remove.</param>
/// <param name="accountDistribution">The distribution between individual <see cref="CargoAccountPrototype"/>.</param>
/// <param name="dirty">Whether to mark the bank account component as dirty.</param>
[PublicAPI]
public void UpdateBankAccount(
Entity<StationBankAccountComponent?> ent,
int balanceAdded,
Dictionary<ProtoId<CargoAccountPrototype>, double> accountDistribution,
bool dirty = true)
{
if (!Resolve(ent, ref ent.Comp))
return;
foreach (var (account, percent) in accountDistribution)
{
var accountBalancedAdded = (int) Math.Round(percent * balanceAdded);
ent.Comp.Accounts[account] += accountBalancedAdded;
}
var ev = new BankBalanceUpdatedEvent(ent, ent.Comp.Accounts);
RaiseLocalEvent(ent, ref ev, true);
if (!dirty)
return;
Dirty(ent);
}
}
[NetSerializable, Serializable]