154 lines
4.9 KiB
C#
154 lines
4.9 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Server.Services.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;
|
|
CdnBaseUrl = NormalizeCdnBaseUrl(_config["Cdn:BaseUrl"] ?? "https://assets.aeternalisgames.com");
|
|
}
|
|
|
|
[BindProperty]
|
|
public BuilderRoomTemplate Room { get; set; } = new();
|
|
|
|
public List<TileMapSheet> TileSheets { get; set; } = new();
|
|
public List<ObjectTemplate> Objects { get; set; } = new();
|
|
|
|
public string CdnBaseUrl;// => _config["Cdn:BaseUrl"]?.TrimEnd('/') ?? "";
|
|
|
|
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();
|
|
}
|
|
private static 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;
|
|
}
|
|
}
|