lila/app/controllers/Importer.scala

63 lines
2 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
2014-02-17 02:12:19 -07:00
def importGame = Open { implicit ctx =>
2015-04-23 01:01:32 -06:00
fuccess {
Ok(html.game.importGame(env.forms.importForm))
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(
2015-04-23 01:01:32 -06:00
failure => fuccess {
Ok(html.game.importGame(failure))
2013-05-09 09:11:06 -06:00
},
data => env.importer(data, ctx.userId) flatMap { game =>
(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))
} inject Redirect(routes.Round.watcher(game.id, "white"))
2013-05-09 09:11:06 -06:00
} recover {
case e =>
logger.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
}