129 lines
3.4 KiB
C#
129 lines
3.4 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Text.Json;
|
|
|
|
namespace GameServices.Admin.Pages.DungeonBuilder.Stamps;
|
|
|
|
public class EditModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly IConfiguration _config;
|
|
|
|
public EditModel(GameDbContext db, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_config = config;
|
|
}
|
|
|
|
[BindProperty]
|
|
public TileStamp Stamp { get; set; } = new();
|
|
|
|
public List<TileMapSheet> TileSheets { get; set; } = new();
|
|
public List<TileDefinition> TileDefinitions { get; set; } = new();
|
|
|
|
public string TileDefinitionsJson => JsonSerializer.Serialize(
|
|
TileDefinitions.Select(t => new
|
|
{
|
|
id = t.Id,
|
|
sheetId = t.TileMapSheetId,
|
|
frameIndex = t.FrameIndex,
|
|
name = t.Name
|
|
})
|
|
);
|
|
|
|
public string CdnBaseUrl => NormalizeCdnBaseUrl(_config["Cdn:BaseUrl"]);
|
|
|
|
public string? Message { get; set; }
|
|
public bool IsError { get; set; }
|
|
|
|
public async Task OnGetAsync(int? id)
|
|
{
|
|
await LoadReferenceData();
|
|
|
|
if (id.HasValue)
|
|
{
|
|
var stamp = await _db.TileStamps.FindAsync(id.Value);
|
|
if (stamp != null)
|
|
{
|
|
Stamp = stamp;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Stamp.IsEnabled = true;
|
|
Stamp.Width = 1;
|
|
Stamp.Height = 1;
|
|
}
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
await LoadReferenceData();
|
|
|
|
if (string.IsNullOrWhiteSpace(Stamp.Name))
|
|
{
|
|
IsError = true;
|
|
Message = "Name is required.";
|
|
return Page();
|
|
}
|
|
|
|
Stamp.UpdatedAt = DateTime.UtcNow;
|
|
|
|
if (Stamp.Id == 0)
|
|
{
|
|
Stamp.CreatedAt = DateTime.UtcNow;
|
|
_db.TileStamps.Add(Stamp);
|
|
Message = "Stamp created!";
|
|
}
|
|
else
|
|
{
|
|
var existing = await _db.TileStamps.FindAsync(Stamp.Id);
|
|
if (existing == null)
|
|
{
|
|
IsError = true;
|
|
Message = "Stamp not found.";
|
|
return Page();
|
|
}
|
|
|
|
existing.Name = Stamp.Name;
|
|
existing.Description = Stamp.Description;
|
|
existing.Width = Stamp.Width;
|
|
existing.Height = Stamp.Height;
|
|
existing.TilesJson = Stamp.TilesJson;
|
|
existing.IsEnabled = Stamp.IsEnabled;
|
|
existing.UpdatedAt = DateTime.UtcNow;
|
|
}
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
return RedirectToPage(new { id = Stamp.Id });
|
|
}
|
|
|
|
private async Task LoadReferenceData()
|
|
{
|
|
TileSheets = await _db.TileMapSheets
|
|
.Where(s => s.IsEnabled)
|
|
.OrderBy(s => s.Name)
|
|
.ToListAsync();
|
|
|
|
TileDefinitions = await _db.TileDefinitions
|
|
.Where(t => t.IsEnabled)
|
|
.OrderBy(t => t.TileMapSheetId)
|
|
.ThenBy(t => t.FrameIndex)
|
|
.ToListAsync();
|
|
}
|
|
|
|
private string NormalizeCdnBaseUrl(string? baseUrl)
|
|
{
|
|
baseUrl = string.IsNullOrWhiteSpace(baseUrl)
|
|
? "https://assets.aeternalisgames.com"
|
|
: baseUrl;
|
|
baseUrl = baseUrl.TrimEnd('/');
|
|
if (!baseUrl.EndsWith("/downloads", StringComparison.OrdinalIgnoreCase))
|
|
baseUrl += "/downloads";
|
|
return baseUrl;
|
|
}
|
|
}
|