// © SUNRISE, An EULA/CLA with a hosting restriction, full text: https://github.com/space-sunrise/space-station-14/blob/master/CLA.txt
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.EntitySystems;
using Content.Server.Paper;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers;
using Robust.Shared.Network;
using Robust.Shared.Timing;
using System.Collections.Frozen;
using Content.Shared.Paper;
using Robust.Shared.Utility;
using static Content.Shared.Paper.PaperComponent;
using Robust.Shared.Prototypes;
using System.Linq;
using Robust.Shared.Serialization;
using Content.Shared.Chemistry.Reagent;
using System.Linq;
using System.Text;
using Content.Shared.Power;
namespace Content.Server.Chemistry.EntitySystems;
///
public sealed class VaccinatorSystem : SharedVaccinatorSystem
{
///
///
[Dependency] private readonly PaperSystem _paperSystem = default!;
[Dependency] private readonly MetaDataSystem _metaData = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
///
/// A cache of all reactions indexed by at most ONE of their required reactants.
/// I.e., even if a reaction has more than one reagent, it will only ever appear once in this dictionary.
///
private FrozenDictionary> _reactionsSingle = default!;
///
/// A cache of all reactions indexed by one of their required reactants.
///
private FrozenDictionary> _reactions = default!;
public override void Initialize()
{
base.Initialize();
InitializeReactionCache();
SubscribeLocalEvent(OnPowerChanged);
SubscribeNetworkEvent(OnPaper);
}
///
/// Handles building the reaction cache.
///
private void InitializeReactionCache()
{
// Construct single-reaction dictionary.
var dict = new Dictionary>();
foreach (var reaction in _prototypeManager.EnumeratePrototypes())
{
// For this dictionary we only need to cache based on the first reagent.
var reagent = reaction.Reactants.Keys.First();
var list = dict.GetOrNew(reagent);
list.Add(reaction);
}
_reactionsSingle = dict.ToFrozenDictionary();
dict.Clear();
foreach (var reaction in _prototypeManager.EnumeratePrototypes())
{
foreach (var reagent in reaction.Reactants.Keys)
{
var list = dict.GetOrNew(reagent);
list.Add(reaction);
}
}
_reactions = dict.ToFrozenDictionary();
}
private void OnPaper(PaperInputTextMessageLigyb args)
{
EntityUid? paper = null;
bool printeds = false;
List wasProto = new List();
foreach (var reactant in args.ReagentQuantity)
{
if (_prototypeManager.TryIndex(reactant.Reagent.Prototype, out ReagentPrototype? protoss))
{
if (protoss.Group != "Infect")
{
if (paper != null)
{
EntityManager.DeleteEntity(paper);
return;
}
}
}
if (wasProto.Contains(reactant.Reagent.Prototype)) { continue; } else { wasProto.Add(reactant.Reagent.Prototype); }
if (_reactions.TryGetValue(reactant.Reagent.Prototype, out var reactantReactions))
{
if (printeds) continue;
else printeds = true;
var printed = EntityManager.SpawnEntity("ForensicReportPaper", Transform(GetEntity(args.Uid)).Coordinates);
paper = printed;
_metaData.SetEntityName(printed, "Технология изготовления вакцины");
var text = new StringBuilder();
text.AppendLine("Для изготовления вакцины, требуется:");
text.AppendLine();
foreach (var r in reactantReactions)
{
foreach (var reactan in r.Reactants)
{
if (r.MixingCategories == null)
{
_prototypeManager.TryIndex(reactan.Key, out ReagentPrototype? proto);
string no = "";
text.AppendLine($"{proto?.LocalizedName ?? no}: {reactan.Value.Amount}u");
}
}
}
text.AppendLine("После чего положить полученную жидкость в вакцинатор, добавив одну каплю крови здорового человека.");
if(TryComp(printed, out var paperComp))
{
_paperSystem.SetContent((printed, paperComp), text.ToString());
}
}
}
}
private void OnPowerChanged(Entity ent, ref PowerChangedEvent args)
{
if (!args.Powered)
StopMix(ent);
}
protected override bool HasPower(Entity entity)
{
return this.IsPowered(entity, EntityManager);
}
}