From 420a4c922101668578792c76493be2cb9eea19f3 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Wed, 1 Jul 2020 22:52:12 +0200 Subject: Add setup sidebar to choose wonder sides Resolves: https://github.com/joffrey-bion/seven-wonders/issues/35 --- .../commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt | 4 ++++ .../kotlin/org/luxons/sevenwonders/model/wonders/Wonders.kt | 8 +++----- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'sw-common-model/src/commonMain') diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt index 1438600a..324482bb 100644 --- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt +++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Lobby.kt @@ -20,6 +20,10 @@ data class LobbyDTO( val hasEnoughPlayers: Boolean, val maxPlayersReached: Boolean ) { + private val wondersByName = allWonders.associateBy { it.name } + + fun findWonder(name: String): PreGameWonder = wondersByName[name] ?: error("Unknown wonder '$name'") + fun joinability(userDisplayName: String): Actionability = when { state != State.LOBBY -> Actionability(false, "Cannot join: the game has already started") maxPlayersReached -> Actionability(false, "Cannot join: the game is full") 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 888e1c47..fcecd010 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 @@ -32,11 +32,9 @@ enum class WonderSide { fun List.deal(nbPlayers: Int, random: Random = Random): List = 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) -} +fun PreGameWonder.withRandomSide(random: Random = Random): AssignedWonder = withSide(WonderSide.values().random(random)) + +fun PreGameWonder.withSide(side: WonderSide): AssignedWonder = AssignedWonder(name, side, images.getValue(side)) @Serializable data class ApiWonder( -- cgit