actor that monitors finished games and stores them for puzzle API

pull/2156/head
Thibault Duplessis 2016-07-29 12:57:58 +02:00
parent e9306e614d
commit 9a93e292df
6 changed files with 45 additions and 13 deletions

View File

@ -34,10 +34,6 @@ object Export extends LilaController {
}
}
def random = Action.async {
Ok.fuccess
}
def pdf(id: String) = Open { implicit ctx =>
OnlyHumans {
OptionResult(GameRepo game id) { game =>

View File

@ -164,6 +164,20 @@ object Puzzle extends LilaController {
}
}
def recentGame = Action.async {
import akka.pattern.ask
import makeTimeout.short
Env.game.recentGoodGameActor ? true mapTo manifest[Option[String]] flatMap {
_ ?? lila.game.GameRepo.gameWithInitialFen map {
_ ?? {
case (game, initialFen) => Ok {
Env.api.pgnDump(game, initialFen.map(_.value)).toString
}
}
}
}
}
def embed = Action { req =>
Ok {
val bg = get("bg", req) | "light"

View File

@ -107,6 +107,7 @@ GET /training/history controllers.Puzzle.history
GET /training/daily controllers.Puzzle.daily
GET /training/embed controllers.Puzzle.embed
GET /training/frame controllers.Puzzle.frame
GET /training/api/game.pgn controllers.Puzzle.recentGame
GET /training/export/png/:id.png controllers.Export.puzzlePng(id: Int)
GET /training/:id controllers.Puzzle.show(id: Int)
GET /training/:id/load controllers.Puzzle.load(id: Int)
@ -250,7 +251,6 @@ POST /$gameId<\w{8}>/request-analysis controllers.Analyse.requestAnalysi
GET /game/export/$gameId<\w{8}>.pgn controllers.Export.pgn(gameId: String)
GET /game/export/pdf/$gameId<\w{8}>.pdf controllers.Export.pdf(gameId: String)
GET /game/export/png/$gameId<\w{8}>.png controllers.Export.png(gameId: String)
GET /game/export/png/random.png controllers.Export.random
# Fishnet
POST /fishnet/acquire controllers.Fishnet.acquire

View File

@ -72,6 +72,9 @@ final class Env(
// load captcher actor
private val captcher = system.actorOf(Props(new Captcher), name = CaptcherName)
val recentGoodGameActor = system.actorOf(Props[RecentGoodGame], name = "recent-good-game")
system.lilaBus.subscribe(recentGoodGameActor, 'finishGame)
scheduler.message(CaptcherDuration) {
captcher -> actorApi.NewCaptcha
}

View File

@ -246,14 +246,6 @@ object GameRepo {
.skip(Random nextInt distribution)
.uno[Game]
def randomFinished(distribution: Int): Fu[Option[Game]] = coll.find(
Query.finished ++ Query.rated ++
Query.variantStandard ++ Query.bothRatingsGreaterThan(1600)
).sort(Query.sortCreated)
.skip(Random nextInt distribution)
.cursor[Game](ReadPreference.secondary)
.uno
def insertDenormalized(g: Game, ratedCheck: Boolean = true, initialFen: Option[chess.format.FEN] = None): Funit = {
val g2 = if (ratedCheck && g.rated && g.userIds.distinct.size != 2)
g.copy(mode = chess.Mode.Casual)

View File

@ -0,0 +1,27 @@
package lila.game
import akka.actor._
private final class RecentGoodGame extends Actor {
val maxIds = 30
var ids = List.empty[Game.ID]
def matches(g: lila.game.Game) =
g.variant.standard &&
g.rated &&
g.turns >= 10 &&
g.averageUsersRating.??(1800 <=) &&
g.clock.??(_.estimateTotalTime >= 5 * 60)
def receive = {
case lila.game.actorApi.FinishGame(g, _, _) if matches(g) =>
ids = g.id :: ids.take(maxIds - 1)
case true => ids match {
case head :: tail =>
sender ! head.some
ids = tail
case _ => sender ! none
}
}
}