feat: BAC-27 Implement the API Endpoint on the backend
This commit is contained in:
120
knockoutwhistweb/app/controllers/StatusController.scala
Normal file
120
knockoutwhistweb/app/controllers/StatusController.scala
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import auth.{AuthAction, AuthenticatedRequest}
|
||||||
|
import de.knockoutwhist.control.GameState.{FinishedMatch, InGame, Lobby, MainMenu, SelectTrump, TieBreak}
|
||||||
|
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.{Action, *}
|
||||||
|
|
||||||
|
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)
|
||||||
|
gameLobby.logic.getCurrentState match {
|
||||||
|
case Lobby =>
|
||||||
|
Json.obj(
|
||||||
|
"host" -> userSession.host,
|
||||||
|
"players" -> gameLobby.getUsers.map(p => Json.obj("id" -> p.id, "name" -> p.name, "isSelf" -> (p.id == user.id)))
|
||||||
|
)
|
||||||
|
case SelectTrump =>
|
||||||
|
Json.obj(
|
||||||
|
|
||||||
|
)
|
||||||
|
case MainMenu =>
|
||||||
|
Json.obj(
|
||||||
|
|
||||||
|
)
|
||||||
|
case InGame =>
|
||||||
|
Json.obj(
|
||||||
|
|
||||||
|
)
|
||||||
|
case TieBreak =>
|
||||||
|
Json.obj(
|
||||||
|
|
||||||
|
)
|
||||||
|
case FinishedMatch =>
|
||||||
|
Json.obj(
|
||||||
|
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user