Implement castles conversion and remove game statuses
parent
a322c77131
commit
661f5891c0
|
@ -1,6 +1,7 @@
|
|||
package lila.system
|
||||
|
||||
import ornicar.scalalib.{IntStatus, IntStatuses}
|
||||
import lila.chess.Situation
|
||||
|
||||
sealed case class GameStatus(id: Int, name: String) extends IntStatus
|
||||
sealed case class GameVariant(id: Int, name: String) extends IntStatus
|
||||
|
@ -19,6 +20,13 @@ object GameStatuses extends IntStatuses[GameStatus] {
|
|||
GameStatus(35, "outoftime"),
|
||||
GameStatus(36, "cheat")
|
||||
)
|
||||
|
||||
def fromSituation(situation: Situation): Option[Int] = {
|
||||
if (situation.checkMate) find("checkmate")
|
||||
else if (situation.staleMate) find("stalemate")
|
||||
else if (situation.autoDraw) find("draw")
|
||||
else None
|
||||
} map (_.toInt)
|
||||
}
|
||||
|
||||
object GameVariants extends IntStatuses[GameVariant] {
|
||||
|
|
|
@ -16,7 +16,8 @@ case class DbGame(
|
|||
turns: Int,
|
||||
clock: Option[DbClock],
|
||||
lastMove: Option[String],
|
||||
positionHashes: String = "") {
|
||||
positionHashes: String = "",
|
||||
castles: String = "KQkq") {
|
||||
|
||||
def playerById(id: String): Option[DbPlayer] = playersById get id
|
||||
|
||||
|
@ -50,16 +51,7 @@ case class DbGame(
|
|||
}
|
||||
|
||||
Game(
|
||||
board = Board(
|
||||
pieces,
|
||||
History(
|
||||
lastMove = lastMove flatMap {
|
||||
case MoveString(a, b) ⇒ for (o ← posAt(a); d ← posAt(b)) yield (o, d)
|
||||
case _ ⇒ None
|
||||
},
|
||||
positionHashes = positionHashes grouped History.hashSize toList
|
||||
)
|
||||
),
|
||||
board = Board(pieces, toChessHistory),
|
||||
player = if (0 == turns % 2) White else Black,
|
||||
pgnMoves = pgn,
|
||||
clock = for {
|
||||
|
@ -78,12 +70,25 @@ case class DbGame(
|
|||
)
|
||||
}
|
||||
|
||||
private def toChessHistory = History(
|
||||
lastMove = lastMove flatMap {
|
||||
case MoveString(a, b) ⇒ for (o ← posAt(a); d ← posAt(b)) yield (o, d)
|
||||
case _ ⇒ None
|
||||
},
|
||||
castles = Map(
|
||||
White -> (castles contains 'K', castles contains 'Q'),
|
||||
Black -> (castles contains 'k', castles contains 'q')
|
||||
),
|
||||
positionHashes = positionHashes grouped History.hashSize toList
|
||||
)
|
||||
|
||||
def update(game: Game, move: Move): DbGame = {
|
||||
val allPieces = (game.board.pieces map {
|
||||
case (pos, piece) ⇒ (pos, piece, false)
|
||||
}) ++ (game.deads map {
|
||||
case (pos, piece) ⇒ (pos, piece, true)
|
||||
})
|
||||
val (history, situation) = (game.board.history, game.situation)
|
||||
val events = (Event fromMove move) ::: (Event fromSituation game.situation)
|
||||
copy(
|
||||
pgn = game.pgnMoves,
|
||||
|
@ -95,7 +100,18 @@ case class DbGame(
|
|||
evts = player.newEvts(events :+ Event.possibleMoves(game.situation, color))
|
||||
),
|
||||
turns = game.turns,
|
||||
positionHashes = game.board.history.positionHashes mkString
|
||||
positionHashes = history.positionHashes mkString,
|
||||
castles = List(
|
||||
if (history canCastle White on KingSide) "K" else "",
|
||||
if (history canCastle White on QueenSide) "Q" else "",
|
||||
if (history canCastle Black on KingSide) "k" else "",
|
||||
if (history canCastle Black on QueenSide) "q" else ""
|
||||
) mkString,
|
||||
status =
|
||||
if (situation.checkMate) DbGame.MATE
|
||||
else if (situation.staleMate) DbGame.STALEMATE
|
||||
else if (situation.autoDraw) DbGame.DRAW
|
||||
else status
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -105,4 +121,15 @@ object DbGame {
|
|||
val gameIdSize = 8
|
||||
val playerIdSize = 4
|
||||
val fullIdSize = 12
|
||||
|
||||
val CREATED = 10
|
||||
val STARTED = 20
|
||||
val ABORTED = 25
|
||||
val MATE = 30
|
||||
val RESIGN = 31
|
||||
val STALEMATE = 32
|
||||
val TIMEOUT = 33
|
||||
val DRAW = 34
|
||||
val OUTOFTIME = 35
|
||||
val CHEAT = 36
|
||||
}
|
||||
|
|
|
@ -23,12 +23,13 @@ trait Fixtures {
|
|||
clock = None
|
||||
)
|
||||
|
||||
def newDbGameWithBoard(b: Board) =
|
||||
newDbGame.update(Game(b), anyMove)
|
||||
def newDbGameWithBoard(b: Board) = newDbGame.update(Game(b), anyMove)
|
||||
|
||||
def newDbGameWithRandomIds() = newDbGame.copy(
|
||||
def newDbGameWithRandomIds() = randomizeIds(newDbGame)
|
||||
|
||||
def randomizeIds(game: DbGame) = game.copy(
|
||||
id = randomString(gameIdSize),
|
||||
players = newDbGame.players map (_.copy(id = randomString(playerIdSize)))
|
||||
players = game.players map (_.copy(id = randomString(playerIdSize)))
|
||||
)
|
||||
|
||||
lazy val white = newDbPlayer("white", "ip ar jp bn kp cb lp dq mp ek np fb op gn pp hr")
|
||||
|
|
|
@ -8,10 +8,9 @@ class ServerTest extends SystemTest {
|
|||
val repo = env.gameRepo
|
||||
val server = env.server
|
||||
|
||||
def insert() = {
|
||||
val game = newDbGameWithRandomIds
|
||||
repo insert game
|
||||
game
|
||||
def insert(dbGame: DbGame = newDbGameWithRandomIds) = {
|
||||
repo insert dbGame
|
||||
dbGame
|
||||
}
|
||||
def move(game: DbGame, m: String = "d2 d4") = for {
|
||||
player ← game playerByColor "white"
|
||||
|
@ -119,5 +118,27 @@ B p p
|
|||
}
|
||||
}
|
||||
}
|
||||
"play on playing game" in {
|
||||
val dbGame = insert(randomizeIds(newDbGameWithBoard("""
|
||||
PP kr
|
||||
K
|
||||
""")))
|
||||
move(dbGame, "a1 b1") must beSome.like { case r ⇒ r must beSuccess }
|
||||
}
|
||||
"play on finished game" in {
|
||||
"by checkmate" in {
|
||||
val game = insert(randomizeIds(newDbGameWithBoard("""
|
||||
PP
|
||||
K r
|
||||
""")))
|
||||
move(game, "a1 b1") must beSome.like { case r ⇒ r must beFailure }
|
||||
}
|
||||
"by autodraw" in {
|
||||
val game = insert(randomizeIds(newDbGameWithBoard("""
|
||||
k
|
||||
K B""")))
|
||||
move(game, "a1 b1") must beSome.like { case r ⇒ r must beFailure }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue