feat: BAC-27 Implemented endpoint which returns information about the current state
This commit is contained in:
Submodule knockoutwhistfrontend updated: 5d080bba47...a04c370a75
@@ -9,7 +9,7 @@ import logic.user.SessionManager
|
|||||||
import model.users.User
|
import model.users.User
|
||||||
import play.api.libs.json.{JsValue, Json}
|
import play.api.libs.json.{JsValue, Json}
|
||||||
import play.api.mvc.{Action, *}
|
import play.api.mvc.{Action, *}
|
||||||
import util.WebUIUtils
|
import util.{WebUIUtils, WebsocketEventMapper}
|
||||||
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -90,61 +90,7 @@ class StatusController @Inject()(
|
|||||||
|
|
||||||
private def mapGameState(gameLobby: GameLobby, user: User): JsValue = {
|
private def mapGameState(gameLobby: GameLobby, user: User): JsValue = {
|
||||||
val userSession = gameLobby.getUserSession(user.id)
|
val userSession = gameLobby.getUserSession(user.id)
|
||||||
gameLobby.logic.getCurrentState match {
|
WebsocketEventMapper.stateToJson(userSession)
|
||||||
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(
|
|
||||||
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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))
|
||||||
|
|||||||
Reference in New Issue
Block a user