better format exported PGN

This commit is contained in:
Thibault Duplessis 2018-04-04 02:22:32 +02:00
parent d19ccf7ea9
commit 456a0f3e0e
4 changed files with 31 additions and 20 deletions

View file

@ -39,13 +39,17 @@ object Game extends LilaController {
ExportRateLimitPerUser(user.id, cost = 1) {
val since = getLong("since", req) map { ts => new DateTime(ts) }
val until = getLong("until", req) map { ts => new DateTime(ts) }
val max = getInt("max", req)
val moves = getBoolOpt("moves", req) | true
val tags = getBoolOpt("tags", req) | true
val clocks = getBoolOpt("clocks", req) | true
val max = getInt("max", req) map (_ atLeast 1)
val perSecond = MaxPerSecond(me match {
case None => 10
case Some(m) if m is user.id => 50
case Some(_) => 20
})
val config = PgnDump.Config(user, since, until, max, perSecond)
val formatFlags = lila.game.PgnDump.WithFlags(moves = moves, tags = tags, clocks = clocks)
val config = PgnDump.Config(user, since, until, max, formatFlags, perSecond)
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
Ok.chunked(Env.api.pgnDump.exportUserGames(config)).withHeaders(
CONTENT_TYPE -> pgnContentType,

View file

@ -29,14 +29,16 @@ trait RequestGetter {
get(name, req) flatMap parseLongOption
protected def getBool(name: String)(implicit ctx: UserContext) =
getInt(name).contains(1) || get(name).contains("true")
getInt(name) exists trueish
protected def getBool(name: String, req: RequestHeader) =
getInt(name, req).contains(1) || get(name, req).contains("true")
getInt(name, req) exists trueish
protected def getBoolOpt(name: String)(implicit ctx: UserContext) =
getInt(name) map (1==)
getInt(name) map (trueish)
protected def getBoolOpt(name: String, req: RequestHeader) =
getInt(name, req) map (1==)
getInt(name, req) map (trueish)
private def trueish(v: Any) = v == 1 || v == "true"
}

View file

@ -27,10 +27,10 @@ final class PgnDump(
def filename(game: Game) = dumper filename game
private val toPgn =
private def toPgn(flags: WithFlags) =
Enumeratee.mapM[Game].apply[String] { game =>
GameRepo initialFen game flatMap { initialFen =>
apply(game, initialFen, WithFlags()).map(pgn => s"$pgn\n\n\n")
apply(game, initialFen, flags).map(pgn => s"$pgn\n\n\n")
}
}
@ -50,14 +50,14 @@ final class PgnDump(
Query.user(config.user.id) ++ Query.createdBetween(config.since, config.until),
Query.sortCreated,
batchSize = config.perSecond.value
).bulkEnumerator(maxDocs = config.max | Int.MaxValue) &> throttle &> toPgn
).bulkEnumerator(maxDocs = config.max | Int.MaxValue) &> throttle &> toPgn(config.flags)
}
def exportGamesFromIds(ids: List[String]): Enumerator[String] =
Enumerator.enumerate(ids grouped 50) &>
Enumeratee.mapM[List[String]].apply[List[Game]](GameRepo.gamesFromSecondary) &>
Enumeratee.mapConcat(identity) &>
toPgn
toPgn(WithFlags())
}
object PgnDump {
@ -67,6 +67,7 @@ object PgnDump {
since: Option[DateTime] = None,
until: Option[DateTime] = None,
max: Option[Int] = None,
flags: WithFlags,
perSecond: MaxPerSecond
)
}

View file

@ -18,15 +18,17 @@ final class PgnDump(
val imported = game.pgnImport.flatMap { pgni =>
Parser.full(pgni.pgn).toOption
}
val ts = tags(game, initialFen, imported)
val fenSituation = ts.fen.map(_.value) flatMap Forsyth.<<<
val moves2 = fenSituation.??(_.situation.color.black).fold(".." +: game.pgnMoves, game.pgnMoves)
val turns = makeTurns(
moves2,
fenSituation.map(_.fullMoveNumber) | 1,
flags.clocks ?? ~game.bothClockStates,
game.startColor
)
val ts = if (flags.tags) tags(game, initialFen, imported) else Tags(Nil)
val turns = flags.moves ?? {
val fenSituation = ts.fen.map(_.value) flatMap Forsyth.<<<
val moves2 = fenSituation.??(_.situation.color.black).fold(".." +: game.pgnMoves, game.pgnMoves)
makeTurns(
moves2,
fenSituation.map(_.fullMoveNumber) | 1,
flags.clocks ?? ~game.bothClockStates,
game.startColor
)
}
Pgn(ts, turns)
}
@ -140,7 +142,9 @@ final class PgnDump(
object PgnDump {
case class WithFlags(
clocks: Boolean = true
clocks: Boolean = true,
moves: Boolean = true,
tags: Boolean = true
)
def result(game: Game) =