Add NPC shop admin pages with slot-based inventory editing

This commit is contained in:
dingo
2026-02-14 10:41:32 -05:00
parent 40d8570550
commit 715c78b616
7 changed files with 498 additions and 1 deletions
+4 -1
View File
@@ -293,7 +293,10 @@
<div class="card">
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<h2 class="text-lg font-semibold text-white">Linked Systems</h2>
<a href="/admin/npcs/dialogtrees" class="text-sm text-indigo-400 hover:underline">Manage Dialog Trees</a>
<div class="flex items-center gap-3">
<a href="/admin/npcs/shops" class="text-sm text-indigo-400 hover:underline">Manage Shops</a>
<a href="/admin/npcs/dialogtrees" class="text-sm text-indigo-400 hover:underline">Manage Dialog Trees</a>
</div>
</div>
<div class="p-6 grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
+1
View File
@@ -11,6 +11,7 @@
<p class="text-gray-400 mt-1">Manage NPC definitions, stats, behavior, and interactions</p>
</div>
<div class="flex items-center gap-2">
<a href="/admin/npcs/shops" class="btn btn-secondary">Shops</a>
<a href="/admin/npcs/dialogtrees" class="btn btn-secondary">Dialog Trees</a>
<a href="/admin/npcs/edit" class="btn btn-primary flex items-center gap-2">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+202
View File
@@ -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()
}));
}
<div class="space-y-6 max-w-6xl">
<div class="flex items-center gap-4">
<a href="/admin/npcs/shops" class="p-2 hover:bg-gray-800 rounded-lg">
<svg class="w-6 h-6 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
</svg>
</a>
<div>
<h1 class="text-3xl font-bold text-white">@(Model.IsNew ? "Create NPC Shop" : "Edit NPC Shop")</h1>
<p class="text-gray-400 mt-1">@(Model.IsNew ? "Create a slot-based shop inventory" : $"ID: {Model.Shop.Id}")</p>
</div>
</div>
@if (!string.IsNullOrEmpty(Model.Message))
{
<div class="p-4 rounded-lg @(Model.IsError ? "bg-red-900/50 border border-red-700 text-red-200" : "bg-green-900/50 border border-green-700 text-green-200")">
@Model.Message
</div>
}
<form method="post" class="card p-6 space-y-4">
@Html.AntiForgeryToken()
<input type="hidden" name="Shop.Id" value="@Model.Shop.Id" />
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="md:col-span-2">
<label class="label">Shop Name *</label>
<input type="text" name="Shop.Name" value="@Model.Shop.Name" class="input" maxlength="64" required />
</div>
<div class="flex items-center pt-6">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" name="Shop.IsEnabled" value="true" checked="@Model.Shop.IsEnabled" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
<span class="text-gray-300">Enabled</span>
</label>
</div>
</div>
<div class="flex items-center gap-3">
<button type="submit" class="btn btn-primary">@(Model.IsNew ? "Create Shop" : "Save Shop")</button>
<a href="/admin/npcs/shops" class="btn btn-secondary">Back</a>
</div>
</form>
@if (!Model.IsNew)
{
<div class="card overflow-hidden">
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<h2 class="text-lg font-semibold text-white">Shop Slots (@Model.Items.Count)</h2>
<button type="button" class="btn btn-primary btn-sm" onclick="openItemModal()">+ Add Slot</button>
</div>
<table class="w-full">
<thead class="bg-gray-900/50">
<tr>
<th class="table-header w-20">Slot</th>
<th class="table-header">Item</th>
<th class="table-header w-20">Qty</th>
<th class="table-header w-24">Price</th>
<th class="table-header w-20">Restock</th>
<th class="table-header w-24">Interval</th>
<th class="table-header w-24">Actions</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-700">
@foreach (var i in Model.Items)
{
<tr class="hover:bg-gray-700/30">
<td class="table-cell font-mono text-gray-300">@i.SlotIndex</td>
<td class="table-cell">
<div class="text-white">@i.ItemDefinition?.Name</div>
<div class="text-xs text-gray-500">ID: @i.ItemDefinitionId</div>
</td>
<td class="table-cell text-gray-300">@i.Quantity</td>
<td class="table-cell text-yellow-300">@i.PriceGold</td>
<td class="table-cell text-gray-300">@(i.Restocks ? "Yes" : "No")</td>
<td class="table-cell text-gray-300">@i.RestockIntervalSeconds s</td>
<td class="table-cell">
<div class="flex gap-2">
<button type="button" class="btn btn-secondary btn-sm" onclick="openItemModal(@i.SlotIndex, @i.ItemDefinitionId, @i.Quantity, @i.PriceGold, @i.Restocks.ToString().ToLower(), @i.RestockIntervalSeconds)">Edit</button>
<form method="post" asp-page-handler="DeleteItem" class="inline" onsubmit="return confirm('Delete this slot item?');">
@Html.AntiForgeryToken()
<input type="hidden" name="shopId" value="@Model.Shop.Id" />
<input type="hidden" name="itemId" value="@i.Id" />
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</div>
</td>
</tr>
}
@if (!Model.Items.Any())
{
<tr>
<td colspan="7" class="table-cell text-center text-gray-500 py-8">No slot items yet.</td>
</tr>
}
</tbody>
</table>
</div>
<div id="itemModal" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50">
<div class="bg-gray-800 rounded-xl p-6 max-w-2xl w-full mx-4 border border-gray-700">
<h3 class="text-lg font-semibold text-white mb-4">Upsert Shop Slot</h3>
<form method="post" asp-page-handler="UpsertItem" class="space-y-4">
@Html.AntiForgeryToken()
<input type="hidden" name="shopId" value="@Model.Shop.Id" />
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="label">Slot Index *</label>
<input id="slotIndex" type="number" name="slotIndex" class="input" min="0" required />
</div>
<div>
<label class="label">Filter Items</label>
<input id="itemSearch" type="text" class="input" placeholder="Type to filter items..." oninput="filterItems()" />
</div>
<div class="md:col-span-2">
<label class="label">Item *</label>
<select id="itemDefinitionId" name="itemDefinitionId" class="input" size="10" style="height:auto;" required>
@foreach (var item in Model.ItemDefinitions)
{
<option value="@item.Id" data-search="@($"{item.Name} {item.Rarity} {item.Type} {item.Id}".ToLowerInvariant())">@item.Name (@item.Rarity / @item.Type)</option>
}
</select>
</div>
<div>
<label class="label">Quantity *</label>
<input id="quantity" type="number" name="quantity" class="input" min="1" value="1" required />
</div>
<div>
<label class="label">Price (Gold) *</label>
<input id="priceGold" type="number" name="priceGold" class="input" min="0" value="1" required />
</div>
<div>
<label class="label">Restock Interval (seconds)</label>
<input id="restockIntervalSeconds" type="number" name="restockIntervalSeconds" class="input" min="1" value="300" />
</div>
<div class="flex items-center pt-6">
<label class="flex items-center gap-2 cursor-pointer">
<input id="restocks" type="checkbox" name="restocks" value="true" checked class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
<span class="text-gray-300">Restocks</span>
</label>
</div>
</div>
<div class="flex justify-end gap-3 pt-2">
<button type="button" class="btn btn-secondary" onclick="closeItemModal()">Cancel</button>
<button type="submit" class="btn btn-primary">Save Slot</button>
</div>
</form>
</div>
</div>
}
</div>
@section Scripts {
<script>
const itemCatalog = @Html.Raw(itemCatalog);
function openItemModal(slotIndex, itemId, qty, price, restocks, interval) {
document.getElementById('slotIndex').value = slotIndex ?? '';
document.getElementById('itemDefinitionId').value = itemId ?? '';
document.getElementById('quantity').value = qty ?? 1;
document.getElementById('priceGold').value = price ?? 1;
document.getElementById('restocks').checked = (restocks ?? true) === true;
document.getElementById('restockIntervalSeconds').value = interval ?? 300;
document.getElementById('itemSearch').value = '';
filterItems();
document.getElementById('itemModal').classList.remove('hidden');
document.getElementById('itemModal').classList.add('flex');
}
function closeItemModal() {
document.getElementById('itemModal').classList.add('hidden');
document.getElementById('itemModal').classList.remove('flex');
}
function filterItems() {
const term = (document.getElementById('itemSearch').value || '').toLowerCase();
const select = document.getElementById('itemDefinitionId');
for (let i = 0; i < select.options.length; i++) {
const opt = select.options[i];
const text = (opt.dataset.search || opt.text || '').toLowerCase();
opt.style.display = term === '' || text.includes(term) ? '' : 'none';
}
}
document.addEventListener('keydown', e => {
if (e.key === 'Escape') closeItemModal();
});
</script>
}
@@ -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<DungeonNpcShopItem> Items { get; set; } = new();
public List<DungeonItemDefinition> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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();
}
}
+68
View File
@@ -0,0 +1,68 @@
@page "/admin/npcs/shops"
@model Npcs.ShopsModel
@{
ViewData["Title"] = "NPC Shops";
}
<div class="space-y-6">
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-white">NPC Shops</h1>
<p class="text-gray-400 mt-1">Create and manage shop inventories for NPCs</p>
</div>
<a href="/admin/npcs/shops/edit" class="btn btn-primary">Create Shop</a>
</div>
<div class="card p-4">
<form method="get" class="flex items-end gap-4">
<div class="flex-1">
<label class="label">Search</label>
<input type="text" name="search" value="@Model.Search" class="input" placeholder="Search by shop name..." />
</div>
<button class="btn btn-secondary" type="submit">Filter</button>
<a class="btn btn-secondary" href="/admin/npcs/shops">Clear</a>
</form>
</div>
<div class="card overflow-hidden">
<table class="w-full">
<thead class="bg-gray-900/50">
<tr>
<th class="table-header w-16">ID</th>
<th class="table-header">Name</th>
<th class="table-header w-24">Slots</th>
<th class="table-header w-20">Enabled</th>
<th class="table-header w-24">Actions</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-700">
@foreach (var s in Model.Shops)
{
<tr class="hover:bg-gray-700/30">
<td class="table-cell font-mono text-gray-500">@s.Id</td>
<td class="table-cell text-white font-medium">@s.Name</td>
<td class="table-cell text-gray-300">@Model.ItemCountByShopId.GetValueOrDefault(s.Id)</td>
<td class="table-cell text-gray-300">@(s.IsEnabled ? "Yes" : "No")</td>
<td class="table-cell">
<div class="flex gap-2">
<a href="/admin/npcs/shops/edit?id=@s.Id" class="btn btn-secondary btn-sm">Edit</a>
<form method="post" asp-page-handler="Delete" class="inline" onsubmit="return confirm('Delete this shop?');">
@Html.AntiForgeryToken()
<input type="hidden" name="id" value="@s.Id" />
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</div>
</td>
</tr>
}
@if (!Model.Shops.Any())
{
<tr>
<td colspan="5" class="table-cell text-center text-gray-500 py-10">No shops found.</td>
</tr>
}
</tbody>
</table>
</div>
</div>
+52
View File
@@ -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<DungeonNpcShop> Shops { get; set; } = new();
public Dictionary<int, int> 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<IActionResult> OnPostDeleteAsync(int id)
{
var shop = await _db.DungeonNpcShops.FindAsync(id);
if (shop != null)
{
_db.DungeonNpcShops.Remove(shop);
await _db.SaveChangesAsync();
}
return RedirectToPage();
}
}
@@ -117,6 +117,10 @@
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5V4H2v16h5m10 0v-2a4 4 0 00-4-4H11a4 4 0 00-4 4v2m10 0H7m8-10a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
<span>NPCs</span>
</a>
<a href="/admin/npcs/shops" class="sidebar-link @(ViewContext.RouteData.Values["page"]?.ToString()?.StartsWith("/Npcs/Shop") == true ? "active" : "")" data-tooltip="Shops">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-1.5 6h12M9 21a1 1 0 100-2 1 1 0 000 2zm8 0a1 1 0 100-2 1 1 0 000 2z"/></svg>
<span>Shops</span>
</a>
<a href="/admin/npcs/dialogtrees" class="sidebar-link @(ViewContext.RouteData.Values["page"]?.ToString()?.StartsWith("/Npcs/Dialog") == true ? "active" : "")" data-tooltip="Dialog Trees">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-4l-4 4v-4z"/></svg>
<span>Dialog Trees</span>