147 lines
4.4 KiB
C#
147 lines
4.4 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Server.Services.Admin.Pages.DungeonBuilder;
|
|
|
|
public class TileEditModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly IConfiguration _config;
|
|
|
|
public TileEditModel(GameDbContext db, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_config = config;
|
|
}
|
|
|
|
[BindProperty]
|
|
public TileTemplate Tile { get; set; } = new();
|
|
|
|
public List<TileMapSheet> TileSheets { get; set; } = new();
|
|
|
|
public string CdnBaseUrl => NormalizeCdnBaseUrl(_config["Cdn:BaseUrl"]);
|
|
|
|
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;
|
|
}
|
|
|
|
public string? Message { get; set; }
|
|
public bool IsError { get; set; }
|
|
|
|
public async Task OnGetAsync(int? id)
|
|
{
|
|
await LoadReferenceData();
|
|
|
|
if (id.HasValue)
|
|
{
|
|
var tile = await _db.TileTemplates
|
|
.Include(t => t.PrimaryTileDefinition)
|
|
.ThenInclude(td => td!.TileMapSheet)
|
|
.FirstOrDefaultAsync(t => t.Id == id.Value);
|
|
|
|
if (tile != null)
|
|
{
|
|
Tile = tile;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Defaults
|
|
Tile.IsEnabled = true;
|
|
Tile.Width = 1;
|
|
Tile.Height = 1;
|
|
Tile.Weight = 100;
|
|
Tile.MinTier = 1;
|
|
Tile.IsWalkable = true;
|
|
Tile.SocketN = SocketType.Open;
|
|
Tile.SocketE = SocketType.Open;
|
|
Tile.SocketS = SocketType.Open;
|
|
Tile.SocketW = SocketType.Open;
|
|
}
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
await LoadReferenceData();
|
|
|
|
if (string.IsNullOrWhiteSpace(Tile.Name))
|
|
{
|
|
IsError = true;
|
|
Message = "Name is required.";
|
|
return Page();
|
|
}
|
|
|
|
Tile.UpdatedAt = DateTime.UtcNow;
|
|
|
|
// Handle nullable primary tile
|
|
if (string.IsNullOrEmpty(Request.Form["Tile.PrimaryTileDefinitionId"]))
|
|
{
|
|
Tile.PrimaryTileDefinitionId = null;
|
|
}
|
|
|
|
if (Tile.Id == 0)
|
|
{
|
|
Tile.CreatedAt = DateTime.UtcNow;
|
|
_db.TileTemplates.Add(Tile);
|
|
Message = "Tile template created!";
|
|
}
|
|
else
|
|
{
|
|
var existing = await _db.TileTemplates.FindAsync(Tile.Id);
|
|
if (existing == null)
|
|
{
|
|
IsError = true;
|
|
Message = "Tile template not found.";
|
|
return Page();
|
|
}
|
|
|
|
existing.Name = Tile.Name;
|
|
existing.Description = Tile.Description;
|
|
existing.Category = Tile.Category;
|
|
existing.Tags = Tile.Tags;
|
|
existing.Biome = Tile.Biome;
|
|
existing.Width = Tile.Width;
|
|
existing.Height = Tile.Height;
|
|
existing.TilesJson = Tile.TilesJson;
|
|
existing.PrimaryTileDefinitionId = Tile.PrimaryTileDefinitionId;
|
|
existing.IsWalkable = Tile.IsWalkable;
|
|
existing.BlocksProjectiles = Tile.BlocksProjectiles;
|
|
existing.BlocksLineOfSight = Tile.BlocksLineOfSight;
|
|
existing.BlocksMovement = Tile.BlocksMovement;
|
|
existing.SocketN = Tile.SocketN;
|
|
existing.SocketE = Tile.SocketE;
|
|
existing.SocketS = Tile.SocketS;
|
|
existing.SocketW = Tile.SocketW;
|
|
existing.SocketCustom = Tile.SocketCustom;
|
|
existing.Weight = Tile.Weight;
|
|
existing.MinTier = Tile.MinTier;
|
|
existing.MaxTier = Tile.MaxTier;
|
|
existing.IsEnabled = Tile.IsEnabled;
|
|
existing.UpdatedAt = DateTime.UtcNow;
|
|
|
|
Message = "Changes saved!";
|
|
}
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
return RedirectToPage(new { id = Tile.Id });
|
|
}
|
|
|
|
private async Task LoadReferenceData()
|
|
{
|
|
TileSheets = await _db.TileMapSheets
|
|
.Where(s => s.IsEnabled)
|
|
.OrderBy(s => s.Name)
|
|
.ToListAsync();
|
|
}
|
|
}
|