Upgrade NPC admin editor selectors and add dialog tree admin UI
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
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 DialogTreeEditModel : AdminPageModel
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
|
||||
public DialogTreeEditModel(GameDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public DungeonNpcDialogTree Tree { get; set; } = new();
|
||||
|
||||
public List<DungeonNpcDialogNode> Nodes { get; set; } = new();
|
||||
public List<DungeonNpcDialogOption> Options { get; set; } = new();
|
||||
public List<DungeonItemDefinition> ItemDefinitions { get; set; } = new();
|
||||
|
||||
[FromQuery(Name = "nodeId")]
|
||||
public int? SelectedNodeId { get; set; }
|
||||
|
||||
public bool IsNew => Tree.Id == 0;
|
||||
public string? Message { get; set; }
|
||||
public bool IsError { get; set; }
|
||||
|
||||
public async Task OnGetAsync(int? id, int? nodeId)
|
||||
{
|
||||
await LoadLookupsAsync();
|
||||
|
||||
if (id.HasValue)
|
||||
{
|
||||
var tree = await _db.DungeonNpcDialogTrees.FindAsync(id.Value);
|
||||
if (tree != null)
|
||||
{
|
||||
Tree = tree;
|
||||
await LoadTreeDataAsync(tree.Id, nodeId);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Tree = new DungeonNpcDialogTree
|
||||
{
|
||||
Name = "",
|
||||
Description = "",
|
||||
IsEnabled = true
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
await LoadLookupsAsync();
|
||||
|
||||
Tree.Name = (Tree.Name ?? "").Trim();
|
||||
Tree.Description ??= "";
|
||||
Tree.IsEnabled = Request.Form.ContainsKey("Tree.IsEnabled");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Tree.Name))
|
||||
{
|
||||
IsError = true;
|
||||
Message = "Name is required.";
|
||||
return Page();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (Tree.Id == 0)
|
||||
{
|
||||
Tree.CreatedAt = DateTime.UtcNow;
|
||||
Tree.UpdatedAt = DateTime.UtcNow;
|
||||
_db.DungeonNpcDialogTrees.Add(Tree);
|
||||
await _db.SaveChangesAsync();
|
||||
return RedirectToPage("/Npcs/DialogTreeEdit", new { id = Tree.Id });
|
||||
}
|
||||
|
||||
var existing = await _db.DungeonNpcDialogTrees.FindAsync(Tree.Id);
|
||||
if (existing == null)
|
||||
{
|
||||
IsError = true;
|
||||
Message = "Dialog tree not found.";
|
||||
return Page();
|
||||
}
|
||||
|
||||
existing.Name = Tree.Name;
|
||||
existing.Description = Tree.Description;
|
||||
existing.IsEnabled = Tree.IsEnabled;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
Tree = existing;
|
||||
Message = "Dialog tree saved.";
|
||||
await LoadTreeDataAsync(existing.Id, SelectedNodeId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
IsError = true;
|
||||
Message = $"Error: {ex.Message}";
|
||||
if (Tree.Id > 0) await LoadTreeDataAsync(Tree.Id, SelectedNodeId);
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAddNodeAsync(
|
||||
int treeId,
|
||||
string nodeKey,
|
||||
string text,
|
||||
bool isRoot,
|
||||
bool isTerminal,
|
||||
string? rewardType,
|
||||
int? rewardItemDefinitionId,
|
||||
int rewardItemQuantity,
|
||||
long rewardGold,
|
||||
long rewardXp,
|
||||
string? rewardQuestKey)
|
||||
{
|
||||
if (isRoot)
|
||||
{
|
||||
var roots = await _db.DungeonNpcDialogNodes
|
||||
.Where(n => n.DialogTreeId == treeId && n.IsRoot)
|
||||
.ToListAsync();
|
||||
foreach (var r in roots) r.IsRoot = false;
|
||||
}
|
||||
|
||||
var node = new DungeonNpcDialogNode
|
||||
{
|
||||
DialogTreeId = treeId,
|
||||
NodeKey = (nodeKey ?? "").Trim(),
|
||||
Text = (text ?? "").Trim(),
|
||||
IsRoot = isRoot,
|
||||
IsTerminal = isTerminal,
|
||||
RewardType = string.IsNullOrWhiteSpace(rewardType) ? null : rewardType.Trim(),
|
||||
RewardItemDefinitionId = rewardItemDefinitionId,
|
||||
RewardItemQuantity = Math.Max(1, rewardItemQuantity),
|
||||
RewardGold = Math.Max(0, rewardGold),
|
||||
RewardXp = Math.Max(0, rewardXp),
|
||||
RewardQuestKey = string.IsNullOrWhiteSpace(rewardQuestKey) ? null : rewardQuestKey.Trim()
|
||||
};
|
||||
|
||||
_db.DungeonNpcDialogNodes.Add(node);
|
||||
await _db.SaveChangesAsync();
|
||||
return RedirectToPage("/Npcs/DialogTreeEdit", new { id = treeId, nodeId = node.Id });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteNodeAsync(int treeId, int nodeId)
|
||||
{
|
||||
var node = await _db.DungeonNpcDialogNodes.FindAsync(nodeId);
|
||||
if (node != null && node.DialogTreeId == treeId)
|
||||
{
|
||||
_db.DungeonNpcDialogNodes.Remove(node);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
return RedirectToPage("/Npcs/DialogTreeEdit", new { id = treeId });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAddOptionAsync(
|
||||
int treeId,
|
||||
int nodeId,
|
||||
string playerText,
|
||||
int? nextNodeId,
|
||||
NpcDialogConditionType conditionType,
|
||||
string? conditionValue,
|
||||
int conditionQuantity,
|
||||
bool consumeConditionItem,
|
||||
int sortOrder)
|
||||
{
|
||||
var node = await _db.DungeonNpcDialogNodes.FirstOrDefaultAsync(n => n.Id == nodeId && n.DialogTreeId == treeId);
|
||||
if (node == null)
|
||||
{
|
||||
return RedirectToPage("/Npcs/DialogTreeEdit", new { id = treeId });
|
||||
}
|
||||
|
||||
var option = new DungeonNpcDialogOption
|
||||
{
|
||||
NodeId = nodeId,
|
||||
PlayerText = (playerText ?? "").Trim(),
|
||||
NextNodeId = nextNodeId,
|
||||
ConditionType = conditionType,
|
||||
ConditionValue = string.IsNullOrWhiteSpace(conditionValue) ? null : conditionValue.Trim(),
|
||||
ConditionQuantity = Math.Max(1, conditionQuantity),
|
||||
ConsumeConditionItem = consumeConditionItem,
|
||||
SortOrder = sortOrder
|
||||
};
|
||||
|
||||
_db.DungeonNpcDialogOptions.Add(option);
|
||||
await _db.SaveChangesAsync();
|
||||
return RedirectToPage("/Npcs/DialogTreeEdit", new { id = treeId, nodeId });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteOptionAsync(int treeId, int nodeId, int optionId)
|
||||
{
|
||||
var option = await _db.DungeonNpcDialogOptions.FindAsync(optionId);
|
||||
if (option != null && option.NodeId == nodeId)
|
||||
{
|
||||
_db.DungeonNpcDialogOptions.Remove(option);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
return RedirectToPage("/Npcs/DialogTreeEdit", new { id = treeId, nodeId });
|
||||
}
|
||||
|
||||
private async Task LoadLookupsAsync()
|
||||
{
|
||||
ItemDefinitions = await _db.DungeonItemDefinitions
|
||||
.Where(i => i.IsEnabled)
|
||||
.OrderBy(i => i.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
private async Task LoadTreeDataAsync(int treeId, int? nodeId)
|
||||
{
|
||||
Nodes = await _db.DungeonNpcDialogNodes
|
||||
.Where(n => n.DialogTreeId == treeId)
|
||||
.OrderByDescending(n => n.IsRoot)
|
||||
.ThenBy(n => n.Id)
|
||||
.ToListAsync();
|
||||
|
||||
SelectedNodeId = nodeId ?? Nodes.FirstOrDefault()?.Id;
|
||||
|
||||
if (SelectedNodeId.HasValue)
|
||||
{
|
||||
Options = await _db.DungeonNpcDialogOptions
|
||||
.Where(o => o.NodeId == SelectedNodeId.Value)
|
||||
.OrderBy(o => o.SortOrder)
|
||||
.ThenBy(o => o.Id)
|
||||
.ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
Options = new List<DungeonNpcDialogOption>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user