Add AggregateID for the PlayerNotificationEventListener and CommandHandler

This commit is contained in:
2025-04-14 23:39:50 +02:00
parent 0374712824
commit 1a96c68521
11 changed files with 96 additions and 42 deletions
@@ -7,5 +7,5 @@ import eventDemo.libs.event.EventBus
import eventDemo.libs.event.EventBusInMemory
class GameEventBusInMemory :
GameEventBus,
EventBus<GameEvent, GameId> by EventBusInMemory<GameEvent, GameId>()
GameEventBus(),
EventBus<GameEvent, GameId> by EventBusInMemory()
@@ -1,12 +1,16 @@
package eventDemo.adapter.interfaceLayer
import eventDemo.business.command.GameCommandHandler
import eventDemo.business.entity.GameId
import eventDemo.business.entity.Player
import eventDemo.business.event.eventListener.PlayerNotificationEventListener
import eventDemo.business.notification.Notification
import eventDemo.configuration.ktor.BadRequestException
import eventDemo.configuration.ktor.HttpErrorBadRequest
import eventDemo.libs.fromFrameChannel
import eventDemo.libs.toObjectChannel
import io.github.oshai.kotlinlogging.withLoggingContext
import io.ktor.http.parameters
import io.ktor.server.application.ApplicationCall
import io.ktor.server.auth.authenticate
import io.ktor.server.auth.jwt.JWTPrincipal
@@ -18,6 +22,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.launch
import java.util.UUID
@DelicateCoroutinesApi
fun Route.gameWebSocket(
@@ -25,18 +30,27 @@ fun Route.gameWebSocket(
commandHandler: GameCommandHandler,
) {
authenticate {
webSocket("/game") {
webSocket("/game/{id}") {
val currentPlayer = call.getPlayer()
val gameId =
call.parameters["id"]?.let { GameId(UUID.fromString(it)) }
?: throw BadRequestException(HttpErrorBadRequest("No ID fore the game"))
val outgoingFrameChannel: SendChannel<Notification> = fromFrameChannel(outgoing)
withLoggingContext("currentPlayer" to currentPlayer.toString()) {
GlobalScope.launch {
commandHandler.handle(
currentPlayer,
gameId,
toObjectChannel(incoming),
outgoingFrameChannel,
)
}
playerNotificationListener.startListening({ outgoingFrameChannel.trySendBlocking(it) }, currentPlayer)
playerNotificationListener.startListening(
{ outgoingFrameChannel.trySendBlocking(it) },
currentPlayer,
gameId,
)
}
}
}