From 37b8735f7db8e6743af83c6df88efd6f1ae372b0 Mon Sep 17 00:00:00 2001 From: Ken Huang <100591800+m1a2st@users.noreply.github.com> Date: Thu, 12 Oct 2023 15:41:25 +0800 Subject: [PATCH] feat start game event broadcast (#181) * feat start game event broadcast --- .../gaas/application/usecases/StartGameUseCase.kt | 13 +++++++++---- .../waterballsa/gaas/events/StartedGameEvent.kt | 15 +++++++++++++++ .../gaas/events/enums/EventMessageType.kt | 10 ++++++++++ .../gaas/exceptions/enums/SocketIOEventMessage.kt | 7 ------- 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt create mode 100644 domain/src/main/kotlin/tw/waterballsa/gaas/events/enums/EventMessageType.kt delete mode 100644 domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt index 5a7d2818..738eb7cd 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt @@ -6,6 +6,9 @@ import tw.waterballsa.gaas.application.eventbus.EventBus import tw.waterballsa.gaas.application.repositories.RoomRepository import tw.waterballsa.gaas.application.repositories.UserRepository import tw.waterballsa.gaas.domain.Room +import tw.waterballsa.gaas.events.StartedGameEvent +import tw.waterballsa.gaas.events.StartedGameEvent.Data +import tw.waterballsa.gaas.events.enums.EventMessageType.GAME_STARTED import tw.waterballsa.gaas.exceptions.PlatformException import tw.waterballsa.gaas.exceptions.enums.PlatformError.GAME_START_FAILED import javax.inject.Named @@ -29,11 +32,12 @@ class StartGameUseCase( validateAllPlayersReady() } - val gameServerUrl = room.startGameByHost(jwtToken) + val startedGameEvent = room.startGameByHost(jwtToken) room.startGame() roomRepository.update(room) - presenter.present(gameServerUrl) + presenter.present(startedGameEvent.data.gameUrl) + eventBus.broadcast(startedGameEvent) } } @@ -59,11 +63,12 @@ class StartGameUseCase( } } - private fun Room.startGameByHost(jwtToken: String): String { + private fun Room.startGameByHost(jwtToken: String): StartedGameEvent { val gameServerHost = game.backEndUrl val startGameRequest = StartGameRequest(players.map { it.toGamePlayer() }) + val startGameResponse = gameService.startGame(gameServerHost, jwtToken, startGameRequest) - return gameService.startGame(gameServerHost, jwtToken, startGameRequest).url + return StartedGameEvent(GAME_STARTED, Data(startGameResponse.url, roomId!!)) } private fun Room.Player.toGamePlayer(): StartGameRequest.GamePlayer = diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt new file mode 100644 index 00000000..34545c5e --- /dev/null +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt @@ -0,0 +1,15 @@ +package tw.waterballsa.gaas.events + +import tw.waterballsa.gaas.domain.Room +import tw.waterballsa.gaas.events.enums.EventMessageType + +data class StartedGameEvent( + val type: EventMessageType, + val data: Data +) : DomainEvent() { + + data class Data( + val gameUrl: String, + val roomId: Room.Id, + ) +} diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/events/enums/EventMessageType.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/events/enums/EventMessageType.kt new file mode 100644 index 00000000..994848e8 --- /dev/null +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/events/enums/EventMessageType.kt @@ -0,0 +1,10 @@ +package tw.waterballsa.gaas.events.enums + +enum class EventMessageType( + val eventName: String +) { + CHAT_MESSAGE("CHAT_MESSAGE"), + CHATROOM_JOIN("CHATROOM_JOIN"), + CONNECT_EVENT("CONNECT_EVENT"), + GAME_STARTED("GAME_STARTED"), +} diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt deleted file mode 100644 index 31ac09dc..00000000 --- a/domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt +++ /dev/null @@ -1,7 +0,0 @@ -package tw.waterballsa.gaas.exceptions.enums - -enum class SocketIOEventMessage(val eventName: String) { - CHAT_MESSAGE("CHAT_MESSAGE"), - CHATROOM_JOIN("CHATROOM_JOIN"), - CONNECT_EVENT("CONNECT_EVENT"), -}