Skip to content

Commit

Permalink
Add initial Mystery Gift Database support
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Nov 22, 2024
1 parent 3bfa3f5 commit 9bc5224
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Pkmds.Web/Components/MainTabPages/MysteryGiftDatabaseTab.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@inherits BasePkmdsComponent

@if (AppState is { SaveFile: { } saveFile } && MysteryGiftsList.Count > 0)
{
<MudPagination @bind-Selected="@CurrentPage"
@bind-Selected:after="@GoToPage"
Count="@TotalPages"
ShowNextButton />

<MudSelect T="int"
@bind-Value="PageSize"
@bind-Value:after="OnPageSizeChange"
Label="Items per page">
<MudSelectItem Value="10">10</MudSelectItem>
<MudSelectItem Value="20">20</MudSelectItem>
<MudSelectItem Value="50">50</MudSelectItem>
</MudSelect>

<div class="overflow-scroll"
style="height: calc(100vh - 240px);">
<MudGrid Spacing="1">
@foreach (var mysteryGift in PaginatedItems)
{
<MudItem xs="12"
sm="6"
md="4"
lg="3"
xl="2"
xxl="2"
Class="@ColumnClass">
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<div>
<object class="pkm-sprite"
type="image/png"
style="object-fit: contain;"
data="@SpriteHelper.GetMysteryGiftSpriteFileName(mysteryGift)"
title="@mysteryGift.Name">
<img class="pkm-sprite"
src="@(mysteryGift.IsItem ? SpriteHelper.ItemFallbackImageFileName : SpriteHelper.PokemonFallbackImageFileName)"
title="">
</object>
</div>
<MudText>
@mysteryGift.CardHeader
</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
@((MarkupString)RenderListAsHtml(mysteryGift.GetTextLines()))
</MudCardContent>
<MudCardActions>
<MudButton OnClick="@(() => OnClickCopy(mysteryGift))"
ButtonType="@ButtonType.Button"
Variant="@Variant.Filled"
StartIcon="@Icons.Material.Filled.ContentCopy"
Color="@Color.Default"
Size="@Size.Small"
title="Copy Pokémon"
Disabled="@(mysteryGift.Species == (ushort)Species.None)">
Copy
</MudButton>
</MudCardActions>
</MudCard>
</MudItem>
}
</MudGrid>
</div>
}
106 changes: 106 additions & 0 deletions Pkmds.Web/Components/MainTabPages/MysteryGiftDatabaseTab.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
namespace Pkmds.Web.Components.MainTabPages;

public partial class MysteryGiftDatabaseTab
{
[Parameter]
public bool FilterUnavailableSpecies { get; set; } = true;

private List<MysteryGift> MysteryGiftsList = [];

private List<MysteryGift> PaginatedItems = [];
private int CurrentPage = 1;
private int PageSize = 20; // Number of items per page

private int TotalPages => (int)Math.Ceiling((double)MysteryGiftsList.Count / PageSize);

protected override void OnInitialized()
{
base.OnInitialized();
LoadData();
}

private void LoadData()
{
if (AppState is not { SaveFile: { } saveFile })
{
return;
}

var encounterDatabase = EncounterEvent.GetAllEvents();

if (FilterUnavailableSpecies)
{
encounterDatabase = saveFile switch
{
SAV9SV s9 => encounterDatabase.Where(IsPresent(s9.Personal)),
SAV8SWSH s8 => encounterDatabase.Where(IsPresent(s8.Personal)),
SAV8BS b8 => encounterDatabase.Where(IsPresent(b8.Personal)),
SAV8LA a8 => encounterDatabase.Where(IsPresent(a8.Personal)),
SAV7b => encounterDatabase.Where(z => z is WB7),
SAV7 => encounterDatabase.Where(z => z.Generation < 7 || z is WC7),
_ => encounterDatabase.Where(z => z.Generation <= saveFile.Generation),
};
}

MysteryGiftsList = [.. encounterDatabase];

foreach (var mysteryGift in MysteryGiftsList)
{
mysteryGift.GiftUsed = false;
}

UpdatePaginatedItems();

static Func<MysteryGift, bool> IsPresent<TTable>(TTable pt) where TTable : IPersonalTable => z => pt.IsPresentInGame(z.Species, z.Form);
}

private void UpdatePaginatedItems() => PaginatedItems = MysteryGiftsList
.Skip((CurrentPage - 1) * PageSize)
.Take(PageSize)
.ToList();

private void GoToPage() => UpdatePaginatedItems();

private void OnPageSizeChange()
{
CurrentPage = 1; // Reset to the first page
UpdatePaginatedItems();
}

private async Task OnClickCopy(MysteryGift gift)
{
if (gift.Species == (ushort)Species.None || AppState is not { SaveFile: { } saveFile })
{
return;
}

var temp = gift.ConvertToPKM(saveFile);
var pokemon = EntityConverter.ConvertToType(temp, saveFile.PKMType, out var c);

if (!c.IsSuccess() || pokemon is null)
{
await DialogService.ShowMessageBox("Error", c.GetDisplayString(temp, saveFile.PKMType));
return;
}

saveFile.AdaptPKM(pokemon);
AppState.CopiedPokemon = pokemon.Clone();

Snackbar.Add("The selected Pokémon has been copied.");
}

private static string RenderListAsHtml(IReadOnlyList<string> items, string tag = "p")
{
if (items == null || items.Count == 0)
{
return string.Empty;
}

var builder = new StringBuilder();
foreach (var item in items)
{
builder.AppendFormat("<{0}>{1}</{0}>", tag, WebUtility.HtmlEncode(item));
}
return builder.ToString();
}
}
6 changes: 6 additions & 0 deletions Pkmds.Web/Components/SaveFileComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ else
</div>
</MudTabPanel>

<MudTabPanel Text="Mystery Gifts">
<div class="mt-3">
<MysteryGiftDatabaseTab />
</div>
</MudTabPanel>

</MudTabs>
}
1 change: 1 addition & 0 deletions Pkmds.Web/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
global using System.Collections.ObjectModel;
global using System.Globalization;
global using System.Linq.Expressions;
global using System.Net;
global using System.Reflection;
global using System.Security.Cryptography;
global using System.Text;
Expand Down
4 changes: 4 additions & 0 deletions Pkmds.Web/SpriteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public static class SpriteHelper
public const string ItemFallbackImageFileName = $"{SpritesRoot}bi/bitem_unk.png";
public const string PokemonFallbackImageFileName = $"{SpritesRoot}a/a_unknown.png";

public static string GetMysteryGiftSpriteFileName(MysteryGift gift) => gift.IsItem
? GetItemSpriteFilename(gift.ItemID, gift.Context)
: GetPokemonSpriteFilename(gift.Species, gift.Context, gift.IsEgg, gift.Form, 0, gift.Gender);

public static string GetPokemonSpriteFilename(PKM? pokemon) => pokemon is null
? PokemonFallbackImageFileName
: GetPokemonSpriteFilename(pokemon.Species, pokemon.Context, pokemon.IsEgg, pokemon.Form, pokemon.GetFormArgument(0), pokemon.Gender);
Expand Down

0 comments on commit 9bc5224

Please sign in to comment.