Files
systemrevivalserver/Services/Admin/Pages/RoomTemplates/Edit.cshtml.cs
T
2025-12-28 00:08:33 -05:00

139 lines
4.5 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataServer.Data;
using DataServer.Data.Entities;
using System.Text.Json;
namespace Server.Services.Admin.Pages.RoomTemplates;
public class EditModel : AdminPageModel
{
private readonly GameDbContext _db;
private readonly IConfiguration _config;
public EditModel(GameDbContext db, IConfiguration config)
{
_db = db;
_config = config;
}
[BindProperty]
public RoomTemplate Room { get; set; } = new();
public List<TileMapSheet> TileMapSheets { get; set; } = new();
public List<TileDefinition> AllTiles { get; set; } = new();
public string CdnBaseUrl { get; set; } = "";
public string? Message { get; set; }
public bool IsError { get; set; }
public async Task OnGetAsync(int? id)
{
CdnBaseUrl = _config["Cdn:BaseUrl"]?.TrimEnd('/') ?? "https://assets.aeternalisgames.com";
if (!CdnBaseUrl.EndsWith("/downloads")) CdnBaseUrl += "/downloads";
TileMapSheets = await _db.TileMapSheets
.Where(s => s.IsEnabled)
.Include(s => s.Tiles!.Where(t => t.IsEnabled))
.OrderBy(s => s.Name)
.ToListAsync();
AllTiles = TileMapSheets.SelectMany(s => s.Tiles ?? new List<TileDefinition>()).ToList();
if (id.HasValue)
{
var room = await _db.RoomTemplates.FindAsync(id.Value);
if (room != null)
{
Room = room;
}
}
else
{
Room = new RoomTemplate
{
Width = 10,
Height = 10,
MinTier = 1,
Weight = 100,
IsEnabled = true
};
}
}
public async Task<IActionResult> OnPostAsync()
{
CdnBaseUrl = _config["Cdn:BaseUrl"]?.TrimEnd('/') ?? "https://assets.aeternalisgames.com";
if (!CdnBaseUrl.EndsWith("/downloads")) CdnBaseUrl += "/downloads";
try
{
if (Room.Id == 0)
{
Room.CreatedAt = DateTime.UtcNow;
Room.UpdatedAt = DateTime.UtcNow;
_db.RoomTemplates.Add(Room);
await _db.SaveChangesAsync();
Message = $"Room template '{Room.Name}' created.";
return RedirectToPage(new { id = Room.Id });
}
else
{
var existing = await _db.RoomTemplates.FindAsync(Room.Id);
if (existing == null)
{
Message = "Room template not found.";
IsError = true;
return Page();
}
existing.Name = Room.Name;
existing.Description = Room.Description;
existing.Tags = Room.Tags;
existing.Type = Room.Type;
existing.Width = Room.Width;
existing.Height = Room.Height;
existing.MinTier = Room.MinTier;
existing.MaxTier = Room.MaxTier;
existing.Weight = Room.Weight;
existing.TileDataJson = Room.TileDataJson;
existing.SpawnPointsJson = Room.SpawnPointsJson;
existing.ConnectionPointsJson = Room.ConnectionPointsJson;
existing.IsEnabled = Room.IsEnabled;
existing.UpdatedAt = DateTime.UtcNow;
await _db.SaveChangesAsync();
Room = existing;
Message = "Room template updated.";
}
}
catch (Exception ex)
{
Message = $"Error: {ex.Message}";
IsError = true;
}
TileMapSheets = await _db.TileMapSheets
.Where(s => s.IsEnabled)
.Include(s => s.Tiles!.Where(t => t.IsEnabled))
.OrderBy(s => s.Name)
.ToListAsync();
AllTiles = TileMapSheets.SelectMany(s => s.Tiles ?? new List<TileDefinition>()).ToList();
return Page();
}
public async Task<IActionResult> OnPostSaveTilesAsync(int roomId, string tileData)
{
var room = await _db.RoomTemplates.FindAsync(roomId);
if (room != null)
{
room.TileDataJson = tileData;
room.UpdatedAt = DateTime.UtcNow;
await _db.SaveChangesAsync();
return new JsonResult(new { success = true });
}
return new JsonResult(new { success = false, error = "Room not found" });
}
}