From d5b033e7311b814cbcd7efe4c9236e55f23a5f3b Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 11 Mar 2025 22:34:02 +0100 Subject: [PATCH] change validation on builder to error log --- .../eventDemo/app/command/ErrorNotifier.kt | 2 +- .../command/IWantToJoinTheGameCommand.kt | 6 ----- .../command/command/IamReadyToPlayCommand.kt | 4 +++- .../app/event/projection/GameStateBuilder.kt | 22 +++++++++++++++---- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/eventDemo/app/command/ErrorNotifier.kt b/src/main/kotlin/eventDemo/app/command/ErrorNotifier.kt index 4515190..a17b81f 100644 --- a/src/main/kotlin/eventDemo/app/command/ErrorNotifier.kt +++ b/src/main/kotlin/eventDemo/app/command/ErrorNotifier.kt @@ -17,7 +17,7 @@ fun errorNotifier( ErrorNotification(message = it) .let { notification -> logger.atWarn { - message = "Notification send ERROR: ${notification.message}" + message = "Notification ERROR sent: ${notification.message}" payload = mapOf( "notification" to notification, diff --git a/src/main/kotlin/eventDemo/app/command/command/IWantToJoinTheGameCommand.kt b/src/main/kotlin/eventDemo/app/command/command/IWantToJoinTheGameCommand.kt index 20305f1..46bd4c5 100644 --- a/src/main/kotlin/eventDemo/app/command/command/IWantToJoinTheGameCommand.kt +++ b/src/main/kotlin/eventDemo/app/command/command/IWantToJoinTheGameCommand.kt @@ -7,7 +7,6 @@ import eventDemo.app.event.GameEventHandler import eventDemo.app.event.event.NewPlayerEvent import eventDemo.app.event.projection.GameState import eventDemo.libs.command.CommandId -import io.github.oshai.kotlinlogging.KotlinLogging import kotlinx.serialization.Serializable /** @@ -30,7 +29,6 @@ data class IWantToJoinTheGameCommand( playerErrorNotifier: ErrorNotifier, eventHandler: GameEventHandler, ) { - val logger = KotlinLogging.logger {} if (!state.isStarted) { eventHandler.handle( NewPlayerEvent( @@ -39,10 +37,6 @@ data class IWantToJoinTheGameCommand( ), ) } else { - logger.atWarn { - message = "The game is already started" - payload = mapOf("player" to this@IWantToJoinTheGameCommand.payload.player) - } playerErrorNotifier("The game is already started") } } diff --git a/src/main/kotlin/eventDemo/app/command/command/IamReadyToPlayCommand.kt b/src/main/kotlin/eventDemo/app/command/command/IamReadyToPlayCommand.kt index 28fad91..d68d300 100644 --- a/src/main/kotlin/eventDemo/app/command/command/IamReadyToPlayCommand.kt +++ b/src/main/kotlin/eventDemo/app/command/command/IamReadyToPlayCommand.kt @@ -32,7 +32,9 @@ data class IamReadyToPlayCommand( val playerExist: Boolean = state.players.contains(payload.player) val playerIsAlreadyReady: Boolean = state.readyPlayers.contains(payload.player) - if (!playerExist) { + if (state.isStarted) { + playerErrorNotifier("The game is already started") + } else if (!playerExist) { playerErrorNotifier("You are not in the game") } else if (playerIsAlreadyReady) { playerErrorNotifier("You are already ready") diff --git a/src/main/kotlin/eventDemo/app/event/projection/GameStateBuilder.kt b/src/main/kotlin/eventDemo/app/event/projection/GameStateBuilder.kt index 7dcb532..8358e72 100644 --- a/src/main/kotlin/eventDemo/app/event/projection/GameStateBuilder.kt +++ b/src/main/kotlin/eventDemo/app/event/projection/GameStateBuilder.kt @@ -31,9 +31,17 @@ fun Collection.buildStateFromEvents(): GameState { fun GameState.apply(event: GameEvent): GameState = let { state -> + val logger = KotlinLogging.logger { } if (event is PlayerActionEvent) { if (state.currentPlayerTurn != event.player) { - error("inconsistent player turn. currentPlayerTurn: $currentPlayerTurn | player: ${event.player}") + logger.atError { + message = "Inconsistent player turn. CurrentPlayerTurn: $currentPlayerTurn | Player: ${event.player}" + payload = + mapOf( + "CurrentPlayerTurn" to (currentPlayerTurn ?: "No currentPlayerTurn"), + "Player" to event.player, + ) + } } } when (event) { @@ -67,7 +75,9 @@ fun GameState.apply(event: GameEvent): GameState = } is NewPlayerEvent -> { - if (state.isStarted) error("The game is already started") + if (state.isStarted) { + logger.error { "The game is already started" } + } state.copy( players = state.players + event.player, @@ -75,14 +85,18 @@ fun GameState.apply(event: GameEvent): GameState = } is PlayerReadyEvent -> { - if (state.isStarted) error("The game is already started") + if (state.isStarted) { + logger.error { "The game is already started" } + } state.copy( readyPlayers = state.readyPlayers + event.player, ) } is PlayerHavePassEvent -> { - if (event.takenCard != state.deck.stack.first()) error("taken card is not ot top of the stack") + if (event.takenCard != state.deck.stack.first()) { + logger.error { "taken card is not ot top of the stack: ${event.takenCard}" } + } state.copy( currentPlayerTurn = nextPlayerTurn, deck = state.deck.takeOneCardFromStackTo(event.player),