Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8812b0fad4 | ||
| dd5e8e65e5 |
@@ -224,3 +224,8 @@
|
|||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
* FRO-29 Websocket Communication ([#104](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/issues/104)) ([fa3d21e](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/fa3d21e3038eb07369764850a9ad9badd269ac57))
|
* FRO-29 Websocket Communication ([#104](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/issues/104)) ([fa3d21e](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/fa3d21e3038eb07369764850a9ad9badd269ac57))
|
||||||
|
## (2025-12-10)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* BAC-27 Implemented endpoint which returns information about the current state ([#103](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/issues/103)) ([dd5e8e6](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/dd5e8e65e55f02a7618b3c60e8fc7087774e5106))
|
||||||
|
|||||||
Submodule knockoutwhistfrontend updated: 5d080bba47...a04c370a75
94
knockoutwhistweb/app/controllers/StatusController.scala
Normal file
94
knockoutwhistweb/app/controllers/StatusController.scala
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import auth.AuthAction
|
||||||
|
import logic.PodManager
|
||||||
|
import logic.game.GameLobby
|
||||||
|
import logic.user.SessionManager
|
||||||
|
import model.users.User
|
||||||
|
import play.api.libs.json.{JsValue, Json}
|
||||||
|
import play.api.mvc.*
|
||||||
|
import util.WebsocketEventMapper
|
||||||
|
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class StatusController @Inject()(
|
||||||
|
val controllerComponents: ControllerComponents,
|
||||||
|
val sessionManager: SessionManager,
|
||||||
|
val authAction: AuthAction
|
||||||
|
) extends BaseController {
|
||||||
|
|
||||||
|
def requestStatus(): Action[AnyContent] = {
|
||||||
|
Action { implicit request =>
|
||||||
|
val userOpt = getUserFromSession(request)
|
||||||
|
if (userOpt.isEmpty) {
|
||||||
|
Ok(
|
||||||
|
Json.obj(
|
||||||
|
"status" -> "unauthenticated"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val user = userOpt.get
|
||||||
|
val gameOpt = PodManager.identifyGameOfUser(user)
|
||||||
|
if (gameOpt.isEmpty) {
|
||||||
|
Ok(
|
||||||
|
Json.obj(
|
||||||
|
"status" -> "authenticated",
|
||||||
|
"username" -> user.name,
|
||||||
|
"inGame" -> "false"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val game = gameOpt.get
|
||||||
|
Ok(
|
||||||
|
Json.obj(
|
||||||
|
"status" -> "authenticated",
|
||||||
|
"username" -> user.name,
|
||||||
|
"inGame" -> "true",
|
||||||
|
"gameId" -> game.id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def game(gameId: String): Action[AnyContent] = {
|
||||||
|
Action { implicit request =>
|
||||||
|
val userOpt = getUserFromSession(request)
|
||||||
|
if (userOpt.isEmpty) {
|
||||||
|
Unauthorized("User not authenticated")
|
||||||
|
} else {
|
||||||
|
val user = userOpt.get
|
||||||
|
val gameOpt = PodManager.getGame(gameId)
|
||||||
|
if (gameOpt.isEmpty) {
|
||||||
|
NotFound("Game not found")
|
||||||
|
} else {
|
||||||
|
val game = gameOpt.get
|
||||||
|
if (!game.getPlayers.contains(user.id)) {
|
||||||
|
Forbidden("User not part of this game")
|
||||||
|
} else {
|
||||||
|
Ok(
|
||||||
|
Json.obj(
|
||||||
|
"gameId" -> game.id,
|
||||||
|
"state" -> game.logic.getCurrentState.toString,
|
||||||
|
"data" -> mapGameState(game, user)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
|
||||||
|
private def getUserFromSession(request: RequestHeader): Option[User] = {
|
||||||
|
val session = request.cookies.get("sessionId")
|
||||||
|
if (session.isDefined)
|
||||||
|
return sessionManager.getUserBySession(session.get.value)
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
private def mapGameState(gameLobby: GameLobby, user: User): JsValue = {
|
||||||
|
val userSession = gameLobby.getUserSession(user.id)
|
||||||
|
WebsocketEventMapper.stateToJson(userSession)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -57,12 +57,12 @@ object WebsocketEventMapper {
|
|||||||
Json.obj(
|
Json.obj(
|
||||||
"id" -> ("request-" + java.util.UUID.randomUUID().toString),
|
"id" -> ("request-" + java.util.UUID.randomUUID().toString),
|
||||||
"event" -> obj.id,
|
"event" -> obj.id,
|
||||||
"state" -> toJson(session),
|
"state" -> stateToJson(session),
|
||||||
"data" -> data
|
"data" -> data
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
def toJson(session: UserSession): JsValue = {
|
def stateToJson(session: UserSession): JsValue = {
|
||||||
session.gameLobby.getLogic.getCurrentState match {
|
session.gameLobby.getLogic.getCurrentState match {
|
||||||
case Lobby => Json.toJson(LobbyInfoDTO(session.gameLobby, session.user))
|
case Lobby => Json.toJson(LobbyInfoDTO(session.gameLobby, session.user))
|
||||||
case InGame => Json.toJson(GameInfoDTO(session.gameLobby, session.user))
|
case InGame => Json.toJson(GameInfoDTO(session.gameLobby, session.user))
|
||||||
|
|||||||
@@ -27,4 +27,8 @@ GET /logout controllers.UserController.logout()
|
|||||||
GET /game/:id controllers.IngameController.game(id: String)
|
GET /game/:id controllers.IngameController.game(id: String)
|
||||||
|
|
||||||
# Websocket
|
# Websocket
|
||||||
GET /websocket controllers.WebsocketController.socket()
|
GET /websocket controllers.WebsocketController.socket()
|
||||||
|
|
||||||
|
# Status
|
||||||
|
GET /status controllers.StatusController.requestStatus()
|
||||||
|
GET /status/:gameId controllers.StatusController.game(gameId: String)
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
MAJOR=4
|
MAJOR=4
|
||||||
MINOR=9
|
MINOR=10
|
||||||
PATCH=1
|
PATCH=0
|
||||||
|
|||||||
Reference in New Issue
Block a user