lila/app/controllers/Game.scala

69 lines
2.4 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
2018-03-31 19:48:52 -06:00
import scala.concurrent.duration._
2018-03-07 16:55:02 -07:00
2013-05-06 19:57:42 -06:00
import lila.app._
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-03-07 16:50:59 -07:00
def exportForm = Auth { implicit ctx => me =>
2017-01-25 09:45:00 -07:00
Env.security.forms.emptyWithCaptcha map {
2018-03-07 16:50:59 -07:00
case (form, captcha) => Ok(html.game.export(form, captcha))
2017-01-25 09:45:00 -07:00
}
}
2018-03-07 16:50:59 -07:00
def exportConfirm = AuthBody { implicit ctx => me =>
2017-01-25 09:45:00 -07:00
implicit val req = ctx.body
2018-03-07 16:50:59 -07:00
Env.security.forms.empty.bindFromRequest.fold(
err => Env.security.forms.anyCaptcha map { captcha =>
BadRequest(html.game.export(err, captcha))
},
2018-04-02 16:01:00 -06:00
_ => fuccess(streamGamesPgn(me, since = none, until = none))
2018-03-07 16:50:59 -07:00
)
}
2018-03-31 19:48:52 -06:00
def exportApi = Scoped(_.Game.Read) { req => me =>
val since = getLong("since", req) map { ts => new DateTime(ts) }
2018-04-02 16:01:00 -06:00
val until = getLong("until", req) map { ts => new DateTime(ts) }
2018-04-02 17:19:53 -06:00
val max = getInt("max", req)
fuccess(streamGamesPgn(me, since, until, max))
2018-03-07 16:50:59 -07:00
}
2018-03-07 16:55:02 -07:00
private val ExportRateLimitPerUser = new lila.memo.RateLimit[lila.user.User.ID](
credits = 10,
duration = 1 day,
name = "game export per user",
key = "game_export.user"
)
2018-04-02 17:19:53 -06:00
private def streamGamesPgn(user: lila.user.User, since: Option[DateTime], until: Option[DateTime], max: Option[Int] = None) =
2018-03-07 16:55:02 -07:00
ExportRateLimitPerUser(user.id, cost = 1) {
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
2018-04-02 17:19:53 -06:00
Ok.chunked(Env.api.pgnDump.exportUserGames(user.id, since, until, max | Int.MaxValue)).withHeaders(
2018-03-07 16:55:02 -07:00
CONTENT_TYPE -> pgnContentType,
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${user.username}_$date.pgn")
)
}
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
}