feat(ui): Websocket
Started implementing functionality to the Websocket
This commit is contained in:
@@ -28,7 +28,7 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e
|
||||
else canInteract = Some(InteractionType.Card)
|
||||
case _ =>
|
||||
}
|
||||
websocketActor.foreach(_.transmitEventToClient(event, gameLobby))
|
||||
websocketActor.foreach(_.transmitEventToClient(event, gameLobby, user))
|
||||
}
|
||||
|
||||
override def id: UUID = user.id
|
||||
@@ -41,7 +41,7 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e
|
||||
|
||||
def handleWebResponse(eventType: String, data: JsObject, user: User, gameLobby: GameLobby): Unit = {
|
||||
lock.lock()
|
||||
Try {
|
||||
val result = Try {
|
||||
eventType match {
|
||||
case "Ping" =>
|
||||
// No action needed for Ping
|
||||
@@ -49,9 +49,32 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e
|
||||
case "Start Game" =>
|
||||
println("INSIDE HANDLE WEB RESPONSE" + data)
|
||||
gameLobby.startGame(user)
|
||||
case "play Card" =>
|
||||
println("PLAYING CARD..." + data)
|
||||
val maybeCardIndex: Option[Int] = (data \ "cardindex").asOpt[Int]
|
||||
maybeCardIndex match {
|
||||
case Some(index) =>
|
||||
val session = gameLobby.getUserSession(user.id)
|
||||
gameLobby.playCard(session, index)
|
||||
case None =>
|
||||
println("Card Index not found or is not a number.")
|
||||
}
|
||||
case "Picked Trumpsuit" =>
|
||||
val maybeSuitIndex: Option[Int] = (data \ "suitIndex").asOpt[Int]
|
||||
maybeSuitIndex match {
|
||||
case Some(index) =>
|
||||
val session = gameLobby.getUserSession(user.id)
|
||||
gameLobby.selectTrump(session, index)
|
||||
case None =>
|
||||
println("Card Index not found or is not a number.")
|
||||
}
|
||||
}
|
||||
}
|
||||
lock.unlock()
|
||||
if (result.isFailure) {
|
||||
val throwable = result.failed.get
|
||||
throw throwable
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package model.sessions
|
||||
|
||||
import de.knockoutwhist.utils.events.SimpleEvent
|
||||
import logic.PodManager
|
||||
import logic.game.GameLobby
|
||||
import model.users.User
|
||||
import org.apache.pekko.actor.{Actor, ActorRef}
|
||||
import play.api.libs.json.{JsObject, JsValue, Json}
|
||||
import util.WebsocketEventMapper
|
||||
@@ -96,7 +99,8 @@ class UserWebsocketActor(
|
||||
transmitTextToClient(jsonObj.toString())
|
||||
}
|
||||
|
||||
def transmitEventToClient(event: SimpleEvent): Unit = {
|
||||
def transmitEventToClient(event: SimpleEvent, gameLobby: GameLobby, user: User): Unit = {
|
||||
val session = gameLobby.getUserSession(user.id)
|
||||
transmitJsonToClient(WebsocketEventMapper.toJson(event, session))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user