feat(ui): Websocket
Started implementing functionality to Websocket.
This commit is contained in:
@@ -2,9 +2,11 @@ package model.sessions
|
||||
|
||||
import de.knockoutwhist.events.player.{RequestCardEvent, RequestTieChoiceEvent, RequestTrumpSuitEvent}
|
||||
import de.knockoutwhist.utils.events.SimpleEvent
|
||||
import logic.PodManager
|
||||
import logic.game.GameLobby
|
||||
import model.users.User
|
||||
import play.api.libs.json.JsObject
|
||||
import play.api.libs.json.Format.GenericFormat
|
||||
import play.api.libs.json.{JsError, JsObject, JsResult, JsSuccess, JsValue}
|
||||
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
@@ -26,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))
|
||||
websocketActor.foreach(_.transmitEventToClient(event, gameLobby))
|
||||
}
|
||||
|
||||
override def id: UUID = user.id
|
||||
@@ -44,6 +46,21 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e
|
||||
case "Ping" =>
|
||||
// No action needed for Ping
|
||||
()
|
||||
case "Start Game" =>
|
||||
println("INSIDE HANDLE WEB RESPONSE" + data)
|
||||
val gameId: String = (data \ "gameId").get.toString
|
||||
val cleanGameId: String = gameId.replaceAll("^[\"']|[\"']$", "")
|
||||
val user: JsObject = (data \ "user").asOpt[JsObject].get
|
||||
val gameLobby: GameLobby = PodManager.getGame(cleanGameId).get
|
||||
val realUser: JsResult[User] = user.validate[User]
|
||||
val uu: User = realUser match {
|
||||
case JsSuccess(extractedUser, _) =>
|
||||
extractedUser
|
||||
case e: JsError =>
|
||||
println("FAILED" + JsError.toJson(e).toString())
|
||||
throw new Exception("Failed to deserialize User object: " + JsError.toJson(e).toString())
|
||||
}
|
||||
gameLobby.startGame(uu)
|
||||
}
|
||||
}
|
||||
lock.unlock()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package model.sessions
|
||||
|
||||
import de.knockoutwhist.utils.events.SimpleEvent
|
||||
import logic.game.GameLobby
|
||||
import org.apache.pekko.actor.{Actor, ActorRef}
|
||||
import play.api.libs.json.{JsObject, JsValue, Json}
|
||||
import util.WebsocketEventMapper
|
||||
@@ -27,6 +28,7 @@ class UserWebsocketActor(
|
||||
Try {
|
||||
jsonObject match {
|
||||
case Success(value) =>
|
||||
println("SUCCESS, RECEIVED SUCCESSFUL MESSAGE..." + jsonObject)
|
||||
handle(value)
|
||||
case Failure(exception) =>
|
||||
transmitTextToClient(s"Error parsing JSON: ${exception.getMessage}")
|
||||
@@ -86,12 +88,12 @@ class UserWebsocketActor(
|
||||
}
|
||||
}
|
||||
|
||||
def transmitJsonToClient(jsonObj: JsObject): Unit = {
|
||||
def transmitJsonToClient(jsonObj: JsValue): Unit = {
|
||||
transmitTextToClient(jsonObj.toString())
|
||||
}
|
||||
|
||||
def transmitEventToClient(event: SimpleEvent): Unit = {
|
||||
transmitJsonToClient(WebsocketEventMapper.toJson(event))
|
||||
def transmitEventToClient(event: SimpleEvent, gameLobby: GameLobby): Unit = {
|
||||
transmitJsonToClient(WebsocketEventMapper.toJson(event, gameLobby))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user