improve eval

This commit is contained in:
Chayapak 2023-07-02 20:25:53 +07:00
parent 9a4b4ed078
commit 9d85b489bc
4 changed files with 68 additions and 15 deletions

View file

@ -0,0 +1,16 @@
package land.chipmunk.chayapak.chomens_bot.evalFunctions;
import land.chipmunk.chayapak.chomens_bot.Bot;
public class ChatFunction extends EvalFunction {
public ChatFunction (Bot bot) {
super("chat", bot);
}
@Override
public void execute(Object... args) {
final String message = (String) args[0];
bot.chat.send(message);
}
}

View file

@ -0,0 +1,16 @@
package land.chipmunk.chayapak.chomens_bot.evalFunctions;
import land.chipmunk.chayapak.chomens_bot.Bot;
public class CoreFunction extends EvalFunction {
public CoreFunction (Bot bot) {
super("core", bot);
}
@Override
public void execute(Object... args) {
final String command = (String) args[0];
bot.core.run(command);
}
}

View file

@ -0,0 +1,20 @@
package land.chipmunk.chayapak.chomens_bot.evalFunctions;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.EvalOutput;
public class EvalFunction {
public final String name;
protected final Bot bot;
public EvalFunction (
String name,
Bot bot
) {
this.name = name;
this.bot = bot;
}
public void execute (Object ...args) {}
}

View file

@ -4,9 +4,11 @@ import io.socket.client.IO;
import io.socket.client.Socket; import io.socket.client.Socket;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.EvalOutput; import land.chipmunk.chayapak.chomens_bot.data.EvalOutput;
import land.chipmunk.chayapak.chomens_bot.evalFunctions.ChatFunction;
import land.chipmunk.chayapak.chomens_bot.evalFunctions.CoreFunction;
import land.chipmunk.chayapak.chomens_bot.evalFunctions.EvalFunction;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class EvalPlugin { public class EvalPlugin {
@ -20,17 +22,28 @@ public class EvalPlugin {
private final Map<Integer, CompletableFuture<EvalOutput>> futures = new HashMap<>(); private final Map<Integer, CompletableFuture<EvalOutput>> futures = new HashMap<>();
public final List<EvalFunction> functions = new ArrayList<>();
public EvalPlugin (Bot bot) { public EvalPlugin (Bot bot) {
functions.add(new CoreFunction(bot));
functions.add(new ChatFunction(bot));
try { try {
socket = IO.socket(bot.config.eval.address); socket = IO.socket(bot.config.eval.address);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
socket.on(Socket.EVENT_CONNECT, (args) -> connected = true); socket.on(Socket.EVENT_CONNECT, (args) -> {
connected = true;
socket.emit("setFunctions", "chat", "core");
});
socket.on(Socket.EVENT_DISCONNECT, (args) -> connected = false); socket.on(Socket.EVENT_DISCONNECT, (args) -> connected = false);
socket.on(Socket.EVENT_CONNECT_ERROR, (args) -> connected = false); socket.on(Socket.EVENT_CONNECT_ERROR, (args) -> connected = false);
for (EvalFunction function : functions) socket.on(BRIDGE_PREFIX + function.name, function::execute);
socket.on("codeOutput", (args) -> { socket.on("codeOutput", (args) -> {
final int id = (int) args[0]; final int id = (int) args[0];
final boolean isError = (boolean) args[1]; final boolean isError = (boolean) args[1];
@ -41,18 +54,6 @@ public class EvalPlugin {
future.complete(new EvalOutput(isError, output)); future.complete(new EvalOutput(isError, output));
}); });
socket.on(BRIDGE_PREFIX + "chat", (args) -> {
final String message = (String) args[0];
bot.chat.send(message);
});
socket.on(BRIDGE_PREFIX + "core", (args) -> {
final String command = (String) args[0];
bot.core.run(command);
});
socket.connect(); socket.connect();
} }