diff --git a/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor b/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor index 27cae3a..c390d72 100644 --- a/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor +++ b/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor @@ -17,9 +17,10 @@ @bind-Value:set="@((int strain) => SetPokerusStrain(strain))"> @for (var i = 0; i < 16; i++) { - - @i + var localIndex = i; + + @localIndex } @@ -29,11 +30,12 @@ - @for (var i = 0; i < 3; i++) + @foreach (var day in PokerusDays) { - - @i + var localDay = day; + + @localDay } diff --git a/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor.cs b/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor.cs index 2b6f661..ae37286 100644 --- a/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor.cs +++ b/Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor.cs @@ -5,6 +5,21 @@ public partial class PokerusComponent [Parameter, EditorRequired] public PKM? Pokemon { get; set; } + private List PokerusDays { get; set; } = []; + + protected override void OnParametersSet() + { + base.OnParametersSet(); + + if (Pokemon is null) + { + return; + } + + var max = Pokerus.GetMaxDuration(Pokemon.PokerusStrain); + PokerusDays = Enumerable.Range(0, max + 1).ToList(); + } + private void SetPokerusInfected(bool infected) { if (Pokemon is null) @@ -13,6 +28,30 @@ private void SetPokerusInfected(bool infected) } Pokemon.IsPokerusInfected = infected; + + if (!infected && Pokemon.IsPokerusCured) + { + Pokemon.IsPokerusCured = false; + return; + } + + if (infected) + { + if (Pokemon.PokerusStrain == 0) + { + Pokemon.PokerusStrain = 1; + } + + if (Pokemon.PokerusDays == 0) + { + Pokemon.PokerusDays = 1; + } + } + + if (!infected) + { + Pokemon.PokerusStrain = Pokemon.PokerusDays = 0; + } } private void SetPokerusCured(bool cured) @@ -23,6 +62,30 @@ private void SetPokerusCured(bool cured) } Pokemon.IsPokerusCured = cured; + + if (cured) + { + if (Pokemon.PokerusStrain == 0) + { + Pokemon.PokerusStrain = 1; + } + + Pokemon.PokerusDays = 0; + Pokemon.IsPokerusInfected = true; + } + else if (!Pokemon.IsPokerusInfected) + { + Pokemon.PokerusStrain = 0; + } + else + { + Pokemon.PokerusDays = 1; + } + + if (!cured && Pokemon.IsPokerusInfected && Pokemon.PokerusDays == 0) + { + Pokemon.PokerusDays = 1; + } } private void SetPokerusStrain(int strain) @@ -31,7 +94,9 @@ private void SetPokerusStrain(int strain) { return; } + Pokemon.PokerusStrain = strain; + ChangePokerusDaysList(-1, strain, Pokemon.PokerusDays); } private void SetPokerusDays(int days) @@ -40,7 +105,39 @@ private void SetPokerusDays(int days) { return; } + Pokemon.PokerusDays = days; + + var strain = Pokemon.PokerusStrain; + bool? cured = null; + + if (Pokerus.IsSusceptible(strain, days)) + { + cured = Pokemon.IsPokerusInfected = false; // No Strain = Never Cured / Infected, triggers Strain update + } + else if (Pokerus.IsImmune(strain, days)) + { + cured = true; // Any Strain = Cured + } + + if (cured is not null) + { + Pokemon.IsPokerusCured = cured.Value; + } } -} + private void ChangePokerusDaysList(int oldStrain, int newStrain, int currentDuration) + { + if (oldStrain == newStrain) + { + return; + } + + PokerusDays.Clear(); + var max = Pokerus.GetMaxDuration(newStrain); + PokerusDays = Enumerable.Range(0, max + 1).ToList(); + + // Set the days back if they're legal + SetPokerusDays(Math.Min(max, currentDuration)); + } +}