lila/app/controllers/Game.scala

64 lines
2.1 KiB
Scala
Raw Normal View History

2013-05-06 19:57:42 -06:00
package controllers
import play.api.mvc.Action
2013-05-06 19:57:42 -06:00
import lila.app._
2014-12-07 17:14:03 -07:00
import lila.common.HTTPRequest
2014-02-17 02:12:19 -07:00
import lila.game.{ Game => GameModel, GameRepo }
import play.api.http.ContentTypes
2013-05-06 19:57:42 -06:00
import views._
object Game extends LilaController with BaseGame {
private def paginator = Env.game.paginator
private def analysePaginator = Env.analyse.paginator
private def cached = Env.game.cached
2013-05-09 17:00:27 -06:00
private def searchEnv = Env.gameSearch
def searchForm = searchEnv.forms.search
2014-02-17 02:12:19 -07:00
def search(page: Int) = OpenBody { implicit ctx =>
2014-12-07 17:14:03 -07:00
if (HTTPRequest.isBot(ctx.req)) notFound
else Reasonable(page, 100) {
2013-05-09 17:00:27 -06:00
implicit def req = ctx.body
2014-02-17 02:12:19 -07:00
makeListMenu flatMap { listMenu =>
2013-05-09 17:00:27 -06:00
searchForm.bindFromRequest.fold(
2014-02-17 02:12:19 -07:00
failure => Ok(html.game.search(listMenu, failure)).fuccess,
2014-03-05 13:11:55 -07:00
data => searchEnv.nonEmptyQuery(data) ?? { query =>
searchEnv.paginator(query, page) map (_.some)
} map { pager =>
Ok(html.game.search(listMenu, searchForm fill data, pager))
}
2014-03-02 15:58:13 -07:00
)
2013-05-09 17:00:27 -06:00
}
}
}
2013-05-06 19:57:42 -06:00
def export(user: String) = Auth { implicit ctx =>
_ =>
Env.security.forms.emptyWithCaptcha map {
case (form, captcha) => Ok(html.game.export(user, form, captcha))
}
}
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 {
play.api.Logger("export").info(s"$user from ${ctx.req.remoteAddress}")
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
Ok.chunked(Env.game export userId).withHeaders(
CONTENT_TYPE -> ContentTypes.TEXT,
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${me.username}_$date.pgn"))
})
else notFound
}
2013-05-06 19:57:42 -06:00
}