create GameCommandRunner to extract business logic
This commit is contained in:
@@ -1,15 +1,8 @@
|
||||
package eventDemo.app.command
|
||||
|
||||
import eventDemo.app.command.command.GameCommand
|
||||
import eventDemo.app.command.command.ICantPlayCommand
|
||||
import eventDemo.app.command.command.IWantToJoinTheGameCommand
|
||||
import eventDemo.app.command.command.IWantToPlayCardCommand
|
||||
import eventDemo.app.command.command.IamReadyToPlayCommand
|
||||
import eventDemo.app.entity.Player
|
||||
import eventDemo.app.event.GameEventHandler
|
||||
import eventDemo.app.event.event.GameEvent
|
||||
import eventDemo.app.event.projection.GameStateRepository
|
||||
import eventDemo.app.notification.ErrorNotification
|
||||
import eventDemo.app.notification.Notification
|
||||
import eventDemo.libs.command.CommandStreamChannelBuilder
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
@@ -22,9 +15,8 @@ import kotlinx.coroutines.channels.SendChannel
|
||||
* This action can be executing an action and produce a new [GameEvent] after verification.
|
||||
*/
|
||||
class GameCommandHandler(
|
||||
private val eventHandler: GameEventHandler,
|
||||
private val gameStateRepository: GameStateRepository,
|
||||
private val commandStreamChannel: CommandStreamChannelBuilder<GameCommand>,
|
||||
private val runner: GameCommandRunner,
|
||||
) {
|
||||
private val logger = KotlinLogging.logger { }
|
||||
|
||||
@@ -48,30 +40,7 @@ class GameCommandHandler(
|
||||
message = "Handle command: $command"
|
||||
payload = mapOf("command" to command)
|
||||
}
|
||||
command.run(outgoingErrorChannelNotification)
|
||||
runner.run(command, outgoingErrorChannelNotification)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun GameCommand.run(outgoingErrorChannelNotification: SendChannel<Notification>) {
|
||||
val gameState = gameStateRepository.get(payload.gameId)
|
||||
val playerErrorNotifier = errorNotifier(outgoingErrorChannelNotification)
|
||||
|
||||
when (this) {
|
||||
is IWantToPlayCardCommand -> run(gameState, playerErrorNotifier, eventHandler)
|
||||
is IamReadyToPlayCommand -> run(gameState, playerErrorNotifier, eventHandler)
|
||||
is IWantToJoinTheGameCommand -> run(gameState, playerErrorNotifier, eventHandler)
|
||||
is ICantPlayCommand -> run(gameState, playerErrorNotifier, eventHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun errorNotifier(channel: SendChannel<Notification>): suspend (String) -> Unit =
|
||||
{
|
||||
val logger = KotlinLogging.logger { }
|
||||
val notification = ErrorNotification(message = it)
|
||||
logger.atWarn {
|
||||
message = "Notification send ERROR: ${notification.message}"
|
||||
payload = mapOf("notification" to notification)
|
||||
}
|
||||
channel.send(notification)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user