Skip to content

Commit

Permalink
Add mob and npc search command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Oct 4, 2023
1 parent f6bcee7 commit 168009f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
33 changes: 33 additions & 0 deletions src/plugin/Edelstein.Plugin.Rue/Commands/Admin/MobCommand.cs
Original file line number Diff line number Diff line change
@@ -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<IMobTemplate>
{
private readonly ITemplateManager<IMobStringTemplate> _strings;

public MobCommand(
ITemplateManager<IMobTemplate> templates,
ITemplateManager<IMobStringTemplate> strings
) : base(templates)
=> _strings = strings;

public override string Name => "Mob";
public override string Description => "Searches a specified mob";

protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
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;
}
34 changes: 34 additions & 0 deletions src/plugin/Edelstein.Plugin.Rue/Commands/Admin/NPCCommand.cs
Original file line number Diff line number Diff line change
@@ -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<INPCTemplate>
{
private readonly ITemplateManager<INPCStringTemplate> _strings;

public NPCCommand(
ITemplateManager<INPCTemplate> templates,
ITemplateManager<INPCStringTemplate> strings
) : base(templates)
=> _strings = strings;

public override string Name => "NPC";
public override string Description => "Searches a specified NPC";

protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
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;
}
22 changes: 18 additions & 4 deletions src/plugin/Edelstein.Plugin.Rue/Commands/CommandManager.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,11 +12,24 @@ public partial class CommandManager : Repository<string, ICommand>, ICommandMana

private async Task<ICommand?> 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<bool> Process(IFieldUser user, string text)
Expand Down
10 changes: 9 additions & 1 deletion src/plugin/Edelstein.Plugin.Rue/RueGamePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
Expand All @@ -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));
Expand Down

0 comments on commit 168009f

Please sign in to comment.