lila/app/controllers/Game.scala

96 lines
2.8 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-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 =>
2013-05-09 17:00:27 -06:00
Reasonable(page, 100) {
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
2014-02-17 02:12:19 -07:00
def realtime = Open { implicit ctx =>
GameRepo.featuredCandidates map lila.tv.Featured.sort map (_ take 9) zip
2013-06-05 02:26:42 -06:00
makeListMenu map {
2014-02-17 02:12:19 -07:00
case (games, menu) => html.game.realtime(games, menu)
2013-06-05 02:26:42 -06:00
}
2013-05-06 19:57:42 -06:00
}
2014-02-17 02:12:19 -07:00
def all(page: Int) = Open { implicit ctx =>
2013-05-06 19:57:42 -06:00
Reasonable(page) {
paginator recent page zip makeListMenu map {
2014-02-17 02:12:19 -07:00
case (pag, menu) => html.game.all(pag, menu)
2013-05-06 19:57:42 -06:00
}
}
}
2014-02-17 02:12:19 -07:00
def checkmate(page: Int) = Open { implicit ctx =>
2013-05-06 19:57:42 -06:00
Reasonable(page) {
paginator checkmate page zip makeListMenu map {
2014-02-17 02:12:19 -07:00
case (pag, menu) => html.game.checkmate(pag, menu)
2013-05-06 19:57:42 -06:00
}
}
}
2014-02-17 02:12:19 -07:00
def bookmark(page: Int) = Auth { implicit ctx =>
me =>
2013-05-06 19:57:42 -06:00
Reasonable(page) {
2013-05-09 09:11:06 -06:00
Env.bookmark.api.gamePaginatorByUser(me, page) zip makeListMenu map {
2014-02-17 02:12:19 -07:00
case (pag, menu) => html.game.bookmarked(pag, menu)
2013-05-06 19:57:42 -06:00
}
}
}
2014-02-17 02:12:19 -07:00
def analysed(page: Int) = Open { implicit ctx =>
2013-05-06 19:57:42 -06:00
Reasonable(page) {
analysePaginator games page zip makeListMenu map {
2014-02-17 02:12:19 -07:00
case (pag, menu) => html.game.analysed(pag, menu)
2013-05-06 19:57:42 -06:00
}
}
}
2014-02-17 02:12:19 -07:00
def imported(page: Int) = Open { implicit ctx =>
2013-05-06 19:57:42 -06:00
Reasonable(page) {
paginator imported page zip makeListMenu map {
2014-02-17 02:12:19 -07:00
case (pag, menu) => html.game.imported(pag, menu)
2013-05-06 19:57:42 -06:00
}
}
}
def export(user: String) = Auth { implicit ctx =>
me =>
if (me.id == user.toLowerCase) fuccess {
play.api.Logger("export").info(s"$user from ${ctx.req.remoteAddress}")
2014-07-17 16:15:02 -06:00
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 user).withHeaders(
CONTENT_TYPE -> ContentTypes.TEXT,
2014-07-20 15:55:47 -06:00
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_${me.username}_$date.pgn"))
}
else notFound
}
2013-05-06 19:57:42 -06:00
}