diff --git a/Tools/BiblioTech/BaseCodexCommand.cs b/Tools/BiblioTech/BaseCodexCommand.cs deleted file mode 100644 index 0f6780e..0000000 --- a/Tools/BiblioTech/BaseCodexCommand.cs +++ /dev/null @@ -1,27 +0,0 @@ -using BiblioTech.Options; -using CodexPlugin; -using Core; - -namespace BiblioTech -{ - public abstract class BaseCodexCommand : BaseDeploymentCommand - { - private readonly CoreInterface ci; - - public BaseCodexCommand(CoreInterface ci) - { - this.ci = ci; - } - - protected override async Task ExecuteDeploymentCommand(CommandContext context, CodexDeployment codexDeployment) - { - var codexContainers = codexDeployment.CodexInstances.Select(c => c.Containers).ToArray(); - - var group = ci.WrapCodexContainers(codexContainers); - - await Execute(context, group); - } - - protected abstract Task Execute(CommandContext context, ICodexNodeGroup codexGroup); - } -} diff --git a/Tools/BiblioTech/Commands/AdminCommand.cs b/Tools/BiblioTech/Commands/AdminCommand.cs index 9def408..62c3832 100644 --- a/Tools/BiblioTech/Commands/AdminCommand.cs +++ b/Tools/BiblioTech/Commands/AdminCommand.cs @@ -15,11 +15,17 @@ namespace BiblioTech.Commands private readonly WhoIsCommand whoIsCommand = new WhoIsCommand(); private readonly NetInfoCommand netInfoCommand; private readonly DebugPeerCommand debugPeerCommand; + private readonly AddSprCommand addSprCommand; + private readonly ClearSprsCommand clearSprsCommand; + private readonly GetSprCommand getSprCommand; - public AdminCommand(CoreInterface ci) + public AdminCommand(CoreInterface ci, SprCommand sprCommand) { netInfoCommand = new NetInfoCommand(ci); debugPeerCommand = new DebugPeerCommand(ci); + addSprCommand = new AddSprCommand(sprCommand); + clearSprsCommand = new ClearSprsCommand(sprCommand); + getSprCommand = new GetSprCommand(sprCommand); } public override string Name => "admin"; @@ -35,7 +41,10 @@ namespace BiblioTech.Commands deployRemoveCommand, whoIsCommand, netInfoCommand, - debugPeerCommand + debugPeerCommand, + addSprCommand, + clearSprsCommand, + getSprCommand }; protected override async Task Invoke(CommandContext context) @@ -60,6 +69,9 @@ namespace BiblioTech.Commands await whoIsCommand.CommandHandler(context); await netInfoCommand.CommandHandler(context); await debugPeerCommand.CommandHandler(context); + await addSprCommand.CommandHandler(context); + await clearSprsCommand.CommandHandler(context); + await deployUploadCommand.CommandHandler(context); } public class ClearUserAssociationCommand : SubCommandOption @@ -379,5 +391,77 @@ namespace BiblioTech.Commands return content.ToArray(); } } + + public class AddSprCommand : SubCommandOption + { + private readonly SprCommand sprCommand; + private readonly StringOption stringOption = new StringOption("spr", "Codex SPR", true); + + public AddSprCommand(SprCommand sprCommand) + : base(name: "addspr", + description: "Adds a Codex SPR, to be given to users with '/boot'.") + { + this.sprCommand = sprCommand; + } + + public override CommandOption[] Options => new[] { stringOption }; + + protected override async Task onSubCommand(CommandContext context) + { + var spr = await stringOption.Parse(context); + + if (!string.IsNullOrEmpty(spr) ) + { + sprCommand.Add(spr); + await context.Followup("A-OK!"); + } + else + { + await context.Followup("SPR is null or empty."); + } + } + } + + public class ClearSprsCommand : SubCommandOption + { + private readonly SprCommand sprCommand; + private readonly StringOption stringOption = new StringOption("areyousure", "set to 'true' if you are.", true); + + public ClearSprsCommand(SprCommand sprCommand) + : base(name: "clearsprs", + description: "Clears all Codex SPRs in the bot. Users won't be able to use '/boot' till new ones are added.") + { + this.sprCommand = sprCommand; + } + + public override CommandOption[] Options => new[] { stringOption }; + + protected override async Task onSubCommand(CommandContext context) + { + var areyousure = await stringOption.Parse(context); + + if (areyousure != "true") return; + + sprCommand.Clear(); + await context.Followup("Cleared all SPRs."); + } + } + + public class GetSprCommand: SubCommandOption + { + private readonly SprCommand sprCommand; + + public GetSprCommand(SprCommand sprCommand) + : base(name: "getsprs", + description: "Shows all Codex SPRs in the bot.") + { + this.sprCommand = sprCommand; + } + + protected override async Task onSubCommand(CommandContext context) + { + await context.Followup("SPRs: " + string.Join(", ", sprCommand.Get().Select(s => $"'{s}'"))); + } + } } } diff --git a/Tools/BiblioTech/Commands/SprCommand.cs b/Tools/BiblioTech/Commands/SprCommand.cs index ed19c7f..4b3235d 100644 --- a/Tools/BiblioTech/Commands/SprCommand.cs +++ b/Tools/BiblioTech/Commands/SprCommand.cs @@ -1,61 +1,48 @@ using BiblioTech.Options; -using CodexPlugin; -using Core; namespace BiblioTech.Commands { - public class SprCommand : BaseCodexCommand + public class SprCommand : BaseCommand { private readonly Random random = new Random(); - private readonly List sprCache = new List(); - private DateTime lastUpdate = DateTime.MinValue; - - public SprCommand(CoreInterface ci) : base(ci) - { - } + private readonly List knownSprs = new List(); public override string Name => "boot"; public override string StartingMessage => RandomBusyMessage.Get(); public override string Description => "Gets an SPR. (Signed peer record, used for bootstrapping.)"; - protected override async Task OnInvoke(CommandContext context) + protected override async Task Invoke(CommandContext context) { - if (ShouldUpdate()) - { - return true; - } - await ReplyWithRandomSpr(context); - return false; } - protected override async Task Execute(CommandContext context, ICodexNodeGroup codexGroup) + public void Add(string spr) { - lastUpdate = DateTime.UtcNow; - sprCache.Clear(); + if (knownSprs.Contains(spr)) return; + knownSprs.Add(spr); + } - var infos = codexGroup.Select(c => c.GetDebugInfo()).ToArray(); - sprCache.AddRange(infos.Select(i => i.spr)); + public void Clear() + { + knownSprs.Clear(); + } - await ReplyWithRandomSpr(context); + public string[] Get() + { + return knownSprs.ToArray(); } private async Task ReplyWithRandomSpr(CommandContext context) { - if (!sprCache.Any()) + if (!knownSprs.Any()) { await context.Followup("I'm sorry, no SPRs are available... :c"); return; } - var i = random.Next(0, sprCache.Count); - var spr = sprCache[i]; + var i = random.Next(0, knownSprs.Count); + var spr = knownSprs[i]; await context.Followup($"Your SPR: `{spr}`"); } - - private bool ShouldUpdate() - { - return (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10); - } } } diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index 4bb1175..f027af8 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -49,12 +49,13 @@ namespace BiblioTech var ci = entryPoint.CreateInterface(); var associateCommand = new UserAssociateCommand(); + var sprCommand = new SprCommand(); var handler = new CommandHandler(client, new GetBalanceCommand(ci, associateCommand), new MintCommand(ci, associateCommand), - new SprCommand(ci), + sprCommand, associateCommand, - new AdminCommand(ci) + new AdminCommand(ci, sprCommand) ); await client.LoginAsync(TokenType.Bot, Config.ApplicationToken);