From 168009f69032c8f3ccd4015e8d5a9d736dc08ed8 Mon Sep 17 00:00:00 2001 From: Kaioru Date: Wed, 4 Oct 2023 23:27:46 +0800 Subject: [PATCH] Add mob and npc search command --- .../Commands/Admin/MobCommand.cs | 33 ++++++++++++++++++ .../Commands/Admin/NPCCommand.cs | 34 +++++++++++++++++++ .../Commands/CommandManager.cs | 22 +++++++++--- .../Edelstein.Plugin.Rue/RueGamePlugin.cs | 10 +++++- 4 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 src/plugin/Edelstein.Plugin.Rue/Commands/Admin/MobCommand.cs create mode 100644 src/plugin/Edelstein.Plugin.Rue/Commands/Admin/NPCCommand.cs diff --git a/src/plugin/Edelstein.Plugin.Rue/Commands/Admin/MobCommand.cs b/src/plugin/Edelstein.Plugin.Rue/Commands/Admin/MobCommand.cs new file mode 100644 index 000000000..d4e0f2be2 --- /dev/null +++ b/src/plugin/Edelstein.Plugin.Rue/Commands/Admin/MobCommand.cs @@ -0,0 +1,33 @@ +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Templates; +using Edelstein.Protocol.Gameplay.Game.Objects.User; +using Edelstein.Protocol.Utilities.Templates; + +namespace Edelstein.Plugin.Rue.Commands.Admin; + +public class MobCommand : AbstractTemplateCommand +{ + private readonly ITemplateManager _strings; + + public MobCommand( + ITemplateManager templates, + ITemplateManager strings + ) : base(templates) + => _strings = strings; + + public override string Name => "Mob"; + public override string Description => "Searches a specified mob"; + + protected override async Task> Indices() + { + var result = new List(); + var strings = (await _strings.RetrieveAll()).ToList(); + + result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), s.Name))); + result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Name, s.Name))); + + return result; + } + + protected override Task Execute(IFieldUser user, IMobTemplate template, TemplateCommandArgs args) + => Task.CompletedTask; +} diff --git a/src/plugin/Edelstein.Plugin.Rue/Commands/Admin/NPCCommand.cs b/src/plugin/Edelstein.Plugin.Rue/Commands/Admin/NPCCommand.cs new file mode 100644 index 000000000..e30b0b325 --- /dev/null +++ b/src/plugin/Edelstein.Plugin.Rue/Commands/Admin/NPCCommand.cs @@ -0,0 +1,34 @@ +using Edelstein.Protocol.Gameplay.Game.Objects.NPC.Templates; +using Edelstein.Protocol.Gameplay.Game.Objects.User; +using Edelstein.Protocol.Utilities.Templates; + +namespace Edelstein.Plugin.Rue.Commands.Admin; + +public class NPCCommand : AbstractTemplateCommand +{ + private readonly ITemplateManager _strings; + + public NPCCommand( + ITemplateManager templates, + ITemplateManager strings + ) : base(templates) + => _strings = strings; + + public override string Name => "NPC"; + public override string Description => "Searches a specified NPC"; + + protected override async Task> Indices() + { + var result = new List(); + var strings = (await _strings.RetrieveAll()).ToList(); + + result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), s.Name))); + result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Name, $"{s.Name}{(!string.IsNullOrWhiteSpace(s.Func) ? $": {s.Func}" : "")}"))); + result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Func, $"{s.Name}{(!string.IsNullOrWhiteSpace(s.Func) ? $": {s.Func}" : "")}"))); + + return result; + } + + protected override Task Execute(IFieldUser user, INPCTemplate template, TemplateCommandArgs args) + => Task.CompletedTask; +} diff --git a/src/plugin/Edelstein.Plugin.Rue/Commands/CommandManager.cs b/src/plugin/Edelstein.Plugin.Rue/Commands/CommandManager.cs index 554355bc1..376950159 100644 --- a/src/plugin/Edelstein.Plugin.Rue/Commands/CommandManager.cs +++ b/src/plugin/Edelstein.Plugin.Rue/Commands/CommandManager.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System.Collections.Immutable; +using System.Text.RegularExpressions; using Edelstein.Common.Utilities.Repositories; using Edelstein.Protocol.Gameplay.Game.Objects.User; @@ -11,11 +12,24 @@ public partial class CommandManager : Repository, ICommandMana private async Task GetCommand(IFieldUser user, string name) { - return (await RetrieveAll()) + var results = (await RetrieveAll()) .Where(c => c.Check(user)) - .FirstOrDefault(c => + .Where(c => c.Name.StartsWith(name, StringComparison.OrdinalIgnoreCase) || - c.Aliases.Any(s => s.StartsWith(name, StringComparison.OrdinalIgnoreCase))); + c.Aliases.Any(s => s.StartsWith(name, StringComparison.OrdinalIgnoreCase))) + .ToImmutableArray(); + + if (results.Length <= 1) return results.FirstOrDefault(); + + var i = 0; + var select = await user.Prompt(s => s.AskMenu($"Multiple command found with name '{name}', did you mean..", results.ToImmutableDictionary( + c => i++, + c => c.Name + )), -1); + + return select == -1 + ? null + : results[select]; } public virtual Task Process(IFieldUser user, string text) diff --git a/src/plugin/Edelstein.Plugin.Rue/RueGamePlugin.cs b/src/plugin/Edelstein.Plugin.Rue/RueGamePlugin.cs index 8f02c7c57..68f43636b 100644 --- a/src/plugin/Edelstein.Plugin.Rue/RueGamePlugin.cs +++ b/src/plugin/Edelstein.Plugin.Rue/RueGamePlugin.cs @@ -34,6 +34,15 @@ await commandManager.Insert(new FieldCommand( ctx.Templates.Field, ctx.Templates.FieldString )); + await commandManager.Insert(new NPCCommand( + ctx.Templates.NPC, + ctx.Templates.NPCString + )); + await commandManager.Insert(new MobTemporaryStatCommand()); + await commandManager.Insert(new MobCommand( + ctx.Templates.Mob, + ctx.Templates.MobString + )); await commandManager.Insert(new ContiMoveCommand( ctx.Managers.ContiMove )); @@ -51,7 +60,6 @@ await commandManager.Insert(new QuestCommand( await commandManager.Insert(new EquipCommand()); await commandManager.Insert(new StatCommand()); await commandManager.Insert(new TemporaryStatCommand()); - await commandManager.Insert(new MobTemporaryStatCommand()); await commandManager.Insert(new DebugCommand()); ctx.Pipelines.FieldOnPacketUserChat.Add(PipelinePriority.High, new FieldOnPacketUserChatCommandPlug(commandManager));