-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial Mystery Gift Database support
- Loading branch information
1 parent
3bfa3f5
commit 9bc5224
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
Pkmds.Web/Components/MainTabPages/MysteryGiftDatabaseTab.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
Pkmds.Web/Components/MainTabPages/MysteryGiftDatabaseTab.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters