Files
systemrevivalserver/Services/Admin/Pages/DungeonBuilder/Tiles.cshtml.cs
T
2026-01-10 11:20:06 -05:00

197 lines
6.4 KiB
C#

using DataServer.Data;
using DataServer.Data.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
namespace Server.Services.Admin.Pages.DungeonBuilder;
public class TilesModel : AdminPageModel
{
private readonly GameDbContext _db;
private readonly IConfiguration _config;
public TilesModel(GameDbContext db, IConfiguration config)
{
_db = db;
_config = config;
}
public List<TileTemplate> Tiles { get; set; } = new();
public List<string> Biomes { get; set; } = new();
public List<TileMapSheet> TileSheets { get; set; } = new();
// Pagination
public int Page { get; set; } = 1;
public int PageSize { get; set; } = 24;
public int TotalCount { get; set; }
public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);
// Filters
public string? Search { get; set; }
public int? CategoryFilter { get; set; }
public string? BiomeFilter { get; set; }
public int? SheetFilter { get; set; }
public string? Message { get; set; }
public bool IsError { get; set; }
// Cache for sheet lookups
private Dictionary<int, TileMapSheet> _sheetCache = new();
public async Task OnGetAsync(string? search, int? category, string? biome, int? sheetId, int page = 1)
{
Search = search;
CategoryFilter = category;
BiomeFilter = biome;
SheetFilter = sheetId;
Page = Math.Max(1, page);
// Load tile sheets for filter dropdown and display
TileSheets = await _db.TileMapSheets
.Where(s => s.IsEnabled)
.OrderBy(s => s.Name)
.ToListAsync();
_sheetCache = TileSheets.ToDictionary(s => s.Id);
// Get unique biomes
Biomes = await _db.TileTemplates
.Where(t => t.Biome != null)
.Select(t => t.Biome!)
.Distinct()
.OrderBy(b => b)
.ToListAsync();
// Build query
var query = _db.TileTemplates.AsQueryable();
if (!string.IsNullOrEmpty(search))
{
query = query.Where(t => t.Name.Contains(search) || (t.Tags != null && t.Tags.Contains(search)));
}
if (category.HasValue)
{
query = query.Where(t => t.Category == (TileTemplateCategory)category.Value);
}
if (!string.IsNullOrEmpty(biome))
{
query = query.Where(t => t.Biome == biome);
}
if (sheetId.HasValue)
{
// Filter by sheet - tiles that use this sheet in TilesJson
var sheetIdStr = $"\"sheetId\":{sheetId.Value}";
var sheetIdStr2 = $"\"sheetId\": {sheetId.Value}";
query = query.Where(t => t.TilesJson != null &&
(t.TilesJson.Contains(sheetIdStr) || t.TilesJson.Contains(sheetIdStr2)));
}
// Get total count for pagination
TotalCount = await query.CountAsync();
// Get page of results
Tiles = await query
.OrderBy(t => t.Category)
.ThenBy(t => t.Name)
.Skip((Page - 1) * PageSize)
.Take(PageSize)
.ToListAsync();
}
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var tile = await _db.TileTemplates.FindAsync(id);
if (tile != null)
{
_db.TileTemplates.Remove(tile);
await _db.SaveChangesAsync();
Message = $"Deleted tile template: {tile.Name}";
}
return RedirectToPage();
}
public string GetCdnUrl(string? path)
{
if (string.IsNullOrEmpty(path)) return "";
var baseUrl = _config["Cdn:BaseUrl"]?.TrimEnd('/') ?? "https://assets.aeternalisgames.com";
if (!baseUrl.EndsWith("/downloads", StringComparison.OrdinalIgnoreCase))
{
baseUrl += "/downloads";
}
return $"{baseUrl}/{path.TrimStart('/')}";
}
/// <summary>
/// Get display info for a tile template by parsing its TilesJson
/// </summary>
public TileDisplayInfo? GetTileDisplayInfo(TileTemplate tile)
{
if (string.IsNullOrEmpty(tile.TilesJson)) return null;
try
{
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var tiles = JsonSerializer.Deserialize<List<TilePlacement>>(tile.TilesJson, options);
var first = tiles?.FirstOrDefault();
if (first == null || !_sheetCache.TryGetValue(first.SheetId, out var sheet))
return null;
var cols = sheet.Columns > 0 ? sheet.Columns : (sheet.TileWidth > 0 ? sheet.SheetWidth / sheet.TileWidth : 1);
var col = first.FrameIndex % cols;
var row = first.FrameIndex / cols;
return new TileDisplayInfo
{
ImageUrl = GetCdnUrl(sheet.CdnPath),
TileWidth = sheet.TileWidth,
TileHeight = sheet.TileHeight,
SheetWidth = sheet.SheetWidth,
SheetHeight = sheet.SheetHeight,
BgX = -col * sheet.TileWidth,
BgY = -row * sheet.TileHeight
};
}
catch
{
return null;
}
}
/// <summary>
/// Build URL for pagination with current filters preserved
/// </summary>
public string GetPageUrl(int page)
{
var query = new List<string> { $"page={page}" };
if (!string.IsNullOrEmpty(Search)) query.Add($"search={Uri.EscapeDataString(Search)}");
if (CategoryFilter.HasValue) query.Add($"category={CategoryFilter}");
if (!string.IsNullOrEmpty(BiomeFilter)) query.Add($"biome={Uri.EscapeDataString(BiomeFilter)}");
if (SheetFilter.HasValue) query.Add($"sheetId={SheetFilter}");
return $"/admin/dungeonbuilder/tiles?{string.Join("&", query)}";
}
private class TilePlacement
{
public int SheetId { get; set; }
public int FrameIndex { get; set; }
}
}
public class TileDisplayInfo
{
public string ImageUrl { get; set; } = "";
public int TileWidth { get; set; }
public int TileHeight { get; set; }
public int SheetWidth { get; set; }
public int SheetHeight { get; set; }
public int BgX { get; set; }
public int BgY { get; set; }
}