317 lines
11 KiB
C#
317 lines
11 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 TileEditModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly IConfiguration _config;
|
|
|
|
private static readonly JsonSerializerOptions _jsonOptions = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
public TileEditModel(GameDbContext db, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_config = config;
|
|
}
|
|
|
|
[BindProperty]
|
|
public TileTemplate Tile { get; set; } = new();
|
|
|
|
[BindProperty]
|
|
public string? SocketConfigJson { get; set; }
|
|
|
|
public List<TileMapSheet> TileSheets { get; set; } = new();
|
|
public List<TileDefinition> TileDefinitions { get; set; } = new();
|
|
|
|
public string TileDefinitionsJson => JsonSerializer.Serialize(
|
|
TileDefinitions.Select(t => new
|
|
{
|
|
id = t.Id,
|
|
sheetId = t.TileMapSheetId,
|
|
frameIndex = t.FrameIndex
|
|
})
|
|
);
|
|
|
|
/// <summary>All existing socket names for autocomplete</summary>
|
|
public List<string> AllSocketNames { 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;
|
|
SocketConfigJson = tile.SocketConfigJson;
|
|
|
|
// If no new config but has legacy data, migrate it
|
|
if (string.IsNullOrEmpty(SocketConfigJson))
|
|
{
|
|
var config = MigrateLegacySockets(tile);
|
|
SocketConfigJson = JsonSerializer.Serialize(config, _jsonOptions);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Defaults for new template
|
|
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;
|
|
|
|
// Create default socket configuration
|
|
var defaultConfig = SocketConfiguration.CreateDefault(1, 1);
|
|
SocketConfigJson = JsonSerializer.Serialize(defaultConfig, _jsonOptions);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// Process socket configuration
|
|
if (!string.IsNullOrEmpty(SocketConfigJson))
|
|
{
|
|
Tile.SocketConfigJson = SocketConfigJson;
|
|
|
|
// Extract any new custom socket names and add to database
|
|
await ExtractAndSaveSocketNames(SocketConfigJson);
|
|
|
|
// Update legacy fields for backward compatibility
|
|
try
|
|
{
|
|
var config = JsonSerializer.Deserialize<SocketConfiguration>(SocketConfigJson, _jsonOptions);
|
|
if (config != null)
|
|
{
|
|
Tile.SocketN = config.North.FirstOrDefault()?.Type ?? SocketType.Open;
|
|
Tile.SocketE = config.East.FirstOrDefault()?.Type ?? SocketType.Open;
|
|
Tile.SocketS = config.South.FirstOrDefault()?.Type ?? SocketType.Open;
|
|
Tile.SocketW = config.West.FirstOrDefault()?.Type ?? SocketType.Open;
|
|
|
|
// Store first custom name found (legacy compatibility)
|
|
var customName = config.North.FirstOrDefault(s => s.Type == SocketType.Custom)?.CustomName
|
|
?? config.East.FirstOrDefault(s => s.Type == SocketType.Custom)?.CustomName
|
|
?? config.South.FirstOrDefault(s => s.Type == SocketType.Custom)?.CustomName
|
|
?? config.West.FirstOrDefault(s => s.Type == SocketType.Custom)?.CustomName;
|
|
Tile.SocketCustom = customName;
|
|
}
|
|
}
|
|
catch { /* Ignore parse errors, keep existing values */ }
|
|
}
|
|
|
|
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.SocketConfigJson = Tile.SocketConfigJson;
|
|
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 });
|
|
}
|
|
|
|
/// <summary>
|
|
/// API endpoint to get socket names for autocomplete
|
|
/// </summary>
|
|
public async Task<IActionResult> OnGetSocketNamesAsync(string? query)
|
|
{
|
|
var names = await _db.SocketNames
|
|
.Where(s => string.IsNullOrEmpty(query) || s.Name.Contains(query.ToLower()))
|
|
.OrderByDescending(s => s.UsageCount)
|
|
.ThenBy(s => s.Name)
|
|
.Take(20)
|
|
.Select(s => s.Name)
|
|
.ToListAsync();
|
|
|
|
return new JsonResult(names);
|
|
}
|
|
|
|
private async Task LoadReferenceData()
|
|
{
|
|
TileSheets = await _db.TileMapSheets
|
|
.Where(s => s.IsEnabled)
|
|
.OrderBy(s => s.Name)
|
|
.ToListAsync();
|
|
|
|
TileDefinitions = await _db.TileDefinitions
|
|
.Where(t => t.IsEnabled)
|
|
.ToListAsync();
|
|
|
|
AllSocketNames = await _db.SocketNames
|
|
.OrderByDescending(s => s.UsageCount)
|
|
.ThenBy(s => s.Name)
|
|
.Select(s => s.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Migrate legacy socket fields to new JSON format
|
|
/// </summary>
|
|
private SocketConfiguration MigrateLegacySockets(TileTemplate tile)
|
|
{
|
|
var config = SocketConfiguration.CreateDefault(tile.Width, tile.Height);
|
|
|
|
// Set first socket of each edge from legacy fields
|
|
if (config.North.Count > 0)
|
|
{
|
|
config.North[0].Type = tile.SocketN;
|
|
if (tile.SocketN == SocketType.Custom)
|
|
config.North[0].CustomName = tile.SocketCustom?.ToLower();
|
|
}
|
|
if (config.East.Count > 0)
|
|
{
|
|
config.East[0].Type = tile.SocketE;
|
|
if (tile.SocketE == SocketType.Custom)
|
|
config.East[0].CustomName = tile.SocketCustom?.ToLower();
|
|
}
|
|
if (config.South.Count > 0)
|
|
{
|
|
config.South[0].Type = tile.SocketS;
|
|
if (tile.SocketS == SocketType.Custom)
|
|
config.South[0].CustomName = tile.SocketCustom?.ToLower();
|
|
}
|
|
if (config.West.Count > 0)
|
|
{
|
|
config.West[0].Type = tile.SocketW;
|
|
if (tile.SocketW == SocketType.Custom)
|
|
config.West[0].CustomName = tile.SocketCustom?.ToLower();
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract custom socket names from config and add new ones to database
|
|
/// </summary>
|
|
private async Task ExtractAndSaveSocketNames(string configJson)
|
|
{
|
|
try
|
|
{
|
|
var config = JsonSerializer.Deserialize<SocketConfiguration>(configJson, _jsonOptions);
|
|
if (config == null) return;
|
|
|
|
var allSockets = config.North
|
|
.Concat(config.East)
|
|
.Concat(config.South)
|
|
.Concat(config.West);
|
|
|
|
var customNames = allSockets
|
|
.Where(s => s.Type == SocketType.Custom && !string.IsNullOrWhiteSpace(s.CustomName))
|
|
.Select(s => s.CustomName!.ToLower().Trim())
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
foreach (var name in customNames)
|
|
{
|
|
var existing = await _db.SocketNames.FirstOrDefaultAsync(s => s.Name == name);
|
|
if (existing != null)
|
|
{
|
|
existing.UsageCount++;
|
|
}
|
|
else
|
|
{
|
|
_db.SocketNames.Add(new SocketName
|
|
{
|
|
Name = name,
|
|
UsageCount = 1,
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch { /* Ignore parse errors */ }
|
|
}
|
|
}
|