Skip to content

Commit

Permalink
start on /lobby request
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Oct 3, 2024
1 parent 795b004 commit 89f4e34
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/kotlin/game/GameMode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package game

enum class GameMode {
Entropy,
Vectropy,
}
11 changes: 11 additions & 0 deletions core/src/main/kotlin/game/GameSettings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package game

data class GameSettings(
val mode: GameMode,
val jokerQuantity: Int,
val jokerValue: Int,
val includeMoons: Boolean,
val includeStars: Boolean,
val negativeJacks: Boolean,
val cardReveal: Boolean,
)
1 change: 1 addition & 0 deletions core/src/main/kotlin/http/ClientErrorCodes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ val INTERNAL_SERVER_ERROR = ClientErrorCode("internalServerError")
val UPDATE_REQUIRED = ClientErrorCode("updateRequired")
val INVALID_API_VERSION = ClientErrorCode("invalidApiVersion")
val EMPTY_NAME = ClientErrorCode("emptyName")
val INVALID_SESSION = ClientErrorCode("invalidSession")
5 changes: 5 additions & 0 deletions core/src/main/kotlin/http/CustomHeader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package http

object CustomHeader {
const val SESSION_ID = "session-id"
}
1 change: 1 addition & 0 deletions core/src/main/kotlin/http/Routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ object Routes {
const val HEALTH_CHECK = "/health-check"
const val DEV_COMMAND = "/dev-command"
const val BEGIN_SESSION = "/begin-session"
const val LOBBY = "/lobby"
}
3 changes: 3 additions & 0 deletions core/src/main/kotlin/http/dto/LobbyResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package http.dto

data class LobbyResponse(val rooms: List<RoomSummary>, val users: List<OnlineUser>)
3 changes: 3 additions & 0 deletions core/src/main/kotlin/http/dto/OnlineUser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package http.dto

data class OnlineUser(val name: String)
11 changes: 11 additions & 0 deletions core/src/main/kotlin/http/dto/RoomSummary.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package http.dto

import game.GameSettings

data class RoomSummary(
val name: String,
val gameSettings: GameSettings,
val capacity: Int,
val players: Int,
val observers: Int,
)
41 changes: 41 additions & 0 deletions server/src/main/kotlin/routes/RoutingUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package routes

import auth.Session
import http.CustomHeader
import http.INVALID_SESSION
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.request.header
import java.util.UUID
import util.ServerGlobals

suspend fun requiresSession(call: ApplicationCall, fn: suspend (session: Session) -> Unit) {
val sessionIdStr =
call.request.header(CustomHeader.SESSION_ID)
?: throw ClientException(
HttpStatusCode.Unauthorized,
INVALID_SESSION,
"Missing session id",
)

try {
UUID.fromString(sessionIdStr)
} catch (e: IllegalArgumentException) {
throw ClientException(
HttpStatusCode.BadRequest,
INVALID_SESSION,
"Session ID was not a valid UUID",
e,
)
}

val session =
ServerGlobals.sessionStore.find(sessionIdStr)
?: throw ClientException(
HttpStatusCode.Unauthorized,
INVALID_SESSION,
"No session found for ID $sessionIdStr",
)

fn(session)
}
24 changes: 24 additions & 0 deletions server/src/main/kotlin/routes/lobby/LobbyController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package routes.lobby

import http.Routes
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.response.respond
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import routes.requiresSession

object LobbyController {
private val lobbyService = LobbyService()

fun installRoutes(application: Application) {
application.routing { get(Routes.LOBBY) { doGetLobby(call) } }
}

private suspend fun doGetLobby(call: ApplicationCall) =
requiresSession(call) {
val response = lobbyService.getLobby()
call.respond(response)
}
}
9 changes: 9 additions & 0 deletions server/src/main/kotlin/routes/lobby/LobbyService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package routes.lobby

import http.dto.LobbyResponse

class LobbyService {
fun getLobby(): LobbyResponse {
return LobbyResponse(emptyList(), emptyList())
}
}

0 comments on commit 89f4e34

Please sign in to comment.