Skip to content

Commit

Permalink
feat: fully implement requeue option
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Mar 10, 2024
1 parent 744c39d commit 8bda683
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
}

dependencies {
api("dev.emortal.minestom:core:87615bb")
api("dev.emortal.minestom:core:eafe4f7")
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @param gameCreator a function that can be called to create a game instance
*/
public record GameSdkConfig(int minPlayers, int maxGames, int minTrackingInterval, int maxTrackingInterval,
boolean lobbyOnFinish, @NotNull GameCreator gameCreator) {
FinishBehaviour finishBehaviour, @NotNull GameCreator gameCreator) {

public static @NotNull Builder builder() {
return new BuilderImpl();
Expand All @@ -33,7 +33,7 @@ interface GameCreatorStep {

@NotNull GameCreatorStep maxTrackingInterval(int interval);

@NotNull GameCreatorStep lobbyOnFinish(boolean lobbyOnFinish);
@NotNull GameCreatorStep finishBehaviour(FinishBehaviour finishBehaviour);

@NotNull EndStep gameCreator(@NotNull GameCreator creator);
}
Expand All @@ -50,7 +50,7 @@ private static final class BuilderImpl implements Builder, Builder.MaxGamesStep,
private int maxGames;
private int minTrackingInterval = GameTracker.DEFAULT_MIN_UPDATE_INTERVAL;
private int maxTrackingInterval = GameTracker.DEFAULT_MAX_UPDATE_INTERVAL;
private boolean lobbyOnFinish = true;
private FinishBehaviour finishBehaviour = FinishBehaviour.LOBBY;
private GameCreator gameCreator;

@Override
Expand Down Expand Up @@ -78,8 +78,8 @@ private static final class BuilderImpl implements Builder, Builder.MaxGamesStep,
}

@Override
public @NotNull GameCreatorStep lobbyOnFinish(boolean lobbyOnFinish) {
this.lobbyOnFinish = lobbyOnFinish;
public @NotNull GameCreatorStep finishBehaviour(FinishBehaviour finishBehaviour) {
this.finishBehaviour = finishBehaviour;
return this;
}

Expand All @@ -92,7 +92,11 @@ private static final class BuilderImpl implements Builder, Builder.MaxGamesStep,
@Override
public @NotNull GameSdkConfig build() {
return new GameSdkConfig(this.minPlayers, this.maxGames, this.minTrackingInterval, this.maxTrackingInterval,
this.lobbyOnFinish, this.gameCreator);
this.finishBehaviour, this.gameCreator);
}
}

public enum FinishBehaviour {
LOBBY, REQUEUE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ private void movePlayersOnThisServer(@NotNull Game newGame, @NotNull Set<UUID> p
Player player = connectionManager.getOnlinePlayerByUuid(playerId);
if (player == null) continue;

// The old game will be null if the player is queued when the game is finishing. See REQUEUE logic
Game oldGame = this.gameManager.findGame(player);
if (oldGame == null) continue;
if (oldGame != null) GamePlayerTracker.removePlayer(oldGame, player);

GamePlayerTracker.removePlayer(oldGame, player);
GamePlayerTracker.addPlayer(newGame, player);

// Increment after using the index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ private void onGameFinish(@NotNull GameFinishedEvent event) {
}

this.removeGame(game);
if (this.config.lobbyOnFinish()) {
KurushimiMinestomUtils.sendToLobby(game.getPlayers(), () -> this.cleanUpGame(game), () -> this.cleanUpGame(game));
} else {
this.cleanUpGame(game);
switch (this.config.finishBehaviour()) {
case LOBBY ->
KurushimiMinestomUtils.sendToLobby(game.getPlayers(), () -> this.cleanUpGame(game), () -> this.cleanUpGame(game));
case REQUEUE ->
KurushimiMinestomUtils.sendToGamemode(game.getPlayers(), game.getCreationInfo().gameModeId(),
() -> this.cleanUpGame(game), () -> this.cleanUpGame(game), 1);
}
}

Expand Down

0 comments on commit 8bda683

Please sign in to comment.