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
|
(newChessGame, move) = newChessGameAndMove
|
||||||
} yield g2.update(newChessGame, move)).fold(
|
} yield g2.update(newChessGame, move)).fold(
|
||||||
e ⇒ io(failure(e)),
|
e ⇒ io(failure(e)),
|
||||||
g2 ⇒ for {
|
g3 ⇒ 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)
|
|
||||||
_ ← aliveMemo.put(g3.id, color)
|
_ ← 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()
|
} yield success()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,12 @@ import scalaz.effects._
|
||||||
final class Finisher(
|
final class Finisher(
|
||||||
historyRepo: HistoryRepo,
|
historyRepo: HistoryRepo,
|
||||||
userRepo: UserRepo,
|
userRepo: UserRepo,
|
||||||
gameRepo: GameRepo,
|
val gameRepo: GameRepo,
|
||||||
messenger: Messenger,
|
messenger: Messenger,
|
||||||
versionMemo: VersionMemo,
|
val versionMemo: VersionMemo,
|
||||||
aliveMemo: AliveMemo,
|
aliveMemo: AliveMemo,
|
||||||
eloCalculator: EloCalculator,
|
eloCalculator: EloCalculator,
|
||||||
finisherLock: FinisherLock) {
|
finisherLock: FinisherLock) extends IOTools {
|
||||||
|
|
||||||
type ValidIO = Valid[IO[Unit]]
|
type ValidIO = Valid[IO[Unit]]
|
||||||
|
|
||||||
|
@ -48,6 +48,12 @@ final class Finisher(
|
||||||
Some(!player.color) filter pov.game.toChess.board.hasEnoughMaterialToMate)
|
Some(!player.color) filter pov.game.toChess.board.hasEnoughMaterialToMate)
|
||||||
} none !!("no outoftime applicable")
|
} 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 !!(msg: String) = failure(msg.wrapNel)
|
||||||
|
|
||||||
private def finish(
|
private def finish(
|
||||||
|
@ -60,10 +66,9 @@ final class Finisher(
|
||||||
_ ← finisherLock lock game
|
_ ← finisherLock lock game
|
||||||
g2 = game.finish(status, winner)
|
g2 = game.finish(status, winner)
|
||||||
g3 ← message.fold(messenger.systemMessage(g2, _), io(g2))
|
g3 ← message.fold(messenger.systemMessage(g2, _), io(g2))
|
||||||
_ ← gameRepo.applyDiff(game, g3)
|
_ ← save(game, g3)
|
||||||
winnerId = winner flatMap (g3.player(_).userId)
|
winnerId = winner flatMap (g3.player(_).userId)
|
||||||
_ ← gameRepo.finish(g3.id, winnerId)
|
_ ← gameRepo.finish(g3.id, winnerId)
|
||||||
_ ← versionMemo put g3
|
|
||||||
_ ← updateElo(g3)
|
_ ← updateElo(g3)
|
||||||
_ ← incNbGames(g3, White)
|
_ ← incNbGames(g3, White)
|
||||||
_ ← incNbGames(g3, Black)
|
_ ← incNbGames(g3, Black)
|
||||||
|
|
|
@ -20,7 +20,6 @@ object Event {
|
||||||
|
|
||||||
def fromSituation(situation: Situation): List[Event] = List(
|
def fromSituation(situation: Situation): List[Event] = List(
|
||||||
if (situation.check) situation.kingPos map CheckEvent.apply else None,
|
if (situation.check) situation.kingPos map CheckEvent.apply else None,
|
||||||
if (situation.end) Some(EndEvent()) else None,
|
|
||||||
if (situation.threefoldRepetition) Some(ThreefoldEvent()) else None
|
if (situation.threefoldRepetition) Some(ThreefoldEvent()) else None
|
||||||
).flatten
|
).flatten
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue