summaryrefslogtreecommitdiff
path: root/sw-engine
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2020-12-13 00:12:54 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2020-12-13 00:12:54 +0100
commit223dc654f0ba558a12c03f0b19ce1dd289258487 (patch)
tree728a90b52af3d4bda51bd7f4073a046a1edcb874 /sw-engine
parentAdd docker tags based on commit SHA (diff)
downloadseven-wonders-223dc654f0ba558a12c03f0b19ce1dd289258487.tar.gz
seven-wonders-223dc654f0ba558a12c03f0b19ce1dd289258487.tar.bz2
seven-wonders-223dc654f0ba558a12c03f0b19ce1dd289258487.zip
Reduce number of TableState creations
Diffstat (limited to 'sw-engine')
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt71
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Hands.kt4
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Cards.kt5
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt4
4 files changed, 38 insertions, 46 deletions
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt
index 78d09b35..b6cb3c90 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt
@@ -5,19 +5,14 @@ import org.luxons.sevenwonders.engine.boards.Table
import org.luxons.sevenwonders.engine.cards.Card
import org.luxons.sevenwonders.engine.cards.Decks
import org.luxons.sevenwonders.engine.cards.Hands
-import org.luxons.sevenwonders.engine.converters.toHandCard
+import org.luxons.sevenwonders.engine.converters.toHandCards
import org.luxons.sevenwonders.engine.converters.toPlayedMove
import org.luxons.sevenwonders.engine.converters.toTableState
import org.luxons.sevenwonders.engine.data.LAST_AGE
import org.luxons.sevenwonders.engine.effects.SpecialAbility
import org.luxons.sevenwonders.engine.moves.Move
import org.luxons.sevenwonders.engine.moves.resolve
-import org.luxons.sevenwonders.model.Action
-import org.luxons.sevenwonders.model.MoveType
-import org.luxons.sevenwonders.model.PlayerMove
-import org.luxons.sevenwonders.model.PlayerTurnInfo
-import org.luxons.sevenwonders.model.Settings
-import org.luxons.sevenwonders.model.TableState
+import org.luxons.sevenwonders.model.*
import org.luxons.sevenwonders.model.cards.CardBack
import org.luxons.sevenwonders.model.cards.HandCard
import org.luxons.sevenwonders.model.score.ScoreBoard
@@ -48,24 +43,36 @@ class Game internal constructor(
}
private fun startNewTurn() {
- currentTurnInfo = players.map {
- val hand = hands.createHand(it)
- val action = determineAction(hand, it.board)
- createPlayerTurnInfo(it, action, hand)
+ val newTableState = table.toTableState()
+ currentTurnInfo = players.map { player ->
+ val hand = hands.createHand(player)
+ val action = determineAction(hand, player.board)
+ createPlayerTurnInfo(player, action, hand, newTableState)
}
}
private fun startPlayDiscardedTurn() {
- currentTurnInfo = players.map {
- val action = if (it.board.hasSpecial(SpecialAbility.PLAY_DISCARDED)) {
- Action.PLAY_FREE_DISCARDED
- } else {
- Action.WAIT
+ val newTableState = table.toTableState()
+ currentTurnInfo = players.map { player ->
+ val action = when {
+ player.board.hasSpecial(SpecialAbility.PLAY_DISCARDED) -> Action.PLAY_FREE_DISCARDED
+ else -> Action.WAIT
}
- createPlayerTurnInfo(it, action, null)
+ createPlayerTurnInfo(player, action, null, newTableState)
}
}
+ private fun createPlayerTurnInfo(player: Player, action: Action, hand: List<HandCard>?, newTableState: TableState) =
+ PlayerTurnInfo(
+ playerIndex = player.index,
+ table = newTableState,
+ action = action,
+ hand = hand,
+ preparedMove = preparedMoves[player.index]?.toPlayedMove(),
+ discardedCards = discardedCards.toHandCards(player, true).takeIf { action == Action.PLAY_FREE_DISCARDED },
+ neighbourGuildCards = table.getNeighbourGuildCards(player.index).toHandCards(player, true),
+ )
+
private fun startEndGameTurn() {
// some player may need to do additional stuff
startNewTurn()
@@ -79,28 +86,10 @@ class Game internal constructor(
}
}
- private fun createPlayerTurnInfo(player: Player, action: Action, hand: List<HandCard>?): PlayerTurnInfo {
- val neighbourGuildCards = table.getNeighbourGuildCards(player.index).map { it.toHandCard(player, true) }
- val exposedDiscardedCards = if (action == Action.PLAY_FREE_DISCARDED) {
- discardedCards.map { it.toHandCard(player, true) }
- } else {
- null
- }
- return PlayerTurnInfo(
- playerIndex = player.index,
- table = table.toTableState(),
- action = action,
- hand = hand,
- preparedMove = preparedMoves[player.index]?.toPlayedMove(),
- discardedCards = exposedDiscardedCards,
- neighbourGuildCards = neighbourGuildCards,
- )
- }
-
/**
* Returns information for each player about the current turn.
*/
- fun getCurrentTurnInfo(): Collection<PlayerTurnInfo> = currentTurnInfo
+ fun getCurrentTurnInfo(): List<PlayerTurnInfo> = currentTurnInfo
private fun determineAction(hand: List<HandCard>, board: Board): Action = when {
endOfGameReached() -> when {
@@ -150,11 +139,12 @@ class Game internal constructor(
}
/**
- * Plays all the [prepared moves][prepareMove] for the current turn. An exception will be thrown if some players
- * had not prepared their moves (unless these players had nothing to do). To avoid this, please check if everyone
- * is ready using [allPlayersPreparedTheirMove].
+ * Plays all the [prepared moves][prepareMove] for the current turn.
+ *
+ * An exception will be thrown if some players had not prepared their moves (unless these players had nothing to
+ * do). To avoid this, please check if everyone is ready using [allPlayersPreparedTheirMove].
*/
- fun playTurn(): TableState {
+ fun playTurn() {
makeMoves()
if (shouldStartPlayDiscardedTurn()) {
startPlayDiscardedTurn()
@@ -169,7 +159,6 @@ class Game internal constructor(
rotateHandsIfRelevant()
startNewTurn()
}
- return table.toTableState()
}
private fun makeMoves() {
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Hands.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Hands.kt
index 9bb1e6f8..a2a7a805 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Hands.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Hands.kt
@@ -1,7 +1,7 @@
package org.luxons.sevenwonders.engine.cards
import org.luxons.sevenwonders.engine.Player
-import org.luxons.sevenwonders.engine.converters.toHandCard
+import org.luxons.sevenwonders.engine.converters.toHandCards
import org.luxons.sevenwonders.model.cards.HandCard
import org.luxons.sevenwonders.model.cards.HandRotationDirection
@@ -25,7 +25,7 @@ internal class Hands(private val hands: List<List<Card>>) {
return Hands(mutatedHands)
}
- fun createHand(player: Player): List<HandCard> = hands[player.index].map { c -> c.toHandCard(player, false) }
+ fun createHand(player: Player): List<HandCard> = hands[player.index].toHandCards(player, false)
fun rotate(direction: HandRotationDirection): Hands {
val newHands = when (direction) {
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Cards.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Cards.kt
index 7c9c5d32..40c2c2b2 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Cards.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Cards.kt
@@ -17,7 +17,10 @@ internal fun Card.toTableCard(lastMove: Move? = null): TableCard = TableCard(
playedDuringLastMove = lastMove != null && this.name == lastMove.card.name,
)
-internal fun Card.toHandCard(player: Player, forceSpecialFree: Boolean): HandCard = HandCard(
+internal fun List<Card>.toHandCards(player: Player, forceSpecialFree: Boolean) =
+ map { it.toHandCard(player, forceSpecialFree) }
+
+private fun Card.toHandCard(player: Player, forceSpecialFree: Boolean): HandCard = HandCard(
name = name,
color = color,
requirements = requirements.toApiRequirements(),
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt
index 62267c7a..a2e75a5f 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt
@@ -65,10 +65,10 @@ class GameTest {
moveExpectations.forEach { game.prepareMove(it.playerIndex, it.moveToSend) }
assertTrue(game.allPlayersPreparedTheirMove())
- val table = game.playTurn()
+ game.playTurn()
val expectedMoves = moveExpectations.map { it.expectedPlayedMove }
- assertEquals(expectedMoves, table.lastPlayedMoves)
+ assertEquals(expectedMoves, game.getCurrentTurnInfo()[0].table.lastPlayedMoves)
}
private fun PlayerTurnInfo.firstAvailableMove(): MoveExpectation? = when (action) {
bgstack15