Skip to content

Commit

Permalink
feat start game event broadcast (#181)
Browse files Browse the repository at this point in the history
* feat start game event broadcast
  • Loading branch information
m1a2st authored Oct 12, 2023
1 parent 8810599 commit 37b8735
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
}
Original file line number Diff line number Diff line change
@@ -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"),
}

This file was deleted.

0 comments on commit 37b8735

Please sign in to comment.