Better move request implementation

This commit is contained in:
Thibault Duplessis 2012-03-16 15:31:26 +01:00
parent 23d61a5d1a
commit bee62af887
5 changed files with 28 additions and 24 deletions

14
app/DataForm.scala Normal file
View file

@ -0,0 +1,14 @@
package lila.http
import play.api.data._
import play.api.data.Forms._
object DataForm {
val moveForm = Form(tuple(
"from" -> text,
"to" -> text,
"promotion" -> optional(text),
"b" -> optional(number)
))
}

View file

@ -1,12 +0,0 @@
package lila.http
import play.api.data._
import play.api.data.Forms._
object LilaForm {
val move = Form(tuple(
"from" -> text,
"to" -> text
))
}

View file

@ -1,6 +1,7 @@
package controllers
import lila.http._
import DataForm._
import play.api._
import play.api.mvc._
@ -10,12 +11,11 @@ object Application extends Controller {
val env = new HttpEnv(Play.unsafeApplication.configuration.underlying)
def move(fullId: String) = Action { implicit request
(for {
move LilaForm.move.bindFromRequest.value toValid "Invalid move"
_ env.server.play(fullId, move).unsafePerformIO
} yield ()).fold(
(moveForm.bindFromRequest.value toValid "Invalid move" flatMap { move =>
env.server.play(fullId, move._1, move._2, move._3).unsafePerformIO
}).fold(
e BadRequest(e.list mkString "\n"),
a Ok("ok")
_ Ok("ok")
)
}

1
bc
View file

@ -1,2 +1,3 @@
Game.lastMove (and no more in pieces notation)
clock time in milliseconds (and no more floating seconds)
request promotion is not wrapped in options anymore

View file

@ -13,15 +13,16 @@ final class Server(repo: GameRepo, ai: Ai) {
promString: Option[String] = None): IO[Valid[Map[Pos, List[Pos]]]] =
(decodeMoveString(moveString) toValid "Wrong move").fold(
e io(failure(e)),
move play(fullId, move, promString)
move play(fullId, move._1, move._2, promString)
)
def play(
fullId: String,
moveString: (String, String),
fromString: String,
toString: String,
promString: Option[String] = None): IO[Valid[Map[Pos, List[Pos]]]] =
repo playerGame fullId flatMap { game
doPlay(game, fullId, moveString, promString).fold(
doPlay(game, fromString, toString, promString).fold(
e io(failure(e)),
a repo save a map { _ success(a.toChess.situation.destinations) }
)
@ -29,12 +30,12 @@ final class Server(repo: GameRepo, ai: Ai) {
def doPlay(
g1: DbGame,
fullId: String,
moveString: (String, String),
origString: String,
destString: String,
promString: Option[String]): Valid[DbGame] = for {
g2 if (g1.playable) success(g1) else failure("Game is not playable" wrapNel)
orig posAt(moveString._1) toValid "Wrong orig " + moveString
dest posAt(moveString._2) toValid "Wrong dest " + moveString
orig posAt(origString) toValid "Wrong orig " + origString
dest posAt(destString) toValid "Wrong dest " + destString
promotion Role promotable promString toValid "Wrong promotion " + promString
chessGame = g2.toChess
newChessGameAndMove chessGame(orig, dest, promotion)