Files

554 lines
28 KiB
Plaintext

@page "/admin/npcs/edit"
@model Npcs.EditModel
@using System.Text.Json
@using DataServer.Data.Enums
@{
ViewData["Title"] = Model.IsNew ? "Create NPC" : $"Edit: {Model.Npc.Name}";
var selectedAbilitiesJson = JsonSerializer.Serialize(Model.SelectedAbilityIds);
var selectedAnimationsJson = JsonSerializer.Serialize(Model.SelectedAnimations);
var animationCatalogJson = JsonSerializer.Serialize(Model.AllAnimations.Select(a => new
{
id = a.Id,
key = a.Key,
sheetName = a.SpriteSheet != null ? a.SpriteSheet.Name : "",
sheetPath = a.SpriteSheet != null ? a.SpriteSheet.CdnPath : "",
frameSequence = a.FrameSequence,
fps = a.Fps,
loop = a.Loop,
flipX = a.FlipX,
flipY = a.FlipY,
search = ($"{a.Key} {(a.Name ?? "")} {(a.SpriteSheet != null ? a.SpriteSheet.Name : "")} {(a.SpriteSheet != null ? a.SpriteSheet.CdnPath : "")}").ToLowerInvariant()
}));
var resistanceOptions = string.Join("", Model.ResistanceTypes.Select(rt => $"<option value=\"{rt.Key}\">{rt.DisplayName}</option>"));
}
<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">Damage Resistances</h2>
</div>
<div class="p-6">
<div id="resistancesList" class="space-y-2 mb-4">
@{
Dictionary<string, float> resistances = new();
try
{
resistances = JsonSerializer.Deserialize<Dictionary<string, float>>(Model.Npc.ResistancesJson ?? "{}") ?? new();
}
catch { }
}
@foreach (var resist in resistances)
{
<div class="bg-gray-800 border border-gray-700 rounded-lg p-3 flex items-center gap-4 resistance-row">
<select name="resist_name[]" class="input w-40">
@foreach (var rt in Model.ResistanceTypes)
{
<option value="@rt.Key" selected="@(resist.Key == rt.Key)">@rt.DisplayName</option>
}
</select>
<div class="flex items-center gap-2 flex-1">
<input type="range" name="resist_value[]" value="@resist.Value" min="-1" max="1" step="0.05" class="flex-1" oninput="this.nextElementSibling.value = Math.round(this.value * 100)" />
<div class="flex items-center gap-1">
<input type="number" value="@((resist.Value * 100).ToString("0"))" min="-100" max="100" step="5" class="input w-20 text-center text-sm" oninput="this.parentElement.previousElementSibling.value = this.value / 100" />
<span class="text-gray-400">%</span>
</div>
</div>
<button type="button" onclick="this.closest('.resistance-row').remove()" class="btn btn-danger btn-sm">Remove</button>
</div>
}
</div>
<div class="flex items-center gap-4 pt-4 border-t border-gray-700">
<select id="newResistType" class="input w-48">
<option value="">Select type...</option>
@foreach (var rt in Model.ResistanceTypes)
{
<option value="@rt.Key">@rt.DisplayName</option>
}
</select>
<input type="number" id="newResistValue" class="input w-24" value="50" min="-100" max="100" step="5" />
<span class="text-gray-400">%</span>
<button type="button" onclick="addResistance()" class="btn btn-secondary">Add</button>
</div>
</div>
</div>
<div class="card">
<div class="p-4 border-b border-gray-700">
<h2 class="text-lg font-semibold text-white">Abilities</h2>
</div>
<div class="p-6 space-y-4">
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="md:col-span-2">
<label class="label">Filter Abilities</label>
<input id="abilitySearch" type="text" class="input" placeholder="Type to filter abilities..." oninput="filterAbilities()" />
</div>
<div>
<label class="label">Ability List</label>
<select id="abilityPicker" class="input" size="8" style="height:auto;">
@foreach (var a in Model.AllAbilities)
{
<option value="@a.Id" data-search="@($"{a.Name} {a.Key} {a.AbilityType}".ToLowerInvariant())">@a.Name (@a.AbilityType)</option>
}
</select>
</div>
</div>
<button type="button" class="btn btn-secondary" onclick="addSelectedAbility()">Add Selected Ability</button>
<div>
<label class="label">Selected Abilities</label>
<div id="selectedAbilities" class="space-y-2"></div>
</div>
</div>
</div>
<div class="card" id="animations-section">
<div class="p-4 border-b border-gray-700 flex items-center justify-between">
<h2 class="text-lg font-semibold text-white">Animations</h2>
<button type="button" class="btn btn-primary btn-sm" onclick="showAddAnimationModal()">+ Add Animation</button>
</div>
<div class="p-6 overflow-x-auto">
<div id="animationHiddenInputs"></div>
<table class="w-full">
<thead class="bg-gray-900/50">
<tr>
<th class="table-header">Key</th>
<th class="table-header">Sheet</th>
<th class="table-header">Frames</th>
<th class="table-header">FPS</th>
<th class="table-header">Options</th>
<th class="table-header">Override Key</th>
<th class="table-header">Status</th>
<th class="table-header">Actions</th>
</tr>
</thead>
<tbody id="npcAnimationTableBody" class="divide-y divide-gray-700"></tbody>
</table>
</div>
</div>
<div id="addAnimationModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center">
<div class="bg-gray-900 rounded-xl shadow-xl p-6 w-full max-w-md mx-4 border border-gray-700">
<h3 class="text-lg font-semibold text-white mb-4">Add Animation</h3>
<div class="space-y-4">
<div>
<label class="label">Search</label>
<input id="animationSearch" type="text" class="input mb-2" placeholder="Type to search animations..." oninput="filterAnimations()" autocomplete="off" />
<select id="animationSelect" class="input" size="10" style="height: auto;">
@foreach (var anim in Model.AllAnimations)
{
<option value="@anim.Id" data-search="@($"[{(anim.SpriteSheet?.Name ?? "?")}] {anim.Key} {anim.FrameSequence} {anim.Fps}".ToLowerInvariant())">[@(anim.SpriteSheet?.Name ?? "?")] @anim.Key - @anim.FrameSequence (@anim.Fps fps)</option>
}
</select>
</div>
<div>
<label class="label">Override Key (optional)</label>
<input id="animationOverrideKey" type="text" class="input" placeholder="e.g. idle_down" />
</div>
</div>
<div class="flex justify-end gap-3 pt-4">
<button type="button" onclick="closeAddAnimationModal()" class="btn btn-secondary">Cancel</button>
<button type="button" onclick="addAnimationFromModal()" class="btn btn-primary">Assign</button>
</div>
</div>
</div>
<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>
<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>
<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>
<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>
<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>
</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>
@section Scripts {
<script>
const selectedAbilityIds = @Html.Raw(selectedAbilitiesJson);
const selectedAnimations = @Html.Raw(selectedAnimationsJson);
const animationCatalog = @Html.Raw(animationCatalogJson);
const resistanceOptionsHtml = `@Html.Raw(resistanceOptions)`;
const abilityById = {
@foreach (var a in Model.AllAbilities)
{
@:@a.Id: "@a.Name (@a.AbilityType)",
}
};
function renderSelectedAbilities() {
const container = document.getElementById('selectedAbilities');
container.innerHTML = '';
selectedAbilityIds.forEach(id => {
const row = document.createElement('div');
row.className = 'flex items-center justify-between bg-gray-800 border border-gray-700 rounded p-2';
const label = abilityById[id] || ('Ability #' + id);
row.innerHTML = `<span class="text-gray-200">${label}</span><div class="flex items-center gap-2"><input type="hidden" name="ability_ids[]" value="${id}"/><button type="button" class="btn btn-danger btn-sm" onclick="removeAbility(${id})">Remove</button></div>`;
container.appendChild(row);
});
}
function removeAbility(id) {
const idx = selectedAbilityIds.indexOf(id);
if (idx >= 0) {
selectedAbilityIds.splice(idx, 1);
renderSelectedAbilities();
}
}
function addSelectedAbility() {
const select = document.getElementById('abilityPicker');
const id = parseInt(select.value || '0', 10);
if (!id) return;
if (!selectedAbilityIds.includes(id)) {
selectedAbilityIds.push(id);
renderSelectedAbilities();
}
}
function filterAbilities() {
const term = (document.getElementById('abilitySearch').value || '').toLowerCase();
const select = document.getElementById('abilityPicker');
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';
}
}
function addResistance() {
const typeSelect = document.getElementById('newResistType');
const valueInput = document.getElementById('newResistValue');
const key = typeSelect.value;
if (!key) return;
const percent = parseInt(valueInput.value || '0', 10);
const value = percent / 100;
const row = document.createElement('div');
row.className = 'bg-gray-800 border border-gray-700 rounded-lg p-3 flex items-center gap-4 resistance-row';
row.innerHTML = `
<select name="resist_name[]" class="input w-40">${resistanceOptionsHtml.replace(`value="${key}"`, `value="${key}" selected`)}</select>
<div class="flex items-center gap-2 flex-1">
<input type="range" name="resist_value[]" value="${value}" min="-1" max="1" step="0.05" class="flex-1" oninput="this.nextElementSibling.value = Math.round(this.value * 100)" />
<div class="flex items-center gap-1">
<input type="number" value="${percent}" min="-100" max="100" step="5" class="input w-20 text-center text-sm" oninput="this.parentElement.previousElementSibling.value = this.value / 100" />
<span class="text-gray-400">%</span>
</div>
</div>
<button type="button" onclick="this.closest('.resistance-row').remove()" class="btn btn-danger btn-sm">Remove</button>
`;
document.getElementById('resistancesList').appendChild(row);
typeSelect.value = '';
valueInput.value = '50';
}
function getAnimationById(id) {
return animationCatalog.find(a => a.id === id) || null;
}
function showAddAnimationModal() {
document.getElementById('addAnimationModal').classList.remove('hidden');
document.getElementById('addAnimationModal').classList.add('flex');
}
function closeAddAnimationModal() {
document.getElementById('addAnimationModal').classList.add('hidden');
document.getElementById('addAnimationModal').classList.remove('flex');
document.getElementById('animationSearch').value = '';
filterAnimations();
}
function filterAnimations() {
const term = (document.getElementById('animationSearch').value || '').toLowerCase();
const select = document.getElementById('animationSelect');
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';
}
}
function addAnimationFromModal() {
const select = document.getElementById('animationSelect');
const id = parseInt(select.value || '0', 10);
if (!id) return;
const override = (document.getElementById('animationOverrideKey').value || '').trim();
const exists = selectedAnimations.some(a => a.animationDefinitionId === id && (a.overrideKey || '') === override);
if (exists) return;
selectedAnimations.push({
animationDefinitionId: id,
overrideKey: override || null,
isEnabled: true
});
document.getElementById('animationOverrideKey').value = '';
closeAddAnimationModal();
renderAnimationTable();
}
function removeAnimation(idx) {
selectedAnimations.splice(idx, 1);
renderAnimationTable();
}
function toggleAnimation(idx) {
selectedAnimations[idx].isEnabled = !selectedAnimations[idx].isEnabled;
renderAnimationTable();
}
function renderAnimationTable() {
const body = document.getElementById('npcAnimationTableBody');
const hiddenInputs = document.getElementById('animationHiddenInputs');
body.innerHTML = '';
hiddenInputs.innerHTML = '';
if (selectedAnimations.length === 0) {
const tr = document.createElement('tr');
tr.innerHTML = '<td class="table-cell text-center text-gray-500 py-8" colspan="8">No animations assigned. Add animations from the selector.</td>';
body.appendChild(tr);
return;
}
selectedAnimations.forEach((row, idx) => {
const anim = getAnimationById(row.animationDefinitionId);
if (!anim) return;
const key = row.overrideKey && row.overrideKey.length ? row.overrideKey : anim.key;
const options = [anim.loop ? 'Loop' : null, anim.flipX ? 'FlipX' : null, anim.flipY ? 'FlipY' : null].filter(Boolean).join(', ');
const tr = document.createElement('tr');
tr.className = 'hover:bg-gray-700/30';
tr.innerHTML = `
<td class="table-cell text-white font-mono">${anim.key}</td>
<td class="table-cell text-gray-300 text-xs">${anim.sheetName}</td>
<td class="table-cell text-gray-300 font-mono text-xs">${anim.frameSequence}</td>
<td class="table-cell text-gray-300">${anim.fps}</td>
<td class="table-cell text-gray-400 text-xs">${options || '-'}</td>
<td class="table-cell text-gray-300 font-mono text-xs">
<input type="text" class="input" value="${row.overrideKey || ''}" placeholder="-" onchange="updateOverride(${idx}, this.value)" />
</td>
<td class="table-cell">
<span class="${row.isEnabled ? 'text-green-400' : 'text-gray-500'} text-xs">${row.isEnabled ? 'Active' : 'Disabled'}</span>
</td>
<td class="table-cell">
<div class="flex gap-2">
<button type="button" class="btn btn-secondary btn-sm" onclick="toggleAnimation(${idx})">${row.isEnabled ? 'Disable' : 'Enable'}</button>
<button type="button" class="btn btn-danger btn-sm" onclick="removeAnimation(${idx})">Remove</button>
</div>
</td>
`;
body.appendChild(tr);
hiddenInputs.insertAdjacentHTML('beforeend', `<input type="hidden" name="npc_anim_id[]" value="${row.animationDefinitionId}">`);
hiddenInputs.insertAdjacentHTML('beforeend', `<input type="hidden" name="npc_anim_override_key[]" value="${(row.overrideKey || '').replace(/"/g, '&quot;')}">`);
if (row.isEnabled) {
hiddenInputs.insertAdjacentHTML('beforeend', `<input type="hidden" name="npc_anim_enabled[]" value="${idx}">`);
}
});
}
function updateOverride(idx, value) {
selectedAnimations[idx].overrideKey = (value || '').trim() || null;
renderAnimationTable();
}
document.addEventListener('keydown', e => {
if (e.key === 'Escape') {
closeAddAnimationModal();
}
});
renderSelectedAbilities();
renderAnimationTable();
</script>
}