Finish a game with a move

This commit is contained in:
Thibault Duplessis 2012-03-31 21:53:46 +02:00
parent 689b70de6a
commit 4b8c3b86d3
4 changed files with 23 additions and 16 deletions

View file

@ -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()
)
}

View file

@ -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)

View file

@ -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

3
todo
View file

@ -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