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; using Robust.Shared.Utility; namespace Content.Shared.Cargo; public abstract class SharedCargoSystem : EntitySystem { [Dependency] protected readonly IGameTiming Timing = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapInit); } private void OnMapInit(Entity ent, ref MapInitEvent args) { ent.Comp.NextIncomeTime = Timing.CurTime + ent.Comp.IncomeDelay; Dirty(ent); } /// /// For a given station, retrieves the balance in a specific account. /// public int GetBalanceFromAccount(Entity station, ProtoId account) { if (!Resolve(station, ref station.Comp)) return 0; return station.Comp.Accounts.GetValueOrDefault(account); } /// /// For a station, creates a distribution between one the bank's account and the other accounts. /// The primary account receives the majority percentage listed on the bank account, with the remaining /// funds distributed to all accounts based on /// public Dictionary, double> CreateAccountDistribution(Entity stationBank) { var distribution = new Dictionary, double> { { stationBank.Comp.PrimaryAccount, stationBank.Comp.PrimaryCut } }; var remaining = 1.0 - stationBank.Comp.PrimaryCut; foreach (var (account, percentage) in stationBank.Comp.RevenueDistribution) { var existing = distribution.GetOrNew(account); distribution[account] = existing + remaining * percentage; } return distribution; } /// /// Returns information about the given bank account. /// /// Station to get bank account info from. /// Bank account prototype ID to get info for. /// The amount of money in the account /// Whether or not the bank account exists. public bool TryGetAccount(Entity station, ProtoId accountPrototypeId, out int money) { money = 0; if (!Resolve(station, ref station.Comp)) return false; return station.Comp.Accounts.TryGetValue(accountPrototypeId, out money); } /// /// Returns a readonly dictionary of all accounts and their money info. /// /// Station to get bank account info from. /// Whether or not the bank account exists. public IReadOnlyDictionary, int> GetAccounts(Entity station) { if (!Resolve(station, ref station.Comp)) return new Dictionary, int>(); return station.Comp.Accounts; } /// /// Attempts to adjust the money of a certain bank account. /// /// Station where the bank account is from /// the id of the bank account /// how much money to set the account to /// Whether or not it should create the account if it doesn't exist. /// Whether to mark the bank account component as dirty. /// Whether or not setting the value succeeded. public bool TryAdjustBankAccount( Entity station, ProtoId 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; } /// /// Attempts to set the money of a certain bank account. /// /// Station where the bank account is from /// the id of the bank account /// how much money to set the account to /// Whether or not it should create the account if it doesn't exist. /// Whether to mark the bank account component as dirty. /// Whether or not setting the value succeeded. public bool TrySetBankAccount( Entity station, ProtoId 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 ent, int balanceAdded, ProtoId account, bool dirty = true) { UpdateBankAccount( ent, balanceAdded, new Dictionary, double> { {account, 1} }, dirty: dirty); } /// /// Adds or removes funds from the . /// /// The station. /// The amount of funds to add or remove. /// The distribution between individual . /// Whether to mark the bank account component as dirty. [PublicAPI] public void UpdateBankAccount( Entity ent, int balanceAdded, Dictionary, 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] public enum CargoConsoleUiKey : byte { Orders, Bounty, Shuttle, Telepad } [NetSerializable, Serializable] public enum CargoPalletConsoleUiKey : byte { Sale } [Serializable, NetSerializable] public enum CargoTelepadState : byte { Unpowered, Idle, Teleporting, }; [Serializable, NetSerializable] public enum CargoTelepadVisuals : byte { State, };