1
0
mirror of synced 2024-12-29 23:03:06 +01:00
TaikoLocalServer/TaikoWebUI/Components/Song/SongLeaderboardCard.razor.cs

124 lines
3.9 KiB
C#
Raw Normal View History

2024-06-03 01:55:44 +02:00

using Microsoft.Extensions.Options;
using TaikoWebUI.Settings;
2024-06-03 01:55:44 +02:00
namespace TaikoWebUI.Components.Song;
public partial class SongLeaderboardCard
{
[Inject]
IOptions<WebUiSettings> UiSettings { get; set; } = default!;
2024-06-03 01:55:44 +02:00
[Parameter]
public int SongId { get; set; }
[Parameter]
public int Baid { get; set; }
[Parameter]
2024-06-03 17:27:18 +02:00
public Difficulty Difficulty { get; set; } = Difficulty.None;
private SongLeaderboardResponse? response = null;
private List<SongLeaderboard> LeaderboardScores { get; set; } = new();
private int TotalRows { get; set; } = 0;
2024-06-03 17:27:18 +02:00
private string SelectedDifficulty { get; set; } = "None";
private bool isPaginationEnabled = true;
2024-06-05 04:33:52 +02:00
private int TotalPages { get; set; } = 0;
2024-06-03 17:03:09 +02:00
private bool isLoading = true;
2024-06-05 04:33:52 +02:00
private int currentPage = 1;
private int pageSize = 10;
protected override void OnInitialized()
{
base.OnInitialized();
if (UiSettings.Value.SongLeaderboardSettings.DisablePagination)
{
isPaginationEnabled = false;
}
if (UiSettings.Value.SongLeaderboardSettings.PageSize > 200 |
UiSettings.Value.SongLeaderboardSettings.PageSize <= 0)
{
Console.WriteLine("Invalid LeaderboardSettings.PageSize value in appsettings.json. The value must be between 1 and 200. Defaulting to 10.");
}
if (UiSettings.Value.SongLeaderboardSettings.PageSize > 0 & UiSettings.Value.SongLeaderboardSettings.PageSize <= 200)
{
pageSize = UiSettings.Value.SongLeaderboardSettings.PageSize;
}
}
2024-06-05 04:33:52 +02:00
private async Task GetLeaderboardData()
{
isLoading = true;
response = await Client.GetFromJsonAsync<SongLeaderboardResponse>($"api/SongLeaderboard/{(uint)SongId}?baid={(uint)Baid}&difficulty={(uint)Difficulty}&page={currentPage}&limit={pageSize}");
response.ThrowIfNull();
LeaderboardScores.Clear();
LeaderboardScores.AddRange(response.LeaderboardData);
// set TotalPages
2024-06-05 04:33:52 +02:00
TotalPages = response.TotalPages;
2024-06-10 05:12:03 +02:00
if (response.UserScore != null
&& LeaderboardScores.All(x => x.Baid != response.UserScore.Baid)
&& (LeaderboardScores.Count == 0 || response.UserScore.Rank >= LeaderboardScores[0].Rank))
{
2024-06-10 05:12:03 +02:00
LeaderboardScores.Add(new SongLeaderboard()); // Add an empty row
LeaderboardScores.Add(response.UserScore);
}
2024-06-10 05:12:03 +02:00
TotalRows = LeaderboardScores.Count;
2024-06-05 04:33:52 +02:00
isLoading = false;
}
2024-06-03 17:03:09 +02:00
2024-06-03 01:55:44 +02:00
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
2024-06-03 17:27:18 +02:00
// get highScoresTab from LocalStorage
var songPageDifficulty = await LocalStorage.GetItemAsync<string>("songPageDifficulty");
if (songPageDifficulty != null)
{
SelectedDifficulty = songPageDifficulty;
Difficulty = Enum.Parse<Difficulty>(SelectedDifficulty);
}
else
{
// set default difficulty to Easy
SelectedDifficulty = Difficulty.Easy.ToString();
Difficulty = Difficulty.Easy;
}
2024-06-05 04:33:52 +02:00
await GetLeaderboardData();
2024-06-03 17:03:09 +02:00
isLoading = false;
2024-06-03 03:00:54 +02:00
}
2024-06-05 05:44:09 +02:00
private async Task OnDifficultyChange(string difficulty = "None")
2024-06-03 03:00:54 +02:00
{
2024-06-03 17:03:09 +02:00
isLoading = true;
2024-06-03 03:00:54 +02:00
SelectedDifficulty = difficulty;
Difficulty = Enum.Parse<Difficulty>(SelectedDifficulty);
2024-06-03 17:27:18 +02:00
await LocalStorage.SetItemAsync("songPageDifficulty", SelectedDifficulty);
2024-06-05 04:33:52 +02:00
await GetLeaderboardData();
2024-06-03 17:27:18 +02:00
2024-06-05 04:33:52 +02:00
currentPage = 1;
2024-06-03 17:03:09 +02:00
isLoading = false;
2024-06-03 01:55:44 +02:00
}
2024-06-05 04:33:52 +02:00
private async Task OnPageChange(int page)
{
currentPage = page;
await GetLeaderboardData();
}
2024-06-05 01:53:41 +02:00
private string GetActiveRowClass(SongLeaderboard leaderboard, int index)
{
return leaderboard.Baid == Baid ? "is-current-user" : "";
}
2024-06-03 01:55:44 +02:00
}