diff --git a/system/src/main/scala/AppXhr.scala b/system/src/main/scala/AppXhr.scala index a866344d23..770db94582 100644 --- a/system/src/main/scala/AppXhr.scala +++ b/system/src/main/scala/AppXhr.scala @@ -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() ) } diff --git a/system/src/main/scala/Finisher.scala b/system/src/main/scala/Finisher.scala index d35ef9655c..870d903db2 100644 --- a/system/src/main/scala/Finisher.scala +++ b/system/src/main/scala/Finisher.scala @@ -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) diff --git a/system/src/main/scala/model/Event.scala b/system/src/main/scala/model/Event.scala index ce4333a1b2..b8e0d73963 100644 --- a/system/src/main/scala/model/Event.scala +++ b/system/src/main/scala/model/Event.scala @@ -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 diff --git a/todo b/todo index 66007f4b06..479daba2a9 100644 --- a/todo +++ b/todo @@ -1,6 +1,3 @@ move times blurs ie9 testing (frozen sync/clock) -moretime in chat -lose with 20 seconds, then clock shows 00:00 -rated game without elo exchange