lila/app/controllers/Game.scala

52 lines
1.8 KiB
Scala
Raw Normal View History

2013-05-06 19:57:42 -06:00
package controllers
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))
}
2017-01-25 09:45:00 -07:00
else fuccess {
Redirect(routes.Round.watcher(game.id, game.firstColor.name))
}
}
}
2017-01-25 09:45:00 -07:00
def export(user: String) = Auth { implicit ctx => _ =>
Env.security.forms.emptyWithCaptcha map {
case (form, captcha) => Ok(html.game.export(user, form, captcha))
}
}
2017-01-25 09:45:00 -07:00
def exportConfirm(user: String) = AuthBody { implicit ctx => me =>
implicit val req = ctx.body
val userId = user.toLowerCase
if (me.id == userId)
Env.security.forms.empty.bindFromRequest.fold(
err => Env.security.forms.anyCaptcha map { captcha =>
BadRequest(html.game.export(userId, err, captcha))
},
_ => fuccess {
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
Ok.chunked(Env.api.pgnDump exportUserGames userId).withHeaders(
CONTENT_TYPE -> pgnContentType,
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${me.username}_$date.pgn"))
})
else notFound
}
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
}