diff --git a/knockoutwhistweb/app/controllers/JavaScriptRoutingController.scala b/knockoutwhistweb/app/controllers/JavaScriptRoutingController.scala new file mode 100644 index 0000000..b25f417 --- /dev/null +++ b/knockoutwhistweb/app/controllers/JavaScriptRoutingController.scala @@ -0,0 +1,23 @@ +package controllers + +import auth.{AuthAction, AuthenticatedRequest} +import logic.PodManager +import play.api.mvc.{Action, AnyContent, BaseController, ControllerComponents} +import play.api.routing.JavaScriptReverseRouter + +import javax.inject.Inject + +class JavaScriptRoutingController @Inject()( + val controllerComponents: ControllerComponents, + val authAction: AuthAction, + val podManager: PodManager + ) extends BaseController { + def javascriptRoutes(): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] => + Ok( + JavaScriptReverseRouter("jsRoutes")( + routes.javascript.MainMenuController.createGame, + routes.javascript.IngameController.startGame + ) + ).as("text/javascript") + } +} diff --git a/knockoutwhistweb/app/controllers/MainMenuController.scala b/knockoutwhistweb/app/controllers/MainMenuController.scala index 55c4a1b..413df1d 100644 --- a/knockoutwhistweb/app/controllers/MainMenuController.scala +++ b/knockoutwhistweb/app/controllers/MainMenuController.scala @@ -3,6 +3,7 @@ package controllers import auth.{AuthAction, AuthenticatedRequest} import logic.PodManager import play.api.* +import play.api.libs.json.Json import play.api.mvc.* import javax.inject.* @@ -29,16 +30,23 @@ class MainMenuController @Inject()( } def createGame(): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] => - val postData = request.body.asFormUrlEncoded - if (postData.isDefined) { - val gamename = postData.get.get("lobbyname").flatMap(_.headOption).getOrElse(s"${request.user.name}'s Game") - val playeramount = postData.get.get("playeramount").flatMap(_.headOption).getOrElse("") + val jsonBody = request.body.asJson + if (jsonBody.isDefined) { + val gamename: String = (jsonBody.get \ "lobbyname").asOpt[String] + .getOrElse(s"${request.user.name}'s Game") + + val playeramount: String = (jsonBody.get \ "playeramount").asOpt[String] + .getOrElse(throw new IllegalArgumentException("Player amount is required and must be an integer.")) + val gameLobby = podManager.createGame( host = request.user, name = gamename, maxPlayers = playeramount.toInt ) - Redirect(routes.IngameController.game(gameLobby.id)) + Ok(Json.obj( + "status" -> "success", + "redirectUrl" -> routes.IngameController.game(gameLobby.id).url + )) } else { BadRequest("Invalid form submission") } diff --git a/knockoutwhistweb/app/views/main.scala.html b/knockoutwhistweb/app/views/main.scala.html index 12d9c2c..e1cc5d1 100644 --- a/knockoutwhistweb/app/views/main.scala.html +++ b/knockoutwhistweb/app/views/main.scala.html @@ -22,7 +22,7 @@ @* And here's where we render the `Html` object containing * the page content. *@ @content - + diff --git a/knockoutwhistweb/app/views/mainmenu/creategame.scala.html b/knockoutwhistweb/app/views/mainmenu/creategame.scala.html index 4872ddc..fd680fd 100644 --- a/knockoutwhistweb/app/views/mainmenu/creategame.scala.html +++ b/knockoutwhistweb/app/views/mainmenu/creategame.scala.html @@ -3,32 +3,30 @@ @main("Create Game") { @navbar(user)
-
-
-
- - -
-
- - -
-
- - -
- 2 - 3 - 4 - 5 - 6 - 7 -
-
-
- -
+
+
+ +
- +
+ + +
+
+ + +
+ 2 + 3 + 4 + 5 + 6 + 7 +
+
+
+
Create Game
+
+
} \ No newline at end of file diff --git a/knockoutwhistweb/conf/routes b/knockoutwhistweb/conf/routes index bba5dda..c8f312e 100644 --- a/knockoutwhistweb/conf/routes +++ b/knockoutwhistweb/conf/routes @@ -3,7 +3,8 @@ # https://www.playframework.com/documentation/latest/ScalaRouting # ~~~~ - +# For the javascript routing +GET /assets/js/routes controllers.JavaScriptRoutingController.javascriptRoutes() # Primary routes GET / controllers.MainMenuController.index() GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/knockoutwhistweb/public/javascripts/main.js b/knockoutwhistweb/public/javascripts/main.js index 36ec495..08ec3bc 100644 --- a/knockoutwhistweb/public/javascripts/main.js +++ b/knockoutwhistweb/public/javascripts/main.js @@ -77,4 +77,47 @@ }) }) }) -})() \ No newline at end of file +})() + +function createGameJS() { + let lobbyName = document.getElementById("lobbyname").value; + if (lobbyName === "") { + lobbyName = "DefaultLobby" + } + const playerAmount = document.getElementById("playeramount").value; + const jsonObj = { + lobbyname: lobbyName, + playeramount: playerAmount + } + sendGameCreationRequest(jsonObj); +} + +function sendGameCreationRequest(dataObject) { + const route = jsRoutes.controllers.MainMenuController.createGame(); + + fetch(route.url, { + method: route.type, + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(dataObject) + }) + .then(response => { + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + return response.json(); + }) + .then(data => { + if (data.status === 'success') { + // Redirect the user + window.location.href = data.redirectUrl; + } else { + console.error("Game creation failed:", data.message); + } + }) + .catch(error => { + console.error('Fetch error:', error); + alert('Could not create game. Please try again.'); + }); +} \ No newline at end of file