expose delayed PGN for all live games

pull/6413/head
Thibault Duplessis 2020-04-15 10:24:03 -06:00
parent b225d2d84d
commit d7b67d0cec
3 changed files with 29 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package controllers
import play.api.libs.json._
import play.api.mvc._
import scala.concurrent.duration._
import lila.api.Context
import lila.app._
@ -320,4 +321,23 @@ final class Round(
html.game.bits.mini
)
}
private val delayedPgnCache = env.memo.cacheApi[GameModel.ID, Result](32, "round.delayedPgn") {
_.expireAfterWrite(3 seconds)
.buildAsyncFuture { gameId =>
env.round.proxyRepo.game(gameId) flatMap {
case None => NotFound("No such game").fuccess
case Some(game) =>
env.game.gameRepo initialFen game flatMap { fen =>
env.game.pgnDump(game, fen, lila.game.PgnDump.WithFlags().copy(delayMoves = 3)) map { pgn =>
Ok(pgn.toString).as(pgnContentType)
}
}
}
}
}
def delayedPgn(gameId: GameModel.ID) = Action.async {
delayedPgnCache.get(gameId)
}
}

View File

@ -189,6 +189,7 @@ GET /streamer/:username controllers.Streamer.show(username: Strin
GET /$gameId<\w{8}> controllers.Round.watcher(gameId: String, color: String = "white")
GET /$gameId<\w{8}>/$color<white|black> controllers.Round.watcher(gameId: String, color: String)
GET /$fullId<\w{12}> controllers.Round.player(fullId: String)
GET /$gameId<\w{8}>.pgn controllers.Round.delayedPgn(gameId: String)
GET /$gameId<\w{8}>/$color<white|black>/sides controllers.Round.sides(gameId: String, color: String)
GET /$gameId<\w{8}>/continue/:mode controllers.Round.continue(gameId: String, mode: String)
GET /$gameId<\w{8}>/note controllers.Round.readNote(gameId: String)

View File

@ -26,9 +26,13 @@ final class PgnDump(
val turns = flags.moves ?? {
val fenSituation = ts.fen.map(_.value) flatMap Forsyth.<<<
val moves2 =
if (fenSituation.exists(_.situation.color.black)) ".." +: game.pgnMoves else game.pgnMoves
if (fenSituation.exists(_.situation.color.black)) ".." +: game.pgnMoves
else game.pgnMoves
val moves3 =
if (flags.delayMoves > 0) moves2 dropRight flags.delayMoves
else moves2
makeTurns(
moves2,
moves3,
fenSituation.map(_.fullMoveNumber) | 1,
flags.clocks ?? ~game.bothClockStates,
game.startColor
@ -164,7 +168,8 @@ object PgnDump {
evals: Boolean = true,
opening: Boolean = true,
literate: Boolean = false,
pgnInJson: Boolean = false
pgnInJson: Boolean = false,
delayMoves: Int = 0
)
def result(game: Game) =