chore: upgrade versions and rename folders
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package eventDemo.domain.command.action
|
||||
|
||||
import eventDemo.libs.command.Command
|
||||
import eventDemo.libs.event.Event
|
||||
|
||||
sealed interface CommandAction<C : Command, E : Event<*>> {
|
||||
fun run(command: C): (version: Int) -> E
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package eventDemo.domain.command.action
|
||||
|
||||
import eventDemo.domain.command.CommandException
|
||||
import eventDemo.domain.command.command.ICantPlayCommand
|
||||
import eventDemo.domain.event.event.PlayerHavePassEvent
|
||||
import eventDemo.domain.event.projection.GameStateRepository
|
||||
|
||||
/**
|
||||
* A command to perform an action to play a new card
|
||||
*/
|
||||
data class ICantPlay(
|
||||
private val gameStateRepository: GameStateRepository,
|
||||
) : CommandAction<ICantPlayCommand, PlayerHavePassEvent> {
|
||||
override fun run(command: ICantPlayCommand): (version: Int) -> PlayerHavePassEvent {
|
||||
val state = gameStateRepository.get(command.payload.aggregateId)
|
||||
|
||||
if (state.currentPlayerTurn != command.payload.player) {
|
||||
throw CommandException("Its not your turn!")
|
||||
}
|
||||
|
||||
val playableCards = state.playableCards(command.payload.player)
|
||||
if (playableCards.isNotEmpty()) {
|
||||
throw CommandException("You can and must play one card, like ${playableCards.first()::class.simpleName}")
|
||||
}
|
||||
|
||||
val takenCard = state.deck.stack.first()
|
||||
return { version ->
|
||||
PlayerHavePassEvent(
|
||||
aggregateId = command.payload.aggregateId,
|
||||
player = command.payload.player,
|
||||
takenCard = takenCard,
|
||||
version = version,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package eventDemo.domain.command.action
|
||||
|
||||
import eventDemo.domain.command.CommandException
|
||||
import eventDemo.domain.command.command.IWantToJoinTheGameCommand
|
||||
import eventDemo.domain.event.event.NewPlayerEvent
|
||||
import eventDemo.domain.event.projection.GameStateRepository
|
||||
|
||||
/**
|
||||
* A command to perform an action to play a new card
|
||||
*/
|
||||
data class IWantToJoinTheGame(
|
||||
private val gameStateRepository: GameStateRepository,
|
||||
) : CommandAction<IWantToJoinTheGameCommand, NewPlayerEvent> {
|
||||
override fun run(command: IWantToJoinTheGameCommand): (version: Int) -> NewPlayerEvent {
|
||||
val state = gameStateRepository.get(command.payload.aggregateId)
|
||||
if (!state.isStarted) {
|
||||
return {
|
||||
NewPlayerEvent(
|
||||
aggregateId = command.payload.aggregateId,
|
||||
player = command.payload.player,
|
||||
version = it,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
throw CommandException("The game is already started")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package eventDemo.domain.command.action
|
||||
|
||||
import eventDemo.domain.command.CommandException
|
||||
import eventDemo.domain.command.command.IWantToPlayCardCommand
|
||||
import eventDemo.domain.event.event.CardIsPlayedEvent
|
||||
import eventDemo.domain.event.projection.GameStateRepository
|
||||
|
||||
/**
|
||||
* A command to perform an action to play a new card
|
||||
*/
|
||||
data class IWantToPlayCard(
|
||||
private val gameStateRepository: GameStateRepository,
|
||||
) : CommandAction<IWantToPlayCardCommand, CardIsPlayedEvent> {
|
||||
override fun run(command: IWantToPlayCardCommand): (version: Int) -> CardIsPlayedEvent {
|
||||
val state = gameStateRepository.get(command.payload.aggregateId)
|
||||
|
||||
if (!state.isStarted) {
|
||||
throw CommandException("The game is Not started")
|
||||
}
|
||||
if (state.currentPlayerTurn != command.payload.player) {
|
||||
throw CommandException("Its not your turn!")
|
||||
}
|
||||
if (!state.canBePlayThisCard(command.payload.player, command.payload.card)) {
|
||||
throw CommandException("You cannot play this card")
|
||||
}
|
||||
|
||||
return { version ->
|
||||
CardIsPlayedEvent(
|
||||
aggregateId = command.payload.aggregateId,
|
||||
card = command.payload.card,
|
||||
player = command.payload.player,
|
||||
version = version,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package eventDemo.domain.command.action
|
||||
|
||||
import eventDemo.domain.command.CommandException
|
||||
import eventDemo.domain.command.command.IamReadyToPlayCommand
|
||||
import eventDemo.domain.event.event.PlayerReadyEvent
|
||||
import eventDemo.domain.event.projection.GameStateRepository
|
||||
|
||||
/**
|
||||
* A command to set as ready to play
|
||||
*/
|
||||
class IamReadyToPlay(
|
||||
private val gameStateRepository: GameStateRepository,
|
||||
) : CommandAction<IamReadyToPlayCommand, PlayerReadyEvent> {
|
||||
@Throws(CommandException::class)
|
||||
override fun run(command: IamReadyToPlayCommand): (version: Int) -> PlayerReadyEvent {
|
||||
val state = gameStateRepository.get(command.payload.aggregateId)
|
||||
val playerExist: Boolean = state.players.contains(command.payload.player)
|
||||
val playerIsAlreadyReady: Boolean = state.readyPlayers.contains(command.payload.player)
|
||||
|
||||
if (state.isStarted) {
|
||||
throw CommandException("The game is already started")
|
||||
} else if (!playerExist) {
|
||||
throw CommandException("You are not in the game")
|
||||
} else if (playerIsAlreadyReady) {
|
||||
throw CommandException("You are already ready")
|
||||
} else {
|
||||
return { version: Int ->
|
||||
PlayerReadyEvent(
|
||||
aggregateId = command.payload.aggregateId,
|
||||
player = command.payload.player,
|
||||
version = version,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user