move login from state to Command
This commit is contained in:
@@ -41,73 +41,6 @@ data class GameState(
|
|||||||
return players.size == readyPlayers.size && players.all { readyPlayers.contains(it) }
|
return players.size == readyPlayers.size && players.all { readyPlayers.contains(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun canBePlayThisCard(
|
|
||||||
player: Player,
|
|
||||||
card: Card,
|
|
||||||
): Boolean {
|
|
||||||
if (!isReady) return false
|
|
||||||
val cardOnGame = lastCard?.card ?: return false
|
|
||||||
|
|
||||||
return when (cardOnGame) {
|
|
||||||
is Card.NumericCard -> {
|
|
||||||
when (card) {
|
|
||||||
is Card.AllColorCard -> true
|
|
||||||
is Card.NumericCard -> card.number == cardOnGame.number || card.color == cardOnGame.color
|
|
||||||
is Card.ColorCard -> card.color == cardOnGame.color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is Card.ReverseCard -> {
|
|
||||||
when (card) {
|
|
||||||
is Card.ReverseCard -> true
|
|
||||||
is Card.AllColorCard -> true
|
|
||||||
is Card.ColorCard -> card.color == cardOnGame.color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is Card.PassCard -> {
|
|
||||||
if (player.cardOnBoardIsForYou) {
|
|
||||||
false
|
|
||||||
} else {
|
|
||||||
when (card) {
|
|
||||||
is Card.AllColorCard -> true
|
|
||||||
is Card.ColorCard -> card.color == cardOnGame.color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is Card.ChangeColorCard -> {
|
|
||||||
when (card) {
|
|
||||||
is Card.AllColorCard -> true
|
|
||||||
is Card.ColorCard -> card.color == lastColor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is Card.Plus2Card -> {
|
|
||||||
if (player.cardOnBoardIsForYou && card is Card.Plus2Card) {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
when (card) {
|
|
||||||
is Card.AllColorCard -> true
|
|
||||||
is Card.Plus2Card -> true
|
|
||||||
is Card.ColorCard -> card.color == cardOnGame.color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is Card.Plus4Card -> {
|
|
||||||
if (player.cardOnBoardIsForYou && card is Card.Plus4Card) {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
when (card) {
|
|
||||||
is Card.AllColorCard -> true
|
|
||||||
is Card.ColorCard -> card.color == lastColor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private val lastPlayerIndex: Int? get() {
|
private val lastPlayerIndex: Int? get() {
|
||||||
val i = players.indexOf(lastPlayer)
|
val i = players.indexOf(lastPlayer)
|
||||||
return if (i == -1) {
|
return if (i == -1) {
|
||||||
@@ -130,9 +63,9 @@ data class GameState(
|
|||||||
|
|
||||||
val nextPlayer: Player = players.elementAt(nextPlayerIndex)
|
val nextPlayer: Player = players.elementAt(nextPlayerIndex)
|
||||||
|
|
||||||
private val Player.currentIndex: Int get() = players.indexOf(this)
|
val Player.currentIndex: Int get() = players.indexOf(this)
|
||||||
|
|
||||||
private fun Player.playerDiffIndex(nextPlayer: Player): Int =
|
fun Player.playerDiffIndex(nextPlayer: Player): Int =
|
||||||
if (direction == Direction.CLOCKWISE) {
|
if (direction == Direction.CLOCKWISE) {
|
||||||
nextPlayer.currentIndex + this.currentIndex
|
nextPlayer.currentIndex + this.currentIndex
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import kotlinx.serialization.Serializable
|
|||||||
data class IWantToPlayCardCommand(
|
data class IWantToPlayCardCommand(
|
||||||
override val payload: Payload,
|
override val payload: Payload,
|
||||||
) : GameCommand {
|
) : GameCommand {
|
||||||
override val name: String = "PlayCard"
|
|
||||||
override val id: CommandId = CommandId()
|
override val id: CommandId = CommandId()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@@ -33,13 +32,7 @@ data class IWantToPlayCardCommand(
|
|||||||
playerNotifier: (String) -> Unit,
|
playerNotifier: (String) -> Unit,
|
||||||
eventStream: GameEventStream,
|
eventStream: GameEventStream,
|
||||||
) {
|
) {
|
||||||
val commandCardCanBeExecuted: Boolean =
|
if (state.canBePlayThisCard()) {
|
||||||
state.canBePlayThisCard(
|
|
||||||
payload.player,
|
|
||||||
payload.card,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (commandCardCanBeExecuted) {
|
|
||||||
eventStream.publish(
|
eventStream.publish(
|
||||||
CardIsPlayedEvent(
|
CardIsPlayedEvent(
|
||||||
payload.gameId,
|
payload.gameId,
|
||||||
@@ -48,7 +41,70 @@ data class IWantToPlayCardCommand(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
playerNotifier("Command cannot be executed")
|
playerNotifier("You cannot play this card")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun GameState.canBePlayThisCard(): Boolean {
|
||||||
|
if (!isReady) return false
|
||||||
|
val cardOnBoard = lastCard?.card ?: return false
|
||||||
|
return when (cardOnBoard) {
|
||||||
|
is Card.NumericCard -> {
|
||||||
|
when (payload.card) {
|
||||||
|
is Card.AllColorCard -> true
|
||||||
|
is Card.NumericCard -> payload.card.number == cardOnBoard.number || payload.card.color == cardOnBoard.color
|
||||||
|
is Card.ColorCard -> payload.card.color == cardOnBoard.color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is Card.ReverseCard -> {
|
||||||
|
when (payload.card) {
|
||||||
|
is Card.ReverseCard -> true
|
||||||
|
is Card.AllColorCard -> true
|
||||||
|
is Card.ColorCard -> payload.card.color == cardOnBoard.color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is Card.PassCard -> {
|
||||||
|
if (payload.player.cardOnBoardIsForYou) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
when (payload.card) {
|
||||||
|
is Card.AllColorCard -> true
|
||||||
|
is Card.ColorCard -> payload.card.color == cardOnBoard.color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is Card.ChangeColorCard -> {
|
||||||
|
when (payload.card) {
|
||||||
|
is Card.AllColorCard -> true
|
||||||
|
is Card.ColorCard -> payload.card.color == lastColor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is Card.Plus2Card -> {
|
||||||
|
if (payload.player.cardOnBoardIsForYou && payload.card is Card.Plus2Card) {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
when (payload.card) {
|
||||||
|
is Card.AllColorCard -> true
|
||||||
|
is Card.Plus2Card -> true
|
||||||
|
is Card.ColorCard -> payload.card.color == cardOnBoard.color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is Card.Plus4Card -> {
|
||||||
|
if (payload.player.cardOnBoardIsForYou && payload.card is Card.Plus4Card) {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
when (payload.card) {
|
||||||
|
is Card.AllColorCard -> true
|
||||||
|
is Card.ColorCard -> payload.card.color == lastColor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import kotlinx.serialization.Serializable
|
|||||||
data class IamReadyToPlayCommand(
|
data class IamReadyToPlayCommand(
|
||||||
override val payload: Payload,
|
override val payload: Payload,
|
||||||
) : GameCommand {
|
) : GameCommand {
|
||||||
override val name: String = "Ready"
|
|
||||||
override val id: CommandId = CommandId()
|
override val id: CommandId = CommandId()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
|
|||||||
@@ -24,5 +24,4 @@ value class CommandId(
|
|||||||
*/
|
*/
|
||||||
interface Command {
|
interface Command {
|
||||||
val id: CommandId
|
val id: CommandId
|
||||||
val name: String
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ typealias CommandBlock<C> = CommandStream.ComputeStatus.(C) -> Unit
|
|||||||
abstract class CommandStreamInMemory<C : Command> : CommandStream<C> {
|
abstract class CommandStreamInMemory<C : Command> : CommandStream<C> {
|
||||||
private val logger = KotlinLogging.logger {}
|
private val logger = KotlinLogging.logger {}
|
||||||
private val failedCommand = mutableListOf<Command>()
|
private val failedCommand = mutableListOf<Command>()
|
||||||
private val queue: Channel<C> = Channel(onUndeliveredElement = { logger.atWarn { "${it.name} elem not send" } })
|
private val queue: Channel<C> =
|
||||||
|
Channel(onUndeliveredElement = {
|
||||||
|
logger.atWarn { "${it::class.simpleName} command not send" }
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a new [Command] to the queue.
|
* Send a new [Command] to the queue.
|
||||||
|
|||||||
Reference in New Issue
Block a user