2013-05-06 19:57:42 -06:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import lila.app._
|
2016-03-12 05:56:44 -07:00
|
|
|
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
|
|
|
|
2016-09-22 15:54:46 -06:00
|
|
|
def importGame = OpenBody { implicit ctx =>
|
2015-04-23 01:01:32 -06:00
|
|
|
fuccess {
|
2016-09-22 15:54:46 -06:00
|
|
|
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
|
|
|
|
),
|
2016-03-12 05:56:44 -07:00
|
|
|
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),
|
2017-02-14 08:34:07 -07:00
|
|
|
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 {
|
2016-03-12 05:56:44 -07:00
|
|
|
case e =>
|
2016-11-02 06:15:41 -06:00
|
|
|
controllerLogger.branch("importer").warn(
|
2017-02-14 08:34:07 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2016-02-09 02:57:23 -07:00
|
|
|
|
2016-03-12 23:03:41 -07:00
|
|
|
import lila.game.GameRepo
|
|
|
|
import org.joda.time.DateTime
|
|
|
|
private val masterGameEncodingFixedAt = new DateTime(2016, 3, 9, 0, 0)
|
|
|
|
|
2016-02-12 03:29:25 -07:00
|
|
|
def masterGame(id: String, orientation: String) = Open { implicit ctx =>
|
2016-02-15 04:04:17 -07:00
|
|
|
def redirectAtFen(game: lila.game.Game) = Redirect {
|
|
|
|
val url = routes.Round.watcher(game.id, orientation).url
|
2016-02-19 09:49:26 -07:00
|
|
|
val fenParam = get("fen").??(f => s"?fen=$f")
|
|
|
|
s"$url$fenParam"
|
2016-02-15 04:04:17 -07:00
|
|
|
}
|
2016-03-12 23:03:41 -07:00
|
|
|
GameRepo game id flatMap {
|
|
|
|
case Some(game) if game.createdAt.isAfter(masterGameEncodingFixedAt) => fuccess(redirectAtFen(game))
|
|
|
|
case _ => (GameRepo remove id) >> Env.explorer.fetchPgn(id) flatMap {
|
2016-02-09 02:57:23 -07:00
|
|
|
case None => fuccess(NotFound)
|
2016-02-12 00:53:25 -07:00
|
|
|
case Some(pgn) => env.importer(
|
|
|
|
lila.importer.ImportData(pgn, none),
|
2016-02-15 02:18:25 -07:00
|
|
|
user = "lichess".some,
|
2017-02-14 08:34:07 -07:00
|
|
|
forceId = id.some
|
|
|
|
) map redirectAtFen
|
2016-02-09 02:57:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 19:57:42 -06:00
|
|
|
}
|