lila/app/controllers/Game.scala

137 lines
5.3 KiB
Scala
Raw Normal View History

2013-05-06 19:57:42 -06:00
package controllers
2018-03-07 17:20:00 -07:00
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import play.api.mvc._
import scala.concurrent.duration._
2018-03-07 16:55:02 -07:00
2018-05-06 16:40:17 -06:00
import lila.api.GameApiV2
2013-05-06 19:57:42 -06:00
import lila.app._
2018-04-09 18:06:35 -06:00
import lila.common.{ MaxPerSecond, HTTPRequest }
2017-01-25 09:45:00 -07:00
import lila.game.{ GameRepo, Game => GameModel }
2013-05-06 19:57:42 -06:00
import views._
2015-04-23 01:01:32 -06:00
object Game extends LilaController {
2013-05-06 19:57:42 -06:00
2017-01-25 09:45:00 -07:00
def delete(gameId: String) = Auth { implicit ctx => me =>
OptionFuResult(GameRepo game gameId) { game =>
if (game.pgnImport.flatMap(_.user) ?? (me.id==)) {
Env.hub.actor.bookmark ! lila.hub.actorApi.bookmark.Remove(game.id)
(GameRepo remove game.id) >>
(lila.analyse.AnalysisRepo remove game.id) >>
Env.game.cached.clearNbImportedByCache(me.id) inject
Redirect(routes.User.show(me.username))
} else fuccess {
2017-01-25 09:45:00 -07:00
Redirect(routes.Round.watcher(game.id, game.firstColor.name))
}
}
}
2018-05-06 18:10:47 -06:00
def exportOne(id: String) = Open { implicit ctx =>
OptionFuResult(GameRepo game id) { game =>
if (game.playable) BadRequest("Only bots can access their games in progress. See https://lichess.org/api#tag/Chess-Bot").fuccess
2018-05-06 18:10:47 -06:00
else {
val config = GameApiV2.OneConfig(
format = if (HTTPRequest acceptsJson ctx.req) GameApiV2.Format.JSON else GameApiV2.Format.PGN,
imported = getBool("imported"),
2018-05-06 18:17:23 -06:00
flags = requestPgnFlags(ctx.req, extended = true)
2018-05-06 18:10:47 -06:00
)
lila.mon.export.pgn.game()
Env.api.gameApiV2.exportOne(game, config) flatMap { content =>
Env.api.gameApiV2.filename(game, config.format) map { filename =>
Ok(content).withHeaders(
CONTENT_TYPE -> gameContentType(config),
CONTENT_DISPOSITION -> s"attachment; filename=$filename"
)
}
}
}
}
}
def exportByUser(username: String) = OpenOrScoped()(
open = ctx => handleExport(username, ctx.me, ctx.req, oauth = false),
scoped = req => me => handleExport(username, me.some, req, oauth = true)
)
2018-03-07 16:50:59 -07:00
private def handleExport(username: String, me: Option[lila.user.User], req: RequestHeader, oauth: Boolean) =
lila.user.UserRepo named username flatMap {
_ ?? { user =>
2018-05-08 20:38:45 -06:00
Api.GlobalLinearLimitPerIP(HTTPRequest lastRemoteAddress req) {
Api.GlobalLinearLimitPerUserOption(me) {
val format = GameApiV2.Format byRequest req
WithVs(req) { vs =>
val config = GameApiV2.ByUserConfig(
user = user,
format = format,
vs = vs,
since = getLong("since", req) map { ts => new DateTime(ts) },
until = getLong("until", req) map { ts => new DateTime(ts) },
max = getInt("max", req) map (_ atLeast 1),
rated = getBoolOpt("rated", req),
perfType = ~get("perfType", req) split "," flatMap { lila.rating.PerfType(_) } toSet,
color = get("color", req) flatMap chess.Color.apply,
analysed = getBoolOpt("analysed", req),
flags = requestPgnFlags(req, extended = false),
perSecond = MaxPerSecond(me match {
case Some(m) if m is user.id => 50
case Some(_) if oauth => 20 // bonus for oauth logged in only (not for XSRF)
case _ => 10
})
)
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
Ok.chunked(Env.api.gameApiV2.exportByUser(config)).withHeaders(
CONTENT_TYPE -> gameContentType(config),
CONTENT_DISPOSITION -> s"attachment; filename=lichess_${user.username}_$date.${format.toString.toLowerCase}"
).fuccess
2018-04-09 18:06:35 -06:00
}
}
}
}
}
2018-03-07 16:50:59 -07:00
def exportByIds = Action.async(parse.tolerantText) { req =>
2018-05-08 20:38:45 -06:00
Api.GlobalLinearLimitPerIP(HTTPRequest lastRemoteAddress req) {
val format = GameApiV2.Format byRequest req
val config = GameApiV2.ByIdsConfig(
ids = req.body.split(',').take(300),
format = GameApiV2.Format byRequest req,
flags = requestPgnFlags(req, extended = false),
perSecond = MaxPerSecond(20)
)
Ok.chunked(Env.api.gameApiV2.exportByIds(config)).withHeaders(
CONTENT_TYPE -> gameContentType(config)
).fuccess
}
}
private def WithVs(req: RequestHeader)(f: Option[lila.user.User] => Fu[Result]): Fu[Result] =
get("vs", req) match {
case None => f(none)
case Some(name) => lila.user.UserRepo named name flatMap {
case None => notFoundJson(s"No such opponent: $name")
case Some(user) => f(user.some)
}
}
2018-05-06 18:17:23 -06:00
private def requestPgnFlags(req: RequestHeader, extended: Boolean) =
2018-05-06 18:10:47 -06:00
lila.game.PgnDump.WithFlags(
moves = getBoolOpt("moves", req) | true,
tags = getBoolOpt("tags", req) | true,
2018-05-06 18:17:23 -06:00
clocks = getBoolOpt("clocks", req) | extended,
evals = getBoolOpt("evals", req) | extended,
opening = getBoolOpt("opening", req) | extended
2018-05-06 18:10:47 -06:00
)
private def gameContentType(config: GameApiV2.Config) = config.format match {
case GameApiV2.Format.PGN => pgnContentType
case GameApiV2.Format.JSON => config match {
case _: GameApiV2.OneConfig => JSON
case _ => ndJsonContentType
2018-05-06 18:10:47 -06:00
}
}
2017-01-25 09:45:00 -07:00
private[controllers] def preloadUsers(game: GameModel): Funit =
Env.user.lightUserApi preloadMany game.userIds
2013-05-06 19:57:42 -06:00
}