Skip to content

Commit

Permalink
Fix location list, fixes #70 (#75)
Browse files Browse the repository at this point in the history
* Update README.md

* Add missing accessibility modifier

* Slightly better pattern for Gen III HoF record

* Revert AoT

* Fix location list, other minor tweaks
  • Loading branch information
codemonkey85 authored Dec 8, 2024
1 parent 6e23ad2 commit d0fc09b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 27 deletions.
5 changes: 3 additions & 2 deletions Pkmds.Web/Components/EditForms/Tabs/MetTab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ AppState.SelectedSlotsAreValid)
<MudSelect Label="Origin Game"
Variant="@Variant.Outlined"
@bind-Value="@Pokemon.Version"
@bind-Value:after="@OriginGameChanged"
ToStringFunc="@(version => GameInfo.GetVersionName(version))"
Disabled="@(saveGeneration <= 2)"
For="@(() => Pokemon.Version)">
Expand All @@ -27,7 +28,7 @@ AppState.SelectedSlotsAreValid)
<MudAutocomplete T="@ComboItem"
Label="Met Location"
Variant="@Variant.Outlined"
@bind-Value:get="@(AppService.GetMetLocationComboItem(Pokemon.MetLocation))"
@bind-Value:get="@GetMetLocation()"
@bind-Value:set="@(metLocation => Pokemon.MetLocation = (ushort)metLocation.Value)"
SearchFunc="@SearchMetLocations"
ToStringFunc="@(metLocation => metLocation?.Text)" />
Expand Down Expand Up @@ -105,7 +106,7 @@ AppState.SelectedSlotsAreValid)
<MudAutocomplete T="@ComboItem"
Label="Egg Met Location"
Variant="@Variant.Outlined"
@bind-Value:get="@(AppService.GetMetLocationComboItem(Pokemon.EggLocation))"
@bind-Value:get="@GetEggMetLocation()"
@bind-Value:set="@(metLocation => Pokemon.EggLocation = (ushort)metLocation.Value)"
DebounceInterval="200"
SearchFunc="@SearchEggMetLocations"
Expand Down
120 changes: 103 additions & 17 deletions Pkmds.Web/Components/EditForms/Tabs/MetTab.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,100 @@ public partial class MetTab : IDisposable
[Parameter, EditorRequired]
public PKM? Pokemon { get; set; }

/// <summary>
/// Currently loaded met location group that is populating Met and Egg location comboboxes
/// </summary>
private GameVersion origintrack;

private EntityContext originFormat = EntityContext.None;

private GameVersion currentLocationSearchVersion = GameVersion.Any;

private EntityContext currentLocationSearchContext = EntityContext.None;

protected override void OnInitialized() =>
RefreshService.OnAppStateChanged += StateHasChanged;

public void Dispose() =>
RefreshService.OnAppStateChanged -= StateHasChanged;

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

if (AppState.SaveFile is not { } saveFile)
{
return;
}

CheckMetLocationChange(saveFile.Version, saveFile.Context);
}

private void CheckMetLocationChange(GameVersion version, EntityContext context)
{
if (AppState.SaveFile is not { } saveFile)
{
return;
}

// Does the list of locations need to be changed to another group?
var group = GameUtil.GetMetLocationVersionGroup(version);
if (group is GameVersion.Invalid)
{
group = GameUtil.GetMetLocationVersionGroup(saveFile.Version);
if (group is GameVersion.Invalid || version is GameVersion.Any)
{
version = group = context.GetSingleGameVersion();
}
}
if (group != origintrack || context != originFormat)
{
currentLocationSearchVersion = version;
currentLocationSearchContext = context;
}

origintrack = group;
originFormat = context;
}

private ComboItem GetMetLocation()
{
if (Pokemon is not { } pkm)
{
return new("NONE", -1);
}

CheckMetLocationChange(pkm.Version, pkm.Context);

return AppService.GetMetLocationComboItem(Pokemon.MetLocation, currentLocationSearchVersion, currentLocationSearchContext);
}

private ComboItem GetEggMetLocation()
{
if (Pokemon is not { } pkm)
{
return new("NONE", -1);
}

CheckMetLocationChange(pkm.Version, pkm.Context);
return AppService.GetMetLocationComboItem(Pokemon.EggLocation, currentLocationSearchVersion, currentLocationSearchContext, true);
}

private void OriginGameChanged()
{
if (Pokemon is not { } pkm)
{
return;
}

CheckMetLocationChange(pkm.Version, pkm.Context);
}

private Task<IEnumerable<ComboItem>> SearchMetLocations(string searchString, CancellationToken token) =>
Task.FromResult(AppService.SearchMetLocations(searchString));
Task.FromResult(AppService.SearchMetLocations(searchString, currentLocationSearchVersion, currentLocationSearchContext));

private Task<IEnumerable<ComboItem>> SearchEggMetLocations(string searchString, CancellationToken token) =>
Task.FromResult(AppService.SearchMetLocations(searchString, isEggLocation: true));
Task.FromResult(AppService.SearchMetLocations(searchString, currentLocationSearchVersion, currentLocationSearchContext, isEggLocation: true));

private MetTimeOfDay GetMetTimeOfDay => Pokemon is not (PK2 and ICaughtData2 c2)
? MetTimeOfDay.None
Expand Down Expand Up @@ -48,22 +131,25 @@ private void MetAsEggChanged(bool newValue)
return;
}


if (newValue == false)
{
if (Pokemon.IsEgg)
{
Pokemon.IsEgg = false;
}
Pokemon.EggDay = Pokemon.EggMonth = Pokemon.EggYear = 0;
Pokemon.EggLocation = 0;
}

if (newValue == true)
switch (newValue)
{
var currentMetDate = Pokemon.MetDate;
Pokemon.SetEggMetData(Pokemon.Version, Pokemon.Version);
Pokemon.EggMetDate = Pokemon.MetDate = currentMetDate;
case false:
{
if (Pokemon.IsEgg)
{
Pokemon.IsEgg = false;
}
Pokemon.EggDay = Pokemon.EggMonth = Pokemon.EggYear = 0;
Pokemon.EggLocation = 0;
break;
}
case true:
{
var currentMetDate = Pokemon.MetDate;
Pokemon.SetEggMetData(Pokemon.Version, Pokemon.Version);
Pokemon.EggMetDate = Pokemon.MetDate = currentMetDate;
break;
}
}
}
}
10 changes: 5 additions & 5 deletions Pkmds.Web/Services/AppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void ClearSelection()
RefreshService.Refresh();
}

public string GetPokemonSpeciesName(ushort speciesId) => GetSpeciesComboItem(speciesId)?.Text ?? string.Empty;
public string GetPokemonSpeciesName(ushort speciesId) => GetSpeciesComboItem(speciesId).Text;

public IEnumerable<ComboItem> SearchPokemonNames(string searchString) => AppState.SaveFile is null || searchString is not { Length: > 0 }
? []
Expand Down Expand Up @@ -100,16 +100,16 @@ public void LoadPokemonStats(PKM? pokemon)
pokemon.SetStats(stats);
}

public IEnumerable<ComboItem> SearchMetLocations(string searchString, bool isEggLocation = false) => AppState.SaveFile is null || searchString is not { Length: > 0 }
public IEnumerable<ComboItem> SearchMetLocations(string searchString, GameVersion gameVersion, EntityContext entityContext, bool isEggLocation = false) => AppState.SaveFile is null || searchString is not { Length: > 0 }
? []
: GameInfo.GetLocationList(AppState.SaveFile.Version.GetSingleVersion(), AppState.SaveFile.Context, isEggLocation)
: GameInfo.GetLocationList(gameVersion, entityContext, isEggLocation)
.DistinctBy(l => l.Value)
.Where(metLocation => metLocation.Text.Contains(searchString, StringComparison.OrdinalIgnoreCase))
.OrderBy(metLocation => metLocation.Text);

public ComboItem GetMetLocationComboItem(ushort metLocationId, bool isEggLocation = false) => AppState.SaveFile is null
public ComboItem GetMetLocationComboItem(ushort metLocationId, GameVersion gameVersion, EntityContext entityContext, bool isEggLocation = false) => AppState.SaveFile is null
? default!
: GameInfo.GetLocationList(AppState.SaveFile.Version.GetSingleVersion(), AppState.SaveFile.Context, isEggLocation)
: GameInfo.GetLocationList(gameVersion, entityContext, isEggLocation)
.DistinctBy(l => l.Value)
.FirstOrDefault(metLocation => metLocation.Value == metLocationId) ?? default!;

Expand Down
6 changes: 3 additions & 3 deletions Pkmds.Web/Services/IAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public interface IAppService

IEnumerable<ComboItem> SearchAbilityNames(string searchString);

IEnumerable<ComboItem> SearchMetLocations(string searchString, bool isEggLocation = false);
IEnumerable<ComboItem> SearchMetLocations(string searchString, GameVersion gameVersion, EntityContext entityContext, bool isEggLocation = false);

ComboItem GetMetLocationComboItem(ushort metLocationId, bool isEggLocation = false);
ComboItem GetMetLocationComboItem(ushort metLocationId, GameVersion gameVersion, EntityContext entityContext, bool isEggLocation = false);

IEnumerable<ComboItem> SearchMoves(string searchString);

ComboItem GetMoveComboItem(int moveId);

public void SavePokemon(PKM? SelectedPokemon);
public void SavePokemon(PKM? selectedPokemon);

string GetCleanFileName(PKM pkm);

Expand Down

0 comments on commit d0fc09b

Please sign in to comment.