Finish a game with a move
This commit is contained in:
parent
689b70de6a
commit
4b8c3b86d3
|
@ -32,14 +32,20 @@ final class AppXhr(
|
|||
(newChessGame, move) = newChessGameAndMove
|
||||
} yield g2.update(newChessGame, move)).fold(
|
||||
e ⇒ io(failure(e)),
|
||||
g2 ⇒ for {
|
||||
g3 ← if (g2.player.isAi && g2.playable) for {
|
||||
aiResult ← ai(g2) map (_.toOption err "AI failure")
|
||||
(newChessGame, move) = aiResult
|
||||
} yield g2.update(newChessGame, move)
|
||||
else io(g2)
|
||||
_ ← save(g1, g3)
|
||||
g3 ⇒ for {
|
||||
_ ← aliveMemo.put(g3.id, color)
|
||||
_ ← if (g3.finished) for {
|
||||
_ ← save(g1, g3)
|
||||
_ ← finisher.moveFinish(g3, color)
|
||||
} yield ()
|
||||
else if (g3.player.isAi && g3.playable) for {
|
||||
aiResult ← ai(g3) map (_.toOption err "AI failure")
|
||||
(newChessGame, move) = aiResult
|
||||
g4 = g3.update(newChessGame, move)
|
||||
_ ← save(g1, g4)
|
||||
_ ← finisher.moveFinish(g4, !color)
|
||||
} yield ()
|
||||
else save(g1, g3)
|
||||
} yield success()
|
||||
)
|
||||
}
|
||||
|
|
|
@ -10,12 +10,12 @@ import scalaz.effects._
|
|||
final class Finisher(
|
||||
historyRepo: HistoryRepo,
|
||||
userRepo: UserRepo,
|
||||
gameRepo: GameRepo,
|
||||
val gameRepo: GameRepo,
|
||||
messenger: Messenger,
|
||||
versionMemo: VersionMemo,
|
||||
val versionMemo: VersionMemo,
|
||||
aliveMemo: AliveMemo,
|
||||
eloCalculator: EloCalculator,
|
||||
finisherLock: FinisherLock) {
|
||||
finisherLock: FinisherLock) extends IOTools {
|
||||
|
||||
type ValidIO = Valid[IO[Unit]]
|
||||
|
||||
|
@ -48,6 +48,12 @@ final class Finisher(
|
|||
Some(!player.color) filter pov.game.toChess.board.hasEnoughMaterialToMate)
|
||||
} none !!("no outoftime applicable")
|
||||
|
||||
def moveFinish(game: DbGame, color: Color): IO[Unit] = (game.status match {
|
||||
case Mate ⇒ finish(game, Mate, Some(color))
|
||||
case status @ (Stalemate | Draw) ⇒ finish(game, status)
|
||||
case _ ⇒ success(io())
|
||||
}) | io()
|
||||
|
||||
private def !!(msg: String) = failure(msg.wrapNel)
|
||||
|
||||
private def finish(
|
||||
|
@ -60,10 +66,9 @@ final class Finisher(
|
|||
_ ← finisherLock lock game
|
||||
g2 = game.finish(status, winner)
|
||||
g3 ← message.fold(messenger.systemMessage(g2, _), io(g2))
|
||||
_ ← gameRepo.applyDiff(game, g3)
|
||||
_ ← save(game, g3)
|
||||
winnerId = winner flatMap (g3.player(_).userId)
|
||||
_ ← gameRepo.finish(g3.id, winnerId)
|
||||
_ ← versionMemo put g3
|
||||
_ ← updateElo(g3)
|
||||
_ ← incNbGames(g3, White)
|
||||
_ ← incNbGames(g3, Black)
|
||||
|
|
|
@ -20,7 +20,6 @@ object Event {
|
|||
|
||||
def fromSituation(situation: Situation): List[Event] = List(
|
||||
if (situation.check) situation.kingPos map CheckEvent.apply else None,
|
||||
if (situation.end) Some(EndEvent()) else None,
|
||||
if (situation.threefoldRepetition) Some(ThreefoldEvent()) else None
|
||||
).flatten
|
||||
|
||||
|
|
Loading…
Reference in a new issue