From 715c78b6162647c38c076f84090e86efc50e807a Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 14 Feb 2026 10:41:32 -0500 Subject: [PATCH] Add NPC shop admin pages with slot-based inventory editing --- Services/Admin/Pages/Npcs/Edit.cshtml | 5 +- Services/Admin/Pages/Npcs/Index.cshtml | 1 + Services/Admin/Pages/Npcs/ShopEdit.cshtml | 202 +++++++++++++++++++ Services/Admin/Pages/Npcs/ShopEdit.cshtml.cs | 167 +++++++++++++++ Services/Admin/Pages/Npcs/Shops.cshtml | 68 +++++++ Services/Admin/Pages/Npcs/Shops.cshtml.cs | 52 +++++ Services/Admin/Pages/Shared/_Layout.cshtml | 4 + 7 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 Services/Admin/Pages/Npcs/ShopEdit.cshtml create mode 100644 Services/Admin/Pages/Npcs/ShopEdit.cshtml.cs create mode 100644 Services/Admin/Pages/Npcs/Shops.cshtml create mode 100644 Services/Admin/Pages/Npcs/Shops.cshtml.cs diff --git a/Services/Admin/Pages/Npcs/Edit.cshtml b/Services/Admin/Pages/Npcs/Edit.cshtml index f066be9..b097c8a 100644 --- a/Services/Admin/Pages/Npcs/Edit.cshtml +++ b/Services/Admin/Pages/Npcs/Edit.cshtml @@ -293,7 +293,10 @@
diff --git a/Services/Admin/Pages/Npcs/Index.cshtml b/Services/Admin/Pages/Npcs/Index.cshtml index b4763e5..b0e9817 100644 --- a/Services/Admin/Pages/Npcs/Index.cshtml +++ b/Services/Admin/Pages/Npcs/Index.cshtml @@ -11,6 +11,7 @@

Manage NPC definitions, stats, behavior, and interactions

+ Shops Dialog Trees diff --git a/Services/Admin/Pages/Npcs/ShopEdit.cshtml b/Services/Admin/Pages/Npcs/ShopEdit.cshtml new file mode 100644 index 0000000..e7d6f82 --- /dev/null +++ b/Services/Admin/Pages/Npcs/ShopEdit.cshtml @@ -0,0 +1,202 @@ +@page "/admin/npcs/shops/edit" +@model Npcs.ShopEditModel +@using System.Text.Json +@{ + ViewData["Title"] = Model.IsNew ? "Create NPC Shop" : $"Edit Shop: {Model.Shop.Name}"; + var itemCatalog = JsonSerializer.Serialize(Model.ItemDefinitions.Select(i => new + { + id = i.Id, + name = i.Name, + rarity = i.Rarity.ToString(), + type = i.Type.ToString(), + search = ($"{i.Name} {i.Rarity} {i.Type} {i.Id}").ToLowerInvariant() + })); +} + +
+
+ + + + + +
+

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

+

@(Model.IsNew ? "Create a slot-based shop inventory" : $"ID: {Model.Shop.Id}")

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

Shop Slots (@Model.Items.Count)

+ +
+ + + + + + + + + + + + + + @foreach (var i in Model.Items) + { + + + + + + + + + + } + @if (!Model.Items.Any()) + { + + + + } + +
SlotItemQtyPriceRestockIntervalActions
@i.SlotIndex +
@i.ItemDefinition?.Name
+
ID: @i.ItemDefinitionId
+
@i.Quantity@i.PriceGold@(i.Restocks ? "Yes" : "No")@i.RestockIntervalSeconds s +
+ +
+ @Html.AntiForgeryToken() + + + +
+
+
No slot items yet.
+
+ + + } +
+ +@section Scripts { + +} + diff --git a/Services/Admin/Pages/Npcs/ShopEdit.cshtml.cs b/Services/Admin/Pages/Npcs/ShopEdit.cshtml.cs new file mode 100644 index 0000000..b3d773a --- /dev/null +++ b/Services/Admin/Pages/Npcs/ShopEdit.cshtml.cs @@ -0,0 +1,167 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using DataServer.Data; +using DataServer.Data.Entities; + +namespace GameServices.Admin.Pages.Npcs; + +public class ShopEditModel : AdminPageModel +{ + private readonly GameDbContext _db; + + public ShopEditModel(GameDbContext db) + { + _db = db; + } + + [BindProperty] + public DungeonNpcShop Shop { get; set; } = new(); + + public List Items { get; set; } = new(); + public List ItemDefinitions { get; set; } = new(); + + public bool IsNew => Shop.Id == 0; + public string? Message { get; set; } + public bool IsError { get; set; } + + public async Task OnGetAsync(int? id) + { + await LoadItemDefinitionsAsync(); + + if (id.HasValue) + { + var shop = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == id.Value); + if (shop != null) + { + Shop = shop; + Items = await _db.DungeonNpcShopItems + .Include(i => i.ItemDefinition) + .Where(i => i.ShopId == shop.Id) + .OrderBy(i => i.SlotIndex) + .ToListAsync(); + return; + } + } + + Shop = new DungeonNpcShop + { + Name = "", + IsEnabled = true + }; + } + + public async Task OnPostAsync() + { + await LoadItemDefinitionsAsync(); + Shop.Name = (Shop.Name ?? "").Trim(); + Shop.IsEnabled = Request.Form.ContainsKey("Shop.IsEnabled"); + + if (string.IsNullOrWhiteSpace(Shop.Name)) + { + IsError = true; + Message = "Shop name is required."; + return Page(); + } + + try + { + if (Shop.Id == 0) + { + Shop.CreatedAt = DateTime.UtcNow; + Shop.UpdatedAt = DateTime.UtcNow; + _db.DungeonNpcShops.Add(Shop); + await _db.SaveChangesAsync(); + return RedirectToPage("/Npcs/ShopEdit", new { id = Shop.Id }); + } + + var existing = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == Shop.Id); + if (existing == null) + { + IsError = true; + Message = "Shop not found."; + return Page(); + } + + existing.Name = Shop.Name; + existing.IsEnabled = Shop.IsEnabled; + existing.UpdatedAt = DateTime.UtcNow; + await _db.SaveChangesAsync(); + + Shop = existing; + Items = await _db.DungeonNpcShopItems + .Include(i => i.ItemDefinition) + .Where(i => i.ShopId == Shop.Id) + .OrderBy(i => i.SlotIndex) + .ToListAsync(); + Message = "Shop saved."; + } + catch (Exception ex) + { + IsError = true; + Message = $"Error: {ex.Message}"; + } + + return Page(); + } + + public async Task OnPostUpsertItemAsync( + int shopId, + int slotIndex, + int itemDefinitionId, + int quantity, + long priceGold, + bool restocks, + int restockIntervalSeconds) + { + var shop = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == shopId); + if (shop == null) return RedirectToPage("/Npcs/Shops"); + + var item = await _db.DungeonNpcShopItems.FirstOrDefaultAsync(i => i.ShopId == shopId && i.SlotIndex == slotIndex); + if (item == null) + { + item = new DungeonNpcShopItem + { + ShopId = shopId, + SlotIndex = Math.Max(0, slotIndex) + }; + _db.DungeonNpcShopItems.Add(item); + } + + item.ItemDefinitionId = itemDefinitionId; + item.Quantity = Math.Max(1, quantity); + item.PriceGold = Math.Max(0, priceGold); + item.Restocks = restocks; + item.RestockIntervalSeconds = Math.Max(1, restockIntervalSeconds); + if (item.Restocks && !item.LastRestockAt.HasValue) + { + item.LastRestockAt = DateTime.UtcNow; + } + + shop.UpdatedAt = DateTime.UtcNow; + await _db.SaveChangesAsync(); + return RedirectToPage("/Npcs/ShopEdit", new { id = shopId }); + } + + public async Task OnPostDeleteItemAsync(int shopId, int itemId) + { + var item = await _db.DungeonNpcShopItems.FirstOrDefaultAsync(i => i.Id == itemId && i.ShopId == shopId); + if (item != null) + { + _db.DungeonNpcShopItems.Remove(item); + var shop = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == shopId); + if (shop != null) shop.UpdatedAt = DateTime.UtcNow; + await _db.SaveChangesAsync(); + } + + return RedirectToPage("/Npcs/ShopEdit", new { id = shopId }); + } + + private async Task LoadItemDefinitionsAsync() + { + ItemDefinitions = await _db.DungeonItemDefinitions + .Where(i => i.IsEnabled) + .OrderBy(i => i.Name) + .ToListAsync(); + } +} + diff --git a/Services/Admin/Pages/Npcs/Shops.cshtml b/Services/Admin/Pages/Npcs/Shops.cshtml new file mode 100644 index 0000000..eb252a2 --- /dev/null +++ b/Services/Admin/Pages/Npcs/Shops.cshtml @@ -0,0 +1,68 @@ +@page "/admin/npcs/shops" +@model Npcs.ShopsModel +@{ + ViewData["Title"] = "NPC Shops"; +} + +
+
+
+

NPC Shops

+

Create and manage shop inventories for NPCs

+
+ Create Shop +
+ +
+
+
+ + +
+ + Clear +
+
+ +
+ + + + + + + + + + + + @foreach (var s in Model.Shops) + { + + + + + + + + } + @if (!Model.Shops.Any()) + { + + + + } + +
IDNameSlotsEnabledActions
@s.Id@s.Name@Model.ItemCountByShopId.GetValueOrDefault(s.Id)@(s.IsEnabled ? "Yes" : "No") +
+ Edit +
+ @Html.AntiForgeryToken() + + +
+
+
No shops found.
+
+
+ diff --git a/Services/Admin/Pages/Npcs/Shops.cshtml.cs b/Services/Admin/Pages/Npcs/Shops.cshtml.cs new file mode 100644 index 0000000..44a9bee --- /dev/null +++ b/Services/Admin/Pages/Npcs/Shops.cshtml.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using DataServer.Data; +using DataServer.Data.Entities; + +namespace GameServices.Admin.Pages.Npcs; + +public class ShopsModel : AdminPageModel +{ + private readonly GameDbContext _db; + + public ShopsModel(GameDbContext db) + { + _db = db; + } + + [FromQuery(Name = "search")] + public string? Search { get; set; } + + public List Shops { get; set; } = new(); + public Dictionary ItemCountByShopId { get; set; } = new(); + + public async Task OnGetAsync() + { + var query = _db.DungeonNpcShops.AsQueryable(); + if (!string.IsNullOrWhiteSpace(Search)) + { + query = query.Where(s => s.Name.Contains(Search)); + } + + Shops = await query.OrderBy(s => s.Id).ToListAsync(); + var ids = Shops.Select(s => s.Id).ToList(); + ItemCountByShopId = await _db.DungeonNpcShopItems + .Where(i => ids.Contains(i.ShopId)) + .GroupBy(i => i.ShopId) + .Select(g => new { ShopId = g.Key, Count = g.Count() }) + .ToDictionaryAsync(x => x.ShopId, x => x.Count); + } + + public async Task OnPostDeleteAsync(int id) + { + var shop = await _db.DungeonNpcShops.FindAsync(id); + if (shop != null) + { + _db.DungeonNpcShops.Remove(shop); + await _db.SaveChangesAsync(); + } + + return RedirectToPage(); + } +} + diff --git a/Services/Admin/Pages/Shared/_Layout.cshtml b/Services/Admin/Pages/Shared/_Layout.cshtml index 4f26fa0..cd45d3c 100644 --- a/Services/Admin/Pages/Shared/_Layout.cshtml +++ b/Services/Admin/Pages/Shared/_Layout.cshtml @@ -117,6 +117,10 @@ NPCs + + + Shops + Dialog Trees