Add admin NPC management pages and sidebar navigation
This commit is contained in:
@@ -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}";
|
||||
}
|
||||
|
||||
<div class="space-y-6 max-w-6xl">
|
||||
<div class="flex items-center gap-4">
|
||||
<a href="/admin/npcs" 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" : "Edit NPC")</h1>
|
||||
<p class="text-gray-400 mt-1">@(Model.IsNew ? "Add a new NPC definition" : $"ID: {Model.Npc.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="space-y-6">
|
||||
@Html.AntiForgeryToken()
|
||||
<input type="hidden" name="Npc.Id" value="@Model.Npc.Id" />
|
||||
|
||||
<div class="card">
|
||||
<div class="p-4 border-b border-gray-700">
|
||||
<h2 class="text-lg font-semibold text-white">Basic Information</h2>
|
||||
</div>
|
||||
<div class="p-6 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||
<div class="lg:col-span-2">
|
||||
<label class="label">Name *</label>
|
||||
<input type="text" name="Npc.Name" value="@Model.Npc.Name" class="input" maxlength="64" required />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Key</label>
|
||||
<input type="text" name="Npc.Key" value="@Model.Npc.Key" class="input" maxlength="64" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Level</label>
|
||||
<input type="number" name="Npc.Level" value="@Model.Npc.Level" class="input" min="1" />
|
||||
</div>
|
||||
<div class="lg:col-span-4">
|
||||
<label class="label">Description</label>
|
||||
<textarea name="Npc.Description" class="input" maxlength="256" rows="2">@Model.Npc.Description</textarea>
|
||||
</div>
|
||||
<div class="lg:col-span-4 flex flex-wrap items-center gap-6 pt-2">
|
||||
<label class="flex items-center gap-2 cursor-pointer">
|
||||
<input type="checkbox" name="Npc.IsEnabled" value="true" checked="@Model.Npc.IsEnabled" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
|
||||
<span class="text-gray-300">Enabled</span>
|
||||
</label>
|
||||
<label class="flex items-center gap-2 cursor-pointer">
|
||||
<input type="checkbox" name="Npc.IsKillable" value="true" checked="@Model.Npc.IsKillable" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
|
||||
<span class="text-gray-300">Killable</span>
|
||||
</label>
|
||||
<label class="flex items-center gap-2 cursor-pointer">
|
||||
<input type="checkbox" name="Npc.CanWander" value="true" checked="@Model.Npc.CanWander" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
|
||||
<span class="text-gray-300">Can Wander</span>
|
||||
</label>
|
||||
<label class="flex items-center gap-2 cursor-pointer">
|
||||
<input type="checkbox" name="Npc.CanShop" value="true" checked="@Model.Npc.CanShop" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
|
||||
<span class="text-gray-300">Can Shop</span>
|
||||
</label>
|
||||
<label class="flex items-center gap-2 cursor-pointer">
|
||||
<input type="checkbox" name="Npc.HasDialog" value="true" checked="@Model.Npc.HasDialog" class="w-4 h-4 rounded border-gray-600 bg-gray-800 text-indigo-600" />
|
||||
<span class="text-gray-300">Has Dialog</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="p-4 border-b border-gray-700">
|
||||
<h2 class="text-lg font-semibold text-white">Behavior & Combat</h2>
|
||||
</div>
|
||||
<div class="p-6 grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-4">
|
||||
<div>
|
||||
<label class="label">Behavior Mode</label>
|
||||
<select name="Npc.BehaviorMode" class="input">
|
||||
@foreach (var b in Enum.GetValues<NpcBehaviorMode>())
|
||||
{
|
||||
<option value="@b" selected="@(Model.Npc.BehaviorMode == b)">@b</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">On Attack</label>
|
||||
<select name="Npc.OnAttackBehavior" class="input">
|
||||
@foreach (var b in Enum.GetValues<NpcOnAttackBehavior>())
|
||||
{
|
||||
<option value="@b" selected="@(Model.Npc.OnAttackBehavior == b)">@b</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Max HP</label>
|
||||
<input type="number" name="Npc.MaxHealth" value="@Model.Npc.MaxHealth" class="input" min="1" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Attack Power</label>
|
||||
<input type="number" name="Npc.AttackPower" value="@Model.Npc.AttackPower" class="input" min="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Defense</label>
|
||||
<input type="number" name="Npc.Defense" value="@Model.Npc.Defense" class="input" min="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Strength</label>
|
||||
<input type="number" name="Npc.Strength" value="@Model.Npc.Strength" class="input" min="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Dexterity</label>
|
||||
<input type="number" name="Npc.Dexterity" value="@Model.Npc.Dexterity" class="input" min="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Intelligence</label>
|
||||
<input type="number" name="Npc.Intelligence" value="@Model.Npc.Intelligence" class="input" min="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Wisdom</label>
|
||||
<input type="number" name="Npc.Wisdom" value="@Model.Npc.Wisdom" class="input" min="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Endurance</label>
|
||||
<input type="number" name="Npc.Endurance" value="@Model.Npc.Endurance" class="input" min="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Wander Radius (px)</label>
|
||||
<input type="number" name="Npc.WanderRadiusPx" value="@Model.Npc.WanderRadiusPx" class="input" min="0" step="1" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Wander Interval (s)</label>
|
||||
<input type="number" name="Npc.WanderIntervalSeconds" value="@Model.Npc.WanderIntervalSeconds" class="input" min="0.1" step="0.1" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="p-4 border-b border-gray-700">
|
||||
<h2 class="text-lg font-semibold text-white">Visual & Linked Systems</h2>
|
||||
</div>
|
||||
<div class="p-6 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||
<div>
|
||||
<label class="label">Tint Color</label>
|
||||
<div class="flex gap-2">
|
||||
<input type="color" value="@Model.Npc.TintColor" class="w-10 h-10 rounded border border-gray-700 bg-gray-800 cursor-pointer" oninput="document.getElementById('tintColor').value=this.value" />
|
||||
<input id="tintColor" type="text" name="Npc.TintColor" value="@Model.Npc.TintColor" class="input" maxlength="16" />
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Scale</label>
|
||||
<input type="number" name="Npc.Scale" value="@Model.Npc.Scale" class="input" min="0.1" step="0.1" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Loot Table</label>
|
||||
<select name="Npc.LootTableId" class="input">
|
||||
<option value="">None</option>
|
||||
@foreach (var lt in Model.LootTables)
|
||||
{
|
||||
<option value="@lt.Id" selected="@(Model.Npc.LootTableId == lt.Id)">@lt.Name</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">Shop</label>
|
||||
<select name="Npc.ShopId" class="input">
|
||||
<option value="">None</option>
|
||||
@foreach (var s in Model.Shops)
|
||||
{
|
||||
<option value="@s.Id" selected="@(Model.Npc.ShopId == s.Id)">@s.Name</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="lg:col-span-2">
|
||||
<label class="label">Dialog Tree</label>
|
||||
<select name="Npc.DialogTreeId" class="input">
|
||||
<option value="">None</option>
|
||||
@foreach (var d in Model.DialogTrees)
|
||||
{
|
||||
<option value="@d.Id" selected="@(Model.Npc.DialogTreeId == d.Id)">@d.Name</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="lg:col-span-4">
|
||||
<label class="label">Resistances JSON</label>
|
||||
<textarea name="Npc.ResistancesJson" class="input font-mono text-xs" rows="3">@Model.Npc.ResistancesJson</textarea>
|
||||
</div>
|
||||
<div class="lg:col-span-4">
|
||||
<label class="label">Ability Definition IDs JSON</label>
|
||||
<textarea name="Npc.AbilityDefinitionIdsJson" class="input font-mono text-xs" rows="3">@Model.Npc.AbilityDefinitionIdsJson</textarea>
|
||||
</div>
|
||||
<div class="lg:col-span-4">
|
||||
<label class="label">Sprite Config JSON</label>
|
||||
<textarea name="Npc.SpriteConfigJson" class="input font-mono text-xs" rows="5">@Model.Npc.SpriteConfigJson</textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<button type="submit" class="btn btn-primary">@(Model.IsNew ? "Create NPC" : "Save Changes")</button>
|
||||
<a href="/admin/npcs" class="btn btn-secondary">Back</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
@page "/admin/npcs"
|
||||
@model Npcs.IndexModel
|
||||
@{
|
||||
ViewData["Title"] = "NPCs";
|
||||
}
|
||||
|
||||
<div class="space-y-6">
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold text-white">NPCs</h1>
|
||||
<p class="text-gray-400 mt-1">Manage NPC definitions, stats, behavior, and interactions</p>
|
||||
</div>
|
||||
<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">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
|
||||
</svg>
|
||||
Create NPC
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="card p-4">
|
||||
<form method="get" class="flex flex-wrap items-end gap-4">
|
||||
<div class="flex-1 min-w-[240px]">
|
||||
<label class="label">Search</label>
|
||||
<input type="text" name="search" value="@Model.Search" class="input" placeholder="Search by name, key, description..." />
|
||||
</div>
|
||||
<div class="w-44">
|
||||
<label class="label">Status</label>
|
||||
<select name="enabled" class="input">
|
||||
<option value="">All</option>
|
||||
<option value="true" selected="@(Model.EnabledFilter == true)">Enabled</option>
|
||||
<option value="false" selected="@(Model.EnabledFilter == false)">Disabled</option>
|
||||
</select>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-secondary">Filter</button>
|
||||
<a href="/admin/npcs" class="btn btn-secondary">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-20">Level</th>
|
||||
<th class="table-header w-28">Behavior</th>
|
||||
<th class="table-header w-24">Killable</th>
|
||||
<th class="table-header w-20">Shop</th>
|
||||
<th class="table-header w-20">Dialog</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 npc in Model.Npcs)
|
||||
{
|
||||
<tr class="hover:bg-gray-700/30">
|
||||
<td class="table-cell font-mono text-gray-500">@npc.Id</td>
|
||||
<td class="table-cell">
|
||||
<div class="font-medium text-white">@npc.Name</div>
|
||||
<div class="text-xs text-gray-500">@npc.Key</div>
|
||||
</td>
|
||||
<td class="table-cell text-gray-300">@npc.Level</td>
|
||||
<td class="table-cell text-gray-300">@npc.BehaviorMode</td>
|
||||
<td class="table-cell text-gray-300">@(npc.IsKillable ? "Yes" : "No")</td>
|
||||
<td class="table-cell text-gray-300">@(npc.ShopId.HasValue ? "Yes" : "No")</td>
|
||||
<td class="table-cell text-gray-300">@(npc.DialogTreeId.HasValue ? "Yes" : "No")</td>
|
||||
<td class="table-cell">
|
||||
@if (npc.IsEnabled)
|
||||
{
|
||||
<span class="w-3 h-3 bg-green-500 rounded-full inline-block" title="Enabled"></span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="w-3 h-3 bg-red-500 rounded-full inline-block" title="Disabled"></span>
|
||||
}
|
||||
</td>
|
||||
<td class="table-cell">
|
||||
<div class="flex items-center gap-1">
|
||||
<a href="/admin/npcs/edit?id=@npc.Id" class="p-1.5 hover:bg-gray-700 rounded" title="Edit">
|
||||
<svg class="w-4 h-4 text-indigo-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
|
||||
</svg>
|
||||
</a>
|
||||
<button type="button" onclick="confirmDelete(@npc.Id, '@npc.Name.Replace("'", "\\'"))" class="p-1.5 hover:bg-gray-700 rounded" title="Delete">
|
||||
<svg class="w-4 h-4 text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
@if (!Model.Npcs.Any())
|
||||
{
|
||||
<tr>
|
||||
<td colspan="9" class="table-cell text-center text-gray-500 py-12">
|
||||
No NPCs found. <a href="/admin/npcs/edit" class="text-indigo-400 hover:underline">Create one?</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
@if (Model.TotalPages > 1)
|
||||
{
|
||||
<div class="flex items-center justify-between">
|
||||
<p class="text-sm text-gray-400">
|
||||
Showing @((Model.CurrentPage - 1) * Model.PageSize + 1)-@Math.Min(Model.CurrentPage * Model.PageSize, Model.TotalCount) of @Model.TotalCount
|
||||
</p>
|
||||
<div class="flex items-center gap-2">
|
||||
@if (Model.CurrentPage > 1)
|
||||
{
|
||||
<a href="@Model.GetPageUrl(Model.CurrentPage - 1)" class="btn btn-secondary">Prev</a>
|
||||
}
|
||||
<span class="text-gray-400 px-2">@Model.CurrentPage / @Model.TotalPages</span>
|
||||
@if (Model.CurrentPage < Model.TotalPages)
|
||||
{
|
||||
<a href="@Model.GetPageUrl(Model.CurrentPage + 1)" class="btn btn-secondary">Next</a>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div id="deleteModal" 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-md w-full mx-4 border border-gray-700">
|
||||
<h3 class="text-lg font-semibold text-white mb-2">Delete NPC</h3>
|
||||
<p class="text-gray-400 mb-4">Are you sure you want to delete "<span id="deleteItemName" class="text-white"></span>"?</p>
|
||||
<form method="post" asp-page-handler="Delete" class="flex justify-end gap-3">
|
||||
@Html.AntiForgeryToken()
|
||||
<input type="hidden" name="id" id="deleteItemId" />
|
||||
<button type="button" onclick="closeDeleteModal()" class="btn btn-secondary">Cancel</button>
|
||||
<button type="submit" class="btn btn-danger">Delete</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script>
|
||||
function confirmDelete(id, name) {
|
||||
document.getElementById('deleteItemId').value = id;
|
||||
document.getElementById('deleteItemName').textContent = name;
|
||||
document.getElementById('deleteModal').classList.remove('hidden');
|
||||
document.getElementById('deleteModal').classList.add('flex');
|
||||
}
|
||||
function closeDeleteModal() {
|
||||
document.getElementById('deleteModal').classList.add('hidden');
|
||||
document.getElementById('deleteModal').classList.remove('flex');
|
||||
}
|
||||
document.addEventListener('keydown', e => { if (e.key === 'Escape') closeDeleteModal(); });
|
||||
</script>
|
||||
}
|
||||
@@ -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<DungeonNpcDefinition> 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<string> { $"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<IActionResult> OnPostDeleteAsync(int id)
|
||||
{
|
||||
var npc = await _db.DungeonNpcDefinitions.FindAsync(id);
|
||||
if (npc != null)
|
||||
{
|
||||
_db.DungeonNpcDefinitions.Remove(npc);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return RedirectToPage();
|
||||
}
|
||||
}
|
||||
@@ -113,6 +113,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="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/></svg>
|
||||
<span>Mobs</span>
|
||||
</a>
|
||||
<a href="/admin/npcs" class="sidebar-link @(ViewContext.RouteData.Values["page"]?.ToString()?.StartsWith("/Npcs") == true ? "active" : "")" data-tooltip="NPCs">
|
||||
<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/spritesheets" class="sidebar-link @(ViewContext.RouteData.Values["page"]?.ToString()?.StartsWith("/SpriteSheets") == true ? "active" : "")" data-tooltip="SpriteSheets">
|
||||
<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="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" /> </svg>
|
||||
<span>Sprite Sheets</span>
|
||||
|
||||
Reference in New Issue
Block a user