Files
ZeroSpace/ZeroSpace/ZeroSpace.Web/Pages/Units.razor
T

216 lines
7.7 KiB
Plaintext

@page "/units"
@using ZeroSpace.Model
@using ZeroSpace.Web.Services
@inject IUnitsApiClient UnitsApiClient
<PageTitle>Units</PageTitle>
<div class="section-header d-flex align-items-center mb-4">
<h1 class="mb-0">Units</h1>
<div class="ms-3 flex-grow-1 border-bottom opacity-25"></div>
</div>
@if (isLoading)
{
<div class="d-flex justify-content-center py-5">
<div class="spinner-border text-success" role="status">
<span class="visually-hidden">Loading units...</span>
</div>
</div>
}
else if (errorMessage != null)
{
<div class="alert alert-danger d-flex align-items-center justify-content-between" role="alert">
<div>
<strong>Error:</strong> @errorMessage
</div>
<button class="btn btn-outline-danger btn-sm" @onclick="LoadUnitsAsync">Retry</button>
</div>
}
else if (units == null || units.Count == 0)
{
<div class="alert alert-warning" role="alert">
No units found.
</div>
}
else
{
<div class="table-responsive">
<table class="table table-striped table-hover table-sm align-middle text-nowrap">
<thead class="table-light">
<tr>
@foreach (var col in Columns)
{
<th style="min-width:@col.Width">
<button type="button" class="btn btn-link btn-sm p-0 text-decoration-none text-body fw-bold" @onclick="() => ToggleSort(col.Key)" title="Sort by @col.Title">
@col.Title
<span class="text-muted">@SortGlyph(col.Key)</span>
</button>
</th>
}
</tr>
<tr>
@foreach (var col in Columns)
{
<th>
<input class="form-control form-control-sm" placeholder="Filter" aria-label="Filter @col.Title"
value="@GetFilter(col.Key)"
@oninput="(e) => SetFilter(col.Key, e.Value?.ToString())" />
</th>
}
</tr>
</thead>
<tbody>
@foreach (var unit in PagedUnits)
{
<tr>
@foreach (var col in Columns)
{
<td>@col.Display(unit)</td>
}
</tr>
}
</tbody>
</table>
</div>
<div class="d-flex align-items-center gap-2 mt-2">
<button type="button" class="btn btn-outline-secondary btn-sm" disabled="@(page <= 1)" @onclick="() => ChangePage(page - 1)">Previous</button>
<span class="text-muted">Page @(page) of @TotalPages (@SortedUnits.Count units)</span>
<button type="button" class="btn btn-outline-secondary btn-sm" disabled="@(page >= TotalPages)" @onclick="() => ChangePage(page + 1)">Next</button>
</div>
}
@code {
private List<UnitData>? units;
private bool isLoading = true;
private string? errorMessage;
private string? sortKey;
private bool sortAscending = true;
private int page = 1;
private const int PageSize = 50;
private readonly Dictionary<string, string> filters = new();
private sealed record ColumnDef(
string Key,
string Title,
string Width,
Func<UnitData, IComparable?> SortValue,
Func<UnitData, string> Display);
private static readonly IReadOnlyList<ColumnDef> Columns = new List<ColumnDef>
{
new("Name", "Name", "200px", u => u.Name, u => u.Name),
new("Hexite", "Hexite", "90px", u => u.Hexite, u => u.Hexite.ToString()),
new("Flux", "Flux", "90px", u => u.Flux, u => u.Flux.ToString()),
new("DpsPerTotalCost", "DpsPerTotalCost", "90px", u => u.DpsPerTotalCost, u => u.DpsPerTotalCost.ToString()),
new("HealthPerTotalCost", "HealthPerTotalCost", "90px", u => u.HealthPerTotalCost, u => u.HealthPerTotalCost.ToString()),
new("Supply", "Supply", "90px", u => u.Supply, u => u.Supply.ToString()),
new("ProductionTime", "Production Time", "90px", u => u.ProductionTime, u => u.ProductionTime.ToString()),
new("Health", "Health", "90px", u => u.Health, u => u.Health.ToString()),
new("Shields", "Shields", "90px", u => u.Shields, u => u.Shields.ToString()),
new("ArmorRating", "Armor Rating", "90px", u => u.ArmorRating, u => u.ArmorRating.ToString()),
new("MovementSpeed", "Movement Speed", "90px", u => u.MovementSpeed, u => u.MovementSpeed.ToString()),
new("DamagePerSecond", "Damage Per Second", "90px", u => u.DamagePerSecond, u => u.DamagePerSecond.ToString()),
new("AttackRange", "Attack Range", "90px", u => u.AttackRange, u => u.AttackRange.ToString()),
new("Attributes", "Attributes", "90px", u => string.Join(", ", u.Attributes), u => string.Join(", ", u.Attributes)),
new("Tier", "Tier", "90px", u => u.Tier, u => u.Tier.ToString()),
new("Faction", "Faction", "90px", u => u.Faction, u => u.Faction),
new("Hotkey", "Hotkey", "90px", u => u.Hotkey, u => u.Hotkey ?? string.Empty),
new("BuildAtSameTime", "Build At Same Time", "90px", u => u.BuildAtSameTime, u => u.BuildAtSameTime.ToString()),
new("Limit", "Limit", "90px", u => u.Limit, u => u.Limit?.ToString() ?? string.Empty),
};
private List<UnitData> SortedUnits
{
get
{
var query = (units ?? new List<UnitData>()).AsEnumerable();
foreach (var col in Columns)
{
if (filters.TryGetValue(col.Key, out var text) && !string.IsNullOrWhiteSpace(text))
{
query = query.Where(u => col.Display(u).Contains(text, StringComparison.OrdinalIgnoreCase));
}
}
var sortCol = Columns.FirstOrDefault(c => c.Key == sortKey);
if (sortCol != null)
{
query = sortAscending
? query.OrderBy(u => sortCol.SortValue(u))
: query.OrderByDescending(u => sortCol.SortValue(u));
}
return query.ToList();
}
}
private int TotalPages => Math.Max(1, (int)Math.Ceiling(SortedUnits.Count / (double)PageSize));
private List<UnitData> PagedUnits => SortedUnits.Skip((page - 1) * PageSize).Take(PageSize).ToList();
private void ToggleSort(string key)
{
if (sortKey == key)
{
sortAscending = !sortAscending;
}
else
{
sortKey = key;
sortAscending = true;
}
page = 1;
}
private string SortGlyph(string key) => sortKey == key ? (sortAscending ? "▲" : "▼") : string.Empty;
private string GetFilter(string key) => filters.TryGetValue(key, out var value) ? value : string.Empty;
private void SetFilter(string key, string? value)
{
if (string.IsNullOrEmpty(value))
{
filters.Remove(key);
}
else
{
filters[key] = value;
}
page = 1;
}
private void ChangePage(int newPage)
{
page = Math.Clamp(newPage, 1, TotalPages);
}
protected override async Task OnInitializedAsync()
{
await LoadUnitsAsync();
}
private async Task LoadUnitsAsync()
{
isLoading = true;
errorMessage = null;
try
{
var result = await UnitsApiClient.GetUnitsAsync();
units = result.ToList();
}
catch (Exception ex)
{
errorMessage = $"Failed to load units: {ex.Message}";
}
finally
{
isLoading = false;
}
}
}