Handle rematch

pull/1/merge
Thibault Duplessis 2012-03-18 14:20:38 +01:00
parent c3813f3359
commit 5f9411fbf2
5 changed files with 47 additions and 8 deletions

View File

@ -25,6 +25,12 @@ object DataForm {
"messages" -> nonEmptyText
))
type RematchData = (String, String)
val rematchForm = Form(tuple(
"whiteRedirect" -> nonEmptyText,
"blackRedirect" -> nonEmptyText
))
type EndData = String
val endForm = Form(single(
"messages" -> nonEmptyText

View File

@ -18,6 +18,10 @@ object Internal extends LilaController {
IOk(api updateVersion gameId)
}
def reloadTable(gameId: String) = Action {
IOk(api reloadTable gameId)
}
def end(gameId: String) = Action { implicit request
ValidIOk[String](endForm)(msgs api.end(gameId, msgs))
}
@ -25,4 +29,9 @@ object Internal extends LilaController {
def join(fullId: String) = Action { implicit request
ValidIOk[JoinData](joinForm)(join api.join(fullId, join._1, join._2))
}
def acceptRematch(gameId: String) = Action { implicit request
ValidIOk[RematchData](rematchForm)(rematch
api.acceptRematch(gameId, rematch._1, rematch._2))
}
}

View File

@ -9,6 +9,8 @@ POST /internal/update-version/:gameId controllers.Internal.updateVersion(gameI
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 controllers.Internal.acceptRematch(gameId: String)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)

View File

@ -2,6 +2,7 @@ package lila.system
import model._
import memo._
import lila.chess.{ White, Black }
import scalaz.effects._
final class InternalApi(repo: GameRepo, versionMemo: VersionMemo) {
@ -11,27 +12,43 @@ final class InternalApi(repo: GameRepo, versionMemo: VersionMemo) {
(g1, player) = gameAndPlayer
g2 = g1 withEvents decodeMessages(messages)
g3 = g2.withEvents(g2.opponent(player).color, List(RedirectEvent(url)))
_ repo.applyDiff(g1, g3)
_ versionMemo put g3
_ save(g1, g3)
} yield ()
def talk(gameId: String, author: String, message: String): IO[Unit] = for {
g1 repo game gameId
g2 = g1 withEvents List(MessageEvent(author, message))
_ repo.applyDiff(g1, g2)
_ versionMemo put g2
_ save(g1, g2)
} yield ()
def end(gameId: String, messages: String): IO[Unit] = for {
g1 repo game gameId
g2 = g1 withEvents (EndEvent() :: decodeMessages(messages))
_ repo.applyDiff(g1, g2)
_ versionMemo put g2
_ save(g1, g2)
} yield ()
def acceptRematch(gameId: String, whiteRedirect: String, blackRedirect: String): IO[Unit] = for {
g1 repo game gameId
g2 = g1.withEvents(
List(RedirectEvent(whiteRedirect)),
List(RedirectEvent(blackRedirect)))
_ save(g1, g2)
} yield ()
def updateVersion(gameId: String): IO[Unit] =
repo game gameId flatMap versionMemo.put
def reloadTable(gameId: String): IO[Unit] = for {
g1 repo game gameId
g2 = g1 withEvents List(ReloadTableEvent())
_ save(g1, g2)
} yield ()
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", _) }
}

View File

@ -124,10 +124,15 @@ case class DbGame(
)
def withEvents(color: Color, events: List[Event]): DbGame = color match {
case White copy(whitePlayer = whitePlayer withEvents events)
case Black copy(blackPlayer = blackPlayer withEvents events)
case White withEvents(events, Nil)
case Black withEvents(Nil, events)
}
def withEvents(whiteEvents: List[Event], blackEvents: List[Event]): DbGame =
copy(
whitePlayer = whitePlayer withEvents whiteEvents,
blackPlayer = blackPlayer withEvents blackEvents)
def playable = status < Aborted
def aiLevel: Option[Int] = players find (_.isAi) flatMap (_.aiLevel)