Reorganize controllers and apis
parent
b850efda16
commit
59c8081005
|
@ -6,9 +6,9 @@ import DataForm._
|
|||
import play.api._
|
||||
import mvc._
|
||||
|
||||
object Internal extends LilaController {
|
||||
object AppApiC extends LilaController {
|
||||
|
||||
private val api = env.internalApi
|
||||
private val api = env.appApi
|
||||
|
||||
def talk(gameId: String) = Action { implicit request ⇒
|
||||
ValidIOk[TalkData](talkForm)(talk ⇒ api.talk(gameId, talk._1, talk._2))
|
||||
|
@ -46,6 +46,10 @@ object Internal extends LilaController {
|
|||
Ok(api.activity(gameId, color).toString)
|
||||
}
|
||||
|
||||
def lobbyJoin(gameId: String, color: String) = Action {
|
||||
IOk(api.lobbyJoin(gameId, color))
|
||||
}
|
||||
|
||||
def acceptRematch(gameId: String, color: String, newGameId: String) = Action { implicit request ⇒
|
||||
ValidIOk[RematchData](rematchForm)(rematch ⇒
|
||||
api.acceptRematch(gameId, newGameId, color, rematch._1, rematch._2))
|
|
@ -6,24 +6,28 @@ import DataForm._
|
|||
import play.api._
|
||||
import mvc._
|
||||
|
||||
object Application extends LilaController {
|
||||
object AppXhrC extends LilaController {
|
||||
|
||||
private val xhr = env.appXhr
|
||||
private val pinger = env.pinger
|
||||
private val syncer = env.syncer
|
||||
|
||||
def sync(gameId: String, color: String, version: Int, fullId: String) = Action {
|
||||
JsonOk(env.syncer.sync(gameId, color, version, Some(fullId)).unsafePerformIO)
|
||||
JsonOk(syncer.sync(gameId, color, version, Some(fullId)).unsafePerformIO)
|
||||
}
|
||||
|
||||
def syncPublic(gameId: String, color: String, version: Int) = Action {
|
||||
JsonOk(env.syncer.sync(gameId, color, version, None).unsafePerformIO)
|
||||
JsonOk(syncer.sync(gameId, color, version, None).unsafePerformIO)
|
||||
}
|
||||
|
||||
def move(fullId: String) = Action { implicit request ⇒
|
||||
ValidOk(moveForm.bindFromRequest.toValid flatMap { move ⇒
|
||||
env.server.play(fullId, move._1, move._2, move._3).unsafePerformIO
|
||||
xhr.play(fullId, move._1, move._2, move._3).unsafePerformIO
|
||||
})
|
||||
}
|
||||
|
||||
def ping() = Action { implicit request =>
|
||||
JsonOk(env.pinger.ping(
|
||||
JsonOk(pinger.ping(
|
||||
get("username"),
|
||||
get("player_key"),
|
||||
get("watcher"),
|
|
@ -0,0 +1,16 @@
|
|||
package controllers
|
||||
|
||||
import lila.http._
|
||||
import DataForm._
|
||||
|
||||
import play.api._
|
||||
import mvc._
|
||||
|
||||
object LobbyApiC extends LilaController {
|
||||
|
||||
private val api = env.lobbyApi
|
||||
|
||||
def join(gameId: String, color: String) = Action {
|
||||
IOk(api.join(gameId, color))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package controllers
|
||||
|
||||
import lila.http._
|
||||
import DataForm._
|
||||
|
||||
import play.api._
|
||||
import mvc._
|
||||
|
||||
object LobbyXhrC extends LilaController {
|
||||
|
||||
private val xhr = env.lobbyXhr
|
||||
|
||||
def sync() = Action {
|
||||
Ok("")
|
||||
}
|
||||
}
|
44
conf/routes
44
conf/routes
|
@ -2,25 +2,31 @@
|
|||
# This file defines all application routes (Higher priority routes first)
|
||||
# ~~~~
|
||||
|
||||
# Public API
|
||||
POST /move/:fullId controllers.Application.move(fullId: String)
|
||||
GET /sync/:gameId/:color/:version controllers.Application.syncPublic(gameId: String, color: String, version: Int)
|
||||
GET /sync/:gameId/:color/:version/:fullId controllers.Application.sync(gameId: String, color: String, version: Int, fullId: String)
|
||||
GET /ping controllers.Application.ping()
|
||||
GET /how-many-players-now controllers.Application.nbPlayers()
|
||||
# App XHR
|
||||
POST /move/:fullId controllers.AppXhrC.move(fullId: String)
|
||||
GET /sync/:gameId/:color/:version controllers.AppXhrC.syncPublic(gameId: String, color: String, version: Int)
|
||||
GET /sync/:gameId/:color/:version/:fullId controllers.AppXhrC.sync(gameId: String, color: String, version: Int, fullId: String)
|
||||
GET /ping controllers.AppXhrC.ping()
|
||||
GET /how-many-players-now controllers.AppXhrC.nbPlayers()
|
||||
|
||||
# Private API
|
||||
POST /internal/update-version/:gameId controllers.Internal.updateVersion(gameId: String)
|
||||
POST /internal/end/:gameId controllers.Internal.end(gameId: String)
|
||||
POST /internal/talk/:fullId controllers.Internal.talk(fullId: String)
|
||||
POST /internal/join/:fullId controllers.Internal.join(fullId: String)
|
||||
POST /internal/reload-table/:gameId controllers.Internal.reloadTable(gameId: String)
|
||||
POST /internal/accept-rematch/:gameId/:color/:newGameId controllers.Internal.acceptRematch(gameId: String, color: String, newGameId: String)
|
||||
POST /internal/alive/:gameId/:color controllers.Internal.alive(gameId: String, color: String)
|
||||
POST /internal/draw/:gameId/:color controllers.Internal.draw(gameId: String, color: String)
|
||||
POST /internal/draw-accept/:gameId/:color controllers.Internal.drawAccept(gameId: String, color: String)
|
||||
GET /internal/activity/:gameId/:color controllers.Internal.activity(gameId: String, color: String)
|
||||
GET /internal/nb-players controllers.Application.nbPlayers()
|
||||
# App Private API
|
||||
POST /internal/update-version/:gameId controllers.AppApiC.updateVersion(gameId: String)
|
||||
POST /internal/end/:gameId controllers.AppApiC.end(gameId: String)
|
||||
POST /internal/talk/:fullId controllers.AppApiC.talk(fullId: String)
|
||||
POST /internal/join/:fullId controllers.AppApiC.join(fullId: String)
|
||||
POST /internal/reload-table/:gameId controllers.AppApiC.reloadTable(gameId: String)
|
||||
POST /internal/accept-rematch/:gameId/:color/:newGameId controllers.AppApiC.acceptRematch(gameId: String, color: String, newGameId: String)
|
||||
POST /internal/alive/:gameId/:color controllers.AppApiC.alive(gameId: String, color: String)
|
||||
POST /internal/draw/:gameId/:color controllers.AppApiC.draw(gameId: String, color: String)
|
||||
POST /internal/draw-accept/:gameId/:color controllers.AppApiC.drawAccept(gameId: String, color: String)
|
||||
GET /internal/activity/:gameId/:color controllers.AppApiC.activity(gameId: String, color: String)
|
||||
GET /internal/nb-players controllers.AppXhrC.nbPlayers()
|
||||
|
||||
# Map static resources from the /public folder to the /assets URL path
|
||||
# Lobby XHR
|
||||
GET /lobby/sync controllers.LobbyXhrC.sync()
|
||||
|
||||
# Lobby Private API
|
||||
POST /internal/lobby/join/:gameId/:color controllers.LobbyApiC.join(gameId: String, color: String)
|
||||
|
||||
# Useless, but play2 needs it
|
||||
GET /assets/*file controllers.Assets.at(path="/public", file)
|
||||
|
|
|
@ -6,13 +6,20 @@ import db.GameRepo
|
|||
import lila.chess.{ Color, White, Black }
|
||||
import scalaz.effects._
|
||||
|
||||
final class InternalApi(
|
||||
repo: GameRepo,
|
||||
case class AppApi(
|
||||
gameRepo: GameRepo,
|
||||
versionMemo: VersionMemo,
|
||||
aliveMemo: AliveMemo) {
|
||||
aliveMemo: AliveMemo) extends IOTools {
|
||||
|
||||
def lobbyJoin(gameId: String, colorName: String): IO[Unit] = for {
|
||||
color ← ioColor(colorName)
|
||||
g1 ← gameRepo game gameId
|
||||
_ ← aliveMemo.put(gameId, color)
|
||||
_ ← aliveMemo.put(gameId, !color)
|
||||
} yield ()
|
||||
|
||||
def join(fullId: String, url: String, messages: String): IO[Unit] = for {
|
||||
gameAndPlayer ← repo player fullId
|
||||
gameAndPlayer ← gameRepo player fullId
|
||||
(g1, player) = gameAndPlayer
|
||||
g2 = g1 withEvents decodeMessages(messages)
|
||||
g3 = g2.withEvents(g2.opponent(player).color, List(RedirectEvent(url)))
|
||||
|
@ -20,13 +27,13 @@ final class InternalApi(
|
|||
} yield ()
|
||||
|
||||
def talk(gameId: String, author: String, message: String): IO[Unit] = for {
|
||||
g1 ← repo game gameId
|
||||
g1 ← gameRepo game gameId
|
||||
g2 = g1 withEvents List(MessageEvent(author, message))
|
||||
_ ← save(g1, g2)
|
||||
} yield ()
|
||||
|
||||
def end(gameId: String, messages: String): IO[Unit] = for {
|
||||
g1 ← repo game gameId
|
||||
g1 ← gameRepo game gameId
|
||||
g2 = g1 withEvents (EndEvent() :: decodeMessages(messages))
|
||||
_ ← save(g1, g2)
|
||||
} yield ()
|
||||
|
@ -38,7 +45,7 @@ final class InternalApi(
|
|||
whiteRedirect: String,
|
||||
blackRedirect: String): IO[Unit] = for {
|
||||
color ← ioColor(colorName)
|
||||
g1 ← repo game gameId
|
||||
g1 ← gameRepo game gameId
|
||||
g2 = g1.withEvents(
|
||||
List(RedirectEvent(whiteRedirect)),
|
||||
List(RedirectEvent(blackRedirect)))
|
||||
|
@ -48,10 +55,10 @@ final class InternalApi(
|
|||
} yield ()
|
||||
|
||||
def updateVersion(gameId: String): IO[Unit] =
|
||||
repo game gameId flatMap versionMemo.put
|
||||
gameRepo game gameId flatMap versionMemo.put
|
||||
|
||||
def reloadTable(gameId: String): IO[Unit] = for {
|
||||
g1 ← repo game gameId
|
||||
g1 ← gameRepo game gameId
|
||||
g2 = g1 withEvents List(ReloadTableEvent())
|
||||
_ ← save(g1, g2)
|
||||
} yield ()
|
||||
|
@ -63,7 +70,7 @@ final class InternalApi(
|
|||
|
||||
def draw(gameId: String, colorName: String, messages: String): IO[Unit] = for {
|
||||
color ← ioColor(colorName)
|
||||
g1 ← repo game gameId
|
||||
g1 ← gameRepo game gameId
|
||||
g2 = g1 withEvents decodeMessages(messages)
|
||||
g3 = g2.withEvents(!color, List(ReloadTableEvent()))
|
||||
_ ← save(g1, g3)
|
||||
|
@ -71,7 +78,7 @@ final class InternalApi(
|
|||
|
||||
def drawAccept(gameId: String, colorName: String, messages: String): IO[Unit] = for {
|
||||
color ← ioColor(colorName)
|
||||
g1 ← repo game gameId
|
||||
g1 ← gameRepo game gameId
|
||||
g2 = g1 withEvents (EndEvent() :: decodeMessages(messages))
|
||||
_ ← save(g1, g2)
|
||||
} yield ()
|
||||
|
@ -79,15 +86,6 @@ final class InternalApi(
|
|||
def activity(gameId: String, colorName: String): Int =
|
||||
Color(colorName) some { aliveMemo.activity(gameId, _) } none 0
|
||||
|
||||
private def ioColor(colorName: String): IO[Color] = io {
|
||||
Color(colorName) err "Invalid color"
|
||||
}
|
||||
|
||||
private def save(g1: DbGame, g2: DbGame): IO[Unit] = for {
|
||||
_ ← repo.applyDiff(g1, g2)
|
||||
_ ← versionMemo put g2
|
||||
} yield ()
|
||||
|
||||
private def decodeMessages(messages: String): List[MessageEvent] =
|
||||
(messages split '$').toList map { MessageEvent("system", _) }
|
||||
}
|
|
@ -7,8 +7,8 @@ import lila.chess._
|
|||
import Pos.posAt
|
||||
import scalaz.effects._
|
||||
|
||||
final class Server(
|
||||
repo: GameRepo,
|
||||
final class AppXhr(
|
||||
gameRepo: GameRepo,
|
||||
ai: Ai,
|
||||
versionMemo: VersionMemo,
|
||||
aliveMemo: AliveMemo) {
|
||||
|
@ -27,11 +27,11 @@ final class Server(
|
|||
fromString: String,
|
||||
toString: String,
|
||||
promString: Option[String] = None): IO[Valid[Unit]] =
|
||||
repo player fullId flatMap {
|
||||
gameRepo player fullId flatMap {
|
||||
case (g1, player) ⇒ purePlay(g1, fromString, toString, promString).fold(
|
||||
e ⇒ io(failure(e)),
|
||||
g2 ⇒ for {
|
||||
_ ← repo.applyDiff(g1, g2)
|
||||
_ ← gameRepo.applyDiff(g1, g2)
|
||||
_ ← versionMemo put g2
|
||||
_ ← aliveMemo.put(g2.id, player.color)
|
||||
} yield success(Unit)
|
|
@ -0,0 +1,22 @@
|
|||
package lila.system
|
||||
|
||||
import db.GameRepo
|
||||
import memo.VersionMemo
|
||||
import model._
|
||||
import lila.chess.Color
|
||||
import scalaz.effects._
|
||||
|
||||
trait IOTools {
|
||||
|
||||
val gameRepo: GameRepo
|
||||
val versionMemo: VersionMemo
|
||||
|
||||
def ioColor(colorName: String): IO[Color] = io {
|
||||
Color(colorName) err "Invalid color"
|
||||
}
|
||||
|
||||
def save(g1: DbGame, g2: DbGame): IO[Unit] = for {
|
||||
_ ← gameRepo.applyDiff(g1, g2)
|
||||
_ ← versionMemo put g2
|
||||
} yield ()
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package lila.system
|
||||
|
||||
import model._
|
||||
import memo._
|
||||
import db.GameRepo
|
||||
import lila.chess.{ Color, White, Black }
|
||||
import scalaz.effects._
|
||||
|
||||
case class LobbyApi(
|
||||
gameRepo: GameRepo,
|
||||
versionMemo: VersionMemo,
|
||||
aliveMemo: AliveMemo) extends IOTools {
|
||||
|
||||
def join(gameId: String, colorName: String): IO[Unit] = for {
|
||||
color ← ioColor(colorName)
|
||||
g1 ← gameRepo game gameId
|
||||
_ ← aliveMemo.put(gameId, color)
|
||||
_ ← aliveMemo.put(gameId, !color)
|
||||
} yield ()
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package lila.system
|
||||
|
||||
import model._
|
||||
import memo._
|
||||
import db.GameRepo
|
||||
import lila.chess.{ Color, White, Black }
|
||||
import scalaz.effects._
|
||||
|
||||
final class LobbyXhr(
|
||||
gameRepo: GameRepo,
|
||||
versionMemo: VersionMemo,
|
||||
aliveMemo: AliveMemo) {
|
||||
}
|
|
@ -10,7 +10,7 @@ import scala.math.max
|
|||
import org.apache.commons.lang3.StringEscapeUtils.escapeXml
|
||||
|
||||
final class Syncer(
|
||||
repo: GameRepo,
|
||||
gameRepo: GameRepo,
|
||||
versionMemo: VersionMemo,
|
||||
aliveMemo: AliveMemo,
|
||||
duration: Int,
|
||||
|
@ -24,7 +24,7 @@ final class Syncer(
|
|||
for {
|
||||
color ← io { Color(colorString) err "Invalid color" }
|
||||
_ ← io { versionWait(gameId, color, version) }
|
||||
gameAndPlayer ← repo.player(gameId, color)
|
||||
gameAndPlayer ← gameRepo.player(gameId, color)
|
||||
(game, player) = gameAndPlayer
|
||||
isPrivate = fullId some { game.isPlayerFullId(player, _) } none false
|
||||
_ ← versionMemo put game
|
||||
|
|
|
@ -9,19 +9,29 @@ import memo._
|
|||
|
||||
final class SystemEnv(config: Config) {
|
||||
|
||||
lazy val server = new Server(
|
||||
repo = gameRepo,
|
||||
lazy val appXhr = new AppXhr(
|
||||
gameRepo = gameRepo,
|
||||
ai = ai,
|
||||
versionMemo = versionMemo,
|
||||
aliveMemo = aliveMemo)
|
||||
|
||||
lazy val internalApi = new InternalApi(
|
||||
repo = gameRepo,
|
||||
lazy val appApi = new AppApi(
|
||||
gameRepo = gameRepo,
|
||||
versionMemo = versionMemo,
|
||||
aliveMemo = aliveMemo)
|
||||
|
||||
lazy val lobbyXhr = new LobbyXhr(
|
||||
gameRepo = gameRepo,
|
||||
versionMemo = versionMemo,
|
||||
aliveMemo = aliveMemo)
|
||||
|
||||
lazy val lobbyApi = new LobbyApi(
|
||||
gameRepo = gameRepo,
|
||||
versionMemo = versionMemo,
|
||||
aliveMemo = aliveMemo)
|
||||
|
||||
lazy val syncer = new Syncer(
|
||||
repo = gameRepo,
|
||||
gameRepo = gameRepo,
|
||||
versionMemo = versionMemo,
|
||||
aliveMemo = aliveMemo,
|
||||
duration = getMilliseconds("sync.duration"),
|
||||
|
|
Loading…
Reference in New Issue