currently refactoring a lot
This commit is contained in:
parent
700b5d5d10
commit
31eb8a92eb
42 changed files with 454 additions and 433 deletions
|
|
@ -4,7 +4,6 @@ plugins {
|
|||
id("de.kentoj.scrow.scrow-repository")
|
||||
id("de.kentoj.scrow.base-dependencies")
|
||||
id("de.kentoj.scrow.database")
|
||||
id("de.kentoj.scrow.reactor")
|
||||
}
|
||||
|
||||
group = "de.kentoj.scrow"
|
||||
|
|
|
|||
|
|
@ -6,12 +6,15 @@ import com.velocitypowered.api.proxy.ProxyServer;
|
|||
import de.kentoj.scrow.corevelocity.command.OnlineCommand;
|
||||
import de.kentoj.scrow.corevelocity.privmsg.LastTargetCache;
|
||||
import de.kentoj.scrow.corevelocity.privmsg.MsgCommand;
|
||||
import de.kentoj.scrow.corevelocity.privmsg.PrivMsgHandler;
|
||||
import de.kentoj.scrow.corevelocity.privmsg.ReplyCommand;
|
||||
import de.kentoj.scrow.corevelocity.server.SendPlayerWatchdog;
|
||||
import de.kentoj.scrow.corevelocity.server.ServerRegisterWatchdog;
|
||||
import de.kentoj.scrow.generated.BuildInfo; // may not resolve until building for the first time
|
||||
import de.kentoj.scrow.generated.BuildInfo;
|
||||
import de.kentoj.scrow.velocity.ScrowAPI;
|
||||
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
|
||||
import lombok.extern.java.Log;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
@Plugin(
|
||||
id = "corevelocity",
|
||||
|
|
@ -28,12 +31,14 @@ public class CoreVelocityPlugin {
|
|||
var cmds = ScrowAPI.getCommandApi();
|
||||
{
|
||||
var lastTargetCache = new LastTargetCache();
|
||||
cmds.register(new ReplyCommand(server, lastTargetCache).getRootNode());
|
||||
cmds.register(new MsgCommand(server, lastTargetCache).getRootNode());
|
||||
var style = new ScrowMessageStyle("MSG", NamedTextColor.LIGHT_PURPLE);
|
||||
var privmsgHelper = new PrivMsgHandler(style);
|
||||
cmds.register(new ReplyCommand(server, lastTargetCache, privmsgHelper, style).getRootNode());
|
||||
cmds.register(new MsgCommand(server, lastTargetCache, privmsgHelper, style).getRootNode());
|
||||
cmds.register(new OnlineCommand(server).getRootNode());
|
||||
}
|
||||
|
||||
var registerWatchdog = new ServerRegisterWatchdog(ScrowAPI.getNats(), ScrowAPI.getInstanceManager(), server);
|
||||
var registerWatchdog = new ServerRegisterWatchdog(ScrowAPI.getNats(), server);
|
||||
var sendPlayerWatchdog = new SendPlayerWatchdog(ScrowAPI.getNats(), server);
|
||||
registerWatchdog.listen();
|
||||
sendPlayerWatchdog.listen();
|
||||
|
|
|
|||
|
|
@ -3,14 +3,13 @@ package de.kentoj.scrow.corevelocity;
|
|||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import de.kentoj.kencommandapi.CommandAPI;
|
||||
import de.kentoj.scrow.velocity.ScrowAPI;
|
||||
import de.kentoj.scrowlib.connection.DatabaseConnectionFactoryImpl;
|
||||
import de.kentoj.scrowlib.instancemanager.InstanceManagerImpl;
|
||||
import de.kentoj.scrowlib.utils.EnvUtils;
|
||||
import io.nats.client.Nats;
|
||||
import io.nats.client.Options;
|
||||
import io.r2dbc.spi.ConnectionFactories;
|
||||
import io.r2dbc.spi.ConnectionFactory;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
|
@ -30,20 +29,14 @@ public class ScrowAPISurface {
|
|||
}
|
||||
|
||||
if (enableDatabase) {
|
||||
ConnectionFactory factory = ConnectionFactories.get(EnvUtils.envOrThrow("HOST_POSTGRES"));
|
||||
ScrowAPI.setDbConFactory(new DatabaseConnectionFactoryImpl(factory));
|
||||
ScrowAPI.setJdbi(Jdbi.create(EnvUtils.envOrThrow("HOST_POSTGRES")));
|
||||
}
|
||||
|
||||
ScrowAPI.setInstanceManager(new ServerManagerImpl(ScrowAPI.getNats()));
|
||||
ScrowAPI.setInstanceManager(new InstanceManagerImpl(ScrowAPI.getNats()));
|
||||
|
||||
ScrowAPI.setCommandApi(new CommandAPI(server));
|
||||
}
|
||||
|
||||
public static void destroyScrowAPI() {
|
||||
try {
|
||||
ScrowAPI.getDbConFactory().close();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +1,40 @@
|
|||
package de.kentoj.scrow.corevelocity.command;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import com.leakyabstractions.result.core.Results;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import de.kentoj.kencommandapi.api.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.api.invocation.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.invocation.CommandExecutor;
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import com.leakyabstractions.result.api.Results;
|
||||
import de.kentoj.kencommandapi.api.invocation.SyncCommandExecutor;
|
||||
import de.kentoj.kencommandapi.api.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.api.node.RootCommandNode;
|
||||
import de.kentoj.kencommandapi.api.platform.MessageStyle;
|
||||
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class OnlineCommand implements CommandExecutor<CommandSource> {
|
||||
public class OnlineCommand implements SyncCommandExecutor<CommandSource> {
|
||||
|
||||
@Getter
|
||||
private final CommandNode<CommandSource> rootNode;
|
||||
private final RootCommandNode<CommandSource> rootNode;
|
||||
|
||||
private final ProxyServer server;
|
||||
|
||||
public OnlineCommand(ProxyServer server) {
|
||||
this.server = server;
|
||||
rootNode = CommandNode.node("online");
|
||||
rootNode = CommandNode.rootNode(ScrowMessageStyle.GENERIC, "online");
|
||||
rootNode.setExecutor(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Result<Object,String>> execute(CommandContext<CommandSource> ctx) {
|
||||
public Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
|
||||
int cnt = server.getPlayerCount();
|
||||
var msg = cnt != 1
|
||||
? "There are currently " + cnt + " players online"
|
||||
: "There is currently 1 player online";
|
||||
ctx.getSender().sendMessage(Component.text(msg));
|
||||
return Result.success().toFuture();
|
||||
return Results.success(new Object());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,52 +1,54 @@
|
|||
package de.kentoj.scrow.corevelocity.privmsg;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import com.leakyabstractions.result.core.Results;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import de.kentoj.kencommandapi.api.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.api.argument.CommandArgument;
|
||||
import de.kentoj.kencommandapi.api.argument.types.StringArgumentType;
|
||||
import de.kentoj.kencommandapi.api.invocation.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.invocation.CommandExecutor;
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import com.leakyabstractions.result.api.Results;
|
||||
import de.kentoj.kencommandapi.api.invocation.SyncCommandExecutor;
|
||||
import de.kentoj.kencommandapi.api.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.api.node.RootCommandNode;
|
||||
import de.kentoj.kencommandapi.api.platform.MessageStyle;
|
||||
import de.kentoj.kencommandapi.type.PlayerArgumentType;
|
||||
import de.kentoj.scrow.velocity.ScrowAPI;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class MsgCommand implements CommandExecutor<CommandSource> {
|
||||
public class MsgCommand implements SyncCommandExecutor<CommandSource> {
|
||||
|
||||
@Getter
|
||||
private final CommandNode<CommandSource> rootNode;
|
||||
private final RootCommandNode<CommandSource> rootNode;
|
||||
private final CommandArgument<CommandSource, Player> targetArg;
|
||||
private final CommandArgument<CommandSource, String> msgArg;
|
||||
|
||||
private final LastTargetCache cache;
|
||||
private final PrivMsgHandler privMsgHandler;
|
||||
|
||||
public MsgCommand(ProxyServer server, LastTargetCache cache) {
|
||||
public MsgCommand(ProxyServer server, LastTargetCache cache, PrivMsgHandler helper, MessageStyle style) {
|
||||
this.cache = cache;
|
||||
this.privMsgHandler = helper;
|
||||
|
||||
targetArg = CommandArgument.arg("target", new PlayerArgumentType(server));
|
||||
msgArg = CommandArgument.arg("msg", new StringArgumentType<>(true));
|
||||
rootNode = CommandNode.node("msg", "w", "privmsg");
|
||||
|
||||
rootNode = CommandNode.rootNode(style, "msg", "w", "privmsg");
|
||||
rootNode.setExecutor(this);
|
||||
rootNode.addArgument(targetArg);
|
||||
rootNode.addArgument(msgArg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Result<Object,String>> execute(CommandContext<CommandSource> ctx) {
|
||||
public Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
|
||||
var target = ctx.getArg(targetArg);
|
||||
var msg = ctx.getArg(msgArg);
|
||||
var sender = (Player)ctx.getSender();
|
||||
var sender = (Player) ctx.getSender();
|
||||
|
||||
var isMessagingSelf = target.getUniqueId().equals(sender.getUniqueId());
|
||||
if (isMessagingSelf) return Result.error("You can't message yourself").toFuture();
|
||||
if (isMessagingSelf) return Results.failure("You can't message yourself");
|
||||
|
||||
cache.setLastTarget(sender.getUniqueId(), target.getUniqueId());
|
||||
PrivmsgHelper.handle(sender, target, msg);
|
||||
return Result.success().toFuture();
|
||||
privMsgHandler.handle(sender, target, msg);
|
||||
return Results.success(new Object());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package de.kentoj.scrow.corevelocity.privmsg;
|
||||
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class PrivMsgHandler {
|
||||
|
||||
private final ScrowMessageStyle style;
|
||||
|
||||
public void handle(Player player, Player target, String msg) {
|
||||
target.sendMessage(style.ok(text(player.getUsername() + " -> You:").append(Component.text(msg))));
|
||||
player.sendMessage(style.ok(text("You -> " + player.getUsername()).append(Component.text(msg))));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package de.kentoj.scrow.corevelocity.privmsg;
|
||||
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.NONE)
|
||||
public class PrivmsgHelper {
|
||||
|
||||
public static void handle(Player player, Player target, String msg) {
|
||||
target.sendMessage(Component.text("[PRIVMSG] " + player.getUsername() + " -> You: " + msg));
|
||||
player.sendMessage(Component.text("[PRIVMSG] " + "you -> " + player.getUsername() + ": " + msg));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +1,51 @@
|
|||
package de.kentoj.scrow.corevelocity.privmsg;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import com.leakyabstractions.result.core.Results;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import de.kentoj.kencommandapi.api.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.api.argument.CommandArgument;
|
||||
import de.kentoj.kencommandapi.api.argument.types.StringArgumentType;
|
||||
import de.kentoj.kencommandapi.api.invocation.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.invocation.CommandExecutor;
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import com.leakyabstractions.result.api.Results;
|
||||
import de.kentoj.kencommandapi.api.invocation.SyncCommandExecutor;
|
||||
import de.kentoj.kencommandapi.api.node.CommandNode;
|
||||
import de.kentoj.kencommandapi.api.node.RootCommandNode;
|
||||
import de.kentoj.kencommandapi.api.platform.MessageStyle;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class ReplyCommand implements CommandExecutor<CommandSource> {
|
||||
public class ReplyCommand implements SyncCommandExecutor<CommandSource> {
|
||||
|
||||
@Getter
|
||||
private final CommandNode<CommandSource> rootNode;
|
||||
private final RootCommandNode<CommandSource> rootNode;
|
||||
private final CommandArgument<CommandSource, String> msgArg;
|
||||
|
||||
private final LastTargetCache cache;
|
||||
private final ProxyServer server;
|
||||
private final PrivMsgHandler privMsgHandler;
|
||||
|
||||
public ReplyCommand(ProxyServer server, LastTargetCache cache) {
|
||||
public ReplyCommand(ProxyServer server, LastTargetCache cache, PrivMsgHandler privMsgHandler, MessageStyle style) {
|
||||
this.cache = cache;
|
||||
this.server = server;
|
||||
this.privMsgHandler = privMsgHandler;
|
||||
|
||||
msgArg = CommandArgument.arg("msg", new StringArgumentType<>(true));
|
||||
rootNode = CommandNode.node("reply", "r");
|
||||
rootNode = CommandNode.rootNode(style, "reply", "r");
|
||||
rootNode.setExecutor(this);
|
||||
rootNode.addArgument(msgArg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Result<Object,String>> execute(CommandContext<CommandSource> ctx) {
|
||||
var sender = (Player)ctx.getSender();
|
||||
public Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
|
||||
var sender = (Player) ctx.getSender();
|
||||
var targetId = cache.getLastTarget(sender.getUniqueId());
|
||||
var target = server.getPlayer(targetId).orElse(null);
|
||||
if (target == null) return Result.error("The player you last messaged is no longer online").toFuture();
|
||||
|
||||
var msg = ctx.getArg(msgArg);
|
||||
|
||||
if (target == null)
|
||||
return Results.failure("The player you last messaged is no longer online");
|
||||
cache.setLastTarget(sender.getUniqueId(), target.getUniqueId());
|
||||
PrivmsgHelper.handle(sender, target, msg);
|
||||
return Result.success().toFuture();
|
||||
privMsgHandler.handle(sender, target, msg);
|
||||
return Results.success(new Object());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
package de.kentoj.scrow.corevelocity.server;
|
||||
|
||||
import com.leakyabstractions.result.core.Results;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.server.ServerInfo;
|
||||
import de.kentoj.scrowlib.messaging.MessageBroker;
|
||||
import de.kentoj.scrowlib.messaging.respose.SafeResult;
|
||||
import de.kentoj.scrowlib.servermanager.InstanceManager;
|
||||
import de.kentoj.scrowlib.messaging.SyncRequestHandler;
|
||||
import io.nats.client.Connection;
|
||||
import lombok.extern.java.Log;
|
||||
import org.bson.Document;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
|
|
@ -17,39 +15,30 @@ import java.net.InetSocketAddress;
|
|||
public class ServerRegisterWatchdog {
|
||||
|
||||
private final MessageBroker natsRepo;
|
||||
private final InstanceManager instanceManager;
|
||||
private final ProxyServer server;
|
||||
|
||||
public ServerRegisterWatchdog(Connection nats, InstanceManager instanceManager, ProxyServer server) {
|
||||
public ServerRegisterWatchdog(Connection nats, ProxyServer server) {
|
||||
this.natsRepo = new MessageBroker(nats);
|
||||
this.instanceManager = instanceManager;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void listen() {
|
||||
natsRepo.subscribe("crown.set-state")
|
||||
.flatMap(ctx -> {
|
||||
var state = ctx.getDocument().getString("state");
|
||||
var handle = ctx.getDocument().getString("handle");
|
||||
return handleStateChange(ctx, state, handle)
|
||||
.then(Mono.fromRunnable(() -> {
|
||||
natsRepo.publishSafeResult(ctx.getMsg().getReplyTo(), SafeResult.wrap(new Document()));
|
||||
}));
|
||||
})
|
||||
.subscribe(null, e -> log.throwing(getClass().getName(), "listen", e));
|
||||
}
|
||||
natsRepo.handle("event.instance.up", (SyncRequestHandler) doc -> {
|
||||
var handle = doc.getString("handle");
|
||||
var port = doc.getInteger("port");
|
||||
return Results.ofCallable(() -> {
|
||||
server.registerServer(new ServerInfo(handle, new InetSocketAddress(port)));
|
||||
return new Document();
|
||||
}).mapFailure(Throwable::getMessage);
|
||||
});
|
||||
|
||||
private @NotNull Result<Object, String> handleStateChange(SubscriptionContext ctx, String state, String handle) {
|
||||
return switch (state) {
|
||||
case "UP" -> instanceManager.getInfo(handle)
|
||||
.doOnNext(instance ->
|
||||
server.registerServer(new ServerInfo(handle, new InetSocketAddress(instance.getPort()))))
|
||||
.then();
|
||||
case "DOWN" -> instanceManager.getInfo(handle)
|
||||
.doOnNext(instance ->
|
||||
server.unregisterServer(new ServerInfo(handle, new InetSocketAddress(instance.getPort()))))
|
||||
.then();
|
||||
default -> Mono.empty();
|
||||
};
|
||||
natsRepo.handle("event.instance.down", (SyncRequestHandler) doc -> {
|
||||
var handle = doc.getString("handle");
|
||||
var port = doc.getInteger("port");
|
||||
return Results.ofCallable(() -> {
|
||||
server.unregisterServer(new ServerInfo(handle, new InetSocketAddress(port)));
|
||||
return new Document();
|
||||
}).mapFailure(Throwable::getMessage);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue