Class JsonNotation

java.lang.Object
net.royalur.notation.JsonNotation
All Implemented Interfaces:
Notation

public class JsonNotation extends Object implements Notation
A notation that can be used to encode games of the Royal Game of Ur into JSON for serialisation. This notation has been created to be read by machines, not humans.
  • Field Details

    • LATEST_VERSION

      public static final int LATEST_VERSION
      The latest version of the JSON notation. If any breaking changes are made to the JSON notation, then this field will be updated to reflect that.
      See Also:
    • VERSION_KEY

      public static final String VERSION_KEY
      The key in the JSON for the version of the notation.
      See Also:
    • METADATA_KEY

      public static final String METADATA_KEY
      The key in the JSON for the metadata of the game.
      See Also:
    • SETTINGS_KEY

      public static final String SETTINGS_KEY
      The key in the JSON for the settings of a game.
      See Also:
    • BOARD_SHAPE_KEY

      public static final String BOARD_SHAPE_KEY
      The key in the JSON for the game setting of the board shape used.
      See Also:
    • PATHS_KEY

      public static final String PATHS_KEY
      The key in the JSON for the game setting of the paths used.
      See Also:
    • DICE_KEY

      public static final String DICE_KEY
      The key in the JSON for the game settings of the dice used.
      See Also:
    • STARTING_PIECE_COUNT_KEY

      public static final String STARTING_PIECE_COUNT_KEY
      The key in the JSON for game setting of the starting piece count.
      See Also:
    • SAFE_ROSETTES_KEY

      public static final String SAFE_ROSETTES_KEY
      The key in the JSON for the game setting of whether rosettes are safe.
      See Also:
    • ROSETTES_GRANT_EXTRA_ROLLS_KEY

      public static final String ROSETTES_GRANT_EXTRA_ROLLS_KEY
      The key in the JSON for the game setting of whether rosettes grant extra rolls.
      See Also:
    • CAPTURES_GRANT_EXTRA_ROLLS_KEY

      public static final String CAPTURES_GRANT_EXTRA_ROLLS_KEY
      The key in the JSON for the game setting of whether rosettes grant extra rolls.
      See Also:
    • INITIAL_STATE_KEY

      public static final String INITIAL_STATE_KEY
      The key in the JSON for the initial state of the board and players.
      See Also:
    • STATES_KEY

      public static final String STATES_KEY
      The key in the JSON for the list of states in the game.
      See Also:
    • STATE_TYPE_KEY

      public static final String STATE_TYPE_KEY
      The key in the JSON for the type of state.
      See Also:
    • STATE_TIME_KEY

      public static final String STATE_TIME_KEY
      The key in the JSON for the time this state was reached in milliseconds since the start of the game.
      See Also:
    • STATE_TYPE_ROLLED

      public static final String STATE_TYPE_ROLLED
      Represents states of type RolledGameState.
      See Also:
    • STATE_TYPE_MOVED

      public static final String STATE_TYPE_MOVED
      Represents states of type MovedGameState.
      See Also:
    • STATE_TYPE_WAITING_FOR_ROLL

      public static final String STATE_TYPE_WAITING_FOR_ROLL
      Represents states of type WaitingForRollGameState.
      See Also:
    • STATE_TYPE_WAITING_FOR_MOVE

      public static final String STATE_TYPE_WAITING_FOR_MOVE
      Represents states of type WaitingForMoveGameState.
      See Also:
    • STATE_TYPE_RESIGNED

      public static final String STATE_TYPE_RESIGNED
      Represents states of type ResignedGameState.
      See Also:
    • STATE_TYPE_ABANDONED

      public static final String STATE_TYPE_ABANDONED
      Represents states of type AbandonedGameState.
      See Also:
    • STATE_TYPE_END

      public static final String STATE_TYPE_END
      Represents states of type EndGameState.
      See Also:
    • ROLL_KEY

      public static final String ROLL_KEY
      The key in the JSON for the roll that was made.
      See Also:
    • ROLL_VALUE_KEY

      public static final String ROLL_VALUE_KEY
      The key in the JSON for the value of a roll.
      See Also:
    • MOVE_KEY

      public static final String MOVE_KEY
      The key in the JSON for a move that was made.
      See Also:
    • MOVE_SOURCE_KEY

      public static final String MOVE_SOURCE_KEY
      The key in the JSON for the source piece of a move.
      See Also:
    • MOVE_DEST_KEY

      public static final String MOVE_DEST_KEY
      The key in the JSON for the destination piece of a move.
      See Also:
    • MOVE_CAPTURED_KEY

      public static final String MOVE_CAPTURED_KEY
      The key in the JSON for the captured piece of a move.
      See Also:
    • AVAILABLE_MOVES_KEY

      public static final String AVAILABLE_MOVES_KEY
      The key in the JSON for the moves that are available in a state.
      See Also:
    • OLD_PIECE_OWNER_KEY

      public static final String OLD_PIECE_OWNER_KEY
      The old key in the JSON for the owner of a piece.
      See Also:
    • OLD_PIECE_INDEX_KEY

      public static final String OLD_PIECE_INDEX_KEY
      The old key in the JSON for the index of a piece on its path.
      See Also:
    • TURN_KEY

      public static final String TURN_KEY
      The key in the JSON for the player whose turn it is in a state.
      See Also:
    • CONTROL_PLAYER_KEY

      public static final String CONTROL_PLAYER_KEY
      The key in the JSON for the player that abandoned the game.
      See Also:
    • ABANDONED_REASON_KEY

      public static final String ABANDONED_REASON_KEY
      The key in the JSON for the reason that a game was abandoned.
      See Also:
    • WINNER_KEY

      public static final String WINNER_KEY
      The key in the JSON for the player that won the game.
      See Also:
    • BOARD_KEY

      public static final String BOARD_KEY
      The key in the JSON for the state of the board.
      See Also:
    • BOARD_PIECES_KEY

      public static final String BOARD_PIECES_KEY
      The key in the JSON for the pieces on a board.
      See Also:
    • PLAYERS_KEY

      public static final String PLAYERS_KEY
      The key in the JSON for the state of the players.
      See Also:
    • PLAYER_PIECES_KEY

      public static final String PLAYER_PIECES_KEY
      The key in the JSON for the pieces of a player state.
      See Also:
    • PLAYER_SCORE_KEY

      public static final String PLAYER_SCORE_KEY
      The key in the JSON for the score of a player state.
      See Also:
  • Constructor Details

    • JsonNotation

      public JsonNotation(Map<String,? extends BoardShapeFactory> boardShapes, Map<String,? extends PathPairFactory> pathPairs, Map<String,? extends DiceFactory> dice, RuleSetProvider ruleSetProvider, com.fasterxml.jackson.core.JsonFactory jsonFactory)
      Instantiates the JSON notation to encode and decode games.
      Parameters:
      pathPairs - The paths that can be parsed in this notation.
      boardShapes - The board shapes that can be parsed in this notation.
      dice - The dice that can be parsed in this notation.
      ruleSetProvider - The provider to create rule sets from game settings.
      jsonFactory - A factory to build generators to write the JSON.
    • JsonNotation

      public JsonNotation(Map<String,? extends BoardShapeFactory> boardShapes, Map<String,? extends PathPairFactory> pathPairs, Map<String,? extends DiceFactory> dice, RuleSetProvider ruleSetProvider)
      Instantiates the JSON notation to encode and decode games.
      Parameters:
      pathPairs - The paths that can be parsed in this notation.
      boardShapes - The board shapes that can be parsed in this notation.
      dice - The dice that can be parsed in this notation.
      ruleSetProvider - The provider to create rule sets from game settings.
    • JsonNotation

      public JsonNotation()
      Instantiates a default JSON notation to read simple games.
  • Method Details

    • getJsonFactory

      public com.fasterxml.jackson.core.JsonFactory getJsonFactory()
    • getObjectMapper

      public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
    • writeRoll

      public void writeRoll(com.fasterxml.jackson.core.JsonGenerator generator, Roll roll) throws IOException
      Throws:
      IOException
    • encodePiece

      public int encodePiece(Piece piece)
    • writePieceField

      protected void writePieceField(com.fasterxml.jackson.core.JsonGenerator generator, String fieldName, @Nullable Piece piece) throws IOException
      Throws:
      IOException
    • writeMove

      public void writeMove(com.fasterxml.jackson.core.JsonGenerator generator, Move move) throws IOException
      Throws:
      IOException
    • writeMoveList

      public void writeMoveList(com.fasterxml.jackson.core.JsonGenerator generator, List<Move> moves) throws IOException
      Throws:
      IOException
    • writeBoard

      public void writeBoard(com.fasterxml.jackson.core.JsonGenerator generator, Board board) throws IOException
      Throws:
      IOException
    • writePlayerState

      public void writePlayerState(com.fasterxml.jackson.core.JsonGenerator generator, PlayerState playerState) throws IOException
      Throws:
      IOException
    • writeRolledState

      public void writeRolledState(com.fasterxml.jackson.core.JsonGenerator generator, RolledGameState state) throws IOException
      Throws:
      IOException
    • writeMovedState

      public void writeMovedState(com.fasterxml.jackson.core.JsonGenerator generator, MovedGameState state) throws IOException
      Throws:
      IOException
    • writeActionState

      public void writeActionState(com.fasterxml.jackson.core.JsonGenerator generator, ActionGameState state) throws IOException
      Throws:
      IOException
    • writeWaitingForRollState

      public void writeWaitingForRollState(com.fasterxml.jackson.core.JsonGenerator generator, WaitingForRollGameState state)
    • writeWaitingForMoveState

      public void writeWaitingForMoveState(com.fasterxml.jackson.core.JsonGenerator generator, WaitingForMoveGameState state) throws IOException
      Throws:
      IOException
    • writePlayableState

      public void writePlayableState(com.fasterxml.jackson.core.JsonGenerator generator, PlayableGameState state) throws IOException
      Throws:
      IOException
    • writeOngoingState

      public void writeOngoingState(com.fasterxml.jackson.core.JsonGenerator generator, OngoingGameState state) throws IOException
      Throws:
      IOException
    • writeResignedState

      public void writeResignedState(com.fasterxml.jackson.core.JsonGenerator generator, ResignedGameState state) throws IOException
      Throws:
      IOException
    • writeAbandonedState

      public void writeAbandonedState(com.fasterxml.jackson.core.JsonGenerator generator, AbandonedGameState state) throws IOException
      Throws:
      IOException
    • writeControlState

      public void writeControlState(com.fasterxml.jackson.core.JsonGenerator generator, ControlGameState state) throws IOException
      Throws:
      IOException
    • writeEndState

      public void writeEndState(com.fasterxml.jackson.core.JsonGenerator generator, EndGameState state) throws IOException
      Throws:
      IOException
    • getStateType

      public String getStateType(GameState state)
    • writeDerivedState

      public void writeDerivedState(com.fasterxml.jackson.core.JsonGenerator generator, GameState state) throws IOException
      Throws:
      IOException
    • writeStates

      public void writeStates(com.fasterxml.jackson.core.JsonGenerator generator, List<GameState> states) throws IOException
      Throws:
      IOException
    • writeCompleteState

      public void writeCompleteState(com.fasterxml.jackson.core.JsonGenerator generator, GameState state) throws IOException
      Throws:
      IOException
    • writeGameSettings

      public void writeGameSettings(com.fasterxml.jackson.core.JsonGenerator generator, GameSettings settings) throws IOException
      Throws:
      IOException
    • writeMetadata

      public void writeMetadata(com.fasterxml.jackson.core.JsonGenerator generator, GameMetadata metadata) throws IOException
      Throws:
      IOException
    • writeGame

      public void writeGame(com.fasterxml.jackson.core.JsonGenerator generator, Game game) throws IOException
      Throws:
      IOException
    • encodeGame

      public String encodeGame(Game game)
      Description copied from interface: Notation
      Encodes the given game, game, into text.
      Specified by:
      encodeGame in interface Notation
      Parameters:
      game - The game to be encoded.
      Returns:
      Text that represents game in this notation.
    • readRoll

      public Roll readRoll(RuleSet rules, com.fasterxml.jackson.databind.node.ObjectNode json)
    • readOldPiece

      public Piece readOldPiece(RuleSet rules, com.fasterxml.jackson.databind.node.ObjectNode json)
    • readNullablePiece

      @Nullable public Piece readNullablePiece(RuleSet rules, com.fasterxml.jackson.databind.node.ObjectNode json, String key)
    • readPiece

      public Piece readPiece(RuleSet rules, com.fasterxml.jackson.databind.node.ObjectNode json, String key)
    • readMove

      public Move readMove(RuleSet rules, com.fasterxml.jackson.databind.node.ObjectNode json)
    • readMoveList

      public List<Move> readMoveList(RuleSet rules, com.fasterxml.jackson.databind.node.ArrayNode json)
    • readBoard

      public Board readBoard(RuleSet rules, com.fasterxml.jackson.databind.node.ObjectNode json)
    • readPlayerState

      public PlayerState readPlayerState(RuleSet rules, PlayerType playerType, com.fasterxml.jackson.databind.node.ObjectNode json)
    • isActionState

      public boolean isActionState(String stateType)
    • isPlayableState

      public boolean isPlayableState(String stateType)
    • isOngoingState

      public boolean isOngoingState(String stateType)
    • isControlState

      public boolean isControlState(String stateType)
    • readRolledState

      public RolledGameState readRolledState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, PlayerType turn)
    • readMovedState

      public MovedGameState readMovedState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, PlayerType turn)
    • readWaitingForRollState

      public WaitingForRollGameState readWaitingForRollState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, PlayerType turn)
    • readWaitingForMoveState

      public WaitingForMoveGameState readWaitingForMoveState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, PlayerType turn)
    • readActionState

      public ActionGameState readActionState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, String stateType, PlayerType turn)
    • readPlayableState

      public PlayableGameState readPlayableState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, String stateType, PlayerType turn)
    • readOngoingState

      public OngoingGameState readOngoingState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, String stateType)
    • readResignedState

      public ResignedGameState readResignedState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, PlayerType player)
    • readAbandonedState

      public AbandonedGameState readAbandonedState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, @Nullable PlayerType player)
    • readControlState

      public ControlGameState readControlState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json, String stateType)
    • readEndState

      public EndGameState readEndState(RuleSet rules, long timeSinceGameStartMs, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json)
    • readDerivedState

      public GameState readDerivedState(RuleSet rules, StateSource stateSource, com.fasterxml.jackson.databind.node.ObjectNode json)
    • readCompleteState

      public GameState readCompleteState(RuleSet rules, com.fasterxml.jackson.databind.node.ObjectNode json)
    • readStates

      public List<GameState> readStates(RuleSet rules, GameState initialState, com.fasterxml.jackson.databind.node.ArrayNode json)
    • readGameSettings

      public GameSettings readGameSettings(com.fasterxml.jackson.databind.node.ObjectNode json)
    • readMetadata

      public GameMetadata readMetadata(com.fasterxml.jackson.databind.node.ObjectNode json)
    • readGameV1Or2

      public Game readGameV1Or2(com.fasterxml.jackson.databind.node.ObjectNode json)
    • readGame

      public Game readGame(com.fasterxml.jackson.databind.node.ObjectNode json)
    • decodeGame

      public Game decodeGame(String encoded)
      Description copied from interface: Notation
      Decodes the game from the text encoded, based upon the rules rules.
      Specified by:
      decodeGame in interface Notation
      Parameters:
      encoded - The text to decode into a game.
      Returns:
      The decoded game.