using System.Linq; using Content.Server.Administration; using Content.Server.Cargo.Systems; using Content.Shared.Administration; using Content.Shared.Cargo.Components; using Content.Shared.Cargo.Prototypes; using Robust.Shared.Prototypes; using Robust.Shared.Toolshed; namespace Content.Server.Station.Commands; [ToolshedCommand, AdminCommand(AdminFlags.Admin)] public sealed class BankCommand : ToolshedCommand { private CargoSystem? _cargo; [CommandImplementation("accounts")] public IEnumerable Accounts([PipedArgument] EntityUid station) { _cargo ??= GetSys(); foreach (var (account, _) in _cargo.GetAccounts(station)) { yield return new BankAccount(account.Id, station, _cargo, EntityManager); } } [CommandImplementation("accounts")] public IEnumerable Accounts([PipedArgument] IEnumerable stations) => stations.SelectMany(Accounts); [CommandImplementation("account")] public BankAccount Account([PipedArgument] EntityUid station, ProtoId account) { _cargo ??= GetSys(); return new BankAccount(account.Id, station, _cargo, EntityManager); } [CommandImplementation("account")] public IEnumerable Account([PipedArgument] IEnumerable stations, ProtoId account) => stations.Select(x => Account(x, account)); [CommandImplementation("adjust")] public IEnumerable Adjust([PipedArgument] IEnumerable @ref, int by) { _cargo ??= GetSys(); var bankAccounts = @ref.ToList(); foreach (var bankAccount in bankAccounts.ToList()) { _cargo.TryAdjustBankAccount(bankAccount.Station, bankAccount.Account, by, true); } return bankAccounts; } [CommandImplementation("set")] public IEnumerable Set([PipedArgument] IEnumerable @ref, int by) { _cargo ??= GetSys(); var bankAccounts = @ref.ToList(); foreach (var bankAccount in bankAccounts.ToList()) { _cargo.TrySetBankAccount(bankAccount.Station, bankAccount.Account, by, true); } return bankAccounts; } [CommandImplementation("amount")] public IEnumerable Amount([PipedArgument] IEnumerable @ref) { _cargo ??= GetSys(); return @ref.Select(bankAccount => (success: _cargo.TryGetAccount(bankAccount.Station, bankAccount.Account, out var money), money)) .Where(result => result.success) .Select(result => result.money); } } public readonly record struct BankAccount( string Account, Entity Station, CargoSystem Cargo, IEntityManager EntityManager) { public override string ToString() { if (!Cargo.TryGetAccount(Station, Account, out var money)) { return $"{EntityManager.ToPrettyString(Station)} Account {Account} : (not a account)"; } return $"{EntityManager.ToPrettyString(Station)} Account {Account} : {money}"; } }