Skip to content

Commit

Permalink
Add Gen III records
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey85 committed Nov 25, 2024
1 parent b447eb8 commit b014cb3
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 13 deletions.
58 changes: 58 additions & 0 deletions Pkmds.Web/Components/MainTabPages/RecordsTab.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@inherits BasePkmdsComponent

@if (SaveFile is not null)
{
<MudSelect Label="Record"
@bind-Value="CurrentRecordIndex"
@bind-Value:after="GetRecord"
Variant="Variant.Filled">
@foreach (var (index, record) in RecordComboItems.Index())
{
var localIndex = index;
<MudSelectItem Value="@localIndex"
@key="@localIndex">
@record.Text
</MudSelectItem>
}
</MudSelect>

<MudNumericField Label="Record Value"
Variant="@Variant.Outlined"
Min="@uint.MinValue"
Max="@uint.MaxValue"
@bind-Value:get="@CurrentRecordValue"
@bind-Value:set="@SetCurrentRecordValue" />

@if (HallOfFameIndexSelected)
{
<div class="form-grid">
<div class="form-field">
<MudNumericField Label="Hours"
T="@uint"
Variant="@Variant.Outlined"
Min="0"
Max="9999"
@bind-Value="@HallOfFameHours"
@bind-Value:after="@ChangeFame" />
</div>
<div class="form-field">
<MudNumericField Label="Minutes"
T="@byte"
Variant="@Variant.Outlined"
Min="0"
Max="59"
@bind-Value="@HallOfFameMinutes"
@bind-Value:after="@ChangeFame" />
</div>
<div class="form-field">
<MudNumericField Label="Seconds"
T="@byte"
Variant="@Variant.Outlined"
Min="0"
Max="59"
@bind-Value="@HallOfFameSeconds"
@bind-Value:after="@ChangeFame" />
</div>
</div>
}
}
112 changes: 112 additions & 0 deletions Pkmds.Web/Components/MainTabPages/RecordsTab.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
namespace Pkmds.Web.Components.MainTabPages;

public partial class RecordsTab
{
[Parameter, EditorRequired]
public SAV3? SaveFile { get; set; }

private int CurrentRecordIndex { get; set; }

private uint? CurrentRecordValue { get; set; }

private Record3? Records { get; set; }

private IList<ComboItem> RecordComboItems { get; set; } = [];

private uint HallOfFameHours { get; set; }

private byte HallOfFameMinutes { get; set; }

private byte HallOfFameSeconds { get; set; }

private bool HallOfFameIndexSelected => CurrentRecordIndex switch
{
(int)RecID3RuSa.FIRST_HOF_PLAY_TIME or (int)RecID3Emerald.FIRST_HOF_PLAY_TIME or (int)RecID3FRLG.FIRST_HOF_PLAY_TIME => true,
_ => false,
};

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

private void LoadRecords()
{
if (SaveFile is null)
{
return;
}

Records = new Record3(SaveFile);
RecordComboItems = Record3.GetItems(SaveFile);
GetRecord();
}

private void GetRecord()
{
if (SaveFile is null || Records is null)
{
return;
}

CurrentRecordValue = Records.GetRecord(CurrentRecordIndex);

if (HallOfFameIndexSelected)
{
SetFameTime(CurrentRecordValue ?? 0U);
}
}

private void SetCurrentRecordValue(uint? newValue)
{
if (SaveFile is null || Records is null)
{
return;
}

CurrentRecordValue = newValue;
Records.SetRecord(CurrentRecordIndex, newValue ?? 0U);

if (HallOfFameIndexSelected)
{
SetFameTime(newValue ?? 0U);
}
}

void ChangeFame()
{
if (!HallOfFameIndexSelected || Records is null)
{
return;
}

Records.SetRecord(1, (uint)(CurrentRecordValue = GetFameTime()));
}

public uint GetFameTime()
{
if (!HallOfFameIndexSelected || Records is null)
{
return 0U;
}

var hrs = Math.Min(9999U, HallOfFameHours);
var min = Math.Min((byte)59, HallOfFameMinutes);
var sec = Math.Min((byte)59, HallOfFameSeconds);

return (uint)((hrs << 16) | (min << 8) | sec);

Check warning on line 98 in Pkmds.Web/Components/MainTabPages/RecordsTab.razor.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first
}

public void SetFameTime(uint time)
{
if (!HallOfFameIndexSelected || Records is null)
{
return;
}

HallOfFameHours = Math.Min(9999U, time >> 16);
HallOfFameMinutes = Math.Min((byte)59, (byte)(time >> 8));
HallOfFameSeconds = Math.Min((byte)59, (byte)time);
}
}
13 changes: 0 additions & 13 deletions Pkmds.Web/Components/MainTabPages/TrainerInfoTab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,3 @@
</div>

<BadgesComponent />

<style>
.form-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
padding: 16px;
}
.form-field {
margin-bottom: 8px;
}
</style>
7 changes: 7 additions & 0 deletions Pkmds.Web/Components/SaveFileComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,12 @@ else
</MudTabPanel>
}

@if (saveFile.Generation == 3 && saveFile is SAV3 sav3)
{
<MudTabPanel Text="Records">
<RecordsTab SaveFile="@sav3" />
</MudTabPanel>
}

</MudTabs>
}
11 changes: 11 additions & 0 deletions Pkmds.Web/wwwroot/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ item-sprite {
font-weight: bold;
}

.form-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
padding: 16px;
}

.form-field {
margin-bottom: 8px;
}

.loading-progress {
position: relative;
display: block;
Expand Down

0 comments on commit b014cb3

Please sign in to comment.