Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disease #779

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Content.Client/Backmen/Disease/DiseaseMachineSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Content.Client.Medical;
using Content.Shared.Backmen.Disease;
using Robust.Client.GameObjects;

namespace Content.Client.Backmen.Disease;

/// <summary>
/// Controls client-side visuals for the
/// disease machines.
/// </summary>
public sealed class DiseaseMachineSystem : VisualizerSystem<DiseaseMachineVisualsComponent>
{
protected override void OnAppearanceChange(EntityUid uid, DiseaseMachineVisualsComponent component, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
return;

if (AppearanceSystem.TryGetData<bool>(uid, DiseaseMachineVisuals.IsOn, out var isOn, args.Component)
&& AppearanceSystem.TryGetData<bool>(uid, DiseaseMachineVisuals.IsRunning, out var isRunning, args.Component))
{
var state = isRunning ? component.RunningState : component.IdleState;
args.Sprite.LayerSetVisible(DiseaseMachineVisualLayers.IsOn, isOn);
args.Sprite.LayerSetState(DiseaseMachineVisualLayers.IsRunning, state);
}
}
}
15 changes: 15 additions & 0 deletions Content.Client/Backmen/Disease/DiseaseMachineVisualsComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Content.Client.Backmen.Disease;

/// <summary>
/// Holds the idle and running state for machines to control
/// playing animtions on the client.
/// </summary>
[RegisterComponent]
public sealed partial class DiseaseMachineVisualsComponent : Component
{
[DataField("idleState", required: true)]
public string IdleState = default!;

[DataField("runningState", required: true)]
public string RunningState = default!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Content.Shared.Backmen.Disease;
using JetBrains.Annotations;

namespace Content.Client.Backmen.Disease.UI;

[UsedImplicitly]
public sealed class VaccineMachineBoundUserInterface : BoundUserInterface
{
private VaccineMachineMenu? _machineMenu;

public VaccineMachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}

protected override void Open()
{
base.Open();

_machineMenu = new VaccineMachineMenu(this);

_machineMenu.OnClose += Close;

_machineMenu.OnServerSelectionButtonPressed += _ =>
{
SendMessage(new VaccinatorServerSelectionMessage());
};

_machineMenu.OpenCentered();
_machineMenu?.PopulateBiomass(Owner);
}

public void CreateVaccineMessage(string disease, int amount)
{
SendMessage(new CreateVaccineMessage(disease, amount));
}

public void RequestSync()
{
SendMessage(new VaccinatorSyncRequestMessage());
}

protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);

switch (state)
{
case VaccineMachineUpdateState msg:
_machineMenu?.UpdateLocked(msg.Locked);
_machineMenu?.PopulateDiseases(msg.Diseases);
_machineMenu?.PopulateBiomass(Owner);
_machineMenu?.UpdateCost(msg.BiomassCost);
_machineMenu?.UpdateServerConnection(msg.HasServer);
break;
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (!disposing)
return;

_machineMenu?.Dispose();
}
}
50 changes: 50 additions & 0 deletions Content.Client/Backmen/Disease/UI/VaccineMachineMenu.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc 'vaccine-machine-menu-title'}"
MinSize="800 400"
SetSize="800 400">
<BoxContainer Orientation="Vertical"
HorizontalExpand="True"
VerticalExpand="True">
<BoxContainer Orientation="Horizontal"
HorizontalExpand="True"
VerticalExpand="True"
SizeFlagsStretchRatio="2"
SeparationOverride="10">
<BoxContainer Orientation="Vertical"
HorizontalExpand="True"
VerticalExpand="True">
<Label Text="{Loc 'vaccine-machine-menu-known-diseases-label'}" />
<ItemList Name="KnownDiseases"
SelectMode="Single"
HorizontalExpand="False"
VerticalExpand="True">
<!-- Diseases are are added here by code from the disease server -->
</ItemList>
</BoxContainer>
<BoxContainer Orientation="Vertical"
Align="Center"
HorizontalExpand="True"
VerticalExpand="True"
SeparationOverride="10">
<Label Name="DiseaseName"
Text="{Loc 'vaccine-machine-menu-none-selected'}"/>
<Label Name="BiomassCost" />
<Label Name="BiomassCurrent" />
<Button Name="ServerSelectionButton"
Access="Public"
Text="{Loc 'research-console-menu-server-selection-button'}" />
<Button Name="ServerSyncButton"
Access="Public"
Text="{Loc 'research-console-menu-server-sync-button'}" />
<Button Name="CreateButton"
Access="Public"
Text="{Loc 'vaccine-machine-menu-create-vaccine-button'}"
VerticalExpand="True"
Disabled="True" />
<SpinBox Name="CreateAmount"
Access="Public"
Value="1" />
</BoxContainer>
</BoxContainer>
</BoxContainer>
</DefaultWindow>
136 changes: 136 additions & 0 deletions Content.Client/Backmen/Disease/UI/VaccineMachineMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using Content.Shared.Materials;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;

namespace Content.Client.Backmen.Disease.UI;

[GenerateTypedNameReferences]
public sealed partial class VaccineMachineMenu : DefaultWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;

private readonly SharedMaterialStorageSystem _storage;

public VaccineMachineBoundUserInterface Owner { get; }

public event Action<BaseButton.ButtonEventArgs>? OnServerSelectionButtonPressed;

private List<(string id, string name)> _knownDiseasePrototypes = new();
public (string id, string name) DiseaseSelected;
public bool Enough = false;
public bool Locked = false;
public int Cost => CreateAmount.Value * CostPer;
public int CostPer = 4;

public VaccineMachineMenu(VaccineMachineBoundUserInterface owner)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_storage = _entityManager.EntitySysManager.GetEntitySystem<SharedMaterialStorageSystem>();

Owner = owner;

ServerSelectionButton.OnPressed += a => OnServerSelectionButtonPressed?.Invoke(a);
DiseaseSelected = ("", ""); // nullability was a bit sussy so

KnownDiseases.OnItemSelected += KnownDiseaseSelected;
CreateAmount.ValueChanged += HandleAmountChanged;

CreateButton.OnPressed += _ =>
{
CreateVaccine();
};
ServerSyncButton.OnPressed += _ =>
{
RequestSync();
};
}

/// <summary>
/// Called when a known disease is selected.
/// </summary>
private void KnownDiseaseSelected(ItemList.ItemListSelectedEventArgs obj)
{
DiseaseSelected = _knownDiseasePrototypes[obj.ItemIndex];
CreateButton.Disabled = !Enough || Locked;

PopulateSelectedDisease();
}

/// <summary>
/// Sends a message to create a vaccine of the selected disease.
/// </summary>
private void CreateVaccine()
{
if (DiseaseSelected == ("", ""))
return;

if (CreateAmount.Value <= 0)
return;

Owner.CreateVaccineMessage(DiseaseSelected.id, CreateAmount.Value);
}

private void RequestSync()
{
Owner.RequestSync();
}

public void PopulateDiseases(List<(string id, string name)> diseases)
{
KnownDiseases.Clear();

_knownDiseasePrototypes.Clear();

foreach (var disease in diseases)
{
KnownDiseases.AddItem(Loc.GetString(disease.name));
_knownDiseasePrototypes.Add((disease.id, Loc.GetString(disease.name)));
}
}

public void UpdateCost(int costPer)
{
CostPer = costPer;
BiomassCost.Text = Loc.GetString("vaccine-machine-menu-biomass-cost", ("value", Cost));
}

public void UpdateLocked(bool locked)
{
Locked = locked;
}

public void UpdateServerConnection(bool hasServer)
{
ServerSyncButton.Disabled = !hasServer;
}

private void HandleAmountChanged(ValueChangedEventArgs args)
{
UpdateCost(CostPer);
}

public void PopulateSelectedDisease()
{
if (DiseaseSelected == ("", ""))
{
CreateButton.Disabled = true;
DiseaseName.Text = Loc.GetString("vaccine-machine-menu-none-selected");
return;
}

DiseaseName.Text = DiseaseSelected.name;
}

public void PopulateBiomass(EntityUid machine)
{
var amt = _storage.GetMaterialAmount(machine, "Biomass");
Enough = (amt > Cost);
BiomassCurrent.Text = Loc.GetString("vaccine-machine-menu-biomass-current", ("value", amt));

if (DiseaseSelected != ("", ""))
CreateButton.Disabled = !Enough || Locked;
}
}
79 changes: 79 additions & 0 deletions Content.IntegrationTests/Tests/Backmen/Disease/TryAddDisease.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Linq;
using Content.Server.Backmen.Disease;
using Content.Shared.Backmen.Disease;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;

namespace Content.IntegrationTests.Tests.Backmen.Disease;

[TestFixture]
[TestOf(typeof(DiseaseSystem))]
public sealed class DiseaseTest
{
[Test]
public async Task AddAllDiseases()
{
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;

var protoManager = server.ResolveDependency<IPrototypeManager>();
var entManager = server.ResolveDependency<IEntityManager>();
var entSysManager = server.ResolveDependency<IEntitySystemManager>();
var diseaseSystem = entSysManager.GetEntitySystem<DiseaseSystem>();

var sickEntity = EntityUid.Invalid;

await server.WaitAssertion(() =>
{
sickEntity = entManager.SpawnEntity("MobHuman", MapCoordinates.Nullspace);
if(!entManager.HasComponent<DiseaseCarrierComponent>(sickEntity))
Assert.Fail("MobHuman has not DiseaseCarrierComponent");
});


foreach (var diseaseProto in protoManager.EnumeratePrototypes<DiseasePrototype>())
{
await server.WaitAssertion(() =>
{
diseaseSystem.TryAddDisease(sickEntity, diseaseProto.ID);
});
await server.WaitIdleAsync();
server.RunTicks(5);
await server.WaitAssertion(() =>
{
if(!entManager.HasComponent<DiseasedComponent>(sickEntity))
Assert.Fail("MobHuman has not DiseasedComponent");
});
if (!entManager.TryGetComponent<DiseaseCarrierComponent>(sickEntity, out var diseaseCarrierComponent))
{
Assert.Fail("MobHuman has not DiseaseCarrierComponent");
}

if (diseaseCarrierComponent.Diseases.All(x => x.ID != diseaseProto.ID))
{
Assert.Fail("Disease not apply");
}
await server.WaitAssertion(() =>
{
diseaseSystem.CureDisease((sickEntity,diseaseCarrierComponent), diseaseProto.ID);
});
await server.WaitIdleAsync();
server.RunTicks(1);
await server.WaitAssertion(() =>
{
if (diseaseCarrierComponent.Diseases.Any(x => x.ID == diseaseProto.ID))
{
Assert.Fail("Disease not remove");
}
if (diseaseCarrierComponent.PastDiseases.All(x => x != diseaseProto.ID))
{
Assert.Fail("Disease immunu not apply");
}
});
}

await pair.CleanReturnAsync();
}
}
Loading
Loading