2024-05-20 23:00:47 +00:00
|
|
|
|
using SharpChat.PacketsS2C;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
2024-05-20 23:00:47 +00:00
|
|
|
|
public class ShutdownRestartCommand : ISockChatClientCommand {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
private readonly ManualResetEvent WaitHandle;
|
2024-05-20 16:16:32 +00:00
|
|
|
|
private readonly Func<bool> ShuttingDown;
|
|
|
|
|
private readonly Action<bool> SetShutdown;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-20 16:16:32 +00:00
|
|
|
|
public ShutdownRestartCommand(
|
|
|
|
|
ManualResetEvent waitHandle,
|
|
|
|
|
Func<bool> shuttingDown,
|
|
|
|
|
Action<bool> setShutdown
|
|
|
|
|
) {
|
2024-05-19 01:53:14 +00:00
|
|
|
|
WaitHandle = waitHandle;
|
2024-05-20 16:16:32 +00:00
|
|
|
|
ShuttingDown = shuttingDown;
|
|
|
|
|
SetShutdown = setShutdown;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
public bool IsMatch(SockChatClientCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return ctx.NameEquals("shutdown")
|
|
|
|
|
|| ctx.NameEquals("restart");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
public void Dispatch(SockChatClientCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
if(ctx.User.UserId != 1) {
|
2024-05-20 23:00:47 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorS2CPacket(ctx.Name));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:16:32 +00:00
|
|
|
|
if(ShuttingDown())
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2024-05-20 16:16:32 +00:00
|
|
|
|
SetShutdown(ctx.NameEquals("restart"));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
ctx.Chat.Update();
|
|
|
|
|
WaitHandle?.Set();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|