lila/modules/simul/src/main/SimulSocket.scala

60 lines
1.9 KiB
Scala

package lila.simul
import akka.actor._
import play.api.libs.json._
import scala.concurrent.duration._
import actorApi._
import lila.game.{ Game, Pov }
import lila.room.RoomSocket.{ Protocol => RP, _ }
import lila.socket.RemoteSocket.{ Protocol => P, _ }
import lila.socket.Socket.makeMessage
private final class SimulSocket(
getSimul: Simul.ID => Fu[Option[Simul]],
jsonView: JsonView,
remoteSocketApi: lila.socket.RemoteSocket,
chat: ActorSelection,
system: ActorSystem
) {
def hostIsOn(simulId: Simul.ID, gameId: Game.ID): Unit =
rooms.tell(simulId, NotifyVersion("hostGame", gameId))
def reload(simulId: Simul.ID): Unit =
getSimul(simulId) foreach {
_ foreach { simul =>
jsonView(simul, none) foreach { obj =>
rooms.tell(simulId, NotifyVersion("reload", obj))
}
}
}
def aborted(simulId: Simul.ID): Unit =
rooms.tell(simulId, NotifyVersion("aborted", Json.obj()))
def startSimul(simul: Simul, firstGame: Game): Unit =
firstGame.playerByUserId(simul.hostId) foreach { player =>
redirectPlayer(simul, Pov(firstGame, player))
}
def startGame(simul: Simul, game: Game): Unit =
game.playerByUserId(simul.hostId) foreach { opponent =>
redirectPlayer(simul, Pov(game, !opponent.color))
}
private def redirectPlayer(simul: Simul, pov: Pov): Unit =
pov.player.userId foreach { userId =>
send(RP.Out.tellRoomUser(RoomId(simul.id), userId, makeMessage("redirect", pov.fullId)))
}
lazy val rooms = makeRoomMap(send, system.lilaBus)
private lazy val handler: Handler = roomHandler(rooms, chat,
roomId => lila.hub.actorApi.shutup.PublicSource.Simul(roomId.value).some)
private lazy val send: String => Unit = remoteSocketApi.makeSender("simul-out").apply _
remoteSocketApi.subscribe("simul-in", P.In.baseReader)(handler orElse remoteSocketApi.baseHandler)
}