feat(websocket)!: Implement WebSocket connection and event handling
This commit is contained in:
@@ -20,7 +20,6 @@ import scala.util.Try
|
||||
@Singleton
|
||||
class IngameController @Inject() (
|
||||
val cc: ControllerComponents,
|
||||
val podManager: PodManager,
|
||||
val authAction: AuthAction,
|
||||
implicit val ec: ExecutionContext
|
||||
) extends AbstractController(cc) {
|
||||
@@ -54,7 +53,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
|
||||
def game(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
game match {
|
||||
case Some(g) =>
|
||||
val results = Try {
|
||||
@@ -70,7 +69,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
}
|
||||
def startGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
val result = Try {
|
||||
game match {
|
||||
case Some(g) =>
|
||||
@@ -111,7 +110,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
}
|
||||
def kickPlayer(gameId: String, playerToKick: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
val playerToKickUUID = UUID.fromString(playerToKick)
|
||||
val result = Try {
|
||||
game.get.leaveGame(playerToKickUUID)
|
||||
@@ -129,7 +128,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
}
|
||||
def leaveGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
val result = Try {
|
||||
game.get.leaveGame(request.user.id)
|
||||
}
|
||||
@@ -147,7 +146,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
|
||||
def playCard(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] => {
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
game match {
|
||||
case Some(g) =>
|
||||
val jsonBody = request.body.asJson
|
||||
@@ -218,7 +217,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
}
|
||||
def playDogCard(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] => {
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
game match {
|
||||
case Some(g) => {
|
||||
val jsonBody = request.body.asJson
|
||||
@@ -282,7 +281,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
}
|
||||
def playTrump(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
game match {
|
||||
case Some(g) =>
|
||||
val jsonBody = request.body.asJson
|
||||
@@ -334,7 +333,7 @@ class IngameController @Inject() (
|
||||
}
|
||||
}
|
||||
def playTie(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
game match {
|
||||
case Some(g) =>
|
||||
val jsonBody = request.body.asJson
|
||||
@@ -388,7 +387,7 @@ class IngameController @Inject() (
|
||||
|
||||
|
||||
def returnToLobby(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||
val game = podManager.getGame(gameId)
|
||||
val game = PodManager.getGame(gameId)
|
||||
game match {
|
||||
case Some(g) =>
|
||||
val result = Try {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package controllers
|
||||
|
||||
import auth.{AuthAction, AuthenticatedRequest}
|
||||
import logic.PodManager
|
||||
import auth.AuthAction
|
||||
import play.api.mvc.{Action, AnyContent, BaseController, ControllerComponents}
|
||||
import play.api.routing.JavaScriptReverseRouter
|
||||
|
||||
@@ -10,7 +9,6 @@ import javax.inject.Inject
|
||||
class JavaScriptRoutingController @Inject()(
|
||||
val controllerComponents: ControllerComponents,
|
||||
val authAction: AuthAction,
|
||||
val podManager: PodManager
|
||||
) extends BaseController {
|
||||
def javascriptRoutes(): Action[AnyContent] =
|
||||
Action { implicit request =>
|
||||
|
||||
@@ -17,7 +17,6 @@ import javax.inject.*
|
||||
class MainMenuController @Inject()(
|
||||
val controllerComponents: ControllerComponents,
|
||||
val authAction: AuthAction,
|
||||
val podManager: PodManager,
|
||||
val ingameController: IngameController
|
||||
) extends BaseController {
|
||||
|
||||
@@ -39,7 +38,7 @@ class MainMenuController @Inject()(
|
||||
val playeramount: String = (jsonBody.get \ "playeramount").asOpt[String]
|
||||
.getOrElse(throw new IllegalArgumentException("Player amount is required."))
|
||||
|
||||
val gameLobby = podManager.createGame(
|
||||
val gameLobby = PodManager.createGame(
|
||||
host = request.user,
|
||||
name = gamename,
|
||||
maxPlayers = playeramount.toInt
|
||||
@@ -64,7 +63,7 @@ class MainMenuController @Inject()(
|
||||
(jsValue \ "gameId").asOpt[String]
|
||||
}
|
||||
if (gameId.isDefined) {
|
||||
val game = podManager.getGame(gameId.get)
|
||||
val game = PodManager.getGame(gameId.get)
|
||||
game match {
|
||||
case Some(g) =>
|
||||
g.addUser(request.user)
|
||||
|
||||
@@ -24,7 +24,6 @@ object PollingController {
|
||||
@Singleton
|
||||
class PollingController @Inject() (
|
||||
val cc: ControllerComponents,
|
||||
val podManager: PodManager,
|
||||
val authAction: AuthAction,
|
||||
val ingameController: IngameController,
|
||||
implicit val ec: ExecutionContext
|
||||
@@ -117,7 +116,7 @@ class PollingController @Inject() (
|
||||
|
||||
val playerId = request.user.id
|
||||
|
||||
podManager.getGame(gameId) match {
|
||||
PodManager.getGame(gameId) match {
|
||||
case Some(game) =>
|
||||
val playerEventQueue = game.getEventsOfPlayer(playerId)
|
||||
if (playerEventQueue.nonEmpty) {
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package controllers
|
||||
import actor.KnockOutWebSocketActor
|
||||
|
||||
|
||||
import auth.AuthAction
|
||||
import logic.PodManager
|
||||
import logic.user.SessionManager
|
||||
import model.sessions.{UserSession, UserWebsocketActor}
|
||||
import org.apache.pekko.actor.{ActorRef, ActorSystem, Props}
|
||||
import org.apache.pekko.stream.Materializer
|
||||
import play.api.*
|
||||
@@ -12,17 +17,28 @@ import javax.inject.*
|
||||
@Singleton
|
||||
class WebsocketController @Inject()(
|
||||
cc: ControllerComponents,
|
||||
val sessionManger: SessionManager,
|
||||
)(implicit system: ActorSystem, mat: Materializer) extends AbstractController(cc) {
|
||||
|
||||
object KnockOutWebSocketActorFactory {
|
||||
def create(out: ActorRef) = {
|
||||
Props(new KnockOutWebSocketActor(out))
|
||||
def create(out: ActorRef, userSession: UserSession): Props = {
|
||||
Props(new UserWebsocketActor(out, userSession))
|
||||
}
|
||||
}
|
||||
def socket() = WebSocket.accept[String, String] { request =>
|
||||
|
||||
|
||||
def socket(): WebSocket = WebSocket.accept[String, String] { request =>
|
||||
val session = request.cookies.get("sessionId")
|
||||
if (session.isEmpty) throw new Exception("No session cookie found")
|
||||
val userOpt = sessionManger.getUserBySession(session.get.value)
|
||||
if (userOpt.isEmpty) throw new Exception("Invalid session")
|
||||
val user = userOpt.get
|
||||
val game = PodManager.identifyGameOfUser(user)
|
||||
if (game.isEmpty) throw new Exception("User is not in a game")
|
||||
val userSession = game.get.getUserSession(user.id)
|
||||
ActorFlow.actorRef { out =>
|
||||
println("Connect received")
|
||||
KnockOutWebSocketActorFactory.create(out)
|
||||
KnockOutWebSocketActorFactory.create(out, userSession)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user