| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- using System.Globalization;
- using System.Linq;
- using Content.Server.Administration;
- using Content.Server.Cargo.Systems;
- using Content.Server.EUI;
- using Content.Server.Item;
- using Content.Server.Power.Components;
- using Content.Shared.Administration;
- using Content.Shared.Item;
- using Content.Shared.Research.Prototypes;
- using Content.Shared.UserInterface;
- using Content.Shared.Weapons.Melee;
- using Robust.Shared.Console;
- using Robust.Shared.Prototypes;
- namespace Content.Server.UserInterface;
- [AdminCommand(AdminFlags.Debug)]
- public sealed class StatValuesCommand : IConsoleCommand
- {
- [Dependency] private readonly EuiManager _eui = default!;
- [Dependency] private readonly IComponentFactory _factory = default!;
- [Dependency] private readonly IEntityManager _entManager = default!;
- [Dependency] private readonly IPrototypeManager _proto = default!;
- public string Command => "showvalues";
- public string Description => Loc.GetString("stat-values-desc");
- public string Help => $"{Command} <cargosell / lathesell / melee / itemsize>";
- public void Execute(IConsoleShell shell, string argStr, string[] args)
- {
- if (shell.Player is not { } pSession)
- {
- shell.WriteError(Loc.GetString("stat-values-server"));
- return;
- }
- if (args.Length != 1)
- {
- shell.WriteError(Loc.GetString("stat-values-args"));
- return;
- }
- StatValuesEuiMessage message;
- switch (args[0])
- {
- case "cargosell":
- message = GetCargo();
- break;
- case "lathesell":
- message = GetLatheMessage();
- break;
- case "melee":
- message = GetMelee();
- break;
- case "itemsize":
- message = GetItem();
- break;
- case "drawrate":
- message = GetDrawRateMessage();
- break;
- default:
- shell.WriteError(Loc.GetString("stat-values-invalid", ("arg", args[0])));
- return;
- }
- var eui = new StatValuesEui();
- _eui.OpenEui(eui, pSession);
- eui.SendMessage(message);
- }
- public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
- {
- if (args.Length == 1)
- {
- return CompletionResult.FromOptions(new[] { "cargosell", "lathesell", "melee", "itemsize", "drawrate" });
- }
- return CompletionResult.Empty;
- }
- private StatValuesEuiMessage GetCargo()
- {
- // Okay so there's no easy way to do this with how pricing works
- // So we'll just get the first value for each prototype ID which is probably good enough for the majority.
- var values = new List<string[]>();
- var priceSystem = _entManager.System<PricingSystem>();
- var metaQuery = _entManager.GetEntityQuery<MetaDataComponent>();
- var prices = new HashSet<string>(256);
- var ents = _entManager.GetEntities().ToArray();
- foreach (var entity in ents)
- {
- if (!metaQuery.TryGetComponent(entity, out var meta))
- continue;
- var id = meta.EntityPrototype?.ID;
- // We'll add it even if we don't have it so we don't have to raise the event again because this is probably faster.
- if (id == null || !prices.Add(id))
- continue;
- var price = priceSystem.GetPrice(entity);
- if (price == 0)
- continue;
- values.Add(new[]
- {
- id,
- $"{price:0}",
- });
- }
- var state = new StatValuesEuiMessage()
- {
- Title = Loc.GetString("stat-cargo-values"),
- Headers = new List<string>()
- {
- Loc.GetString("stat-cargo-id"),
- Loc.GetString("stat-cargo-price"),
- },
- Values = values,
- };
- return state;
- }
- private StatValuesEuiMessage GetItem()
- {
- var values = new List<string[]>();
- var itemSystem = _entManager.System<ItemSystem>();
- var metaQuery = _entManager.GetEntityQuery<MetaDataComponent>();
- var itemQuery = _entManager.GetEntityQuery<ItemComponent>();
- var items = new HashSet<string>(1024);
- var ents = _entManager.GetEntities().ToArray();
- foreach (var entity in ents)
- {
- if (!metaQuery.TryGetComponent(entity, out var meta))
- continue;
- var id = meta.EntityPrototype?.ID;
- // We'll add it even if we don't have it so we don't have to raise the event again because this is probably faster.
- if (id == null || !items.Add(id))
- continue;
- if (!itemQuery.TryGetComponent(entity, out var itemComp))
- continue;
- values.Add(new[]
- {
- id,
- $"{itemSystem.GetItemSizeLocale(itemComp.Size)}",
- });
- }
- var state = new StatValuesEuiMessage
- {
- Title = Loc.GetString("stat-item-values"),
- Headers = new List<string>
- {
- Loc.GetString("stat-item-id"),
- Loc.GetString("stat-item-price"),
- },
- Values = values,
- };
- return state;
- }
- private StatValuesEuiMessage GetMelee()
- {
- var values = new List<string[]>();
- var meleeName = _factory.GetComponentName(typeof(MeleeWeaponComponent));
- foreach (var proto in _proto.EnumeratePrototypes<EntityPrototype>())
- {
- if (proto.Abstract ||
- !proto.Components.TryGetValue(meleeName,
- out var meleeComp))
- {
- continue;
- }
- var comp = (MeleeWeaponComponent) meleeComp.Component;
- // TODO: Wielded damage
- // TODO: Esword damage
- values.Add(new[]
- {
- proto.ID,
- (comp.Damage.GetTotal() * comp.AttackRate).ToString(),
- comp.AttackRate.ToString(CultureInfo.CurrentCulture),
- comp.Damage.GetTotal().ToString(),
- comp.Range.ToString(CultureInfo.CurrentCulture),
- });
- }
- var state = new StatValuesEuiMessage()
- {
- Title = "Cargo sell prices",
- Headers = new List<string>()
- {
- "ID",
- "Price",
- },
- Values = values,
- };
- return state;
- }
- private StatValuesEuiMessage GetLatheMessage()
- {
- var values = new List<string[]>();
- var priceSystem = _entManager.System<PricingSystem>();
- foreach (var proto in _proto.EnumeratePrototypes<LatheRecipePrototype>())
- {
- var cost = 0.0;
- foreach (var (material, count) in proto.Materials)
- {
- var materialPrice = _proto.Index(material).Price;
- cost += materialPrice * count;
- }
- var sell = priceSystem.GetLatheRecipePrice(proto);
- values.Add(new[]
- {
- proto.ID,
- $"{cost:0}",
- $"{sell:0}",
- });
- }
- var state = new StatValuesEuiMessage()
- {
- Title = Loc.GetString("stat-lathe-values"),
- Headers = new List<string>()
- {
- Loc.GetString("stat-lathe-id"),
- Loc.GetString("stat-lathe-cost"),
- Loc.GetString("stat-lathe-sell"),
- },
- Values = values,
- };
- return state;
- }
- private StatValuesEuiMessage GetDrawRateMessage()
- {
- var values = new List<string[]>();
- var powerName = _factory.GetComponentName(typeof(ApcPowerReceiverComponent));
- foreach (var proto in _proto.EnumeratePrototypes<EntityPrototype>())
- {
- if (proto.Abstract ||
- !proto.Components.TryGetValue(powerName,
- out var powerConsumer))
- {
- continue;
- }
- var comp = (ApcPowerReceiverComponent) powerConsumer.Component;
- if (comp.Load == 0)
- continue;
- values.Add(new[]
- {
- proto.ID,
- comp.Load.ToString(CultureInfo.InvariantCulture),
- });
- }
- var state = new StatValuesEuiMessage
- {
- Title = Loc.GetString("stat-drawrate-values"),
- Headers = new List<string>
- {
- Loc.GetString("stat-drawrate-id"),
- Loc.GetString("stat-drawrate-rate"),
- },
- Values = values,
- };
- return state;
- }
- }
|