refactor: fix cast warning
This commit is contained in:
+2
-10
@@ -37,16 +37,8 @@ abstract class GameEventManager(
|
||||
return this
|
||||
}
|
||||
|
||||
protected fun <G : Game> Game.isStatusOrFail(
|
||||
kClass: KClass<G>,
|
||||
message: String,
|
||||
): G {
|
||||
if (kClass.isInstance(this)) {
|
||||
return this as G
|
||||
} else {
|
||||
throw CommandException(message)
|
||||
}
|
||||
}
|
||||
protected inline fun <reified G : Game> Game.isStatusOrFail(message: String): G =
|
||||
this as? G ?: throw CommandException(message)
|
||||
|
||||
protected fun <T> retry(
|
||||
mapAttempts: Int = 5,
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ class JoinTheGameHandler(
|
||||
retry {
|
||||
command
|
||||
.getGame()
|
||||
.isStatusOrFail(GameCreated::class, "The game is started")
|
||||
.isStatusOrFail<GameCreated>("The game is started")
|
||||
.userJoinTheGame(
|
||||
userId = command.userId,
|
||||
name = user.username,
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ class PlayCardHandler(
|
||||
override fun handle(command: PlayCardCommand) {
|
||||
command
|
||||
.getGame()
|
||||
.isStatusOrFail(GameStarted::class, "The game is not started")
|
||||
.isStatusOrFail<GameStarted>("The game is not started")
|
||||
.playTheCard(
|
||||
card = command.payload.card,
|
||||
playerId = command.payload.playerId,
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ class ReadyToPlayHandler(
|
||||
override fun handle(command: ReadyToPlayCommand) {
|
||||
command
|
||||
.getGame()
|
||||
.isStatusOrFail(GameCreated::class, "The game is started")
|
||||
.isStatusOrFail<GameCreated>("The game is started")
|
||||
.setReadyPlayer(command.payload.playerId)
|
||||
.saveEvents()
|
||||
.publishEvents()
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ class TakeCartFromDrawPileHandler(
|
||||
override fun handle(command: TakeCartFromDrawPileCommand) {
|
||||
command
|
||||
.getGame()
|
||||
.isStatusOrFail(GameStarted::class, "The game is not started")
|
||||
.isStatusOrFail<GameStarted>("The game is not started")
|
||||
.playerTakeCartFromDrawPile(command.payload.playerId, 1)
|
||||
.saveEvents()
|
||||
.publishEvents()
|
||||
|
||||
Reference in New Issue
Block a user