feat(websocket)!: Implement WebSocket connection and event handling
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package model.sessions
|
||||
|
||||
enum InteractionType {
|
||||
|
||||
|
||||
case TrumpSuit
|
||||
case Card
|
||||
case DogCard
|
||||
|
||||
@@ -5,9 +5,11 @@ import de.knockoutwhist.utils.events.SimpleEvent
|
||||
import java.util.UUID
|
||||
|
||||
trait PlayerSession {
|
||||
|
||||
|
||||
def id: UUID
|
||||
|
||||
def name: String
|
||||
|
||||
def updatePlayer(event: SimpleEvent): Unit
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ import de.knockoutwhist.utils.events.SimpleEvent
|
||||
import java.util.UUID
|
||||
|
||||
case class SimpleSession(id: UUID, player: AbstractPlayer) extends PlayerSession {
|
||||
|
||||
|
||||
def name: String = player.name
|
||||
|
||||
|
||||
override def updatePlayer(event: SimpleEvent): Unit = {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,18 +4,16 @@ import de.knockoutwhist.events.player.{RequestCardEvent, RequestTieChoiceEvent,
|
||||
import de.knockoutwhist.utils.events.SimpleEvent
|
||||
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
|
||||
import play.api.libs.json.JsObject
|
||||
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import scala.util.{Failure, Success, Try}
|
||||
import scala.util.Try
|
||||
|
||||
class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) extends PlayerSession {
|
||||
val lock: ReentrantLock = ReentrantLock()
|
||||
var canInteract: Option[InteractionType] = None
|
||||
var websocketActor: Option[UserWebsocketActor] = None
|
||||
val lock: ReentrantLock = ReentrantLock()
|
||||
|
||||
override def updatePlayer(event: SimpleEvent): Unit = {
|
||||
event match {
|
||||
@@ -33,7 +31,7 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e
|
||||
override def id: UUID = user.id
|
||||
|
||||
override def name: String = user.name
|
||||
|
||||
|
||||
def resetCanInteract(): Unit = {
|
||||
canInteract = None
|
||||
}
|
||||
@@ -49,5 +47,5 @@ class UserSession(val user: User, val host: Boolean, val gameLobby: GameLobby) e
|
||||
}
|
||||
lock.unlock()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -37,15 +37,6 @@ class UserWebsocketActor(
|
||||
case other =>
|
||||
}
|
||||
|
||||
def transmitEventToClient(event: SimpleEvent): Unit = {
|
||||
val jsonString = WebsocketEventMapper.toJsonString(event)
|
||||
out ! jsonString
|
||||
}
|
||||
|
||||
private def transmitJsonToClient(jsonObj: JsObject): Unit = {
|
||||
out ! jsonObj.toString()
|
||||
}
|
||||
|
||||
private def transmitTextToClient(text: String): Unit = {
|
||||
out ! text
|
||||
}
|
||||
@@ -91,4 +82,13 @@ class UserWebsocketActor(
|
||||
}
|
||||
}
|
||||
|
||||
def transmitJsonToClient(jsonObj: JsObject): Unit = {
|
||||
out ! jsonObj.toString()
|
||||
}
|
||||
|
||||
def transmitEventToClient(event: SimpleEvent): Unit = {
|
||||
val jsonString = WebsocketEventMapper.toJsonString(event)
|
||||
out ! jsonString
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ package model.users
|
||||
import java.util.UUID
|
||||
|
||||
case class User(
|
||||
internalId: Long,
|
||||
id: UUID,
|
||||
name: String,
|
||||
passwordHash: String
|
||||
internalId: Long,
|
||||
id: UUID,
|
||||
name: String,
|
||||
passwordHash: String
|
||||
) {
|
||||
|
||||
def withName(newName: String): User = {
|
||||
|
||||
Reference in New Issue
Block a user