Implement join internal api

This commit is contained in:
Thibault Duplessis 2012-03-18 10:54:27 +01:00
parent 408cf76679
commit bf80c8228f
5 changed files with 29 additions and 0 deletions

View file

@ -16,4 +16,9 @@ object DataForm {
"author" -> nonEmptyText,
"message" -> nonEmptyText
))
val joinForm = Form(tuple(
"redirect" -> nonEmptyText,
"messages" -> nonEmptyText
))
}

View file

@ -23,4 +23,10 @@ object Internal extends LilaController {
def endGame(gameId: String) = Action {
IOk(api endGame gameId)
}
def join(fullId: String) = Action { implicit request
ValidOk(joinForm.bindFromRequest.value toValid "Invalid join" map { join
api.join(fullId, join._1, join._2).unsafePerformIO
})
}
}

View file

@ -8,6 +8,7 @@ GET /sync/:id/:color/:version/:fullId controllers.Application.sync(id: String,
POST /internal/update-version/:gameId controllers.Internal.updateVersion(gameId: String)
POST /internal/end-game/:gameId controllers.Internal.endGame(gameId: String)
POST /internal/talk/:fullId controllers.Internal.talk(fullId: String)
POST /internal/join/:fullId controllers.Internal.join(fullId: String)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)

View file

@ -6,6 +6,16 @@ import scalaz.effects._
final class InternalApi(repo: GameRepo, versionMemo: VersionMemo) {
def join(fullId: String, url: String, messages: String): IO[Unit] = for {
gameAndPlayer repo player fullId
(g1, player) = gameAndPlayer
messageList = (messages split '$').toList
g2 = g1 withEvents (messageList map { MessageEvent("system", _) })
g3 = g2.withEvents(g2.opponent(player).color, List(RedirectEvent(url)))
_ repo.applyDiff(g1, g3)
_ versionMemo put g3
} yield ()
def talk(gameId: String, author: String, message: String): IO[Unit] = for {
g1 repo game gameId
g2 = g1 withEvents List(MessageEvent(author, message))

View file

@ -37,6 +37,8 @@ case class DbGame(
def isPlayerFullId(player: DbPlayer, fullId: String): Boolean =
(fullId.size == DbGame.fullIdSize) && player.id == (fullId drop 8)
def opponent(p: DbPlayer): DbPlayer = player(!(p.color))
def player: DbPlayer = player(if (0 == turns % 2) White else Black)
def fullIdOf(player: DbPlayer): Option[String] =
@ -121,6 +123,11 @@ case class DbGame(
blackPlayer = blackPlayer withEvents events
)
def withEvents(color: Color, events: List[Event]): DbGame = color match {
case White copy(whitePlayer = whitePlayer withEvents events)
case Black copy(blackPlayer = blackPlayer withEvents events)
}
def playable = status < Aborted
def aiLevel: Option[Int] = players find (_.isAi) flatMap (_.aiLevel)