package eventDemo.domain.command.action import eventDemo.domain.command.CommandException import eventDemo.domain.command.command.IamReadyToPlayCommand import eventDemo.domain.event.event.PlayerReadyEvent import eventDemo.domain.event.projection.GameStateRepository /** * A command to set as ready to play */ class IamReadyToPlay( private val gameStateRepository: GameStateRepository, ) : CommandAction { @Throws(CommandException::class) override fun run(command: IamReadyToPlayCommand): (version: Int) -> PlayerReadyEvent { val state = gameStateRepository.get(command.payload.aggregateId) val playerExist: Boolean = state.players.contains(command.payload.player) val playerIsAlreadyReady: Boolean = state.readyPlayers.contains(command.payload.player) if (state.isStarted) { throw CommandException("The game is already started") } else if (!playerExist) { throw CommandException("You are not in the game") } else if (playerIsAlreadyReady) { throw CommandException("You are already ready") } else { return { version: Int -> PlayerReadyEvent( aggregateId = command.payload.aggregateId, player = command.payload.player, version = version, ) } } } }