feat: BAC-27 Implemented endpoint which returns information about the current state

This commit is contained in:
2025-12-06 10:23:31 +01:00
parent da87439a07
commit 1914aa67cd
3 changed files with 5 additions and 59 deletions

View File

@@ -9,7 +9,7 @@ import logic.user.SessionManager
import model.users.User
import play.api.libs.json.{JsValue, Json}
import play.api.mvc.{Action, *}
import util.WebUIUtils
import util.{WebUIUtils, WebsocketEventMapper}
import javax.inject.Inject
@@ -90,61 +90,7 @@ class StatusController @Inject()(
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 =>
val findSelector: Option[AbstractPlayer] = gameLobby.logic.getCurrentMatch match {
case Some(matchImpl) =>
if (matchImpl.roundlist.isEmpty) None
else {
matchImpl.roundlist.last.winner match {
case Some(winner) => Some(winner)
case None => None
}
}
case None => None
}
findSelector match {
case Some(selector) =>
val isSelf = selector.id == user.id
val playerHand = {
val userPlayer = gameLobby.getPlayerByUser(user)
val handOpt = userPlayer.currentHand()
handOpt match {
case Some(hand) =>
WebUIUtils.handToJson(hand)
case None => Json.arr()
}
}
Json.obj(
"selector" -> selector.name,
"isSelf" -> isSelf,
"hand" -> playerHand
)
case None => Json.obj(
"error" -> "No winner found. Please try again later."
)
}
case MainMenu =>
Json.obj()
case InGame =>
Json.obj(
)
case TieBreak =>
Json.obj(
)
case FinishedMatch =>
Json.obj(
)
}
WebsocketEventMapper.stateToJson(userSession)
}
}

View File

@@ -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))