Upgrade NPC admin editor selectors and add dialog tree admin UI
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using DataServer.Data.Enums;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace GameServices.Admin.Pages.Npcs;
|
||||
|
||||
@@ -21,11 +22,40 @@ public class EditModel : AdminPageModel
|
||||
public List<DungeonLootTable> LootTables { get; set; } = new();
|
||||
public List<DungeonNpcShop> Shops { get; set; } = new();
|
||||
public List<DungeonNpcDialogTree> DialogTrees { get; set; } = new();
|
||||
public List<DamageResistanceType> ResistanceTypes { get; set; } = new();
|
||||
public List<MobAbilityDefinition> AllAbilities { get; set; } = new();
|
||||
public List<MobSpriteSheet> AllSpriteSheets { get; set; } = new();
|
||||
public List<int> SelectedAbilityIds { get; set; } = new();
|
||||
public List<NpcSpriteRow> SpriteRows { get; set; } = new();
|
||||
public NpcSpriteConfig SpriteConfig { get; set; } = new();
|
||||
|
||||
public bool IsNew => Npc.Id == 0;
|
||||
public string? Message { get; set; }
|
||||
public bool IsError { get; set; }
|
||||
|
||||
public class NpcSpriteRow
|
||||
{
|
||||
public string Key { get; set; } = "";
|
||||
public int? SheetId { get; set; }
|
||||
public int Frames { get; set; } = 8;
|
||||
public bool Loop { get; set; } = true;
|
||||
}
|
||||
|
||||
public class NpcSpriteConfig
|
||||
{
|
||||
public Dictionary<string, string> Sprites { get; set; } = new();
|
||||
public Dictionary<string, SpriteAnimation> Animations { get; set; } = new();
|
||||
public int FrameWidth { get; set; } = 64;
|
||||
public int FrameHeight { get; set; } = 64;
|
||||
public int Fps { get; set; } = 8;
|
||||
|
||||
public class SpriteAnimation
|
||||
{
|
||||
public int Frames { get; set; } = 8;
|
||||
public bool Loop { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task OnGetAsync(int? id)
|
||||
{
|
||||
await LoadSelectionsAsync();
|
||||
@@ -36,6 +66,7 @@ public class EditModel : AdminPageModel
|
||||
if (npc != null)
|
||||
{
|
||||
Npc = npc;
|
||||
ParseNpcJsonFields();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -68,6 +99,7 @@ public class EditModel : AdminPageModel
|
||||
WanderRadiusPx = 96f,
|
||||
WanderIntervalSeconds = 2.5f
|
||||
};
|
||||
ParseNpcJsonFields();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
@@ -83,14 +115,16 @@ public class EditModel : AdminPageModel
|
||||
Npc.Name = (Npc.Name ?? "").Trim();
|
||||
Npc.Key = (Npc.Key ?? "").Trim();
|
||||
Npc.Description ??= "";
|
||||
Npc.ResistancesJson ??= "{}";
|
||||
Npc.AbilityDefinitionIdsJson ??= "[]";
|
||||
Npc.TintColor = string.IsNullOrWhiteSpace(Npc.TintColor) ? "#ffffff" : Npc.TintColor.Trim();
|
||||
ParseAndSetResistancesFromForm();
|
||||
ParseAndSetAbilityIdsFromForm();
|
||||
ParseAndSetSpriteConfigFromForm();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Npc.Name))
|
||||
{
|
||||
IsError = true;
|
||||
Message = "Name is required.";
|
||||
ParseNpcJsonFields();
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -117,6 +151,7 @@ public class EditModel : AdminPageModel
|
||||
{
|
||||
IsError = true;
|
||||
Message = "NPC not found.";
|
||||
ParseNpcJsonFields();
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -153,12 +188,14 @@ public class EditModel : AdminPageModel
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
Npc = existing;
|
||||
ParseNpcJsonFields();
|
||||
Message = "Changes saved successfully.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
IsError = true;
|
||||
Message = $"Error: {ex.Message}";
|
||||
ParseNpcJsonFields();
|
||||
}
|
||||
|
||||
return Page();
|
||||
@@ -180,5 +217,162 @@ public class EditModel : AdminPageModel
|
||||
.Where(d => d.IsEnabled)
|
||||
.OrderBy(d => d.Name)
|
||||
.ToListAsync();
|
||||
|
||||
ResistanceTypes = await _db.DamageResistanceTypes
|
||||
.Where(r => r.IsEnabled)
|
||||
.OrderBy(r => r.SortOrder)
|
||||
.ToListAsync();
|
||||
|
||||
AllAbilities = await _db.MobAbilityDefinitions
|
||||
.Where(a => a.IsEnabled)
|
||||
.OrderBy(a => a.Name)
|
||||
.ToListAsync();
|
||||
|
||||
AllSpriteSheets = await _db.MobSpriteSheets
|
||||
.Where(s => s.IsEnabled)
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
private void ParseNpcJsonFields()
|
||||
{
|
||||
try
|
||||
{
|
||||
var resistanceMap = JsonSerializer.Deserialize<Dictionary<string, float>>(Npc.ResistancesJson ?? "{}") ?? new();
|
||||
Npc.ResistancesJson = JsonSerializer.Serialize(resistanceMap);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Npc.ResistancesJson = "{}";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SelectedAbilityIds = JsonSerializer.Deserialize<List<int>>(Npc.AbilityDefinitionIdsJson ?? "[]") ?? new();
|
||||
}
|
||||
catch
|
||||
{
|
||||
SelectedAbilityIds = new List<int>();
|
||||
Npc.AbilityDefinitionIdsJson = "[]";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SpriteConfig = string.IsNullOrWhiteSpace(Npc.SpriteConfigJson)
|
||||
? new NpcSpriteConfig()
|
||||
: (JsonSerializer.Deserialize<NpcSpriteConfig>(Npc.SpriteConfigJson!, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
}) ?? new NpcSpriteConfig());
|
||||
}
|
||||
catch
|
||||
{
|
||||
SpriteConfig = new NpcSpriteConfig();
|
||||
}
|
||||
|
||||
SpriteRows = new List<NpcSpriteRow>();
|
||||
foreach (var kv in SpriteConfig.Sprites)
|
||||
{
|
||||
var frames = 8;
|
||||
var loop = true;
|
||||
if (SpriteConfig.Animations.TryGetValue(kv.Key, out var anim))
|
||||
{
|
||||
frames = anim.Frames;
|
||||
loop = anim.Loop;
|
||||
}
|
||||
|
||||
var matchingSheet = AllSpriteSheets.FirstOrDefault(s =>
|
||||
string.Equals(s.CdnPath, kv.Value, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
SpriteRows.Add(new NpcSpriteRow
|
||||
{
|
||||
Key = kv.Key,
|
||||
SheetId = matchingSheet?.Id,
|
||||
Frames = frames,
|
||||
Loop = loop
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseAndSetResistancesFromForm()
|
||||
{
|
||||
var map = new Dictionary<string, float>(StringComparer.OrdinalIgnoreCase);
|
||||
var keys = Request.Form["resist_name[]"].ToArray();
|
||||
var vals = Request.Form["resist_value[]"].ToArray();
|
||||
for (var i = 0; i < keys.Length && i < vals.Length; i++)
|
||||
{
|
||||
var key = (keys[i] ?? "").Trim().ToLowerInvariant();
|
||||
if (string.IsNullOrWhiteSpace(key)) continue;
|
||||
if (!float.TryParse(vals[i], out var val)) continue;
|
||||
map[key] = val;
|
||||
}
|
||||
Npc.ResistancesJson = JsonSerializer.Serialize(map);
|
||||
}
|
||||
|
||||
private void ParseAndSetAbilityIdsFromForm()
|
||||
{
|
||||
var list = new List<int>();
|
||||
foreach (var raw in Request.Form["ability_ids[]"])
|
||||
{
|
||||
if (int.TryParse(raw, out var id) && id > 0 && !list.Contains(id))
|
||||
{
|
||||
list.Add(id);
|
||||
}
|
||||
}
|
||||
SelectedAbilityIds = list;
|
||||
Npc.AbilityDefinitionIdsJson = JsonSerializer.Serialize(list);
|
||||
}
|
||||
|
||||
private void ParseAndSetSpriteConfigFromForm()
|
||||
{
|
||||
var spriteKeys = Request.Form["sprite_key[]"].ToArray();
|
||||
var spriteSheetIds = Request.Form["sprite_sheet_id[]"].ToArray();
|
||||
var spriteFrames = Request.Form["sprite_frames[]"].ToArray();
|
||||
var spriteLoops = Request.Form["sprite_loop[]"].ToArray();
|
||||
|
||||
var config = new NpcSpriteConfig();
|
||||
if (int.TryParse(Request.Form["sprite_frameWidth"], out var fw)) config.FrameWidth = fw;
|
||||
if (int.TryParse(Request.Form["sprite_frameHeight"], out var fh)) config.FrameHeight = fh;
|
||||
if (int.TryParse(Request.Form["sprite_fps"], out var fps)) config.Fps = fps;
|
||||
|
||||
for (var i = 0; i < spriteKeys.Length && i < spriteSheetIds.Length; i++)
|
||||
{
|
||||
var key = (spriteKeys[i] ?? "").Trim();
|
||||
if (string.IsNullOrWhiteSpace(key)) continue;
|
||||
if (!int.TryParse(spriteSheetIds[i], out var sheetId)) continue;
|
||||
var sheet = AllSpriteSheets.FirstOrDefault(s => s.Id == sheetId);
|
||||
if (sheet == null) continue;
|
||||
|
||||
var frames = 8;
|
||||
if (i < spriteFrames.Length && int.TryParse(spriteFrames[i], out var rowFrames))
|
||||
{
|
||||
frames = Math.Max(1, rowFrames);
|
||||
}
|
||||
|
||||
var loop = spriteLoops.Contains(i.ToString());
|
||||
config.Sprites[key] = sheet.CdnPath;
|
||||
config.Animations[key] = new NpcSpriteConfig.SpriteAnimation
|
||||
{
|
||||
Frames = frames,
|
||||
Loop = loop
|
||||
};
|
||||
}
|
||||
|
||||
if (config.Sprites.Count == 0)
|
||||
{
|
||||
SpriteConfig = new NpcSpriteConfig();
|
||||
SpriteRows = new List<NpcSpriteRow>();
|
||||
Npc.SpriteConfigJson = null;
|
||||
return;
|
||||
}
|
||||
|
||||
SpriteConfig = config;
|
||||
Npc.SpriteConfigJson = JsonSerializer.Serialize(config, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
|
||||
// rebuild display rows for round-trip on validation failure
|
||||
ParseNpcJsonFields();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user