Add admin NPC management pages and sidebar navigation

This commit is contained in:
dingo
2026-02-14 01:08:51 -05:00
parent b110dbe5dc
commit 0e71d64baa
5 changed files with 636 additions and 0 deletions
+184
View File
@@ -0,0 +1,184 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataServer.Data;
using DataServer.Data.Entities;
using DataServer.Data.Enums;
namespace GameServices.Admin.Pages.Npcs;
public class EditModel : AdminPageModel
{
private readonly GameDbContext _db;
public EditModel(GameDbContext db)
{
_db = db;
}
[BindProperty]
public DungeonNpcDefinition Npc { get; set; } = new();
public List<DungeonLootTable> LootTables { get; set; } = new();
public List<DungeonNpcShop> Shops { get; set; } = new();
public List<DungeonNpcDialogTree> DialogTrees { get; set; } = new();
public bool IsNew => Npc.Id == 0;
public string? Message { get; set; }
public bool IsError { get; set; }
public async Task OnGetAsync(int? id)
{
await LoadSelectionsAsync();
if (id.HasValue)
{
var npc = await _db.DungeonNpcDefinitions.FirstOrDefaultAsync(n => n.Id == id.Value);
if (npc != null)
{
Npc = npc;
return;
}
}
Npc = new DungeonNpcDefinition
{
Name = "",
Key = "",
Description = "",
Level = 1,
IsEnabled = true,
IsKillable = true,
CanShop = false,
HasDialog = false,
CanWander = true,
BehaviorMode = NpcBehaviorMode.Wander,
OnAttackBehavior = NpcOnAttackBehavior.FightBack,
MaxHealth = 100,
AttackPower = 10,
Defense = 5,
Strength = 5,
Dexterity = 5,
Intelligence = 5,
Wisdom = 5,
Endurance = 5,
ResistancesJson = "{}",
AbilityDefinitionIdsJson = "[]",
TintColor = "#ffffff",
Scale = 1f,
WanderRadiusPx = 96f,
WanderIntervalSeconds = 2.5f
};
}
public async Task<IActionResult> OnPostAsync()
{
await LoadSelectionsAsync();
Npc.IsEnabled = Request.Form.ContainsKey("Npc.IsEnabled");
Npc.IsKillable = Request.Form.ContainsKey("Npc.IsKillable");
Npc.CanShop = Request.Form.ContainsKey("Npc.CanShop");
Npc.HasDialog = Request.Form.ContainsKey("Npc.HasDialog");
Npc.CanWander = Request.Form.ContainsKey("Npc.CanWander");
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();
if (string.IsNullOrWhiteSpace(Npc.Name))
{
IsError = true;
Message = "Name is required.";
return Page();
}
if (string.IsNullOrWhiteSpace(Npc.Key))
{
Npc.Key = Npc.Name
.ToLowerInvariant()
.Replace(" ", "_");
}
try
{
if (Npc.Id == 0)
{
Npc.CreatedAt = DateTime.UtcNow;
Npc.UpdatedAt = DateTime.UtcNow;
_db.DungeonNpcDefinitions.Add(Npc);
await _db.SaveChangesAsync();
return RedirectToPage("/Npcs/Edit", new { id = Npc.Id });
}
var existing = await _db.DungeonNpcDefinitions.FirstOrDefaultAsync(n => n.Id == Npc.Id);
if (existing == null)
{
IsError = true;
Message = "NPC not found.";
return Page();
}
existing.Name = Npc.Name;
existing.Key = Npc.Key;
existing.Description = Npc.Description;
existing.Level = Npc.Level;
existing.IsEnabled = Npc.IsEnabled;
existing.IsKillable = Npc.IsKillable;
existing.CanShop = Npc.CanShop;
existing.HasDialog = Npc.HasDialog;
existing.CanWander = Npc.CanWander;
existing.BehaviorMode = Npc.BehaviorMode;
existing.OnAttackBehavior = Npc.OnAttackBehavior;
existing.MaxHealth = Npc.MaxHealth;
existing.AttackPower = Npc.AttackPower;
existing.Defense = Npc.Defense;
existing.Strength = Npc.Strength;
existing.Dexterity = Npc.Dexterity;
existing.Intelligence = Npc.Intelligence;
existing.Wisdom = Npc.Wisdom;
existing.Endurance = Npc.Endurance;
existing.ResistancesJson = Npc.ResistancesJson;
existing.AbilityDefinitionIdsJson = Npc.AbilityDefinitionIdsJson;
existing.SpriteConfigJson = string.IsNullOrWhiteSpace(Npc.SpriteConfigJson) ? null : Npc.SpriteConfigJson;
existing.TintColor = Npc.TintColor;
existing.Scale = Npc.Scale;
existing.WanderRadiusPx = Npc.WanderRadiusPx;
existing.WanderIntervalSeconds = Npc.WanderIntervalSeconds;
existing.LootTableId = Npc.LootTableId;
existing.ShopId = Npc.ShopId;
existing.DialogTreeId = Npc.DialogTreeId;
existing.UpdatedAt = DateTime.UtcNow;
await _db.SaveChangesAsync();
Npc = existing;
Message = "Changes saved successfully.";
}
catch (Exception ex)
{
IsError = true;
Message = $"Error: {ex.Message}";
}
return Page();
}
private async Task LoadSelectionsAsync()
{
LootTables = await _db.DungeonLootTables
.Where(l => l.IsEnabled)
.OrderBy(l => l.Name)
.ToListAsync();
Shops = await _db.DungeonNpcShops
.Where(s => s.IsEnabled)
.OrderBy(s => s.Name)
.ToListAsync();
DialogTrees = await _db.DungeonNpcDialogTrees
.Where(d => d.IsEnabled)
.OrderBy(d => d.Name)
.ToListAsync();
}
}