ui/Panels/LeaderboardPanel.razor
@using Sandbox;
@using Sandbox.UI;
@namespace SS1
@inherits Panel
@attribute [StyleSheet]
<root>
@if (Leaderboard is null) return;
<label class="header">@LabelText</label>
@if(Leaderboard.Entries.Count() <= 0) return;
<div class="entries">
@{
var offset = 0;
}
@foreach (var entry in Leaderboard.Entries)
{
@if(!IsSurvival && entry.Value <= (10 * 60 - 115))
{
offset++;
continue;
}
<div class="entry" style="color:@(Color.Lerp(Color.White, new Color(0.3f, 0.3f, 0.3f), Utils.Map(entry.Rank - offset, 0, 40, 0f, 1f, EasingType.QuadOut)).Rgba);">
<label class="rank">##@(entry.Rank - offset)</label>
<label class="name">@(entry.DisplayName.Length > 30 ? $"{entry.DisplayName.Substring(0, 30)}..." : entry.DisplayName)</label>
<label class="value">@FormatTime(entry.Value)</label>
</div>
}
</div>
</root>
@code
{
public int Difficulty { get; set; }
private int _lastDifficulty;
public string LabelText { get; set; }
public bool IsSurvival { get; set; }
Sandbox.Services.Leaderboards.Board2 Leaderboard;
protected override void OnAfterTreeRender(bool firstTime)
{
base.OnAfterTreeRender(firstTime);
if(firstTime || Difficulty != _lastDifficulty)
{
// if(IsSurvival)
// {
// Leaderboard = Sandbox.Services.Leaderboards.GetFromStat(Manager.GetStatName(Difficulty, survival: true));
// Leaderboard.SetAggregationMax();
// Leaderboard.SetSortDescending();
// }
// else
// {
Leaderboard = Sandbox.Services.Leaderboards.GetFromStat(Manager.GetStatName(Difficulty));
Leaderboard.SetAggregationMin();
Leaderboard.SetSortAscending();
// }
// Leaderboard = Sandbox.Services.Leaderboards.Get(LeaderboardName);
Refresh();
_lastDifficulty = Difficulty;
// _lastLeaderboardName = LeaderboardName;
}
}
async void Refresh()
{
Leaderboard.MaxEntries = 150;
await Leaderboard.Refresh();
StateHasChanged();
}
// protected override int BuildHash()
// {
// return HashCode.Combine(
// LeaderboardName
// );
// }
public static string FormatTime(double seconds)
{
// Convert the seconds to minutes and remaining seconds
int minutes = (int)seconds / 60;
int remainingSeconds = (int)seconds % 60;
// Format the output as "MM:SS" with leading zeros if needed
return $"{minutes}:{remainingSeconds:D2}";
}
}