summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt9
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/CustomizableSettings.kt47
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Settings.kt18
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt (renamed from sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Api.kt)26
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt28
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt21
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/cards/Cards.kt2
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/wonders/Wonders.kt31
-rw-r--r--sw-common-model/src/commonTest/kotlin/org/luxons/sevenwonders/model/WonderSidePickMethodTest.kt91
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Settings.kt29
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt2
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt30
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt9
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt2
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt2
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt2
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt2
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt2
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt2
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt2
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt17
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt2
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt6
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt10
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt19
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt7
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt14
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt16
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt45
-rw-r--r--sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt9
-rw-r--r--sw-server/src/test/kotlin/org/luxons/sevenwonders/server/lobby/LobbyTest.kt4
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt4
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt2
33 files changed, 235 insertions, 277 deletions
diff --git a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt
index e904986a..8adbfa07 100644
--- a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt
+++ b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt
@@ -14,9 +14,10 @@ import org.hildan.krossbow.stomp.conversions.kxserialization.convertAndSend
import org.hildan.krossbow.stomp.conversions.kxserialization.subscribe
import org.hildan.krossbow.stomp.conversions.kxserialization.withJsonConversions
import org.hildan.krossbow.stomp.sendEmptyMsg
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
import org.luxons.sevenwonders.model.PlayerTurnInfo
+import org.luxons.sevenwonders.model.wonders.AssignedWonder
import org.luxons.sevenwonders.model.api.ConnectedPlayer
import org.luxons.sevenwonders.model.api.LobbyDTO
import org.luxons.sevenwonders.model.api.SEVEN_WONDERS_WS_ENDPOINT
@@ -98,7 +99,11 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat
stompSession.convertAndSend("/app/lobby/reorderPlayers", ReorderPlayersAction(players), ReorderPlayersAction.serializer())
}
- suspend fun updateSettings(settings: CustomizableSettings) {
+ suspend fun reassignWonders(wonders: List<AssignedWonder>) {
+ stompSession.convertAndSend("/app/lobby/reassignWonders", ReassignWondersAction(wonders), ReassignWondersAction.serializer())
+ }
+
+ suspend fun updateSettings(settings: Settings) {
stompSession.convertAndSend("/app/lobby/updateSettings", UpdateSettingsAction(settings), UpdateSettingsAction.serializer())
}
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/CustomizableSettings.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/CustomizableSettings.kt
deleted file mode 100644
index 86a11bc1..00000000
--- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/CustomizableSettings.kt
+++ /dev/null
@@ -1,47 +0,0 @@
-package org.luxons.sevenwonders.model
-
-import kotlinx.serialization.Serializable
-import kotlin.random.Random
-
-@Serializable
-data class CustomizableSettings(
- val randomSeedForTests: Long? = null,
- val timeLimitInSeconds: Int = 45,
- val wonderSidePickMethod: WonderSidePickMethod = WonderSidePickMethod.EACH_RANDOM,
- val initialGold: Int = 3,
- val discardedCardGold: Int = 3,
- val defaultTradingCost: Int = 2,
- val pointsPer3Gold: Int = 1,
- val lostPointsPerDefeat: Int = 1,
- val wonPointsPerVictoryPerAge: Map<Int, Int> = mapOf(1 to 1, 2 to 3, 3 to 5)
-)
-
-enum class WonderSide {
- A,
- B
-}
-
-enum class WonderSidePickMethod {
- ALL_A {
- override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide {
- return WonderSide.A
- }
- },
- ALL_B {
- override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide {
- return WonderSide.B
- }
- },
- EACH_RANDOM {
- override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide {
- return if (random.nextBoolean()) WonderSide.A else WonderSide.B
- }
- },
- SAME_RANDOM_FOR_ALL {
- override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide {
- return lastPickedSide ?: if (random.nextBoolean()) WonderSide.A else WonderSide.B
- }
- };
-
- abstract fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide
-}
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Settings.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Settings.kt
new file mode 100644
index 00000000..21be1c1b
--- /dev/null
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Settings.kt
@@ -0,0 +1,18 @@
+package org.luxons.sevenwonders.model
+
+import kotlinx.serialization.Serializable
+import kotlin.random.Random
+
+@Serializable
+data class Settings(
+ val randomSeedForTests: Long? = null,
+ val timeLimitInSeconds: Int = 45,
+ val initialGold: Int = 3,
+ val discardedCardGold: Int = 3,
+ val defaultTradingCost: Int = 2,
+ val pointsPer3Gold: Int = 1,
+ val lostPointsPerDefeat: Int = 1,
+ val wonPointsPerVictoryPerAge: Map<Int, Int> = mapOf(1 to 1, 2 to 3, 3 to 5)
+) {
+ val random: Random by lazy { randomSeedForTests?.let { Random(it) } ?: Random }
+}
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Api.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt
index d22a350a..1438600a 100644
--- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Api.kt
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt
@@ -1,7 +1,7 @@
package org.luxons.sevenwonders.model.api
import kotlinx.serialization.Serializable
-import org.luxons.sevenwonders.model.api.actions.Icon
+import org.luxons.sevenwonders.model.wonders.PreGameWonder
const val SEVEN_WONDERS_WS_ENDPOINT = "/seven-wonders-websocket"
@@ -15,6 +15,7 @@ data class LobbyDTO(
val name: String,
val owner: String,
val players: List<PlayerDTO>,
+ val allWonders: List<PreGameWonder>,
val state: State,
val hasEnoughPlayers: Boolean,
val maxPlayersReached: Boolean
@@ -43,26 +44,3 @@ data class Actionability(
val canDo: Boolean,
val tooltip: String
)
-
-interface BasicPlayerInfo {
- val username: String
- val displayName: String
- val icon: Icon?
-}
-
-@Serializable
-data class ConnectedPlayer(
- override val username: String,
- override val displayName: String,
- override val icon: Icon?
-) : BasicPlayerInfo
-
-@Serializable
-data class PlayerDTO(
- override val username: String,
- override val displayName: String,
- override val icon: Icon?,
- val index: Int,
- val isGameOwner: Boolean,
- val isReady: Boolean
-) : BasicPlayerInfo
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt
new file mode 100644
index 00000000..7161b53d
--- /dev/null
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt
@@ -0,0 +1,28 @@
+package org.luxons.sevenwonders.model.api
+
+import kotlinx.serialization.Serializable
+import org.luxons.sevenwonders.model.api.actions.Icon
+import org.luxons.sevenwonders.model.wonders.AssignedWonder
+
+interface BasicPlayerInfo {
+ val username: String
+ val displayName: String
+ val icon: Icon?
+}
+
+@Serializable
+data class ConnectedPlayer(
+ override val username: String,
+ override val displayName: String,
+ override val icon: Icon?
+) : BasicPlayerInfo
+
+@Serializable
+data class PlayerDTO(
+ override val username: String,
+ override val displayName: String,
+ override val icon: Icon?,
+ val wonder: AssignedWonder,
+ val isGameOwner: Boolean,
+ val isReady: Boolean
+) : BasicPlayerInfo
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt
index 0687e968..4a721a94 100644
--- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt
@@ -1,7 +1,8 @@
package org.luxons.sevenwonders.model.api.actions
import kotlinx.serialization.Serializable
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.wonders.AssignedWonder
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
/**
@@ -58,8 +59,8 @@ class PrepareMoveAction(
)
/**
- * The action to update the order of the players around the table. Can only be called in the lobby by the owner of the
- * game.
+ * The action to update the order of the players around the table.
+ * Can only be called in the lobby by the owner of the game.
*/
@Serializable
class ReorderPlayersAction(
@@ -70,6 +71,18 @@ class ReorderPlayersAction(
)
/**
+ * The action to update the wonders assigned to each player.
+ * Can only be called in the lobby by the owner of the game.
+ */
+@Serializable
+class ReassignWondersAction(
+ /**
+ * The list of wonders assigned to each player, in the players' order.
+ */
+ val assignedWonders: List<AssignedWonder>
+)
+
+/**
* The action to update the settings of the game. Can only be called in the lobby by the owner of the game.
*/
@Serializable
@@ -77,7 +90,7 @@ class UpdateSettingsAction(
/**
* The new values for the settings.
*/
- val settings: CustomizableSettings
+ val settings: Settings
)
/**
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/cards/Cards.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/cards/Cards.kt
index 96027612..b0e73ed5 100644
--- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/cards/Cards.kt
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/cards/Cards.kt
@@ -46,7 +46,7 @@ data class HandCard(
@Serializable
data class PreparedCard(
- val player: PlayerDTO,
+ val username: String,
val cardBack: CardBack?
)
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/wonders/Wonders.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/wonders/Wonders.kt
index 9e9a5b38..888e1c47 100644
--- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/wonders/Wonders.kt
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/wonders/Wonders.kt
@@ -6,6 +6,37 @@ import org.luxons.sevenwonders.model.cards.CardBack
import org.luxons.sevenwonders.model.cards.PlayabilityLevel
import org.luxons.sevenwonders.model.resources.ResourceTransactions
import org.luxons.sevenwonders.model.resources.ResourceType
+import kotlin.random.Random
+
+typealias WonderName = String
+
+@Serializable
+data class PreGameWonder(
+ val name: WonderName,
+ val images: Map<WonderSide, String>
+)
+
+@Serializable
+data class AssignedWonder(
+ val name: WonderName,
+ val side: WonderSide,
+ val image: String
+)
+
+@Serializable
+enum class WonderSide {
+ A,
+ B
+}
+
+fun List<PreGameWonder>.deal(nbPlayers: Int, random: Random = Random): List<AssignedWonder> =
+ shuffled(random).take(nbPlayers).map { it.withRandomSide(random) }
+
+fun PreGameWonder.withRandomSide(random: Random = Random): AssignedWonder {
+ val side = WonderSide.values().random(random)
+ val sideImage = images.getValue(side)
+ return AssignedWonder(name, side, sideImage)
+}
@Serializable
data class ApiWonder(
diff --git a/sw-common-model/src/commonTest/kotlin/org/luxons/sevenwonders/model/WonderSidePickMethodTest.kt b/sw-common-model/src/commonTest/kotlin/org/luxons/sevenwonders/model/WonderSidePickMethodTest.kt
deleted file mode 100644
index 70a061e3..00000000
--- a/sw-common-model/src/commonTest/kotlin/org/luxons/sevenwonders/model/WonderSidePickMethodTest.kt
+++ /dev/null
@@ -1,91 +0,0 @@
-package org.luxons.sevenwonders.model
-
-import kotlin.random.Random
-import kotlin.test.BeforeTest
-import kotlin.test.Test
-import kotlin.test.assertEquals
-
-class WonderSidePickMethodTest {
-
- private lateinit var random: Random
-
- private lateinit var random2: Random
-
- @BeforeTest
- fun setUp() {
- random = Random(421) // starts with TRUE
- random2 = Random(42) // starts with FALSE
- }
-
- @Test
- fun pick_allA() {
- var side: WonderSide? = null
- repeat(10) {
- side = WonderSidePickMethod.ALL_A.pickSide(random, side)
- assertEquals(WonderSide.A, side)
- }
- }
-
- @Test
- fun pick_allB() {
- var side: WonderSide? = null
- repeat(10) {
- side = WonderSidePickMethod.ALL_B.pickSide(random, side)
- assertEquals(WonderSide.B, side)
- }
- }
-
- @Test
- fun pick_eachRandom() {
- var side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, null)
- assertEquals(WonderSide.A, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side)
- assertEquals(WonderSide.A, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side)
- assertEquals(WonderSide.B, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side)
- assertEquals(WonderSide.A, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side)
- assertEquals(WonderSide.B, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side)
- assertEquals(WonderSide.B, side)
- }
-
- @Test
- fun pick_eachRandom2() {
- var side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, null)
- assertEquals(WonderSide.B, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side)
- assertEquals(WonderSide.B, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side)
- assertEquals(WonderSide.A, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side)
- assertEquals(WonderSide.A, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side)
- assertEquals(WonderSide.B, side)
- side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side)
- assertEquals(WonderSide.B, side)
- }
-
- @Test
- fun pick_allSameRandom_sameAsFirst() {
- repeat(10) {
- val side = WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random,
- WonderSide.A
- )
- assertEquals(WonderSide.A, side)
- }
- repeat(10) {
- val side = WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random,
- WonderSide.B
- )
- assertEquals(WonderSide.B, side)
- }
- }
-
- @Test
- fun pick_allSameRandom_firstIsRandom() {
- assertEquals(WonderSide.A, WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random, null))
- assertEquals(WonderSide.B, WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random2, null))
- }
-}
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Settings.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Settings.kt
deleted file mode 100644
index 7461762a..00000000
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Settings.kt
+++ /dev/null
@@ -1,29 +0,0 @@
-package org.luxons.sevenwonders.engine
-
-import org.luxons.sevenwonders.model.CustomizableSettings
-import org.luxons.sevenwonders.model.WonderSide
-import org.luxons.sevenwonders.model.WonderSidePickMethod
-import kotlin.random.Random
-
-internal class Settings(
- val nbPlayers: Int,
- customSettings: CustomizableSettings = CustomizableSettings()
-) {
- val random: Random = customSettings.randomSeedForTests?.let { Random(it) } ?: Random
- val timeLimitInSeconds: Int = customSettings.timeLimitInSeconds
- val initialGold: Int = customSettings.initialGold
- val discardedCardGold: Int = customSettings.discardedCardGold
- val defaultTradingCost: Int = customSettings.defaultTradingCost
- val pointsPer3Gold: Int = customSettings.pointsPer3Gold
- val lostPointsPerDefeat: Int = customSettings.lostPointsPerDefeat
- val wonPointsPerVictoryPerAge: Map<Int, Int> = customSettings.wonPointsPerVictoryPerAge
-
- private val wonderSidePickMethod: WonderSidePickMethod = customSettings.wonderSidePickMethod
- private var lastPickedSide: WonderSide? = null
-
- fun pickWonderSide(): WonderSide {
- val newSide = wonderSidePickMethod.pickSide(random, lastPickedSide)
- lastPickedSide = newSide
- return newSide
- }
-}
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt
index 3f8f0437..a78d6d4b 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt
@@ -1,7 +1,6 @@
package org.luxons.sevenwonders.engine.boards
import org.luxons.sevenwonders.engine.Player
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
import org.luxons.sevenwonders.engine.effects.SpecialAbility
import org.luxons.sevenwonders.engine.resources.Production
@@ -9,6 +8,7 @@ import org.luxons.sevenwonders.engine.resources.TradingRules
import org.luxons.sevenwonders.engine.resources.mutableResourcesOf
import org.luxons.sevenwonders.engine.wonders.Wonder
import org.luxons.sevenwonders.model.Age
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.cards.Color
import org.luxons.sevenwonders.model.score.PlayerScore
import org.luxons.sevenwonders.model.score.ScoreCategory
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt
index 962b88e1..b33c9d50 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt
@@ -4,7 +4,6 @@ import com.github.salomonbrys.kotson.typeToken
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.luxons.sevenwonders.engine.Game
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.boards.Board
import org.luxons.sevenwonders.engine.data.definitions.DecksDefinition
import org.luxons.sevenwonders.engine.data.definitions.WonderDefinition
@@ -23,7 +22,9 @@ import org.luxons.sevenwonders.engine.effects.ScienceProgress
import org.luxons.sevenwonders.engine.resources.Production
import org.luxons.sevenwonders.engine.resources.Resources
import org.luxons.sevenwonders.model.Age
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.wonders.AssignedWonder
+import org.luxons.sevenwonders.model.Settings
+import org.luxons.sevenwonders.model.wonders.PreGameWonder
import org.luxons.sevenwonders.model.resources.ResourceType
internal const val LAST_AGE: Age = 3
@@ -35,23 +36,30 @@ internal data class GlobalRules(
class GameDefinition internal constructor(
rules: GlobalRules,
- private val wonders: List<WonderDefinition>,
+ wonderDefinitions: List<WonderDefinition>,
private val decksDefinition: DecksDefinition
) {
val minPlayers: Int = rules.minPlayers
val maxPlayers: Int = rules.maxPlayers
- fun initGame(id: Long, customSettings: CustomizableSettings, nbPlayers: Int): Game {
- val settings = Settings(nbPlayers, customSettings)
- val boards = assignBoards(settings, nbPlayers)
- val decks = decksDefinition.prepareDecks(settings.nbPlayers, settings.random)
+ val allWonders: List<PreGameWonder> = wonderDefinitions.map { w ->
+ PreGameWonder(
+ w.name,
+ w.sides.mapValues { (_, def) -> def.image })
+ }
+
+ private val wondersByName = wonderDefinitions.associateBy { it.name }
+
+ fun createGame(id: Long, wonders: Collection<AssignedWonder>, settings: Settings): Game {
+ val nbPlayers = wonders.size
+ val boards = wonders.mapIndexed { index, wonder -> wonder.createBoard(index, settings) }
+ val decks = decksDefinition.prepareDecks(nbPlayers, settings.random)
return Game(id, settings, boards, decks)
}
- private fun assignBoards(settings: Settings, nbPlayers: Int): List<Board> {
- return wonders.shuffled(settings.random)
- .take(nbPlayers)
- .mapIndexed { i, wDef -> Board(wDef.create(settings.pickWonderSide()), i, settings) }
+ private fun AssignedWonder.createBoard(playerIndex: Int, settings: Settings): Board {
+ val wonder = wondersByName[name] ?: error("Unknown wonder '$name'")
+ return Board(wonder.create(side), playerIndex, settings)
}
companion object {
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt
index 4c5c8e09..58964e83 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt
@@ -3,12 +3,13 @@ package org.luxons.sevenwonders.engine.data.definitions
import org.luxons.sevenwonders.engine.cards.Requirements
import org.luxons.sevenwonders.engine.wonders.Wonder
import org.luxons.sevenwonders.engine.wonders.WonderStage
-import org.luxons.sevenwonders.model.WonderSide
+import org.luxons.sevenwonders.model.wonders.WonderName
+import org.luxons.sevenwonders.model.wonders.WonderSide
import org.luxons.sevenwonders.model.resources.ResourceType
internal class WonderDefinition(
- private val name: String,
- private val sides: Map<WonderSide, WonderSideDefinition>
+ val name: WonderName,
+ val sides: Map<WonderSide, WonderSideDefinition>
) {
fun create(wonderSide: WonderSide): Wonder = sides[wonderSide]!!.createWonder(name)
}
@@ -16,7 +17,7 @@ internal class WonderDefinition(
internal class WonderSideDefinition(
private val initialResource: ResourceType,
private val stages: List<WonderStageDefinition>,
- private val image: String
+ val image: String
) {
fun createWonder(name: String): Wonder = Wonder(name, initialResource, stages.map { it.create() }, image)
}
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt
index 942d9db1..aaa26f7e 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt
@@ -1,8 +1,8 @@
package org.luxons.sevenwonders.engine.moves
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
internal class BuildWonderMove(move: PlayerMove, card: Card, player: PlayerContext) :
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt
index 9cb57481..c3ba8e9a 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt
@@ -1,9 +1,9 @@
package org.luxons.sevenwonders.engine.moves
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
import org.luxons.sevenwonders.engine.effects.SpecialAbility
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
import org.luxons.sevenwonders.model.boards.RelativeBoardPosition
import org.luxons.sevenwonders.model.cards.Color
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt
index a8f2aebf..92fb4952 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt
@@ -1,8 +1,8 @@
package org.luxons.sevenwonders.engine.moves
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
internal class DiscardMove(move: PlayerMove, card: Card, player: PlayerContext) :
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt
index 1035c1b8..a1790baa 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt
@@ -1,8 +1,8 @@
package org.luxons.sevenwonders.engine.moves
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.MoveType
import org.luxons.sevenwonders.model.PlayerMove
import org.luxons.sevenwonders.model.resources.ResourceTransactions
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt
index 17b1a48a..250424c7 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt
@@ -1,8 +1,8 @@
package org.luxons.sevenwonders.engine.moves
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
internal class PlayCardMove(move: PlayerMove, card: Card, player: PlayerContext) :
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt
index 310500c7..acaa2c95 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt
@@ -1,8 +1,8 @@
package org.luxons.sevenwonders.engine.moves
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
internal class PlayFreeCardMove(move: PlayerMove, card: Card, playerContext: PlayerContext) :
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt
index 5147ed82..2fc03c92 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt
@@ -1,9 +1,9 @@
package org.luxons.sevenwonders.engine.moves
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.engine.cards.Card
import org.luxons.sevenwonders.engine.effects.SpecialAbility
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
internal class PlayFreeDiscardedCardMove(
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 51a6376a..595ca1e0 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
@@ -1,17 +1,16 @@
package org.luxons.sevenwonders.engine
-import org.luxons.sevenwonders.model.Action
-import org.luxons.sevenwonders.model.PlayedMove
-import org.luxons.sevenwonders.model.PlayerMove
-import org.luxons.sevenwonders.model.PlayerTurnInfo
import org.luxons.sevenwonders.model.cards.HandCard
import org.luxons.sevenwonders.model.cards.TableCard
import org.luxons.sevenwonders.engine.data.GameDefinition
import org.luxons.sevenwonders.engine.data.LAST_AGE
-import org.luxons.sevenwonders.model.MoveType
+import org.luxons.sevenwonders.engine.test.SEED
import org.luxons.sevenwonders.model.resources.ResourceTransactions
import org.luxons.sevenwonders.model.resources.noTransactions
-import org.luxons.sevenwonders.engine.test.testCustomizableSettings
+import org.luxons.sevenwonders.engine.test.testSettings
+import org.luxons.sevenwonders.model.*
+import org.luxons.sevenwonders.model.wonders.deal
+import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
@@ -46,8 +45,10 @@ class GameTest {
}
}
- private fun createGame(nbPlayers: Int): Game =
- GameDefinition.load().initGame(0, testCustomizableSettings(), nbPlayers)
+ private fun createGame(nbPlayers: Int): Game = GameDefinition.load().let {
+ val wonders = it.allWonders.deal(nbPlayers, random = Random(SEED))
+ it.createGame(0, wonders, testSettings())
+ }
private fun playAge(nbPlayers: Int, game: Game, age: Int) {
do {
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt
index 8fd673b2..2b0e5462 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt
@@ -20,7 +20,7 @@ class CardTest {
fun playCardCostingMoney() {
val initialGold = 3
val price = 1
- val settings = testSettings(3, initialGold)
+ val settings = testSettings(initialGold)
val boards = listOf(
Board(Wonder("TestWonder", ResourceType.WOOD, emptyList(), ""), 0, settings),
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt
index b9b7bb3c..d91c3b50 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt
@@ -1,7 +1,8 @@
package org.luxons.sevenwonders.engine.data
import org.junit.Test
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.Settings
+import org.luxons.sevenwonders.model.wonders.deal
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
@@ -14,7 +15,8 @@ class GameDefinitionTest {
assertEquals(3, gameDefinition.minPlayers)
assertEquals(7, gameDefinition.maxPlayers)
- val game = gameDefinition.initGame(0, CustomizableSettings(), 7)
+ val wonders = gameDefinition.allWonders.deal(7)
+ val game = gameDefinition.createGame(0, wonders, Settings())
assertNotNull(game)
}
}
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt
index 764d7890..8ff9aa33 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt
@@ -2,7 +2,6 @@ package org.luxons.sevenwonders.engine.moves
import org.junit.Test
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.model.MoveType
import org.luxons.sevenwonders.engine.boards.Table
import org.luxons.sevenwonders.engine.cards.Card
@@ -11,6 +10,7 @@ import org.luxons.sevenwonders.engine.test.sampleCards
import org.luxons.sevenwonders.engine.test.testCard
import org.luxons.sevenwonders.engine.test.testSettings
import org.luxons.sevenwonders.engine.test.testTable
+import org.luxons.sevenwonders.model.Settings
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.fail
@@ -31,8 +31,8 @@ class BuildWonderMoveTest {
@Test
fun init_failsWhenWonderIsCompletelyBuilt() {
- val settings = testSettings(3)
- val table = testTable(settings)
+ val settings = testSettings()
+ val table = testTable(3, settings)
val hand = sampleCards(7)
fillPlayerWonderLevels(settings, table, hand)
@@ -64,8 +64,8 @@ class BuildWonderMoveTest {
@Test
fun place_increasesWonderLevel() {
- val settings = testSettings(3)
- val table = testTable(settings)
+ val settings = testSettings()
+ val table = testTable(3, settings)
val hand = sampleCards(7)
val cardToUse = hand[0]
val playerContext = PlayerContext(0, table, hand)
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt
index cc97dc54..4db574e2 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt
@@ -2,9 +2,8 @@ package org.luxons.sevenwonders.engine.test
import org.luxons.sevenwonders.engine.Player
import org.luxons.sevenwonders.engine.PlayerContext
-import org.luxons.sevenwonders.engine.Settings
import org.luxons.sevenwonders.model.resources.CountedResource
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
import org.luxons.sevenwonders.engine.boards.Board
import org.luxons.sevenwonders.model.boards.RelativeBoardPosition
@@ -30,18 +29,16 @@ import org.luxons.sevenwonders.engine.resources.resourcesOf
import org.luxons.sevenwonders.engine.wonders.Wonder
import org.luxons.sevenwonders.engine.wonders.WonderStage
-private const val SEED: Long = 42
+internal const val SEED: Long = 42
-internal fun testCustomizableSettings(initialGold: Int = 0): CustomizableSettings =
- CustomizableSettings(randomSeedForTests = SEED).copy(initialGold = initialGold)
+internal fun testSettings(initialGold: Int = 0): Settings = Settings(
+ randomSeedForTests = SEED,
+ initialGold = initialGold
+)
-internal fun testSettings(nbPlayers: Int = 5, initialGold: Int = 0): Settings =
- Settings(nbPlayers, testCustomizableSettings(initialGold))
+internal fun testTable(nbPlayers: Int = 5): Table = testTable(nbPlayers, testSettings())
-internal fun testTable(nbPlayers: Int = 5): Table = testTable(testSettings(nbPlayers))
-
-internal fun testTable(settings: Settings): Table =
- Table(testBoards(settings.nbPlayers, settings))
+internal fun testTable(nbPlayers: Int, settings: Settings): Table = Table(testBoards(nbPlayers, settings))
private fun testBoards(count: Int, settings: Settings): List<Board> = List(count) { testBoard(settings) }
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt
index 6ef13b8e..61bcad89 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt
@@ -2,6 +2,7 @@ package org.luxons.sevenwonders.server.api
import org.luxons.sevenwonders.model.api.LobbyDTO
import org.luxons.sevenwonders.model.api.PlayerDTO
+import org.luxons.sevenwonders.model.wonders.AssignedWonder
import org.luxons.sevenwonders.server.lobby.Lobby
import org.luxons.sevenwonders.server.lobby.Player
@@ -9,10 +10,12 @@ fun Lobby.toDTO(): LobbyDTO = LobbyDTO(
id = id,
name = name,
owner = owner.username,
- players = getPlayers().map { it.toDTO() },
+ players = getPlayers().zip(getAssignedWonders()).map { (p, w) -> p.toDTO(w) },
+ allWonders = allWonders,
state = state,
hasEnoughPlayers = hasEnoughPlayers(),
maxPlayersReached = maxPlayersReached()
)
-fun Player.toDTO() = PlayerDTO(username, displayName, icon, index, isGameOwner, isReady)
+private fun Player.toDTO(wonder: AssignedWonder) =
+ PlayerDTO(username, displayName, icon, wonder, isGameOwner, isReady)
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt
index e4b9c6ab..8b9bbd19 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt
@@ -5,7 +5,6 @@ import org.luxons.sevenwonders.engine.Game
import org.luxons.sevenwonders.model.api.actions.PrepareMoveAction
import org.luxons.sevenwonders.model.cards.PreparedCard
import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness
-import org.luxons.sevenwonders.server.api.toDTO
import org.luxons.sevenwonders.server.lobby.Player
import org.luxons.sevenwonders.server.repositories.PlayerRepository
import org.slf4j.LoggerFactory
@@ -29,8 +28,7 @@ class GameController(
/**
* Notifies the game that the player is ready to receive his hand.
*
- * @param principal
- * the connected user's information
+ * @param principal the connected user's information
*/
@MessageMapping("/game/sayReady")
fun ready(principal: Principal) {
@@ -54,17 +52,15 @@ class GameController(
/**
* Prepares the player's next move. When all players have prepared their moves, all moves are executed.
*
- * @param action
- * the action to prepare the move
- * @param principal
- * the connected user's information
+ * @param action the action to prepare the move
+ * @param principal the connected user's information
*/
@MessageMapping("/game/prepareMove")
fun prepareMove(action: PrepareMoveAction, principal: Principal) {
val player = principal.player
val game = player.game
val preparedCardBack = game.prepareMove(player.index, action.move)
- val preparedCard = PreparedCard(player.toDTO(), preparedCardBack)
+ val preparedCard = PreparedCard(player.username, preparedCardBack)
logger.info("Game {}: player {} prepared move {}", game.id, principal.name, action.move)
sendPreparedCard(game.id, preparedCard)
@@ -85,7 +81,7 @@ class GameController(
val player = principal.player
val game = player.game
game.unprepareMove(player.index)
- val preparedCard = PreparedCard(player.toDTO(), null)
+ val preparedCard = PreparedCard(player.username, null)
logger.info("Game {}: player {} unprepared his move", game.id, principal.name)
sendPreparedCard(game.id, preparedCard)
}
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
index fafcf7c3..1c8cf17e 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt
@@ -5,6 +5,7 @@ import kotlinx.coroutines.launch
import org.hildan.livedoc.core.annotations.Api
import org.luxons.sevenwonders.bot.SevenWondersBot
import org.luxons.sevenwonders.model.api.actions.AddBotAction
+import org.luxons.sevenwonders.model.api.actions.ReassignWondersAction
import org.luxons.sevenwonders.model.api.actions.ReorderPlayersAction
import org.luxons.sevenwonders.model.api.actions.UpdateSettingsAction
import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness
@@ -70,6 +71,21 @@ class LobbyController(
}
/**
+ * Reassigns the wonders in the current lobby. This can only be done by the lobby's owner.
+ *
+ * @param action the action to reassign the wonders
+ * @param principal the connected user's information
+ */
+ @MessageMapping("/lobby/reassignWonders")
+ fun reassignWonders(@Validated action: ReassignWondersAction, principal: Principal) {
+ val lobby = principal.player.ownedLobby
+ lobby.reassignWonders(action.assignedWonders)
+
+ logger.info("Reassigned wonders in game '{}': {}", lobby.name, action.assignedWonders)
+ sendLobbyUpdateToPlayers(lobby)
+ }
+
+ /**
* Updates the game settings. This can only be done by the lobby's owner.
*
* @param action the action to update the settings
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt
index 24bf5066..89d041f0 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt
@@ -2,8 +2,11 @@ package org.luxons.sevenwonders.server.lobby
import org.luxons.sevenwonders.engine.Game
import org.luxons.sevenwonders.engine.data.GameDefinition
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.wonders.AssignedWonder
+import org.luxons.sevenwonders.model.wonders.PreGameWonder
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.api.State
+import org.luxons.sevenwonders.model.wonders.withRandomSide
class Lobby(
val id: Long,
@@ -13,7 +16,11 @@ class Lobby(
) {
private val players: MutableList<Player> = ArrayList(gameDefinition.maxPlayers)
- var settings: CustomizableSettings = CustomizableSettings()
+ val allWonders: List<PreGameWonder> = gameDefinition.allWonders
+
+ private val assignedWonders: MutableList<AssignedWonder> = mutableListOf()
+
+ var settings: Settings = Settings()
var state = State.LOBBY
private set
@@ -24,6 +31,8 @@ class Lobby(
fun getPlayers(): List<Player> = players
+ fun getAssignedWonders(): List<AssignedWonder> = assignedWonders
+
@Synchronized
fun addPlayer(player: Player) {
if (hasStarted()) {
@@ -37,8 +46,15 @@ class Lobby(
}
player.join(this)
players.add(player)
+ assignedWonders.add(pickRandomWonder())
}
+ @Synchronized
+ private fun pickRandomWonder(): AssignedWonder =
+ allWonders.filter { !it.isAssigned() }.random().withRandomSide()
+
+ private fun PreGameWonder.isAssigned() = name in assignedWonders.map { it.name }
+
private fun hasStarted(): Boolean = state != State.LOBBY
fun maxPlayersReached(): Boolean = players.size >= gameDefinition.maxPlayers
@@ -51,7 +67,7 @@ class Lobby(
throw PlayerUnderflowException(gameDefinition.minPlayers)
}
state = State.PLAYING
- val game = gameDefinition.initGame(id, settings, players.size)
+ val game = gameDefinition.createGame(id, assignedWonders, settings)
players.forEachIndexed { index, player -> player.join(game, index) }
return game
}
@@ -67,8 +83,15 @@ class Lobby(
players.sortBy { orderedUsernames.indexOf(it.username) }
}
- private fun find(username: String): Player =
- players.firstOrNull { it.username == username } ?: throw UnknownPlayerException(username)
+ @Synchronized
+ fun reassignWonders(wonders: List<AssignedWonder>) {
+ require(wonders.size == players.size)
+ wonders.forEach {
+ require(it.name in allWonders.map { w -> w.name })
+ }
+ assignedWonders.clear()
+ assignedWonders.addAll(wonders)
+ }
@Synchronized
fun isOwner(username: String?): Boolean = owner.username == username
@@ -78,16 +101,22 @@ class Lobby(
@Synchronized
fun removePlayer(username: String): Player {
- val player = find(username)
- players.remove(player)
+ val playerIndex = find(username)
+ if (playerIndex < 0) {
+ throw UnknownPlayerException(username)
+ }
+ assignedWonders.removeAt(playerIndex)
+ val player = players.removeAt(playerIndex)
player.leave()
- if (player == owner && !players.isEmpty()) {
+ if (player == owner && players.isNotEmpty()) {
owner = players[0]
}
return player
}
+ private fun find(username: String): Int = players.indexOfFirst { it.username == username }
+
fun setEndOfGame() {
state = State.FINISHED
}
diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt
index 9aa1ef0e..2f6e2cdb 100644
--- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt
+++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/LobbyControllerTest.kt
@@ -2,8 +2,7 @@ package org.luxons.sevenwonders.server.controllers
import org.junit.Before
import org.junit.Test
-import org.luxons.sevenwonders.model.CustomizableSettings
-import org.luxons.sevenwonders.model.WonderSidePickMethod.ALL_A
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.api.State
import org.luxons.sevenwonders.model.api.actions.ReorderPlayersAction
import org.luxons.sevenwonders.model.api.actions.UpdateSettingsAction
@@ -142,9 +141,9 @@ class LobbyControllerTest {
addPlayer(lobby, "testuser3")
addPlayer(lobby, "testuser4")
- assertEquals(CustomizableSettings(), lobby.settings)
+ assertEquals(Settings(), lobby.settings)
- val newSettings = CustomizableSettings(12L, 5, ALL_A, 5, 5, 4, 10, 2, HashMap())
+ val newSettings = Settings(12L, 5, 5, 5, 4, 10, 2, HashMap())
val updateSettingsAction = UpdateSettingsAction(newSettings)
val principal = TestPrincipal("testuser")
@@ -161,7 +160,7 @@ class LobbyControllerTest {
addPlayer(lobby, "testuser2")
addPlayer(lobby, "testuser3")
- val updateSettingsAction = UpdateSettingsAction(CustomizableSettings())
+ val updateSettingsAction = UpdateSettingsAction(Settings())
val principal = TestPrincipal("testuser2")
diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/lobby/LobbyTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/lobby/LobbyTest.kt
index 094fe7de..1742dc93 100644
--- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/lobby/LobbyTest.kt
+++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/lobby/LobbyTest.kt
@@ -9,7 +9,7 @@ import org.junit.experimental.theories.Theories
import org.junit.experimental.theories.Theory
import org.junit.runner.RunWith
import org.luxons.sevenwonders.engine.data.GameDefinition
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.api.State
import org.luxons.sevenwonders.server.lobby.Lobby.GameAlreadyStartedException
import org.luxons.sevenwonders.server.lobby.Lobby.PlayerListMismatchException
@@ -240,7 +240,7 @@ class LobbyTest {
@Test
fun setSettings() {
- val settings = CustomizableSettings()
+ val settings = Settings()
lobby.settings = settings
assertSame(settings, lobby.settings)
}
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt
index 6fbaadb9..7badb11c 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt
@@ -1,6 +1,6 @@
package org.luxons.sevenwonders.ui.redux
-import org.luxons.sevenwonders.model.CustomizableSettings
+import org.luxons.sevenwonders.model.Settings
import org.luxons.sevenwonders.model.PlayerMove
import redux.RAction
@@ -14,7 +14,7 @@ data class RequestAddBot(val botDisplayName: String) : RAction
data class RequestReorderPlayers(val orderedPlayers: List<String>) : RAction
-data class RequestUpdateSettings(val settings: CustomizableSettings) : RAction
+data class RequestUpdateSettings(val settings: Settings) : RAction
class RequestStartGame : RAction
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
index 1ff0d0ec..047e948a 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
@@ -71,7 +71,7 @@ private fun gameStateReducer(gameState: GameState?, action: RAction): GameState?
is PreparedMoveEvent -> gameState?.copy(currentPreparedMove = action.move)
is RequestUnprepareMove -> gameState?.copy(currentPreparedMove = null)
is PreparedCardEvent -> gameState?.copy(
- preparedCardsByUsername = gameState.preparedCardsByUsername + (action.card.player.username to action.card.cardBack)
+ preparedCardsByUsername = gameState.preparedCardsByUsername + (action.card.username to action.card.cardBack)
)
is PlayerReadyEvent -> gameState?.copy(players = gameState.players.map { p ->
if (p.username == action.username) p.copy(isReady = true) else p
bgstack15