Skip to content

Commit

Permalink
Fixes #67
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Nov 23, 2024
1 parent 73306ad commit 1ea74c9
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 8 deletions.
16 changes: 9 additions & 7 deletions Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
@bind-Value:set="@((int strain) => SetPokerusStrain(strain))">
@for (var i = 0; i < 16; i++)
{
<MudSelectItem Value="@i"
@key="@i">
@i
var localIndex = i;
<MudSelectItem Value="@localIndex"
@key="@localIndex">
@localIndex
</MudSelectItem>
}
</MudSelect>
Expand All @@ -29,11 +30,12 @@
<MudSelect Label="Days"
@bind-Value:get="@pokemon.PokerusDays"
@bind-Value:set="@((int days) => SetPokerusDays(days))">
@for (var i = 0; i < 3; i++)
@foreach (var day in PokerusDays)
{
<MudSelectItem Value="@i"
@key="@i">
@i
var localDay = day;
<MudSelectItem Value="@localDay"
@key="@localDay">
@localDay
</MudSelectItem>
}
</MudSelect>
Expand Down
99 changes: 98 additions & 1 deletion Pkmds.Web/Components/EditForms/Tabs/PokerusComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ public partial class PokerusComponent
[Parameter, EditorRequired]
public PKM? Pokemon { get; set; }

private List<int> 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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -31,7 +94,9 @@ private void SetPokerusStrain(int strain)
{
return;
}

Pokemon.PokerusStrain = strain;
ChangePokerusDaysList(-1, strain, Pokemon.PokerusDays);
}

private void SetPokerusDays(int days)
Expand All @@ -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));
}
}

0 comments on commit 1ea74c9

Please sign in to comment.