-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Titivate.scala
72 lines (54 loc) · 1.91 KB
/
Titivate.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package lila.round
import akka.actor._
import org.joda.time.DateTime
import scala.concurrent.duration._
import scala.concurrent.Future
import lila.db.api._
import lila.game.tube.gameTube
import lila.game.{ Query, Game, GameRepo }
import lila.hub.actorApi.map.Tell
import lila.round.actorApi.round.{ Outoftime, Abandon }
private[round] final class Titivate(
roundMap: ActorRef,
bookmark: ActorSelection) extends Actor {
object Schedule
object Run
override def preStart() {
scheduleNext
context setReceiveTimeout 30.seconds
}
def scheduler = context.system.scheduler
def scheduleNext = scheduler.scheduleOnce(10 seconds, self, Run)
def receive = {
case ReceiveTimeout =>
val msg = "Titivate timed out!"
logger.error(msg)
throw new RuntimeException(msg)
case Run =>
$enumerate.over[Game]($query(Query.checkable), 5000) { game =>
if (game.finished || game.isPgnImport)
GameRepo unsetCheckAt game
else if (game.outoftime(_ => chess.Clock.maxGraceMillis)) fuccess {
roundMap ! Tell(game.id, Outoftime)
}
else if (game.abandoned) fuccess {
roundMap ! Tell(game.id, Abandon)
}
else if (game.unplayed) {
bookmark ! lila.hub.actorApi.bookmark.Remove(game.id)
GameRepo remove game.id
}
else game.clock match {
case Some(clock) if clock.isRunning =>
val minutes = (clock.estimateTotalTime / 60).toInt
GameRepo.setCheckAt(game, DateTime.now plusMinutes minutes)
case Some(clock) =>
val hours = Game.unplayedHours
GameRepo.setCheckAt(game, DateTime.now plusHours hours)
case None =>
val days = game.daysPerTurn | game.hasAi.fold(Game.aiAbandonedDays, Game.abandonedDays)
GameRepo.setCheckAt(game, DateTime.now plusDays days)
}
} andThenAnyway scheduleNext
}
}