The createStateForNewMatch
is the method responsible for initializing and returning a new game state, which will be used in the match that is about to start.
This method receives a list of ids of the players who will take part in the match and that most often needs to be saved on the match state for later usage by the server-side rules implementation.
@Override
public void createStateForNewMatch(List<Long> players) {
gameState = newState(Board.Origin.TOP_LEFT, 3, 3)
.withPlayers(players)
.build();
}
The game state is game-specific and you may design it in the way that best fits your game. As an example the game state may look like the following:
public final class GameState {
private Long nextPlayerToPlay;
private Integer nextMoveId;
private List<Long> players;
// ...
}
One important aspect to keep in mind about the game states is that they are persisted on the server as strings. This means that you will need to convert your game state from/to a string every time the server invokes the methods getState
and restoreState
. However, you can delegate this work to the provided SPI by extending your server-side rules implementation from the abstract class JavaGameRulesBase<T>
, where <T>
is the class type of your game state, e.g:
public class GameRules extends JavaGameRulesBase<BoardGameState> {
public GameRules(){
super(BoardGameState.class);
}
// ...
}