Improve ai reverse engineering error reporting
This commit is contained in:
parent
5789af91e5
commit
cbe832bcf8
|
@ -31,7 +31,7 @@ final class Cron(env: SystemEnv)(implicit app: Application) {
|
|||
|
||||
spawn("game_auto_finish") { env ⇒
|
||||
env.gameRepo.candidatesToAutofinish flatMap { games ⇒
|
||||
env.finisher outoftimes games.pp
|
||||
env.finisher outoftimes games
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,29 +7,33 @@ final class ReverseEngineering(fromGame: Game, to: Board) {
|
|||
|
||||
val from = fromGame.board
|
||||
|
||||
def move: Option[(Pos, Pos)] =
|
||||
if (from.pieces == to.pieces) None else findMove
|
||||
def move: Valid[(Pos, Pos)] = {
|
||||
if (from.pieces == to.pieces) !!("from and to are similar") else findMove
|
||||
} mapFail { msgs ⇒
|
||||
("ReverseEngineering failure: " + msgs.list mkString "\n").wrapNel
|
||||
}
|
||||
|
||||
private def findMove: Option[(Pos, Pos)] =
|
||||
private def findMove: Valid[(Pos, Pos)] =
|
||||
findMovedPieces match {
|
||||
case List((pos, piece)) ⇒ findPieceNewPos(pos, piece) map { np ⇒ (pos, np) }
|
||||
case List((pos1, piece1), (pos2, piece2)) if (piece1 is King) && (piece2 is Rook) ⇒
|
||||
findCastle(pos2) map { np ⇒ (pos1, np) }
|
||||
case List((pos1, piece1), (pos2, piece2)) if (piece1 is Rook) && (piece2 is King) ⇒
|
||||
findCastle(pos1) map { np ⇒ (pos2, np) }
|
||||
case _ ⇒ None
|
||||
case List((pos1, piece1), (pos2, piece2)) ⇒ !!("two moved pieces, but not a castle")
|
||||
case Nil ⇒ !!("no moved piece found")
|
||||
}
|
||||
|
||||
private def findCastle(rookPos: Pos): Option[Pos] = rookPos.x match {
|
||||
case 1 ⇒ posAt(3, rookPos.y)
|
||||
case 8 ⇒ posAt(7, rookPos.y)
|
||||
case _ ⇒ None
|
||||
private def findCastle(rookPos: Pos): Valid[Pos] = rookPos.x match {
|
||||
case 1 ⇒ posAt(3, rookPos.y) toSuccess "invalid rook pos".wrapNel
|
||||
case 8 ⇒ posAt(7, rookPos.y) toSuccess "invalid rook pos".wrapNel
|
||||
case _ ⇒ !!("fail to find castle, rook on " + rookPos)
|
||||
}
|
||||
|
||||
private def findPieceNewPos(pos: Pos, piece: Piece): Option[Pos] = for {
|
||||
private def findPieceNewPos(pos: Pos, piece: Piece): Valid[Pos] = (for {
|
||||
dests ← fromGame.situation.destinations get pos
|
||||
dest ← dests find { to(_) map (_ is piece.color) getOrElse false }
|
||||
} yield dest
|
||||
} yield dest) toSuccess "can not find piece new pos".wrapNel
|
||||
|
||||
private def findMovedPieces: List[(Pos, Piece)] = {
|
||||
|
||||
|
@ -43,4 +47,6 @@ final class ReverseEngineering(fromGame: Game, to: Board) {
|
|||
case (pos, piece, Some(newPiece)) if piece != newPiece ⇒ pos -> piece
|
||||
} toList
|
||||
}
|
||||
|
||||
private def !!(msg: String) = failure(msg.wrapNel)
|
||||
}
|
||||
|
|
|
@ -57,8 +57,6 @@ final class Finisher(
|
|||
case _ ⇒ success(io())
|
||||
}) | io()
|
||||
|
||||
private def !!(msg: String) = failure(msg.wrapNel)
|
||||
|
||||
private def finish(
|
||||
game: DbGame,
|
||||
status: Status,
|
||||
|
@ -104,4 +102,6 @@ final class Finisher(
|
|||
_ ← historyRepo.addEntry(blackUser.username, blackElo, game.id)
|
||||
} yield ()
|
||||
} | io()
|
||||
|
||||
private def !!(msg: String) = failure(msg.wrapNel)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue