lila/app/controllers/Importer.scala

60 lines
1.7 KiB
Scala
Raw Normal View History

2013-05-06 19:57:42 -06:00
package controllers
import lila.app._
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
},
2015-12-23 04:02:46 -07:00
data => env.importer(data, ctx.userId) map { game =>
if (data.analyse.isDefined && game.analysable) Analyse.addCallbacks(game.id) {
Env.analyse.analyser.getOrGenerate(
game.id,
ctx.userId | "lichess",
userIp = ctx.req.remoteAddress.some,
concurrent = false,
auto = false)
}
2014-02-01 11:45:02 -07:00
Redirect(routes.Round.watcher(game.id, "white"))
2013-05-09 09:11:06 -06:00
} recover {
2014-02-17 02:12:19 -07:00
case e => {
2013-05-09 09:11:06 -06:00
logwarn(e.getMessage)
Redirect(routes.Importer.importGame)
}
}
)
}
def masterGame(id: String) = Open { implicit ctx =>
lila.game.GameRepo game id flatMap {
case Some(game) => fuccess(Redirect(routes.Round.watcher(game.id, game.firstPlayer.color.name)))
case _ => 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 = ctx.userId,
forceId = id.some) map { game =>
Redirect {
val url = routes.Round.watcher(game.id, game.firstPlayer.color.name).url
s"$url#${~getInt("ply")}"
}
2016-02-12 00:53:25 -07:00
}
}
}
}
2013-05-06 19:57:42 -06:00
}