diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-29 18:02:47 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-29 18:07:31 +0200 |
commit | 57a6e5135ee554079c6064bd770d51545800969d (patch) | |
tree | 9c688d1fc261cf6740c63a604695f9301220830f /sw-common-model | |
parent | Ensure error saga starts first (diff) | |
download | seven-wonders-57a6e5135ee554079c6064bd770d51545800969d.tar.gz seven-wonders-57a6e5135ee554079c6064bd770d51545800969d.tar.bz2 seven-wonders-57a6e5135ee554079c6064bd770d51545800969d.zip |
Fix duplicated inconsistent state
Diffstat (limited to 'sw-common-model')
-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 ) |