Skip to content

Commit

Permalink
feat: add game metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Mar 16, 2024
1 parent 8bda683 commit fe89a3a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/dev/emortal/minestom/gamesdk/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import dev.emortal.minestom.gamesdk.config.GameCreationInfo;
import dev.emortal.minestom.gamesdk.internal.GameManager;
import dev.emortal.minestom.gamesdk.util.GameEventPredicates;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.audience.PacketGroupingAudience;
import net.minestom.server.entity.Player;
Expand All @@ -15,11 +18,13 @@
import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class Game implements PacketGroupingAudience, TrackableGame {
protected final @NotNull Set<Meter> meters = new HashSet<>();

private final @NotNull GameCreationInfo creationInfo;
private final @NotNull EventNode<Event> eventNode;
Expand All @@ -33,6 +38,11 @@ public abstract class Game implements PacketGroupingAudience, TrackableGame {
protected Game(@NotNull GameCreationInfo creationInfo) {
this.creationInfo = creationInfo;
this.eventNode = this.createEventNode();

this.meters.add(Gauge.builder("gamesdk.game_player_count", this, game -> game.getPlayers().size())
.tag("gameId", this.creationInfo.id())
.description("The amount of players currently in the game")
.register(Metrics.globalRegistry));
}

/**
Expand Down Expand Up @@ -64,6 +74,10 @@ protected Game(@NotNull GameCreationInfo creationInfo) {
*/
public abstract void onLeave(@NotNull Player player);

public @NotNull Set<Meter> getMeters() {
return this.meters;
}

public abstract @NotNull Instance getSpawningInstance(@NotNull Player player);

public final @NotNull EventNode<Event> getEventNode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import dev.emortal.minestom.gamesdk.game.GameFinishedEvent;
import dev.emortal.minestom.gamesdk.game.GameProvider;
import dev.emortal.minestom.gamesdk.internal.listener.GameStatusListener;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.entity.Player;
Expand Down Expand Up @@ -40,6 +44,10 @@ public GameManager(@NotNull GameSdkConfig config) {
}

GameEventNodes.GAME_MANAGER.addListener(GameFinishedEvent.class, this::onGameFinish);

Gauge.builder("gamesdk.game_count", this.games, Set::size)
.description("The amount of games currently running")
.register(Metrics.globalRegistry);
}

private void initTestMode() {
Expand Down Expand Up @@ -73,6 +81,11 @@ public void startGame(@NotNull Game game) {
for (GameStatusListener listener : this.statusListeners) {
listener.onGameStart(game);
}

game.getMeters().add(Gauge.builder("gamesdk.game_start_time", System.currentTimeMillis(), Long::doubleValue)
.tag("gameId", game.getCreationInfo().id())
.description("The time the game started")
.register(Metrics.globalRegistry));
}

private void removeGame(@NotNull Game game) {
Expand Down Expand Up @@ -109,6 +122,7 @@ private void onGameFinish(@NotNull GameFinishedEvent event) {
private void cleanUpGame(@NotNull Game game) {
this.kickAllRemainingPlayers(game);
game.cleanUp();
game.getMeters().forEach(Meter::close);

// We call this here to ensure all the game's players are disconnected and the game is unregistered, so the check for the
// player count will actually see the new player count after the players are disconnected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
final class PreGameInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(PreGameInitializer.class);

private final @NotNull GameManager gameManager;

private final @NotNull GameSdkConfig config;
private final @NotNull Game game;

Expand All @@ -32,6 +34,7 @@ final class PreGameInitializer {
private final AtomicInteger playerCount = new AtomicInteger();

PreGameInitializer(@NotNull GameManager gameManager, @NotNull GameSdkConfig config, @NotNull Game game) {
this.gameManager = gameManager;
this.config = config;
this.game = game;

Expand Down Expand Up @@ -61,7 +64,7 @@ private void timeOut() {
int actualPlayerCount = this.game.getPlayers().size();
if (actualPlayerCount >= this.config.minPlayers()) {
this.cleanUpPreGame();
this.game.start();
this.gameManager.startGame(this.game);
} else {
// TODO: This isn't a normal finish. We should inform players that the game couldn't start and send them back to the lobby.
this.game.finish();
Expand Down

0 comments on commit fe89a3a

Please sign in to comment.