Skip to content

Commit

Permalink
feat: add magones counts and lists commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Aug 20, 2024
1 parent 844abe4 commit d045ab7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <duration>
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <T> @NotNull StreamObserver<T> createCallback(@NotNull CommandSender sender, @NotNull String sdkMethod,
Expand Down Expand Up @@ -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<String, AgonesSDKProto.GameServer.Status.CounterStatus> counters = gameServer.getStatus().getCountersMap();
StringJoiner joiner = new StringJoiner(", ", "Counters: \n", "");
for (Map.Entry<String, AgonesSDKProto.GameServer.Status.CounterStatus> 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<String, AgonesSDKProto.GameServer.Status.ListStatus> lists = gameServer.getStatus().getListsMap();

StringJoiner joiner = new StringJoiner(", ", "Lists: \n", "");
for (Map.Entry<String, AgonesSDKProto.GameServer.Status.ListStatus> 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) {
Expand Down

0 comments on commit d045ab7

Please sign in to comment.