diff --git a/knockoutwhistfrontend b/knockoutwhistfrontend index 0b8a179..30798ce 160000 --- a/knockoutwhistfrontend +++ b/knockoutwhistfrontend @@ -1 +1 @@ -Subproject commit 0b8a1794a0dc3efb280799a5c35386196ffe8727 +Subproject commit 30798ce7235ed1dc63cbe01021ab99269541ed40 diff --git a/knockoutwhistweb/app/controllers/WebsocketController.scala b/knockoutwhistweb/app/controllers/WebsocketController.scala index 0a9825e..06a0067 100644 --- a/knockoutwhistweb/app/controllers/WebsocketController.scala +++ b/knockoutwhistweb/app/controllers/WebsocketController.scala @@ -21,7 +21,7 @@ class WebsocketController @Inject()( )(implicit system: ActorSystem, mat: Materializer) extends AbstractController(cc) { def socket(): WebSocket = WebSocket.accept[String, String] { request => - val session = request.cookies.get("sessionId") + val session = request.cookies.get("accessToken") 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") diff --git a/knockoutwhistweb/app/dto/subDTO/HandDTO.scala b/knockoutwhistweb/app/dto/subDTO/HandDTO.scala index e954b2a..0dfb2ad 100644 --- a/knockoutwhistweb/app/dto/subDTO/HandDTO.scala +++ b/knockoutwhistweb/app/dto/subDTO/HandDTO.scala @@ -2,13 +2,13 @@ package dto.subDTO import de.knockoutwhist.cards.Hand -case class HandDTO(card: List[CardDTO]) +case class HandDTO(cards: List[CardDTO]) object HandDTO { def apply(hand: Hand): HandDTO = { HandDTO( - card = hand.cards.zipWithIndex.map { case (card, idx) => CardDTO(card, idx) } + cards = hand.cards.zipWithIndex.map { case (card, idx) => CardDTO(card, idx) } ) } diff --git a/knockoutwhistweb/app/dto/subDTO/TrickDTO.scala b/knockoutwhistweb/app/dto/subDTO/TrickDTO.scala index d7c5513..14fbb99 100644 --- a/knockoutwhistweb/app/dto/subDTO/TrickDTO.scala +++ b/knockoutwhistweb/app/dto/subDTO/TrickDTO.scala @@ -8,7 +8,7 @@ object TrickDTO { def apply(trick: Trick): TrickDTO = { TrickDTO( - cards = trick.cards.map { case (card, player) => player.id.toString -> CardDTO(card) }, + cards = trick.cards.map { case (card, player) => player.name -> CardDTO(card) }, firstCard = trick.firstCard.map(card => CardDTO(card)), winner = trick.winner.map(player => PlayerDTO(player)) ) diff --git a/knockoutwhistweb/app/model/sessions/UserSession.scala b/knockoutwhistweb/app/model/sessions/UserSession.scala index 565fa76..aa04bc5 100644 --- a/knockoutwhistweb/app/model/sessions/UserSession.scala +++ b/knockoutwhistweb/app/model/sessions/UserSession.scala @@ -46,13 +46,13 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e case "StartGame" => gameLobby.startGame(user) case "PlayCard" => - val maybeCardIndex: Option[String] = (data \ "cardindex").asOpt[String] + val maybeCardIndex: Option[Int] = (data \ "cardindex").asOpt[Int] maybeCardIndex match { case Some(index) => val session = gameLobby.getUserSession(user.id) - gameLobby.playCard(session, index.toInt) + gameLobby.playCard(session, index) case None => - println("Card Index not found or is not a number.") + println("Card Index not found or is not a number." + data) } case "PickTrumpsuit" => val maybeSuitIndex: Option[Int] = (data \ "suitIndex").asOpt[Int] @@ -74,6 +74,10 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e } case "ReturnToLobby" => gameLobby.returnToLobby(this) + case "LeaveGame" => + gameLobby.leaveGame(user.id, false) + case _ => + println("Unknown event type: " + eventType + " with data: " + data) } } diff --git a/knockoutwhistweb/public/javascripts/websocket.js b/knockoutwhistweb/public/javascripts/websocket.js index 174a8ad..d1aa607 100644 --- a/knockoutwhistweb/public/javascripts/websocket.js +++ b/knockoutwhistweb/public/javascripts/websocket.js @@ -1,11 +1,9 @@ -// javascript -let ws = null; // will be created by connectWebSocket() -const pending = new Map(); // id -> { resolve, reject, timer } -const handlers = new Map(); // eventType -> handler(data) -> (value|Promise) +let ws = null; +const pending = new Map(); +const handlers = new Map(); let timer = null; -// helper to attach message/error/close handlers to a socket function setupSocketHandlers(socket) { socket.onmessage = (event) => { console.debug("SERVER MESSAGE:", event.data); @@ -48,7 +46,6 @@ function setupSocketHandlers(socket) { }; if (!handler) { - // no handler: respond with an error object in data so server can fail it console.warn("No handler for event:", eventType); sendResponse({error: "No handler for event: " + eventType}); return; @@ -91,7 +88,6 @@ function setupSocketHandlers(socket) { }; } -// connect/disconnect helpers function connectWebSocket(url = null) { if (!url) { const loc = window.location; @@ -100,7 +96,6 @@ function connectWebSocket(url = null) { } if (ws && ws.readyState === WebSocket.OPEN) return Promise.resolve(); if (ws && ws.readyState === WebSocket.CONNECTING) { - // already connecting - return a promise that resolves on open return new Promise((resolve, reject) => { const prevOnOpen = ws.onopen; const prevOnError = ws.onerror; @@ -121,7 +116,6 @@ function connectWebSocket(url = null) { return new Promise((resolve, reject) => { ws.onopen = () => { console.log("WebSocket connection established!"); - // start heartbeat timer = setInterval(() => { if (ws && ws.readyState === WebSocket.OPEN) { sendEventAndWait("ping", {}).then(