change validation on builder to error log
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -31,9 +31,17 @@ fun Collection<GameEvent>.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),
|
||||
|
||||
Reference in New Issue
Block a user