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
.getLast(body.game.id)
.cardOnCurrentStack
?.card
?.let { call.respond(it) }
?: call.response.status(HttpStatusCode.BadRequest)
}

View File

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

View File

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

View File

@@ -43,7 +43,7 @@ import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds
@DelicateCoroutinesApi
class GameStateTest :
class GameSimulationTest :
FunSpec({
test("Simulation of a game") {
withTimeout(2.seconds) {
@@ -199,7 +199,8 @@ class GameStateTest :
state.players shouldBeEqual setOf(player1, player2)
state.readyPlayers shouldBeEqual setOf(player1, player2)
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)
}
}
}