From d3f6896ea262818f7ad722a01e4a6d24d2f83aff Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Fri, 18 Dec 2020 17:02:33 +0100 Subject: [PATCH] feat(controller): provide roomId on game creation and game over --- src/main/kotlin/sc/gui/LobbyManager.kt | 21 ++++++++++++------- .../sc/gui/controller/ClientController.kt | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/sc/gui/LobbyManager.kt b/src/main/kotlin/sc/gui/LobbyManager.kt index 265ad6a2..d4bca655 100644 --- a/src/main/kotlin/sc/gui/LobbyManager.kt +++ b/src/main/kotlin/sc/gui/LobbyManager.kt @@ -17,9 +17,11 @@ import sc.server.Configuration import sc.shared.GameResult import sc.shared.SlotDescriptor import java.net.ConnectException +import java.util.concurrent.CompletableFuture +import java.util.concurrent.Future import kotlin.system.exitProcess -data class GameStartException(val error: ProtocolErrorMessage) : Exception("Failed to start game: ${error.message}") +data class GameStartException(val error: ProtocolErrorMessage): Exception("Failed to start game: ${error.message}") class LobbyManager(host: String, port: Int) { var game: IControllableGame? = null @@ -40,10 +42,12 @@ class LobbyManager(host: String, port: Int) { lobby.addListener(lobbyListener) } - fun startNewGame(players: Collection, prepared: Boolean, paused: Boolean, listener: IUpdateListener, onGameStarted: (error: Throwable?) -> Unit, onGameOver: (result: GameResult) -> Unit) { + fun startNewGame(players: Collection, prepared: Boolean, paused: Boolean, listener: IUpdateListener, onGameStarted: (error: Throwable?) -> Unit, onGameOver: (roomId: String, result: GameResult) -> Unit): Future { logger.debug("Starting new game (prepared: {}, paused: {}, players: {})", prepared, paused, players) - this.lobbyListener.setGameOverHandler(onGameOver) + val result = CompletableFuture() val observeRoom = { roomId: String -> + result.complete(roomId) + this.lobbyListener.setGameOverHandler(roomId) { result -> onGameOver(roomId, result) } game = lobby.observeAndControl(roomId, paused).apply { addListener(listener) } } @@ -84,6 +88,7 @@ class LobbyManager(host: String, port: Int) { } join() } + return result } companion object { @@ -91,9 +96,11 @@ class LobbyManager(host: String, port: Int) { } } +typealias GameOverHandler = (GameResult) -> Unit + class LobbyListener(val logger: Logger): ILobbyClientListener { - private var gameOverHandler: (result: GameResult) -> Unit = {} + private val gameOverHandlers = HashMap() private val roomsJoined = HashMap() private val waiters: MutableMap Unit>> = HashMap() @@ -142,7 +149,7 @@ class LobbyListener(val logger: Logger): ILobbyClientListener { override fun onGameOver(roomId: String, data: GameResult) { logger.debug("lobby: $roomId game is over") - gameOverHandler(data) + gameOverHandlers[roomId]?.invoke(data) } override fun onGamePaused(roomId: String, nextPlayer: Player) { @@ -153,8 +160,8 @@ class LobbyListener(val logger: Logger): ILobbyClientListener { logger.debug("lobby: $roomId game was observed") } - fun setGameOverHandler(handler: (result: GameResult) -> Unit) { - this.gameOverHandler = handler + fun setGameOverHandler(roomId: String, handler: GameOverHandler) { + gameOverHandlers[roomId] = handler } } diff --git a/src/main/kotlin/sc/gui/controller/ClientController.kt b/src/main/kotlin/sc/gui/controller/ClientController.kt index bb3fadd1..97d96d93 100644 --- a/src/main/kotlin/sc/gui/controller/ClientController.kt +++ b/src/main/kotlin/sc/gui/controller/ClientController.kt @@ -128,7 +128,7 @@ class ClientController: Controller() { appController.changeViewTo(ViewType.GAME) } } - }, { result -> + }, { room, result -> fire(GameOverEvent(result)) }) }