diff options
Diffstat (limited to 'sw-common-model/src/commonMain/kotlin')
-rw-r--r-- | sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Api.kt | 37 |
1 files changed, 33 insertions, 4 deletions
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/Api.kt index b648b72b..946c93c7 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/Api.kt @@ -15,9 +15,33 @@ data class LobbyDTO( val owner: String, val players: List<PlayerDTO>, val state: State, - val joinAction: Actionability, - val startAction: Actionability -) + val hasEnoughPlayers: Boolean, + val maxPlayersReached: Boolean +) { + 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" + ) + playerNameAlreadyUsed(userDisplayName) -> Actionability( + false, + "Cannot join: already a player named '$userDisplayName' in this game" + ) + else -> Actionability(true, "Join game") + } + + fun startability(username: String): Actionability = when { + !hasEnoughPlayers -> Actionability( + false, + "Cannot start the game, more players needed" + ) + owner != username -> Actionability(false, "Cannot start the game: only the owner can") + else -> Actionability(true, "Start game") + } + + private fun playerNameAlreadyUsed(name: String): Boolean = players.any { it.displayName == name } +} @Serializable data class Actionability( @@ -26,11 +50,16 @@ data class Actionability( ) @Serializable +data class ConnectedPlayer( + val username: String, + val displayName: String +) + +@Serializable data class PlayerDTO( val username: String, val displayName: String, val index: Int, val isGameOwner: Boolean, - val isMe: Boolean, val isReady: Boolean ) |