|
2 | 2 |
|
3 | 3 | import dev.agones.sdk.AgonesSDKProto;
|
4 | 4 | import dev.agones.sdk.SDKGrpc;
|
| 5 | +import dev.agones.sdk.beta.BetaAgonesSDKProto; |
5 | 6 | import dev.emortal.api.agonessdk.IgnoredStreamObserver;
|
| 7 | +import dev.emortal.minestom.core.module.kubernetes.KubernetesModule; |
6 | 8 | import dev.emortal.minestom.gamesdk.config.GameSdkConfig;
|
7 | 9 | import dev.emortal.minestom.gamesdk.game.Game;
|
| 10 | + |
8 | 11 | import java.util.concurrent.atomic.AtomicBoolean;
|
9 | 12 | import java.util.concurrent.atomic.AtomicInteger;
|
10 | 13 |
|
| 14 | +import dev.emortal.minestom.gamesdk.game.GameProvider; |
11 | 15 | import net.minestom.server.MinecraftServer;
|
12 | 16 | import org.jetbrains.annotations.NotNull;
|
13 | 17 | import org.slf4j.Logger;
|
|
16 | 20 | public final class AgonesGameStatusListener implements GameStatusListener {
|
17 | 21 | private static final Logger LOGGER = LoggerFactory.getLogger(AgonesGameStatusListener.class);
|
18 | 22 |
|
19 |
| - private final @NotNull GameSdkConfig config; |
| 23 | + private final @NotNull GameProvider gameProvider; |
| 24 | + private final @NotNull KubernetesModule kubeModule; |
20 | 25 | private final @NotNull SDKGrpc.SDKStub sdk;
|
21 | 26 |
|
22 |
| - private final AtomicInteger gameCount = new AtomicInteger(0); |
23 |
| - private final AtomicBoolean shouldAllocate = new AtomicBoolean(false); |
24 |
| - |
25 |
| - public AgonesGameStatusListener(@NotNull GameSdkConfig config, @NotNull SDKGrpc.SDKStub sdk) { |
26 |
| - this.config = config; |
27 |
| - this.sdk = sdk; |
28 |
| - } |
29 |
| - |
30 |
| - @Override |
31 |
| - public void onGameAdded(@NotNull Game game) { |
32 |
| - int newGameCount = this.gameCount.incrementAndGet(); |
33 |
| - this.updateShouldAllocate(newGameCount); |
| 27 | + public AgonesGameStatusListener(@NotNull GameProvider gameProvider, @NotNull KubernetesModule kubeModule) { |
| 28 | + this.kubeModule = kubeModule; |
| 29 | + this.gameProvider = gameProvider; |
| 30 | + this.sdk = kubeModule.getAgonesSdk(); |
34 | 31 | }
|
35 | 32 |
|
36 | 33 | @Override
|
37 | 34 | public void onGameRemoved(@NotNull Game game) {
|
38 |
| - int newGameCount = this.gameCount.decrementAndGet(); |
39 |
| - this.updateShouldAllocate(newGameCount); |
40 |
| - this.updateReadyIfEmpty(newGameCount); |
41 |
| - } |
42 |
| - |
43 |
| - private void updateShouldAllocate(int gameCount) { |
44 |
| - boolean allocate = gameCount < this.config.maxGames(); |
45 |
| - |
46 |
| - boolean changed = this.shouldAllocate.getAndSet(allocate) != allocate; |
47 |
| - // If the current value is the same as the new value, don't bother updating |
48 |
| - if (!changed) return; |
49 |
| - |
50 |
| - LOGGER.info("Updating should allocate to {} (game count: {})", allocate, gameCount); |
51 |
| - |
52 |
| - AgonesSDKProto.KeyValue keyValue = AgonesSDKProto.KeyValue.newBuilder() |
53 |
| - .setKey("should-allocate") |
54 |
| - .setValue(String.valueOf(allocate)) |
55 |
| - .build(); |
56 |
| - Thread.startVirtualThread(() -> this.sdk.setLabel(keyValue, new IgnoredStreamObserver<>())); |
57 |
| - } |
58 |
| - |
59 |
| - private void updateReadyIfEmpty(int gameCount) { |
60 |
| - int playerCount = MinecraftServer.getConnectionManager().getOnlinePlayers().size(); |
61 |
| - if (playerCount > 0) return; |
62 |
| - |
63 |
| - if (gameCount > 0) { |
64 |
| - // This is really weird. This would only happen if a game didn't unregister itself properly. |
65 |
| - LOGGER.warn("No players online, but there are still games running."); |
66 |
| - } |
67 |
| - |
68 |
| - LOGGER.info("Marking server as ready as no players are online."); |
69 |
| - Thread.startVirtualThread(() -> this.sdk.ready(AgonesSDKProto.Empty.getDefaultInstance(), new IgnoredStreamObserver<>())); |
| 35 | + Thread.startVirtualThread(() -> { |
| 36 | + this.kubeModule.updateAgonesCounter("games", -1); |
| 37 | + this.kubeModule.removeFromAgonesList("games", game.getCreationInfo().id()); |
| 38 | + |
| 39 | + if (this.gameProvider.getGameCount() == 0) { |
| 40 | + LOGGER.info("Marking server as ready as no players are online."); |
| 41 | + Thread.startVirtualThread(() -> this.sdk.ready(AgonesSDKProto.Empty.getDefaultInstance(), new IgnoredStreamObserver<>())); |
| 42 | + } |
| 43 | + }); |
70 | 44 | }
|
71 | 45 | }
|
0 commit comments