feat(websocket)!: Implement WebSocket connection and event handling
This commit is contained in:
@@ -11,14 +11,14 @@ import util.GameUtil
|
||||
import javax.inject.Singleton
|
||||
import scala.collection.mutable
|
||||
|
||||
@Singleton
|
||||
class PodManager {
|
||||
object PodManager {
|
||||
|
||||
val TTL: Long = System.currentTimeMillis() + 86400000L // 24 hours in milliseconds
|
||||
val podIp: String = System.getenv("POD_IP")
|
||||
val podName: String = System.getenv("POD_NAME")
|
||||
|
||||
private val sessions: mutable.Map[String, GameLobby] = mutable.Map()
|
||||
private val userSession: mutable.Map[User, String] = mutable.Map()
|
||||
private val injector: Injector = Guice.createInjector(KnockOutWebConfigurationModule())
|
||||
|
||||
def createGame(
|
||||
@@ -35,6 +35,7 @@ class PodManager {
|
||||
host = host
|
||||
)
|
||||
sessions += (gameLobby.id -> gameLobby)
|
||||
userSession += (host -> gameLobby.id)
|
||||
gameLobby
|
||||
}
|
||||
|
||||
@@ -44,6 +45,30 @@ class PodManager {
|
||||
|
||||
private[logic] def removeGame(gameId: String): Unit = {
|
||||
sessions.remove(gameId)
|
||||
// Also remove all user sessions associated with this game
|
||||
userSession.filterInPlace((_, v) => v != gameId)
|
||||
}
|
||||
|
||||
def registerUserToGame(user: User, gameId: String): Boolean = {
|
||||
if (sessions.contains(gameId)) {
|
||||
userSession += (user -> gameId)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
def unregisterUserFromGame(user: User): Unit = {
|
||||
userSession.remove(user)
|
||||
}
|
||||
|
||||
def identifyGameOfUser(user: User): Option[GameLobby] = {
|
||||
userSession.get(user) match {
|
||||
case Some(gameId) => sessions.get(gameId)
|
||||
case None => None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user