diff --git a/knockoutwhistfrontend b/knockoutwhistfrontend index 5d080bb..a04c370 160000 --- a/knockoutwhistfrontend +++ b/knockoutwhistfrontend @@ -1 +1 @@ -Subproject commit 5d080bba47778d51c8dbbb99d4d7c2b156c5316c +Subproject commit a04c370a7509b95385439b7453fdf8d3c7a304ae diff --git a/knockoutwhistweb/app/controllers/StatusController.scala b/knockoutwhistweb/app/controllers/StatusController.scala new file mode 100644 index 0000000..72daedb --- /dev/null +++ b/knockoutwhistweb/app/controllers/StatusController.scala @@ -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) + } + +} diff --git a/knockoutwhistweb/app/util/WebsocketEventMapper.scala b/knockoutwhistweb/app/util/WebsocketEventMapper.scala index 2a2f011..4e3bb32 100644 --- a/knockoutwhistweb/app/util/WebsocketEventMapper.scala +++ b/knockoutwhistweb/app/util/WebsocketEventMapper.scala @@ -57,12 +57,12 @@ object WebsocketEventMapper { Json.obj( "id" -> ("request-" + java.util.UUID.randomUUID().toString), "event" -> obj.id, - "state" -> toJson(session), + "state" -> stateToJson(session), "data" -> data ) } - def toJson(session: UserSession): JsValue = { + def stateToJson(session: UserSession): JsValue = { session.gameLobby.getLogic.getCurrentState match { case Lobby => Json.toJson(LobbyInfoDTO(session.gameLobby, session.user)) case InGame => Json.toJson(GameInfoDTO(session.gameLobby, session.user)) diff --git a/knockoutwhistweb/conf/routes b/knockoutwhistweb/conf/routes index ac9c094..c5dfd08 100644 --- a/knockoutwhistweb/conf/routes +++ b/knockoutwhistweb/conf/routes @@ -27,4 +27,8 @@ GET /logout controllers.UserController.logout() GET /game/:id controllers.IngameController.game(id: String) # Websocket -GET /websocket controllers.WebsocketController.socket() \ No newline at end of file +GET /websocket controllers.WebsocketController.socket() + +# Status +GET /status controllers.StatusController.requestStatus() +GET /status/:gameId controllers.StatusController.game(gameId: String) \ No newline at end of file