Add AggregateID for the PlayerNotificationEventListener and CommandHandler
This commit is contained in:
@@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user