chore: upgrade versions and rename folders
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package eventDemo.adapter.presenter.query
|
||||
|
||||
import eventDemo.domain.command.GameCommandHandler
|
||||
import eventDemo.domain.command.command.GameCommand
|
||||
import eventDemo.domain.entity.GameId
|
||||
import eventDemo.domain.event.projection.projectionListener.PlayerNotificationListener
|
||||
import eventDemo.domain.notification.Notification
|
||||
import eventDemo.libs.fromFrameChannel
|
||||
import eventDemo.libs.toObjectChannel
|
||||
import io.github.oshai.kotlinlogging.withLoggingContext
|
||||
import io.ktor.server.auth.authenticate
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.websocket.DefaultWebSocketServerSession
|
||||
import io.ktor.server.websocket.webSocket
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.coroutines.channels.SendChannel
|
||||
import kotlinx.coroutines.channels.trySendBlocking
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.UUID
|
||||
|
||||
@DelicateCoroutinesApi
|
||||
fun Route.gameWebSocket(
|
||||
playerNotificationListener: PlayerNotificationListener,
|
||||
commandHandler: GameCommandHandler,
|
||||
) {
|
||||
authenticate {
|
||||
webSocket("/games/new") {
|
||||
runWebSocket(GameId(), commandHandler, playerNotificationListener)
|
||||
}
|
||||
|
||||
webSocket("/games/{id}") {
|
||||
val gameId = GameId(UUID.fromString(call.parameters["id"]!!))
|
||||
runWebSocket(gameId, commandHandler, playerNotificationListener)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@DelicateCoroutinesApi
|
||||
private fun DefaultWebSocketServerSession.runWebSocket(
|
||||
gameId: GameId,
|
||||
commandHandler: GameCommandHandler,
|
||||
playerNotificationListener: PlayerNotificationListener,
|
||||
) {
|
||||
val currentPlayer = call.getPlayerCredentials()
|
||||
val incomingFrameChannel: ReceiveChannel<GameCommand> = toObjectChannel(incoming)
|
||||
val outgoingFrameChannel: SendChannel<Notification> = fromFrameChannel(outgoing)
|
||||
withLoggingContext("currentPlayer" to currentPlayer.toString()) {
|
||||
val notificationListener =
|
||||
playerNotificationListener.startListening(
|
||||
currentPlayer,
|
||||
gameId,
|
||||
) { outgoingFrameChannel.trySendBlocking(it) }
|
||||
|
||||
// TODO change GlobalScope
|
||||
GlobalScope.launch {
|
||||
commandHandler.handleIncomingPlayerCommands(
|
||||
currentPlayer,
|
||||
gameId,
|
||||
incomingFrameChannel,
|
||||
outgoingFrameChannel,
|
||||
)
|
||||
notificationListener.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package eventDemo.adapter.presenter.query
|
||||
|
||||
import eventDemo.domain.event.projection.GameListRepository
|
||||
import io.ktor.resources.Resource
|
||||
import io.ktor.server.auth.authenticate
|
||||
import io.ktor.server.resources.get
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.routing.Route
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@Resource("/games")
|
||||
class Games
|
||||
|
||||
/**
|
||||
* API routes to show all games.
|
||||
*/
|
||||
fun Route.readGamesList(gameListRepository: GameListRepository) {
|
||||
authenticate {
|
||||
// Read the last played card on the game.
|
||||
get<Games> {
|
||||
val gameList = gameListRepository.getList()
|
||||
call.respond(gameList)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package eventDemo.adapter.presenter.query
|
||||
|
||||
import eventDemo.domain.entity.Player
|
||||
import io.ktor.server.application.ApplicationCall
|
||||
import io.ktor.server.auth.jwt.JWTPrincipal
|
||||
import io.ktor.server.auth.principal
|
||||
|
||||
internal fun ApplicationCall.getPlayerCredentials() =
|
||||
principal<JWTPrincipal>()!!.run {
|
||||
Player(
|
||||
id = payload.getClaim("playerid").asString(),
|
||||
name = payload.getClaim("username").asString(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package eventDemo.adapter.presenter.query
|
||||
|
||||
import eventDemo.domain.entity.GameId
|
||||
import eventDemo.domain.event.projection.GameStateRepository
|
||||
import eventDemo.configuration.serializer.GameIdSerializer
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.resources.Resource
|
||||
import io.ktor.server.auth.authenticate
|
||||
import io.ktor.server.resources.get
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.routing.Route
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@Resource("/games/{id}")
|
||||
class Game(
|
||||
@Serializable(with = GameIdSerializer::class)
|
||||
val id: GameId,
|
||||
) {
|
||||
@Serializable
|
||||
@Resource("card/last")
|
||||
class Card(
|
||||
val game: Game,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
@Resource("state")
|
||||
class State(
|
||||
val game: Game,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* API routes to read the game state.
|
||||
*/
|
||||
fun Route.readTheGameState(gameStateRepository: GameStateRepository) {
|
||||
authenticate {
|
||||
// Read the last played card on the game.
|
||||
get<Game.Card> { body ->
|
||||
gameStateRepository
|
||||
.get(body.game.id)
|
||||
.cardOnCurrentStack
|
||||
?.let { call.respond(it) }
|
||||
?: call.response.status(HttpStatusCode.BadRequest)
|
||||
}
|
||||
|
||||
// Read the last played card on the game.
|
||||
get<Game.State> { body ->
|
||||
val state = gameStateRepository.get(body.game.id)
|
||||
call.respond(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user