Better move request implementation
This commit is contained in:
parent
23d61a5d1a
commit
bee62af887
14
app/DataForm.scala
Normal file
14
app/DataForm.scala
Normal 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)
|
||||
))
|
||||
}
|
|
@ -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
|
||||
))
|
||||
}
|
|
@ -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
1
bc
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue