change cardOnCurrentStack on lastCardPlayer

This commit is contained in:
2025-03-17 18:23:29 +01:00
parent 3434d43fc2
commit 48ac4156bd
5 changed files with 14 additions and 19 deletions

View File

@@ -41,7 +41,6 @@ fun Route.readTheGameState(gameStateRepository: GameStateRepository) {
gameStateRepository gameStateRepository
.getLast(body.game.id) .getLast(body.game.id)
.cardOnCurrentStack .cardOnCurrentStack
?.card
?.let { call.respond(it) } ?.let { call.respond(it) }
?: call.response.status(HttpStatusCode.BadRequest) ?: call.response.status(HttpStatusCode.BadRequest)
} }

View File

@@ -12,7 +12,7 @@ data class GameState(
override val lastEventVersion: Int = 0, override val lastEventVersion: Int = 0,
val players: Set<Player> = emptySet(), val players: Set<Player> = emptySet(),
val currentPlayerTurn: Player? = null, val currentPlayerTurn: Player? = null,
val cardOnCurrentStack: LastCard? = null, val lastCardPlayer: Player? = null,
val colorOnCurrentStack: Card.Color? = null, val colorOnCurrentStack: Card.Color? = null,
val direction: Direction = Direction.CLOCKWISE, val direction: Direction = Direction.CLOCKWISE,
val readyPlayers: Set<Player> = emptySet(), val readyPlayers: Set<Player> = emptySet(),
@@ -20,12 +20,6 @@ data class GameState(
val isStarted: Boolean = false, val isStarted: Boolean = false,
val playerWins: Set<Player> = emptySet(), val playerWins: Set<Player> = emptySet(),
) : Projection<GameId> { ) : Projection<GameId> {
@Serializable
data class LastCard(
val card: Card,
val player: Player,
)
enum class Direction { enum class Direction {
CLOCKWISE, CLOCKWISE,
COUNTER_CLOCKWISE, COUNTER_CLOCKWISE,
@@ -39,6 +33,8 @@ data class GameState(
} }
} }
val cardOnCurrentStack: Card? = deck.discard.lastOrNull()
val isReady: Boolean get() { val isReady: Boolean get() {
return players.size == readyPlayers.size && players.all { readyPlayers.contains(it) } return players.size == readyPlayers.size && players.all { readyPlayers.contains(it) }
} }
@@ -98,8 +94,8 @@ data class GameState(
}.let { it % players.size } }.let { it % players.size }
val Player.cardOnBoardIsForYou: Boolean get() { val Player.cardOnBoardIsForYou: Boolean get() {
if (cardOnCurrentStack == null) error("No card") if (lastCardPlayer == null) error("No card")
return this.playerDiffIndex(cardOnCurrentStack.player) == 1 return this.playerDiffIndex(lastCardPlayer) == 1
} }
fun playableCards(player: Player): List<Card> = fun playableCards(player: Player): List<Card> =
@@ -118,7 +114,7 @@ data class GameState(
player: Player, player: Player,
card: Card, card: Card,
): Boolean { ): Boolean {
val cardOnBoard = cardOnCurrentStack?.card ?: return false val cardOnBoard = cardOnCurrentStack ?: return false
return when (cardOnBoard) { return when (cardOnBoard) {
is Card.NumericCard -> { is Card.NumericCard -> {
when (card) { when (card) {
@@ -159,9 +155,8 @@ data class GameState(
true true
} else { } else {
when (card) { when (card) {
is Card.AllColorCard -> true
is Card.Plus2Card -> true is Card.Plus2Card -> true
is Card.ColorCard -> card.color == cardOnBoard.color else -> false
} }
} }
} }

View File

@@ -53,7 +53,7 @@ fun GameState.apply(event: GameEvent): GameState =
currentPlayerTurn = currentPlayerAfterThePlay, currentPlayerTurn = currentPlayerAfterThePlay,
direction = nextDirectionAfterPlay, direction = nextDirectionAfterPlay,
colorOnCurrentStack = color, colorOnCurrentStack = color,
cardOnCurrentStack = GameState.LastCard(event.card, event.player), lastCardPlayer = event.player,
deck = state.deck.putOneCardFromHand(event.player, event.card), deck = state.deck.putOneCardFromHand(event.player, event.card),
) )
} }
@@ -97,7 +97,7 @@ fun GameState.apply(event: GameEvent): GameState =
is GameStartedEvent -> { is GameStartedEvent -> {
state.copy( state.copy(
colorOnCurrentStack = (event.deck.discard.first() as? Card.ColorCard)?.color ?: state.colorOnCurrentStack, colorOnCurrentStack = (event.deck.discard.first() as? Card.ColorCard)?.color ?: state.colorOnCurrentStack,
cardOnCurrentStack = GameState.LastCard(event.deck.discard.first(), event.firstPlayer), lastCardPlayer = null,
currentPlayerTurn = event.firstPlayer, currentPlayerTurn = event.firstPlayer,
deck = event.deck, deck = event.deck,
isStarted = true, isStarted = true,

View File

@@ -100,7 +100,7 @@ class GameStateBuilderTest :
) )
apply(event).also { state -> apply(event).also { state ->
state.aggregateId shouldBeEqual gameId state.aggregateId shouldBeEqual gameId
assertNotNull(state.cardOnCurrentStack).card shouldBeEqual playedCard assertNotNull(state.cardOnCurrentStack) shouldBeEqual playedCard
assertIs<Card.NumericCard>(playedCard).let { assertIs<Card.NumericCard>(playedCard).let {
it.number shouldBeEqual 0 it.number shouldBeEqual 0
it.color shouldBeEqual Card.Color.Red it.color shouldBeEqual Card.Color.Red
@@ -117,7 +117,7 @@ class GameStateBuilderTest :
) )
apply(event).also { state -> apply(event).also { state ->
state.aggregateId shouldBeEqual gameId state.aggregateId shouldBeEqual gameId
assertNotNull(state.cardOnCurrentStack).card shouldBeEqual playedCard assertNotNull(state.cardOnCurrentStack) shouldBeEqual playedCard
assertIs<Card.NumericCard>(playedCard).let { assertIs<Card.NumericCard>(playedCard).let {
it.number shouldBeEqual 7 it.number shouldBeEqual 7
it.color shouldBeEqual Card.Color.Red it.color shouldBeEqual Card.Color.Red

View File

@@ -43,7 +43,7 @@ import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
@DelicateCoroutinesApi @DelicateCoroutinesApi
class GameStateTest : class GameSimulationTest :
FunSpec({ FunSpec({
test("Simulation of a game") { test("Simulation of a game") {
withTimeout(2.seconds) { withTimeout(2.seconds) {
@@ -199,7 +199,8 @@ class GameStateTest :
state.players shouldBeEqual setOf(player1, player2) state.players shouldBeEqual setOf(player1, player2)
state.readyPlayers shouldBeEqual setOf(player1, player2) state.readyPlayers shouldBeEqual setOf(player1, player2)
state.direction shouldBeEqual GameState.Direction.CLOCKWISE state.direction shouldBeEqual GameState.Direction.CLOCKWISE
assertNotNull(state.cardOnCurrentStack) shouldBeEqual GameState.LastCard(assertNotNull(playedCard2), player2) assertNotNull(state.lastCardPlayer) shouldBeEqual player2
assertNotNull(state.cardOnCurrentStack) shouldBeEqual assertNotNull(playedCard2)
} }
} }
} }