diff --git a/Services/Admin/Pages/Npcs/Edit.cshtml b/Services/Admin/Pages/Npcs/Edit.cshtml new file mode 100644 index 0000000..6d62f4a --- /dev/null +++ b/Services/Admin/Pages/Npcs/Edit.cshtml @@ -0,0 +1,210 @@ +@page "/admin/npcs/edit" +@model Npcs.EditModel +@using DataServer.Data.Enums +@{ + ViewData["Title"] = Model.IsNew ? "Create NPC" : $"Edit: {Model.Npc.Name}"; +} + +
+
+ + + + + +
+

@(Model.IsNew ? "Create NPC" : "Edit NPC")

+

@(Model.IsNew ? "Add a new NPC definition" : $"ID: {Model.Npc.Id}")

+
+
+ + @if (!string.IsNullOrEmpty(Model.Message)) + { +
+ @Model.Message +
+ } + +
+ @Html.AntiForgeryToken() + + +
+
+

Basic Information

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + + +
+
+
+ +
+
+

Behavior & Combat

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ +
+
+

Visual & Linked Systems

+
+
+
+ +
+ + +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ +
+ + Back +
+
+
diff --git a/Services/Admin/Pages/Npcs/Edit.cshtml.cs b/Services/Admin/Pages/Npcs/Edit.cshtml.cs new file mode 100644 index 0000000..2bbdd09 --- /dev/null +++ b/Services/Admin/Pages/Npcs/Edit.cshtml.cs @@ -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 LootTables { get; set; } = new(); + public List Shops { get; set; } = new(); + public List 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 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(); + } +} diff --git a/Services/Admin/Pages/Npcs/Index.cshtml b/Services/Admin/Pages/Npcs/Index.cshtml new file mode 100644 index 0000000..7722305 --- /dev/null +++ b/Services/Admin/Pages/Npcs/Index.cshtml @@ -0,0 +1,155 @@ +@page "/admin/npcs" +@model Npcs.IndexModel +@{ + ViewData["Title"] = "NPCs"; +} + +
+
+
+

NPCs

+

Manage NPC definitions, stats, behavior, and interactions

+
+ + + + + Create NPC + +
+ +
+
+
+ + +
+
+ + +
+ + Clear +
+
+ +
+ + + + + + + + + + + + + + + + @foreach (var npc in Model.Npcs) + { + + + + + + + + + + + + } + @if (!Model.Npcs.Any()) + { + + + + } + +
IDNameLevelBehaviorKillableShopDialogEnabledActions
@npc.Id +
@npc.Name
+
@npc.Key
+
@npc.Level@npc.BehaviorMode@(npc.IsKillable ? "Yes" : "No")@(npc.ShopId.HasValue ? "Yes" : "No")@(npc.DialogTreeId.HasValue ? "Yes" : "No") + @if (npc.IsEnabled) + { + + } + else + { + + } + +
+ + + + + + +
+
+ No NPCs found. Create one? +
+
+ + @if (Model.TotalPages > 1) + { +
+

+ Showing @((Model.CurrentPage - 1) * Model.PageSize + 1)-@Math.Min(Model.CurrentPage * Model.PageSize, Model.TotalCount) of @Model.TotalCount +

+
+ @if (Model.CurrentPage > 1) + { + Prev + } + @Model.CurrentPage / @Model.TotalPages + @if (Model.CurrentPage < Model.TotalPages) + { + Next + } +
+
+ } +
+ + + +@section Scripts { + +} diff --git a/Services/Admin/Pages/Npcs/Index.cshtml.cs b/Services/Admin/Pages/Npcs/Index.cshtml.cs new file mode 100644 index 0000000..8481d49 --- /dev/null +++ b/Services/Admin/Pages/Npcs/Index.cshtml.cs @@ -0,0 +1,83 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using DataServer.Data; +using DataServer.Data.Entities; + +namespace GameServices.Admin.Pages.Npcs; + +public class IndexModel : AdminPageModel +{ + private readonly GameDbContext _db; + + public IndexModel(GameDbContext db) + { + _db = db; + } + + public List Npcs { get; set; } = new(); + + [FromQuery(Name = "search")] + public string? Search { get; set; } + + [FromQuery(Name = "enabled")] + public bool? EnabledFilter { get; set; } + + [FromQuery(Name = "page")] + public int CurrentPage { get; set; } = 1; + + public int PageSize { get; set; } = 20; + public int TotalCount { get; set; } + public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize); + + public string GetPageUrl(int page) + { + var parts = new List { $"page={page}" }; + if (!string.IsNullOrWhiteSpace(Search)) parts.Add($"search={Uri.EscapeDataString(Search)}"); + if (EnabledFilter.HasValue) parts.Add($"enabled={EnabledFilter.Value.ToString().ToLowerInvariant()}"); + return "/admin/npcs?" + string.Join("&", parts); + } + + public async Task OnGetAsync() + { + if (CurrentPage < 1) CurrentPage = 1; + + var query = _db.DungeonNpcDefinitions + .Include(n => n.LootTable) + .Include(n => n.Shop) + .Include(n => n.DialogTree) + .AsQueryable(); + + if (!string.IsNullOrWhiteSpace(Search)) + { + query = query.Where(n => + n.Name.Contains(Search) || + n.Key.Contains(Search) || + n.Description.Contains(Search)); + } + + if (EnabledFilter.HasValue) + { + query = query.Where(n => n.IsEnabled == EnabledFilter.Value); + } + + TotalCount = await query.CountAsync(); + + Npcs = await query + .OrderBy(n => n.Id) + .Skip((CurrentPage - 1) * PageSize) + .Take(PageSize) + .ToListAsync(); + } + + public async Task OnPostDeleteAsync(int id) + { + var npc = await _db.DungeonNpcDefinitions.FindAsync(id); + if (npc != null) + { + _db.DungeonNpcDefinitions.Remove(npc); + await _db.SaveChangesAsync(); + } + + return RedirectToPage(); + } +} diff --git a/Services/Admin/Pages/Shared/_Layout.cshtml b/Services/Admin/Pages/Shared/_Layout.cshtml index d13926e..1890d05 100644 --- a/Services/Admin/Pages/Shared/_Layout.cshtml +++ b/Services/Admin/Pages/Shared/_Layout.cshtml @@ -113,6 +113,10 @@ Mobs + + + NPCs + Sprite Sheets