better FEN setup checks
This commit is contained in:
parent
f272aa01d2
commit
02717c649e
|
@ -144,7 +144,7 @@ object Setup extends LilaController with TheftPrevention {
|
|||
OpenBody { ctx ⇒
|
||||
implicit val req = ctx.body
|
||||
FuRedirect(form(ctx).bindFromRequest.fold(
|
||||
f ⇒ fulogwarn(f.errors.toString) inject routes.Lobby.home,
|
||||
f ⇒ fuloginfo(f.errors.toString) inject routes.Lobby.home,
|
||||
config ⇒ op(config)(ctx)
|
||||
))
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ case class AiConfig(
|
|||
color: Color,
|
||||
fen: Option[String] = None) extends Config with GameGenerator with Positional {
|
||||
|
||||
val strictFen = true
|
||||
|
||||
def >> = (variant.id, clock, time, increment, level, color.name, fen).some
|
||||
|
||||
def game = fenGame { chessGame ⇒
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package lila.setup
|
||||
|
||||
import chess.format.Forsyth
|
||||
import chess.{ Game => ChessGame, Board, Situation, Variant, Clock, Speed }
|
||||
import chess.{ Game ⇒ ChessGame, Board, Situation, Variant, Clock, Speed }
|
||||
|
||||
import lila.game.{ GameRepo, Game, Pov }
|
||||
import lila.lobby.Color
|
||||
|
||||
|
@ -44,6 +45,12 @@ trait Positional { self: Config ⇒
|
|||
|
||||
def fen: Option[String]
|
||||
|
||||
def strictFen: Boolean
|
||||
|
||||
lazy val validFen = variant != Variant.FromPosition || {
|
||||
fen ?? { f ⇒ ~(Forsyth <<< f).map(_.situation playable strictFen) }
|
||||
}
|
||||
|
||||
def fenGame(builder: ChessGame ⇒ Game): Game = {
|
||||
val state = fen filter (_ ⇒ variant == Variant.FromPosition) flatMap Forsyth.<<<
|
||||
val chessGame = state.fold(makeGame) {
|
||||
|
|
|
@ -42,8 +42,9 @@ private[setup] final class FormFactory {
|
|||
"increment" -> increment,
|
||||
"level" -> level,
|
||||
"color" -> color,
|
||||
"fen" -> fen(true)
|
||||
"fen" -> fen
|
||||
)(AiConfig.<<)(_.>>)
|
||||
.verifying("Invalid FEN", _.validFen)
|
||||
)
|
||||
|
||||
def aiConfig(implicit ctx: Context): Fu[AiConfig] = savedConfig map (_.ai)
|
||||
|
@ -63,8 +64,10 @@ private[setup] final class FormFactory {
|
|||
"increment" -> increment,
|
||||
"mode" -> mode(ctx.isAuth),
|
||||
"color" -> color,
|
||||
"fen" -> fen(false)
|
||||
)(FriendConfig.<<)(_.>>) verifying ("Invalid clock", _.validClock)
|
||||
"fen" -> fen
|
||||
)(FriendConfig.<<)(_.>>)
|
||||
.verifying("Invalid clock", _.validClock)
|
||||
.verifying("Invalid FEN", _.validFen)
|
||||
)
|
||||
|
||||
def friendConfig(implicit ctx: Context): Fu[FriendConfig] = savedConfig map (_.friend)
|
||||
|
|
|
@ -14,6 +14,8 @@ case class FriendConfig(
|
|||
color: Color,
|
||||
fen: Option[String] = None) extends HumanConfig with GameGenerator with Positional {
|
||||
|
||||
val strictFen = false
|
||||
|
||||
def >> = (variant.id, clock, time, increment, mode.id.some, color.name, fen).some
|
||||
|
||||
def game = fenGame { chessGame ⇒
|
||||
|
|
|
@ -21,8 +21,5 @@ object Mappings {
|
|||
val color = nonEmptyText.verifying(Color.names contains _)
|
||||
val level = number.verifying(AiConfig.levels contains _)
|
||||
val speed = number.verifying(Config.speeds contains _)
|
||||
|
||||
def fen(strict: Boolean) = optional {
|
||||
nonEmptyText verifying { source ⇒ ~(Forsyth <<< source).map(_.situation playable strict) }
|
||||
}
|
||||
val fen = optional(nonEmptyText)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue