lila/modules/round/src/main/Finisher.scala

78 lines
2.5 KiB
Scala
Raw Normal View History

2013-04-08 13:21:03 -06:00
package lila.round
2013-05-24 11:04:49 -06:00
import chess.Color._
import chess.Status._
2013-12-17 15:20:18 -07:00
import chess.{ Status, Color, Speed, Variant }
2013-12-21 05:14:50 -07:00
2013-04-08 13:21:03 -06:00
import lila.db.api._
2014-01-27 06:38:23 -07:00
import lila.game.actorApi.FinishGame
2013-05-24 11:04:49 -06:00
import lila.game.tube.gameTube
import lila.game.{ GameRepo, Game, Pov, Event }
2014-02-17 02:12:19 -07:00
import lila.i18n.I18nKey.{ Select => SelectI18nKey }
2013-05-24 11:04:49 -06:00
import lila.user.tube.userTube
2013-12-17 15:20:18 -07:00
import lila.user.{ User, UserRepo }
2013-04-08 13:21:03 -06:00
private[round] final class Finisher(
2013-04-08 13:21:03 -06:00
messenger: Messenger,
perfsUpdater: PerfsUpdater,
2014-02-27 17:18:22 -07:00
aiPerfApi: lila.ai.AiPerfApi,
2014-01-27 06:38:23 -07:00
bus: lila.common.Bus) {
2013-04-08 13:21:03 -06:00
def apply(
2013-04-08 13:21:03 -06:00
game: Game,
2014-02-17 02:12:19 -07:00
status: Status.type => Status,
2013-04-08 13:21:03 -06:00
winner: Option[Color] = None,
message: Option[SelectI18nKey] = None): Fu[Events] = {
val prog = game.finish(status(Status), winner)
val g = prog.game
(GameRepo save prog) >>
GameRepo.finish(g.id, winner, winner flatMap (g.player(_).userId)) >>
2014-01-27 06:38:23 -07:00
UserRepo.pair(
2014-01-27 17:33:04 -07:00
g.player(White).userId,
g.player(Black).userId).flatMap {
2014-02-17 02:12:19 -07:00
case (whiteO, blackO) => {
2014-01-27 17:33:04 -07:00
val finish = FinishGame(g, whiteO, blackO)
2014-01-27 06:38:23 -07:00
updateCountAndPerfs(finish) inject {
2014-02-01 00:54:03 -07:00
message foreach { messenger.system(g, _) }
2014-01-27 06:38:23 -07:00
bus.publish(finish, 'finishGame)
prog.events
}
}
}
}
2013-12-21 05:14:50 -07:00
2014-03-12 03:14:19 -06:00
private def updateCountAndPerfs(finish: FinishGame): Funit = !finish.isVsSelf ?? {
2014-01-27 06:38:23 -07:00
(finish.white |@| finish.black).tupled ?? {
2014-02-17 02:12:19 -07:00
case (white, black) => perfsUpdater.save(finish.game, white, black)
2014-01-27 06:38:23 -07:00
} zip
2014-02-27 17:18:22 -07:00
addAiGame(finish) zip
2014-01-27 06:38:23 -07:00
(finish.white ?? incNbGames(finish.game)) zip
(finish.black ?? incNbGames(finish.game)) void
2014-03-12 03:14:19 -06:00
}
2013-12-27 06:25:49 -07:00
2014-02-27 17:18:22 -07:00
private def addAiGame(finish: FinishGame): Funit = ~{
import finish._
import lila.rating.Glicko.Result._
for {
level <- game.players.map(_.aiLevel).flatten.headOption
if game.turns > 10
if !game.fromPosition
humanColor <- game.players.find(_.isHuman).map(_.color)
user <- humanColor.fold(white, black)
2014-03-02 08:54:00 -07:00
if !user.engine
2014-02-27 17:18:22 -07:00
result = game.winnerColor match {
case Some(c) if c == humanColor => Loss
case Some(_) => Win
case None => Draw
}
} yield aiPerfApi.add(level, user.perfs.global, result)
}
2013-12-27 06:25:49 -07:00
private def incNbGames(game: Game)(user: User): Funit = game.finished ?? {
UserRepo.incNbGames(user.id, game.rated, game.hasAi,
2014-02-02 17:08:04 -07:00
result = if (game.winnerUserId exists (user.id==)) 1
else if (game.loserUserId exists (user.id==)) -1
else 0)
2013-12-21 05:14:50 -07:00
}
2013-04-08 13:21:03 -06:00
}