create IWantToJoinTheGameCommand
This commit is contained in:
@@ -2,6 +2,7 @@ package eventDemo.app.command
|
|||||||
|
|
||||||
import eventDemo.app.GameState
|
import eventDemo.app.GameState
|
||||||
import eventDemo.app.command.command.GameCommand
|
import eventDemo.app.command.command.GameCommand
|
||||||
|
import eventDemo.app.command.command.IWantToJoinTheGameCommand
|
||||||
import eventDemo.app.command.command.IWantToPlayCardCommand
|
import eventDemo.app.command.command.IWantToPlayCardCommand
|
||||||
import eventDemo.app.command.command.IamReadyToPlayCommand
|
import eventDemo.app.command.command.IamReadyToPlayCommand
|
||||||
import eventDemo.app.entity.Player
|
import eventDemo.app.entity.Player
|
||||||
@@ -42,13 +43,9 @@ class GameCommandHandler(
|
|||||||
val gameState = command.buildGameState()
|
val gameState = command.buildGameState()
|
||||||
|
|
||||||
when (command) {
|
when (command) {
|
||||||
is IWantToPlayCardCommand -> {
|
is IWantToPlayCardCommand -> command.run(gameState, playerNotifier, eventStream)
|
||||||
command.run(gameState, playerNotifier, eventStream)
|
is IamReadyToPlayCommand -> command.run(gameState, playerNotifier, eventStream)
|
||||||
}
|
is IWantToJoinTheGameCommand -> command.run(gameState, playerNotifier, eventStream)
|
||||||
|
|
||||||
is IamReadyToPlayCommand -> {
|
|
||||||
command.run(gameState, playerNotifier, eventStream)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package eventDemo.app.command.command
|
||||||
|
|
||||||
|
import eventDemo.app.GameState
|
||||||
|
import eventDemo.app.entity.GameId
|
||||||
|
import eventDemo.app.entity.Player
|
||||||
|
import eventDemo.app.event.GameEventStream
|
||||||
|
import eventDemo.app.event.event.NewPlayerEvent
|
||||||
|
import eventDemo.libs.command.CommandId
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A command to perform an action to play a new card
|
||||||
|
*/
|
||||||
|
@Serializable
|
||||||
|
@SerialName("JoinGame")
|
||||||
|
data class IWantToJoinTheGameCommand(
|
||||||
|
override val payload: Payload,
|
||||||
|
) : GameCommand {
|
||||||
|
override val id: CommandId = CommandId()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Payload(
|
||||||
|
override val gameId: GameId,
|
||||||
|
override val player: Player,
|
||||||
|
) : GameCommand.Payload
|
||||||
|
|
||||||
|
fun run(
|
||||||
|
state: GameState,
|
||||||
|
playerNotifier: (String) -> Unit,
|
||||||
|
eventStream: GameEventStream,
|
||||||
|
) {
|
||||||
|
if (!state.isStarted) {
|
||||||
|
eventStream.publish(
|
||||||
|
NewPlayerEvent(
|
||||||
|
payload.gameId,
|
||||||
|
payload.player,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
playerNotifier("The game is already started")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user