Better round sockets
parent
502235f08a
commit
8c3705a7d1
|
@ -28,18 +28,6 @@ object App extends LilaController {
|
||||||
username = get("username"))
|
username = get("username"))
|
||||||
}
|
}
|
||||||
|
|
||||||
def gameSocket(gameId: String, color: String) =
|
|
||||||
WebSocket.async[JsValue] { implicit req ⇒
|
|
||||||
implicit val ctx = Context(req, None)
|
|
||||||
env.round.socket.join(
|
|
||||||
uidOption = get("uid"),
|
|
||||||
username = get("username"),
|
|
||||||
gameId = gameId,
|
|
||||||
colorName = color,
|
|
||||||
versionOption = getInt("version"),
|
|
||||||
playerId = get("playerId")).unsafePerformIO
|
|
||||||
}
|
|
||||||
|
|
||||||
def abort(fullId: String) = performAndRedirect(fullId, hand.abort)
|
def abort(fullId: String) = performAndRedirect(fullId, hand.abort)
|
||||||
|
|
||||||
def resign(fullId: String) = performAndRedirect(fullId, hand.resign)
|
def resign(fullId: String) = performAndRedirect(fullId, hand.resign)
|
||||||
|
|
|
@ -2,14 +2,35 @@ package controllers
|
||||||
|
|
||||||
import lila._
|
import lila._
|
||||||
import views._
|
import views._
|
||||||
|
import http.Context
|
||||||
|
import game.Pov
|
||||||
|
import socket.Util.connectionFail
|
||||||
|
|
||||||
import play.api.mvc._
|
import play.api.mvc._
|
||||||
|
import play.api.libs.json._
|
||||||
|
import play.api.libs.iteratee._
|
||||||
|
|
||||||
object Round extends LilaController {
|
object Round extends LilaController {
|
||||||
|
|
||||||
val gameRepo = env.game.gameRepo
|
val gameRepo = env.game.gameRepo
|
||||||
val socket = env.round.socket
|
val socket = env.round.socket
|
||||||
|
|
||||||
|
def websocketWatcher(gameId: String, color: String) =
|
||||||
|
WebSocket.async[JsValue] { req ⇒
|
||||||
|
implicit val ctx = reqToCtx(req)
|
||||||
|
socket.joinWatcher(
|
||||||
|
gameId, color, getInt("version"), get("uid"), get("username")
|
||||||
|
).unsafePerformIO
|
||||||
|
}
|
||||||
|
|
||||||
|
def websocketPlayer(fullId: String) =
|
||||||
|
WebSocket.async[JsValue] { req ⇒
|
||||||
|
implicit val ctx = reqToCtx(req)
|
||||||
|
socket.joinPlayer(
|
||||||
|
fullId, getInt("version"), get("uid"), get("username")
|
||||||
|
).unsafePerformIO
|
||||||
|
}
|
||||||
|
|
||||||
def watcher(gameId: String, color: String) = Open { implicit ctx ⇒
|
def watcher(gameId: String, color: String) = Open { implicit ctx ⇒
|
||||||
IOption(gameRepo.pov(gameId, color)) { pov ⇒
|
IOption(gameRepo.pov(gameId, color)) { pov ⇒
|
||||||
html.round.watcher(pov, version(pov.gameId))
|
html.round.watcher(pov, version(pov.gameId))
|
||||||
|
|
|
@ -35,7 +35,8 @@ final class RoundEnv(
|
||||||
)), name = ActorGameHubMaster)
|
)), name = ActorGameHubMaster)
|
||||||
|
|
||||||
lazy val socket = new Socket(
|
lazy val socket = new Socket(
|
||||||
getGame = gameRepo.game,
|
getWatcherPov = gameRepo.pov,
|
||||||
|
getPlayerPov = gameRepo.pov,
|
||||||
hand = hand,
|
hand = hand,
|
||||||
hubMaster = hubMaster,
|
hubMaster = hubMaster,
|
||||||
messenger = messenger)
|
messenger = messenger)
|
||||||
|
|
|
@ -14,13 +14,15 @@ import play.api.Play.current
|
||||||
|
|
||||||
import scalaz.effects._
|
import scalaz.effects._
|
||||||
|
|
||||||
import game.{ DbGame, PovRef }
|
import game.{ Pov, PovRef }
|
||||||
import chess.Color
|
import chess.Color
|
||||||
import socket.{ Util, Ping, Quit }
|
import socket.{ Ping, Quit }
|
||||||
|
import socket.Util.connectionFail
|
||||||
import implicits.RichJs._
|
import implicits.RichJs._
|
||||||
|
|
||||||
final class Socket(
|
final class Socket(
|
||||||
getGame: String ⇒ IO[Option[DbGame]],
|
getWatcherPov: (String, String) ⇒ IO[Option[Pov]],
|
||||||
|
getPlayerPov: String ⇒ IO[Option[Pov]],
|
||||||
hand: Hand,
|
hand: Hand,
|
||||||
val hubMaster: ActorRef,
|
val hubMaster: ActorRef,
|
||||||
messenger: Messenger) {
|
messenger: Messenger) {
|
||||||
|
@ -77,37 +79,48 @@ final class Socket(
|
||||||
case _ ⇒
|
case _ ⇒
|
||||||
}
|
}
|
||||||
|
|
||||||
def join(
|
def joinWatcher(
|
||||||
uidOption: Option[String],
|
|
||||||
username: Option[String],
|
|
||||||
gameId: String,
|
gameId: String,
|
||||||
colorName: String,
|
colorName: String,
|
||||||
|
version: Option[Int],
|
||||||
|
uid: Option[String],
|
||||||
|
username: Option[String]): IO[SocketPromise] = getWatcherPov(gameId, colorName) map {
|
||||||
|
join(_, false, version, uid, username)
|
||||||
|
}
|
||||||
|
|
||||||
|
def joinPlayer(
|
||||||
|
fullId: String,
|
||||||
|
version: Option[Int],
|
||||||
|
uid: Option[String],
|
||||||
|
username: Option[String]): IO[SocketPromise] = getPlayerPov(fullId) map {
|
||||||
|
join(_, true, version, uid, username)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def join(
|
||||||
|
povOption: Option[Pov],
|
||||||
|
owner: Boolean,
|
||||||
versionOption: Option[Int],
|
versionOption: Option[Int],
|
||||||
playerId: Option[String]): IO[SocketPromise] =
|
uidOption: Option[String],
|
||||||
getGame(gameId) map { gameOption ⇒
|
username: Option[String]): SocketPromise =
|
||||||
val promise: Option[SocketPromise] = for {
|
((povOption |@| uidOption |@| versionOption) apply {
|
||||||
game ← gameOption
|
(pov: Pov, uid: String, version: Int) ⇒
|
||||||
color ← Color(colorName)
|
(for {
|
||||||
version ← versionOption
|
hub ← hubMaster ? GetHub(pov.gameId) mapTo manifest[ActorRef]
|
||||||
uid ← uidOption
|
socket ← hub ? Join(
|
||||||
} yield (for {
|
uid = uid,
|
||||||
hub ← hubMaster ? GetHub(gameId) mapTo manifest[ActorRef]
|
username = username,
|
||||||
socket ← hub ? Join(
|
version = version,
|
||||||
uid = uid,
|
color = pov.color,
|
||||||
username = username,
|
owner = owner
|
||||||
version = version,
|
) map {
|
||||||
color = color,
|
case Connected(member) ⇒ (
|
||||||
owner = (playerId flatMap game.player).isDefined
|
Iteratee.foreach[JsValue](
|
||||||
) map {
|
controller(hub, uid, member, PovRef(pov.gameId, member.color))
|
||||||
case Connected(member) ⇒ (
|
) mapDone { _ ⇒
|
||||||
Iteratee.foreach[JsValue](
|
hub ! Quit(uid)
|
||||||
controller(hub, uid, member, PovRef(gameId, member.color))
|
},
|
||||||
) mapDone { _ ⇒
|
member.channel)
|
||||||
hub ! Quit(uid)
|
}
|
||||||
},
|
} yield socket).asPromise: SocketPromise
|
||||||
member.channel)
|
}) | connectionFail
|
||||||
}
|
|
||||||
} yield socket).asPromise
|
|
||||||
promise | Util.connectionFail
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
@round.layout(title = title, goodies = goodies) {
|
@round.layout(title = title, goodies = goodies) {
|
||||||
<div class="lichess_game clearfix lichess_player_@color not_spectator"
|
<div class="lichess_game clearfix lichess_player_@color not_spectator"
|
||||||
|
data-socket-url="@routes.Round.websocketPlayer(fullId)"
|
||||||
data-table-url="@routes.Round.table(gameId, color.name, fullId)"
|
data-table-url="@routes.Round.table(gameId, color.name, fullId)"
|
||||||
data-players-url="@routes.Round.players(gameId)">
|
data-players-url="@routes.Round.players(gameId)">
|
||||||
<div class="lichess_board_wrap">
|
<div class="lichess_board_wrap">
|
||||||
|
|
|
@ -45,6 +45,7 @@
|
||||||
|
|
||||||
@round.layout(title = title, goodies = goodies) {
|
@round.layout(title = title, goodies = goodies) {
|
||||||
<div class="lichess_game clearfix lichess_player_@color"
|
<div class="lichess_game clearfix lichess_player_@color"
|
||||||
|
data-socket-url="@routes.Round.websocketWatcher(gameId, color.name)"
|
||||||
data-table-url="@routes.Round.table(gameId, color.name, fullId)"
|
data-table-url="@routes.Round.table(gameId, color.name, fullId)"
|
||||||
data-players-url="@routes.Round.players(gameId)">
|
data-players-url="@routes.Round.players(gameId)">
|
||||||
<div class="lichess_board_wrap">
|
<div class="lichess_board_wrap">
|
||||||
|
|
|
@ -5,6 +5,8 @@ GET /games/all controllers.Game.all(page: Int ?= 1)
|
||||||
GET /games/checkmate controllers.Game.checkmate(page: Int ?= 1)
|
GET /games/checkmate controllers.Game.checkmate(page: Int ?= 1)
|
||||||
|
|
||||||
# Round
|
# Round
|
||||||
|
GET /socket/$gameId<[\w\-]{8}>/$color<white|black> controllers.Round.websocketWatcher(gameId: String, color: String)
|
||||||
|
GET /socket/$fullId<[\w\-]{12}> controllers.Round.websocketPlayer(fullId: String)
|
||||||
GET /$gameId<[\w\-]{8}> controllers.Round.watcher(gameId: String, color: String = "white")
|
GET /$gameId<[\w\-]{8}> controllers.Round.watcher(gameId: String, color: String = "white")
|
||||||
GET /$gameId<[\w\-]{8}>/$color<white|black> controllers.Round.watcher(gameId: String, color: String)
|
GET /$gameId<[\w\-]{8}>/$color<white|black> controllers.Round.watcher(gameId: String, color: String)
|
||||||
GET /$fullId<[\w\-]{12}> controllers.Round.player(fullId: String)
|
GET /$fullId<[\w\-]{12}> controllers.Round.player(fullId: String)
|
||||||
|
@ -58,7 +60,6 @@ GET /wiki controllers.Wiki.home
|
||||||
|
|
||||||
# App Public API
|
# App Public API
|
||||||
GET /socket controllers.App.socket
|
GET /socket controllers.App.socket
|
||||||
GET /socket/:gameId/:color controllers.App.gameSocket(gameId: String, color: String)
|
|
||||||
|
|
||||||
GET /ai controllers.Ai.run
|
GET /ai controllers.Ai.run
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue