lila/app/controllers/Importer.scala

70 lines
2.3 KiB
Scala
Raw Normal View History

2013-05-06 19:57:42 -06:00
package controllers
import lila.app._
import lila.common.HTTPRequest
2014-06-26 06:38:14 -06:00
import play.api.libs.json.Json
2013-05-06 19:57:42 -06:00
import views._
2015-04-23 01:01:32 -06:00
object Importer extends LilaController {
2013-05-06 19:57:42 -06:00
2013-05-09 09:11:06 -06:00
private def env = Env.importer
2013-05-06 19:57:42 -06:00
def importGame = OpenBody { implicit ctx =>
2015-04-23 01:01:32 -06:00
fuccess {
val pgn = ctx.body.queryString.get("pgn").flatMap(_.headOption).getOrElse("")
val data = lila.importer.ImportData(pgn, None)
Ok(html.game.importGame(env.forms.importForm.fill(data)))
2013-05-09 09:11:06 -06:00
}
}
2013-05-06 19:57:42 -06:00
2014-02-17 02:12:19 -07:00
def sendGame = OpenBody { implicit ctx =>
2013-05-09 09:11:06 -06:00
implicit def req = ctx.body
env.forms.importForm.bindFromRequest.fold(
2016-10-27 09:47:51 -06:00
failure => negotiate(
html = Ok(html.game.importGame(failure)).fuccess,
api = _ => BadRequest(Json.obj("error" -> "Invalid PGN")).fuccess
),
data => env.importer(data, ctx.userId) flatMap { game =>
2017-01-26 12:31:07 -07:00
(ctx.userId ?? Env.game.cached.clearNbImportedByCache) >>
(data.analyse.isDefined && game.analysable) ?? {
Env.fishnet.analyser(game, lila.fishnet.Work.Sender(
userId = ctx.userId,
ip = HTTPRequest.lastRemoteAddress(ctx.req).some,
mod = isGranted(_.Hunter),
system = false
))
2017-01-26 12:31:07 -07:00
} inject Redirect(routes.Round.watcher(game.id, "white"))
2013-05-09 09:11:06 -06:00
} recover {
case e =>
2016-11-02 06:15:41 -06:00
controllerLogger.branch("importer").warn(
s"Imported game validates but can't be replayed:\n${data.pgn}", e
)
2013-05-09 09:11:06 -06:00
Redirect(routes.Importer.importGame)
}
)
}
import lila.game.GameRepo
import org.joda.time.DateTime
private val masterGameEncodingFixedAt = new DateTime(2016, 3, 9, 0, 0)
def masterGame(id: String, orientation: String) = Open { implicit ctx =>
def redirectAtFen(game: lila.game.Game) = Redirect {
val url = routes.Round.watcher(game.id, orientation).url
val fenParam = get("fen").??(f => s"?fen=$f")
s"$url$fenParam"
}
GameRepo game id flatMap {
case Some(game) if game.createdAt.isAfter(masterGameEncodingFixedAt) => fuccess(redirectAtFen(game))
case _ => (GameRepo remove id) >> Env.explorer.fetchPgn(id) flatMap {
case None => fuccess(NotFound)
2016-02-12 00:53:25 -07:00
case Some(pgn) => env.importer(
lila.importer.ImportData(pgn, none),
user = "lichess".some,
forceId = id.some
) map redirectAtFen
}
}
}
2013-05-06 19:57:42 -06:00
}