223 lines
7.1 KiB
C#
223 lines
7.1 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<TileDefinition> TileDefinitions { get; set; } = new();
|
|
public List<TileStamp> TileStamps { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// TileDefinitions serialized as JSON for JavaScript consumption.
|
|
/// </summary>
|
|
public string TileDefinitionsJson => JsonSerializer.Serialize(
|
|
TileDefinitions.Select(t => new
|
|
{
|
|
id = t.Id,
|
|
name = t.Name,
|
|
type = t.Type.ToString(),
|
|
tags = t.Tags,
|
|
isWalkable = t.IsWalkable,
|
|
sheetId = t.TileMapSheetId,
|
|
frameIndex = t.FrameIndex
|
|
})
|
|
);
|
|
|
|
public string TileStampsJson => JsonSerializer.Serialize(
|
|
TileStamps.Select(s => new
|
|
{
|
|
id = s.Id,
|
|
name = s.Name,
|
|
width = s.Width,
|
|
height = s.Height,
|
|
tiles = ParseStampTiles(s.TilesJson)
|
|
})
|
|
);
|
|
|
|
private class StampTile
|
|
{
|
|
public int X { get; set; }
|
|
public int Y { get; set; }
|
|
public int SheetId { get; set; }
|
|
public int FrameIndex { get; set; }
|
|
public int? TileDefinitionId { get; set; }
|
|
public int? Layer { get; set; }
|
|
}
|
|
|
|
private static List<StampTile> ParseStampTiles(string? json)
|
|
{
|
|
if (string.IsNullOrEmpty(json)) return new List<StampTile>();
|
|
try
|
|
{
|
|
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
|
return JsonSerializer.Deserialize<List<StampTile>>(json, options) ?? new List<StampTile>();
|
|
}
|
|
catch
|
|
{
|
|
return new List<StampTile>();
|
|
}
|
|
}
|
|
|
|
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();
|
|
|
|
TileDefinitions = await _db.TileDefinitions
|
|
.Where(t => t.IsEnabled)
|
|
.OrderBy(t => t.TileMapSheetId)
|
|
.ThenBy(t => t.FrameIndex)
|
|
.ToListAsync();
|
|
|
|
TileStamps = await _db.TileStamps
|
|
.Where(s => s.IsEnabled)
|
|
.OrderBy(s => s.Name)
|
|
.ToListAsync();
|
|
}
|
|
}
|