diff --git a/src/main/java/dev/emortal/minestom/core/module/kubernetes/KubernetesModule.java b/src/main/java/dev/emortal/minestom/core/module/kubernetes/KubernetesModule.java index 885ac6a..b5ec133 100644 --- a/src/main/java/dev/emortal/minestom/core/module/kubernetes/KubernetesModule.java +++ b/src/main/java/dev/emortal/minestom/core/module/kubernetes/KubernetesModule.java @@ -89,7 +89,7 @@ private void loadAgones() { this.sdk = SDKGrpc.newStub(channel); this.betaSdk = dev.agones.sdk.beta.SDKGrpc.newStub(channel); - MinecraftServer.getCommandManager().register(new AgonesCommand(this.sdk)); + MinecraftServer.getCommandManager().register(new AgonesCommand(this.sdk, this.betaSdk)); for (AgonesSDKProto.KeyValue label : this.additionalLabels) { this.sdk.setLabel(label, new IgnoredStreamObserver<>()); diff --git a/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/AgonesCommand.java b/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/AgonesCommand.java index 88719f2..02a99a9 100644 --- a/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/AgonesCommand.java +++ b/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/AgonesCommand.java @@ -11,18 +11,21 @@ public final class AgonesCommand extends Command { - public AgonesCommand(@NotNull SDKGrpc.SDKStub sdk) { + public AgonesCommand(@NotNull SDKGrpc.SDKStub sdk, dev.agones.sdk.beta.SDKGrpc.SDKStub betaSdk) { super("magones"); this.setCondition(ExtraConditions.hasPermission("command.agones")); - var sdkSubs = new SdkSubCommands(sdk); + var sdkSubs = new SdkSubCommands(sdk, betaSdk); // /magones get gameserver this.addSyntax(sdkSubs::executeGetGameServer, new ArgumentLiteral("get"), new ArgumentLiteral("gameserver")); - // /magones reserve - this.addSyntax(sdkSubs::executeReserve, new ArgumentLiteral("reserve"), new ArgumentTime("duration")); - // /magones allocate - this.addSyntax(sdkSubs::executeAllocate, new ArgumentLiteral("allocate")); + + // /magones get count [id] + this.addSyntax(sdkSubs::executeListCounters, new ArgumentLiteral("get"), new ArgumentLiteral("counters")); + + // /magones get list [id] + this.addSyntax(sdkSubs::executeListLists, new ArgumentLiteral("get"), new ArgumentLiteral("list")); + this.addSyntax(sdkSubs::executeGetList, new ArgumentLiteral("get"), new ArgumentLiteral("list"), new ArgumentString("id")); var set = new ArgumentLiteral("set"); var key = new ArgumentString("key"); diff --git a/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/SdkSubCommands.java b/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/SdkSubCommands.java index e244770..d30aa16 100644 --- a/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/SdkSubCommands.java +++ b/src/main/java/dev/emortal/minestom/core/module/kubernetes/command/agones/SdkSubCommands.java @@ -1,8 +1,13 @@ package dev.emortal.minestom.core.module.kubernetes.command.agones; +import allocation.Allocation; import dev.agones.sdk.AgonesSDKProto; import dev.agones.sdk.SDKGrpc; +import dev.agones.sdk.beta.BetaAgonesSDKProto; import io.grpc.stub.StreamObserver; + +import java.util.Map; +import java.util.StringJoiner; import java.util.function.Function; import net.kyori.adventure.text.Component; import net.minestom.server.command.CommandSender; @@ -15,14 +20,20 @@ public final class SdkSubCommands { private static @NotNull Component generateMessage(@NotNull String sdk, @NotNull String method, @NotNull AgonesCommand.RequestStatus status, @NotNull String message) { - String text = "Agones >> [%s.%s] (%s) %s".formatted(sdk, method, status.name(), message); + String text = "Agones >> [%s.%s] (%s)".formatted(sdk, method, status.name()); + if (!message.isEmpty()) { + text += "\n" + message; + } + return Component.text(text, status.getColor()); } private final @NotNull SDKGrpc.SDKStub sdk; + private final @NotNull dev.agones.sdk.beta.SDKGrpc.SDKStub betaSdk; - public SdkSubCommands(@NotNull SDKGrpc.SDKStub sdk) { + public SdkSubCommands(@NotNull SDKGrpc.SDKStub sdk, dev.agones.sdk.beta.SDKGrpc.@NotNull SDKStub betaSdk) { this.sdk = sdk; + this.betaSdk = betaSdk; } private @NotNull StreamObserver createCallback(@NotNull CommandSender sender, @NotNull String sdkMethod, @@ -54,15 +65,40 @@ public void executeGetGameServer(@NotNull CommandSender sender, @NotNull Command this.sdk.getGameServer(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "GetGameServer", Object::toString)); } - public void executeReserve(@NotNull CommandSender sender, @NotNull CommandContext context) { - Duration duration = context.get("duration"); - AgonesSDKProto.Duration agonesDuration = AgonesSDKProto.Duration.newBuilder().setSeconds(duration.getSeconds()).build(); + public void executeListCounters(@NotNull CommandSender sender, @NotNull CommandContext context) { + this.sdk.getGameServer(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "GetGameServer [Counters]", gameServer -> { + Map counters = gameServer.getStatus().getCountersMap(); + StringJoiner joiner = new StringJoiner(", ", "Counters: \n", ""); + for (Map.Entry entry : counters.entrySet()) { + String key = entry.getKey(); + AgonesSDKProto.GameServer.Status.CounterStatus value = entry.getValue(); + joiner.add(" - %s (%s/%s)".formatted(key, value.getCount(), value.getCapacity())); + } + + return joiner.toString(); + })); + } + + public void executeListLists(@NotNull CommandSender sender, @NotNull CommandContext context) { + this.sdk.getGameServer(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "GetGameServer [Lists]", gameServer -> { + Map lists = gameServer.getStatus().getListsMap(); + + StringJoiner joiner = new StringJoiner(", ", "Lists: \n", ""); + for (Map.Entry entry : lists.entrySet()) { + String key = entry.getKey(); + AgonesSDKProto.GameServer.Status.ListStatus value = entry.getValue(); + joiner.add(" - %s (%s/%s)".formatted(key, value.getValuesCount(), value.getCapacity())); + } - this.sdk.reserve(agonesDuration, this.createCallback(sender, "Reserve", v -> "Reserved for %s seconds".formatted(duration.getSeconds()))); + return joiner.toString(); + })); } - public void executeAllocate(@NotNull CommandSender sender, @NotNull CommandContext context) { - this.sdk.allocate(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "Allocate", v -> "Allocated")); + public void executeGetList(@NotNull CommandSender sender, @NotNull CommandContext context) { + String id = context.get("id"); + AgonesSDKProto.KeyValue keyValue = AgonesSDKProto.KeyValue.newBuilder().setKey(id).build(); + this.betaSdk.getList(BetaAgonesSDKProto.GetListRequest.newBuilder().setName(id).build(), + this.createCallback(sender, "GetList", Object::toString)); } public void executeSetAnnotation(@NotNull CommandSender sender, @NotNull CommandContext context) {