206 lines
6.8 KiB
C#
206 lines
6.8 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;
|
|
|
|
public class RoomEditModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly IConfiguration _config;
|
|
|
|
public RoomEditModel(GameDbContext db, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_config = config;
|
|
}
|
|
|
|
[BindProperty]
|
|
public BuilderRoomTemplate Room { get; set; } = new();
|
|
|
|
public List<TileMapSheet> TileSheets { get; set; } = new();
|
|
public List<ObjectTemplate> Objects { get; set; } = new();
|
|
public List<DungeonMobDefinition> Mobs { get; set; } = new();
|
|
public List<TileTemplate> TileTemplates { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// TileTemplates serialized as JSON for JavaScript consumption.
|
|
/// Includes extracted sheetId and frameIndex for direct use.
|
|
/// </summary>
|
|
public string TileTemplatesJson => JsonSerializer.Serialize(
|
|
TileTemplates.Select(t => {
|
|
var (sheetId, frameIndex) = ExtractTileInfo(t.TilesJson);
|
|
return new {
|
|
id = t.Id,
|
|
name = t.Name,
|
|
category = t.Category.ToString(),
|
|
tags = t.Tags,
|
|
isWalkable = t.IsWalkable,
|
|
blocksMovement = t.BlocksMovement,
|
|
weight = t.Weight,
|
|
sheetId = sheetId,
|
|
frameIndex = frameIndex
|
|
};
|
|
}).Where(t => t.sheetId.HasValue)
|
|
);
|
|
|
|
private static (int? sheetId, int frameIndex) ExtractTileInfo(string? tilesJson)
|
|
{
|
|
if (string.IsNullOrEmpty(tilesJson)) return (null, 0);
|
|
try
|
|
{
|
|
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
|
var tiles = JsonSerializer.Deserialize<List<TilePlacement>>(tilesJson, options);
|
|
var first = tiles?.FirstOrDefault();
|
|
return (first?.SheetId, first?.FrameIndex ?? 0);
|
|
}
|
|
catch { return (null, 0); }
|
|
}
|
|
|
|
private class TilePlacement
|
|
{
|
|
public int SheetId { get; set; }
|
|
public int FrameIndex { get; set; }
|
|
}
|
|
|
|
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 room = await _db.BuilderRoomTemplates.FindAsync(id.Value);
|
|
if (room != null)
|
|
{
|
|
Room = room;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Defaults
|
|
Room.IsEnabled = true;
|
|
Room.Width = 10;
|
|
Room.Height = 10;
|
|
Room.Weight = 100;
|
|
Room.MinConnections = 1;
|
|
Room.MaxConnections = 4;
|
|
Room.AllowRotation = true;
|
|
Room.AllowMirror = true;
|
|
}
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
await LoadReferenceData();
|
|
|
|
// Validate
|
|
if (string.IsNullOrWhiteSpace(Room.Name))
|
|
{
|
|
IsError = true;
|
|
Message = "Name is required.";
|
|
return Page();
|
|
}
|
|
|
|
Room.UpdatedAt = DateTime.UtcNow;
|
|
|
|
// Handle nullable spawn coordinates
|
|
if (Request.Form["Room.PlayerSpawnX"].ToString() == "") Room.PlayerSpawnX = null;
|
|
if (Request.Form["Room.PlayerSpawnY"].ToString() == "") Room.PlayerSpawnY = null;
|
|
if (Request.Form["Room.ExitPortalX"].ToString() == "") Room.ExitPortalX = null;
|
|
if (Request.Form["Room.ExitPortalY"].ToString() == "") Room.ExitPortalY = null;
|
|
|
|
if (Room.Id == 0)
|
|
{
|
|
Room.CreatedAt = DateTime.UtcNow;
|
|
_db.BuilderRoomTemplates.Add(Room);
|
|
Message = "Room template created!";
|
|
}
|
|
else
|
|
{
|
|
var existing = await _db.BuilderRoomTemplates.FindAsync(Room.Id);
|
|
if (existing == null)
|
|
{
|
|
IsError = true;
|
|
Message = "Room not found.";
|
|
return Page();
|
|
}
|
|
|
|
existing.Name = Room.Name;
|
|
existing.Description = Room.Description;
|
|
existing.Type = Room.Type;
|
|
existing.Tags = Room.Tags;
|
|
existing.Biome = Room.Biome;
|
|
existing.Width = Room.Width;
|
|
existing.Height = Room.Height;
|
|
existing.TileGridJson = Room.TileGridJson;
|
|
existing.TilePlacementsJson = Room.TilePlacementsJson;
|
|
existing.ObjectPlacementsJson = Room.ObjectPlacementsJson;
|
|
existing.ConnectionPointsJson = Room.ConnectionPointsJson;
|
|
existing.MobSpawnsJson = Room.MobSpawnsJson;
|
|
existing.ItemSpawnsJson = Room.ItemSpawnsJson;
|
|
existing.PlayerSpawnX = Room.PlayerSpawnX;
|
|
existing.PlayerSpawnY = Room.PlayerSpawnY;
|
|
existing.ExitPortalX = Room.ExitPortalX;
|
|
existing.ExitPortalY = Room.ExitPortalY;
|
|
existing.Weight = Room.Weight;
|
|
existing.MinTier = Room.MinTier;
|
|
existing.MaxTier = Room.MaxTier;
|
|
existing.MinConnections = Room.MinConnections;
|
|
existing.MaxConnections = Room.MaxConnections;
|
|
existing.AllowRotation = Room.AllowRotation;
|
|
existing.AllowMirror = Room.AllowMirror;
|
|
existing.IsEnabled = Room.IsEnabled;
|
|
existing.UpdatedAt = DateTime.UtcNow;
|
|
|
|
Message = "Changes saved!";
|
|
}
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
return RedirectToPage(new { id = Room.Id });
|
|
}
|
|
|
|
private async Task LoadReferenceData()
|
|
{
|
|
TileSheets = await _db.TileMapSheets
|
|
.Where(s => s.IsEnabled)
|
|
.OrderBy(s => s.Name)
|
|
.ToListAsync();
|
|
|
|
Objects = await _db.ObjectTemplates
|
|
.Where(o => o.IsEnabled)
|
|
.OrderBy(o => o.Name)
|
|
.ToListAsync();
|
|
|
|
Mobs = await _db.DungeonMobDefinitions
|
|
.Where(m => m.IsEnabled)
|
|
.OrderBy(m => m.Type)
|
|
.ThenBy(m => m.Name)
|
|
.ToListAsync();
|
|
|
|
TileTemplates = await _db.TileTemplates
|
|
.Where(t => t.IsEnabled)
|
|
.OrderBy(t => t.Category)
|
|
.ThenBy(t => t.Name)
|
|
.ToListAsync();
|
|
}
|
|
}
|