feat(base): Fixed logic for websockets and added GameStateEvent. Might've caused instability on other feature branches! (#84)
Reviewed-on: #84 Reviewed-by: lq64 <lq@blackhole.local> Co-authored-by: Janis <janis.e.20@gmx.de> Co-committed-by: Janis <janis.e.20@gmx.de>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import auth.{AuthAction, AuthenticatedRequest}
|
||||
import de.knockoutwhist.control.GameState
|
||||
import de.knockoutwhist.control.GameState.*
|
||||
import exceptions.*
|
||||
import logic.PodManager
|
||||
@@ -29,7 +30,7 @@ class IngameController @Inject()(
|
||||
game match {
|
||||
case Some(g) =>
|
||||
val results = Try {
|
||||
returnInnerHTML(g, request.user)
|
||||
IngameController.returnInnerHTML(g, g.logic.getCurrentState, request.user)
|
||||
}
|
||||
if (results.isSuccess) {
|
||||
Ok(views.html.main("In-Game - Knockout Whist")(results.get))
|
||||
@@ -41,34 +42,6 @@ class IngameController @Inject()(
|
||||
}
|
||||
}
|
||||
|
||||
def returnInnerHTML(gameLobby: GameLobby, user: User): Html = {
|
||||
gameLobby.logic.getCurrentState match {
|
||||
case Lobby => views.html.lobby.lobby(Some(user), gameLobby)
|
||||
case InGame =>
|
||||
views.html.ingame.ingame(
|
||||
gameLobby.getPlayerByUser(user),
|
||||
gameLobby
|
||||
)
|
||||
case SelectTrump =>
|
||||
views.html.ingame.selecttrump(
|
||||
gameLobby.getPlayerByUser(user),
|
||||
gameLobby
|
||||
)
|
||||
case TieBreak =>
|
||||
views.html.ingame.tie(
|
||||
gameLobby.getPlayerByUser(user),
|
||||
gameLobby
|
||||
)
|
||||
case FinishedMatch =>
|
||||
views.html.ingame.finishedMatch(
|
||||
Some(user),
|
||||
gameLobby
|
||||
)
|
||||
case _ =>
|
||||
throw new IllegalStateException(s"Invalid game state for in-game view. GameId: ${gameLobby.id}" + s" State: ${gameLobby.logic.getCurrentState}")
|
||||
}
|
||||
}
|
||||
|
||||
def startGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = PodManager.getGame(gameId)
|
||||
val result = Try {
|
||||
@@ -83,7 +56,7 @@ class IngameController @Inject()(
|
||||
Ok(Json.obj(
|
||||
"status" -> "success",
|
||||
"redirectUrl" -> routes.IngameController.game(gameId).url,
|
||||
"content" -> returnInnerHTML(game.get, request.user).toString()
|
||||
"content" -> IngameController.returnInnerHTML(game.get, game.get.logic.getCurrentState, request.user).toString()
|
||||
))
|
||||
} else {
|
||||
val throwable = result.failed.get
|
||||
@@ -434,5 +407,37 @@ class IngameController @Inject()(
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
object IngameController {
|
||||
|
||||
def returnInnerHTML(gameLobby: GameLobby, gameState: GameState, user: User): Html = {
|
||||
gameState match {
|
||||
case Lobby => views.html.lobby.lobby(Some(user), gameLobby)
|
||||
case InGame =>
|
||||
views.html.ingame.ingame(
|
||||
gameLobby.getPlayerByUser(user),
|
||||
gameLobby
|
||||
)
|
||||
case SelectTrump =>
|
||||
views.html.ingame.selecttrump(
|
||||
gameLobby.getPlayerByUser(user),
|
||||
gameLobby
|
||||
)
|
||||
case TieBreak =>
|
||||
views.html.ingame.tie(
|
||||
gameLobby.getPlayerByUser(user),
|
||||
gameLobby
|
||||
)
|
||||
case FinishedMatch =>
|
||||
views.html.ingame.finishedMatch(
|
||||
Some(user),
|
||||
gameLobby
|
||||
)
|
||||
case _ =>
|
||||
throw new IllegalStateException(s"Invalid game state for in-game view. GameId: ${gameLobby.id}" + s" State: ${gameLobby.logic.getCurrentState}")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,8 +16,7 @@ import javax.inject.*
|
||||
@Singleton
|
||||
class MainMenuController @Inject()(
|
||||
val controllerComponents: ControllerComponents,
|
||||
val authAction: AuthAction,
|
||||
val ingameController: IngameController
|
||||
val authAction: AuthAction
|
||||
) extends BaseController {
|
||||
|
||||
// Pass the request-handling function directly to authAction (no nested Action)
|
||||
@@ -46,7 +45,7 @@ class MainMenuController @Inject()(
|
||||
Ok(Json.obj(
|
||||
"status" -> "success",
|
||||
"redirectUrl" -> routes.IngameController.game(gameLobby.id).url,
|
||||
"content" -> ingameController.returnInnerHTML(gameLobby, request.user).toString
|
||||
"content" -> IngameController.returnInnerHTML(gameLobby, gameLobby.logic.getCurrentState, request.user).toString
|
||||
))
|
||||
} else {
|
||||
BadRequest(Json.obj(
|
||||
@@ -70,7 +69,7 @@ class MainMenuController @Inject()(
|
||||
Ok(Json.obj(
|
||||
"status" -> "success",
|
||||
"redirectUrl" -> routes.IngameController.game(g.id).url,
|
||||
"content" -> ingameController.returnInnerHTML(g, request.user).toString
|
||||
"content" -> IngameController.returnInnerHTML(g, g.logic.getCurrentState, request.user).toString
|
||||
))
|
||||
case None =>
|
||||
NotFound(Json.obj(
|
||||
|
||||
Reference in New Issue
Block a user