lila/app/controllers/Game.scala

76 lines
3.0 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-04-02 22:27:40 -06:00
import play.api.mvc.RequestHeader
import scala.concurrent.duration._
2018-03-07 16:55:02 -07:00
import lila.api.PgnDump
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))
}
}
}
def export(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 =>
RequireHttp11(req) {
2018-04-09 18:06:35 -06:00
Api.GlobalLinearLimitPerIP(HTTPRequest lastRemoteAddress req) {
Api.GlobalLinearLimitPerUserOption(me) {
val config = PgnDump.Config(
user = user,
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,
2018-04-23 06:54:12 -06:00
color = get("color", req) flatMap chess.Color.apply,
2018-05-06 07:55:58 -06:00
analysed = getBoolOpt("analysed", req),
flags = lila.game.PgnDump.WithFlags(
moves = getBoolOpt("moves", req) | true,
tags = getBoolOpt("tags", req) | true,
clocks = getBoolOpt("clocks", req) | false,
evals = getBoolOpt("evals", req) | 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
})
)
2018-04-09 18:06:35 -06:00
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
Ok.chunked(Env.api.pgnDump.exportUserGames(config)).withHeaders(
CONTENT_TYPE -> pgnContentType,
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${user.username}_$date.pgn")
).fuccess
}
}
}
}
}
2018-03-07 16:50:59 -07: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
}