File size: 5,213 Bytes
8c7ffe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using SilkroadBot.Plugins.SDK.Attributes;
using SilkroadBot.Plugins.SDK.Interfaces;
using SilkroadBot.Plugins.SDK.Models;

namespace SilkroadBot.Plugin.CommandCenter;

/// <summary>
/// Command Center plugin - provides a command-based interface for controlling the bot.
/// Supports custom commands, macros, and scheduled tasks.
/// </summary>
[Plugin("command-center", "Command Center", "Command-based bot control with macros and scheduled tasks")]
public class CommandCenterPlugin : PluginBase
{
    public override string Id => "command-center";
    public override string Name => "Command Center";
    public override Version Version => new(1, 0, 0);
    public override string Author => "SilkroadBot Team";
    public override string Description => "Bot control via commands, macros, and task scheduling";

    private readonly Dictionary<string, Func<string[], Task>> _commands = new();
    private readonly List<ScheduledTask> _scheduledTasks = new();
    private CancellationTokenSource? _schedulerCts;

    public override Task InitializeAsync(IPluginContext context)
    {
        base.InitializeAsync(context);
        RegisterDefaultCommands();
        return Task.CompletedTask;
    }

    public override Task StartAsync(CancellationToken ct = default)
    {
        _schedulerCts = new CancellationTokenSource();
        _ = Task.Run(() => SchedulerLoopAsync(_schedulerCts.Token));
        return base.StartAsync(ct);
    }

    public override Task StopAsync()
    {
        _schedulerCts?.Cancel();
        return base.StopAsync();
    }

    /// <summary>
    /// Register a custom command.
    /// </summary>
    public void RegisterCommand(string name, Func<string[], Task> handler)
    {
        _commands[name.ToLower()] = handler;
        Log(LogLevel.Debug, $"Registered command: {name}");
    }

    /// <summary>
    /// Execute a command string.
    /// </summary>
    public async Task<CommandResult> ExecuteAsync(string commandLine)
    {
        var parts = commandLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        if (parts.Length == 0)
            return new CommandResult(false, "Empty command");

        var cmdName = parts[0].ToLower();
        var args = parts.Skip(1).ToArray();

        if (!_commands.TryGetValue(cmdName, out var handler))
            return new CommandResult(false, $"Unknown command: {cmdName}");

        try
        {
            await handler(args);
            return new CommandResult(true, $"Command '{cmdName}' executed successfully");
        }
        catch (Exception ex)
        {
            return new CommandResult(false, $"Command error: {ex.Message}");
        }
    }

    /// <summary>
    /// Schedule a command to run periodically.
    /// </summary>
    public void ScheduleCommand(string name, string command, TimeSpan interval)
    {
        _scheduledTasks.Add(new ScheduledTask
        {
            Name = name,
            Command = command,
            Interval = interval,
            NextRun = DateTime.UtcNow + interval,
            IsEnabled = true
        });
        Log(LogLevel.Information, $"Scheduled '{name}' every {interval.TotalSeconds}s");
    }

    private void RegisterDefaultCommands()
    {
        RegisterCommand("status", async args =>
        {
            var state = Context.GetGameState();
            Log(LogLevel.Information, $"Status: {state.ConnectionState} | HP: {state.HP}/{state.MaxHP} | Pos: {state.Position}");
            await Task.CompletedTask;
        });

        RegisterCommand("heal", async args =>
        {
            Log(LogLevel.Information, "Executing heal command...");
            // TODO: Send heal skill/potion packet
            await Task.CompletedTask;
        });

        RegisterCommand("move", async args =>
        {
            if (args.Length < 2)
            {
                Log(LogLevel.Warning, "Usage: move <x> <y>");
                return;
            }
            Log(LogLevel.Information, $"Moving to ({args[0]}, {args[1]})...");
            await Task.CompletedTask;
        });

        RegisterCommand("stop", async args =>
        {
            Log(LogLevel.Information, "Stopping all actions...");
            await Task.CompletedTask;
        });

        RegisterCommand("help", async args =>
        {
            Log(LogLevel.Information, $"Available commands: {string.Join(", ", _commands.Keys)}");
            await Task.CompletedTask;
        });
    }

    private async Task SchedulerLoopAsync(CancellationToken ct)
    {
        while (!ct.IsCancellationRequested)
        {
            var now = DateTime.UtcNow;
            foreach (var task in _scheduledTasks.Where(t => t.IsEnabled && t.NextRun <= now))
            {
                await ExecuteAsync(task.Command);
                task.NextRun = now + task.Interval;
            }
            await Task.Delay(1000, ct);
        }
    }
}

public record CommandResult(bool Success, string Message);

public class ScheduledTask
{
    public string Name { get; set; } = string.Empty;
    public string Command { get; set; } = string.Empty;
    public TimeSpan Interval { get; set; }
    public DateTime NextRun { get; set; }
    public bool IsEnabled { get; set; }
}