feat(ci): Polling Added polling for when the game starts and a card gets played (#58)
Co-authored-by: LQ63 <lkhermann@web.de> Reviewed-on: #58
This commit is contained in:
@@ -1,32 +1,27 @@
|
||||
package controllers
|
||||
|
||||
import auth.{AuthAction, AuthenticatedRequest}
|
||||
import de.knockoutwhist.cards.Hand
|
||||
import de.knockoutwhist.control.GameState.{InGame, Lobby, SelectTrump, TieBreak}
|
||||
import exceptions.{CantPlayCardException, GameFullException, NotEnoughPlayersException, NotHostException, NotInThisGameException}
|
||||
import exceptions.*
|
||||
import logic.PodManager
|
||||
import logic.game.PollingEvents.CardPlayed
|
||||
import logic.game.PollingEvents.GameStarted
|
||||
import logic.game.{GameLobby, PollingEvents}
|
||||
import model.sessions.{PlayerSession, UserSession}
|
||||
import model.users.User
|
||||
import logic.game.GameLobby
|
||||
import model.sessions.UserSession
|
||||
import play.api.*
|
||||
import play.api.libs.json.{JsArray, JsValue, Json}
|
||||
import play.api.libs.json.{JsValue, Json}
|
||||
import play.api.mvc.*
|
||||
import util.WebUIUtils
|
||||
|
||||
import java.util.UUID
|
||||
import javax.inject.*
|
||||
import scala.concurrent.Future
|
||||
import scala.util.Try
|
||||
import scala.concurrent.ExecutionContext
|
||||
@Singleton
|
||||
class IngameController @Inject()(
|
||||
val controllerComponents: ControllerComponents,
|
||||
val authAction: AuthAction,
|
||||
val podManager: PodManager
|
||||
) extends BaseController {
|
||||
import scala.util.Try
|
||||
|
||||
@Singleton
|
||||
class IngameController @Inject() (
|
||||
val cc: ControllerComponents,
|
||||
val podManager: PodManager,
|
||||
val authAction: AuthAction,
|
||||
implicit val ec: ExecutionContext
|
||||
) extends AbstractController(cc) {
|
||||
def game(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
game match {
|
||||
@@ -41,12 +36,12 @@ class IngameController @Inject()(
|
||||
case SelectTrump =>
|
||||
Ok(views.html.ingame.selecttrump(
|
||||
g.getPlayerByUser(request.user),
|
||||
g.logic
|
||||
g
|
||||
))
|
||||
case TieBreak =>
|
||||
Ok(views.html.ingame.tie(
|
||||
g.getPlayerByUser(request.user),
|
||||
g.logic
|
||||
g
|
||||
))
|
||||
case _ =>
|
||||
InternalServerError(s"Invalid game state for in-game view. GameId: $gameId" + s" State: ${g.logic.getCurrentState}")
|
||||
@@ -54,7 +49,6 @@ class IngameController @Inject()(
|
||||
case None =>
|
||||
NotFound("Game not found")
|
||||
}
|
||||
//NotFound(s"Reached end of game method unexpectedly. GameId: $gameId")
|
||||
}
|
||||
def startGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
@@ -67,30 +61,70 @@ class IngameController @Inject()(
|
||||
}
|
||||
}
|
||||
if (result.isSuccess) {
|
||||
Redirect(routes.IngameController.game(gameId))
|
||||
Ok(Json.obj(
|
||||
"status" -> "success",
|
||||
"redirectUrl" -> routes.IngameController.game(gameId).url
|
||||
))
|
||||
} else {
|
||||
val throwable = result.failed.get
|
||||
throwable match {
|
||||
case _: NotInThisGameException =>
|
||||
BadRequest(throwable.getMessage)
|
||||
BadRequest(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
case _: NotHostException =>
|
||||
Forbidden(throwable.getMessage)
|
||||
Forbidden(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
case _: NotEnoughPlayersException =>
|
||||
BadRequest(throwable.getMessage)
|
||||
BadRequest(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
case _ =>
|
||||
InternalServerError(throwable.getMessage)
|
||||
InternalServerError(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
def kickPlayer(gameId: String, playerToKick: UUID): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
def kickPlayer(gameId: String, playerToKick: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
game.get.leaveGame(playerToKick)
|
||||
Redirect(routes.IngameController.game(gameId))
|
||||
val playerToKickUUID = UUID.fromString(playerToKick)
|
||||
val result = Try {
|
||||
game.get.leaveGame(playerToKickUUID)
|
||||
}
|
||||
if(result.isSuccess) {
|
||||
Ok(Json.obj(
|
||||
"status" -> "success",
|
||||
"redirectUrl" -> routes.IngameController.game(gameId).url
|
||||
))
|
||||
} else {
|
||||
InternalServerError(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> "Something went wrong."
|
||||
))
|
||||
}
|
||||
}
|
||||
def leaveGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
game.get.leaveGame(request.user.id)
|
||||
Redirect(routes.MainMenuController.mainMenu())
|
||||
val result = Try {
|
||||
game.get.leaveGame(request.user.id)
|
||||
}
|
||||
if (result.isSuccess) {
|
||||
Ok(Json.obj(
|
||||
"status" -> "success",
|
||||
"redirectUrl" -> routes.MainMenuController.mainMenu().url
|
||||
))
|
||||
} else {
|
||||
InternalServerError(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> "Something went wrong."
|
||||
))
|
||||
}
|
||||
}
|
||||
def joinGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
@@ -122,7 +156,10 @@ class IngameController @Inject()(
|
||||
val game = podManager.getGame(gameId)
|
||||
game match {
|
||||
case Some(g) =>
|
||||
val cardIdOpt = request.body.asFormUrlEncoded.flatMap(_.get("cardId").flatMap(_.headOption))
|
||||
val jsonBody = request.body.asJson
|
||||
val cardIdOpt: Option[String] = jsonBody.flatMap { jsValue =>
|
||||
(jsValue \ "cardID").asOpt[String]
|
||||
}
|
||||
cardIdOpt match {
|
||||
case Some(cardId) =>
|
||||
var optSession: Option[UserSession] = None
|
||||
@@ -134,27 +171,51 @@ class IngameController @Inject()(
|
||||
}
|
||||
optSession.foreach(_.lock.unlock())
|
||||
if (result.isSuccess) {
|
||||
NoContent
|
||||
Ok(Json.obj(
|
||||
"status" -> "success",
|
||||
"redirectUrl" -> routes.IngameController.game(gameId).url
|
||||
))
|
||||
} else {
|
||||
val throwable = result.failed.get
|
||||
throwable match {
|
||||
case _: CantPlayCardException =>
|
||||
BadRequest(throwable.getMessage)
|
||||
BadRequest(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
case _: NotInThisGameException =>
|
||||
BadRequest(throwable.getMessage)
|
||||
BadRequest(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
case _: IllegalArgumentException =>
|
||||
BadRequest(throwable.getMessage)
|
||||
BadRequest(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
case _: IllegalStateException =>
|
||||
BadRequest(throwable.getMessage)
|
||||
BadRequest(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
case _ =>
|
||||
InternalServerError(throwable.getMessage)
|
||||
InternalServerError(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> throwable.getMessage
|
||||
))
|
||||
}
|
||||
}
|
||||
case None =>
|
||||
BadRequest("cardId parameter is missing")
|
||||
BadRequest(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> "cardId Parameter is missing"
|
||||
))
|
||||
}
|
||||
case None =>
|
||||
NotFound("Game not found")
|
||||
NotFound(Json.obj(
|
||||
"status" -> "failure",
|
||||
"errorMessage" -> "Game not found"
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -253,7 +314,7 @@ class IngameController @Inject()(
|
||||
val session = g.getUserSession(request.user.id)
|
||||
optSession = Some(session)
|
||||
session.lock.lock()
|
||||
g.selectTie(g.getUserSession(request.user.id), tie.toInt - 1)
|
||||
g.selectTie(g.getUserSession(request.user.id), tie.toInt)
|
||||
}
|
||||
optSession.foreach(_.lock.unlock())
|
||||
if (result.isSuccess) {
|
||||
|
||||
@@ -20,7 +20,7 @@ class JavaScriptRoutingController @Inject()(
|
||||
routes.javascript.IngameController.kickPlayer,
|
||||
routes.javascript.IngameController.leaveGame,
|
||||
routes.javascript.IngameController.playCard,
|
||||
routes.javascript.IngameController.polling
|
||||
routes.javascript.PollingController.polling
|
||||
)
|
||||
).as("text/javascript")
|
||||
}
|
||||
|
||||
134
knockoutwhistweb/app/controllers/PollingController.scala
Normal file
134
knockoutwhistweb/app/controllers/PollingController.scala
Normal file
@@ -0,0 +1,134 @@
|
||||
package controllers
|
||||
|
||||
import auth.{AuthAction, AuthenticatedRequest}
|
||||
import de.knockoutwhist.cards.Hand
|
||||
import logic.PodManager
|
||||
import logic.game.{GameLobby, PollingEvents}
|
||||
import logic.game.PollingEvents.{CardPlayed, LobbyUpdate, NewRound, ReloadEvent}
|
||||
import model.sessions.UserSession
|
||||
import model.users.User
|
||||
import play.api.libs.json.{JsArray, JsValue, Json}
|
||||
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents, Result}
|
||||
import util.WebUIUtils
|
||||
|
||||
import javax.inject.{Inject, Singleton}
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
@Singleton
|
||||
class PollingController @Inject() (
|
||||
val cc: ControllerComponents,
|
||||
val podManager: PodManager,
|
||||
val authAction: AuthAction,
|
||||
implicit val ec: ExecutionContext
|
||||
) extends AbstractController(cc) {
|
||||
|
||||
private def buildCardPlayResponse(game: GameLobby, hand: Option[Hand], newRound: Boolean): JsValue = {
|
||||
val currentRound = game.logic.getCurrentRound.get
|
||||
val currentTrick = game.logic.getCurrentTrick.get
|
||||
|
||||
val trickCardsJson = Json.toJson(
|
||||
currentTrick.cards.map { case (card, player) =>
|
||||
Json.obj("cardId" -> WebUIUtils.cardtoString(card), "player" -> player.name)
|
||||
}
|
||||
)
|
||||
val scoreTableJson = Json.toJson(
|
||||
game.getLogic.getPlayerQueue.get.toList.map { player =>
|
||||
Json.obj(
|
||||
"name" -> player.name,
|
||||
"tricks" -> currentRound.tricklist.count(_.winner.contains(player))
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
val stringHand = hand.map { h =>
|
||||
val cardStrings = h.cards.map(WebUIUtils.cardtoString)
|
||||
Json.toJson(cardStrings).as[JsArray]
|
||||
}.getOrElse(Json.arr())
|
||||
|
||||
val firstCardId = currentTrick.firstCard.map(WebUIUtils.cardtoString).getOrElse("BLANK")
|
||||
val nextPlayer = game.getLogic.getPlayerQueue.get.duplicate().nextPlayer().name
|
||||
Json.obj(
|
||||
"status" -> "cardPlayed",
|
||||
"animation" -> newRound,
|
||||
"handData" -> stringHand,
|
||||
"currentPlayerName" -> game.logic.getCurrentPlayer.get.name,
|
||||
"trumpSuit" -> currentRound.trumpSuit.toString,
|
||||
"trickCards" -> trickCardsJson,
|
||||
"scoreTable" -> scoreTableJson,
|
||||
"firstCardId" -> firstCardId,
|
||||
"nextPlayer" -> nextPlayer
|
||||
)
|
||||
}
|
||||
|
||||
private def buildLobbyUsersResponse(game: GameLobby, userSession: UserSession): JsValue = {
|
||||
Json.obj(
|
||||
"status" -> "lobbyUpdate",
|
||||
"host" -> userSession.host,
|
||||
"users" -> game.getUsers.map(u => Json.obj(
|
||||
"name" -> u.name,
|
||||
"id" -> u.id,
|
||||
"self" -> (u.id == userSession.id)
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
def handleEvent(event: PollingEvents, game: GameLobby, userSession: UserSession): Result = {
|
||||
event match {
|
||||
case NewRound =>
|
||||
val player = game.getPlayerByUser(userSession.user)
|
||||
val hand = player.currentHand()
|
||||
val jsonResponse = buildCardPlayResponse(game, hand, true)
|
||||
Ok(jsonResponse)
|
||||
case CardPlayed =>
|
||||
val player = game.getPlayerByUser(userSession.user)
|
||||
val hand = player.currentHand()
|
||||
val jsonResponse = buildCardPlayResponse(game, hand, false)
|
||||
Ok(jsonResponse)
|
||||
case LobbyUpdate =>
|
||||
Ok(buildLobbyUsersResponse(game, userSession))
|
||||
case ReloadEvent =>
|
||||
val jsonResponse = Json.obj(
|
||||
"status" -> "reloadEvent",
|
||||
"redirectUrl" -> routes.IngameController.game(game.id).url
|
||||
)
|
||||
Ok(jsonResponse)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Main Polling Action ---
|
||||
def polling(gameId: String): Action[AnyContent] = authAction.async { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
|
||||
val playerId = request.user.id
|
||||
|
||||
// 1. Safely look up the game
|
||||
podManager.getGame(gameId) match {
|
||||
case Some(game) =>
|
||||
|
||||
// 2. Short-Poll Check (Check for missed events)
|
||||
if (game.getPollingState.nonEmpty) {
|
||||
val event = game.getPollingState.dequeue()
|
||||
|
||||
Future.successful(handleEvent(event, game, game.getUserSession(playerId)))
|
||||
} else {
|
||||
|
||||
val eventPromise = game.registerWaiter(playerId)
|
||||
|
||||
eventPromise.future.map { event =>
|
||||
game.removeWaiter(playerId)
|
||||
handleEvent(event, game, game.getUserSession(playerId))
|
||||
}.recover {
|
||||
case _: Throwable =>
|
||||
game.removeWaiter(playerId)
|
||||
NoContent
|
||||
}
|
||||
}
|
||||
|
||||
case None =>
|
||||
// Game not found
|
||||
Future.successful(NotFound("Game not found."))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user