lila/app/controllers/Tournament.scala

150 lines
4.6 KiB
Scala
Raw Normal View History

2013-05-07 17:44:26 -06:00
package controllers
2013-07-30 15:02:12 -06:00
import play.api.data.Form
2015-01-04 08:43:17 -07:00
import play.api.libs.json._
2013-07-30 15:02:12 -06:00
import play.api.mvc._
2013-12-29 02:51:40 -07:00
import lila.api.Context
2013-07-30 15:02:12 -06:00
import lila.app._
2015-01-18 10:40:22 -07:00
import lila.common.HTTPRequest
import lila.game.{ Pov, GameRepo }
import lila.tournament.{ System, TournamentRepo, PairingRepo, Tournament => Tourney, VisibleTournaments }
2013-12-27 15:12:20 -07:00
import lila.user.UserRepo
2013-12-29 02:51:40 -07:00
import views._
2013-05-07 17:44:26 -06:00
object Tournament extends LilaController {
2013-05-12 09:02:45 -06:00
private def env = Env.tournament
private def repo = TournamentRepo
2013-05-12 16:48:48 -06:00
private def tournamentNotFound(implicit ctx: Context) = NotFound(html.tournament.notFound())
2014-02-17 02:12:19 -07:00
val home = Open { implicit ctx =>
env.api.fetchVisibleTournaments zip
repo.scheduledDedup zip
repo.finished(30) zip
UserRepo.allSortToints(10) map {
case (((visible, scheduled), finished), leaderboard) =>
Ok(html.tournament.home(scheduled, finished, leaderboard, env scheduleJsonView visible))
} map NoCache
2013-05-12 09:02:45 -06:00
}
2014-07-23 15:08:59 -06:00
def help(sysStr: Option[String]) = Open { implicit ctx =>
val system = sysStr flatMap {
case "arena" => System.Arena.some
2015-01-04 02:17:04 -07:00
case _ => none
2014-07-23 15:08:59 -06:00
}
Ok(html.tournament.faqPage(system)).fuccess
}
2013-05-12 09:02:45 -06:00
2014-02-17 02:12:19 -07:00
def show(id: String) = Open { implicit ctx =>
val page = getInt("page")
negotiate(
html = repo byId id flatMap {
_.fold(tournamentNotFound.fuccess) { tour =>
env.version(tour.id) zip env.jsonView(tour, page, ctx.userId) zip chatOf(tour) map {
case ((version, data), chat) => html.tournament.show(tour, version, data, chat)
}
}
},
api = _ => repo byId id flatMap {
case None => NotFound(Json.obj("error" -> "No such tournament")).fuccess
case Some(tour) => env.jsonView(tour, page, ctx.userId) map { Ok(_) }
} map (_ as JSON)
2015-06-19 09:36:31 -06:00
) map NoCache
}
def standing(id: String, page: Int) = Open { implicit ctx =>
OptionFuResult(repo byId id) { tour =>
env.jsonView.standing(tour, page) map { data =>
Ok(data) as JSON
2013-05-12 09:02:45 -06:00
}
}
}
def gameStanding(id: String) = Open { implicit ctx =>
2015-06-11 16:54:51 -06:00
env.api.miniStanding(id, true) map {
case Some(m) if !m.tour.isCreated => Ok(html.tournament.gameStanding(m))
case _ => NotFound
}
}
def userGameNbMini(id: String, user: String, nb: Int) = Open { implicit ctx =>
withUserGameNb(id, user, nb) { pov =>
Ok(html.game.mini(pov))
}
}
def userGameNbShow(id: String, user: String, nb: Int) = Open { implicit ctx =>
withUserGameNb(id, user, nb) { pov =>
Redirect(routes.Round.watcher(pov.game.id, pov.color.name))
}
}
private def withUserGameNb(id: String, user: String, nb: Int)(withPov: Pov => Result)(implicit ctx: Context): Fu[Result] = {
val userId = lila.user.User normalize user
OptionFuResult(PairingRepo.byTourUserNb(id, userId, nb)) { pairing =>
GameRepo game pairing.id map {
_.flatMap { Pov.ofUserId(_, userId) }.fold(NotFound("nope"))(withPov)
}
}
}
def join(id: String) = Auth { implicit ctx =>
2014-02-17 02:12:19 -07:00
implicit me =>
NoLame {
2015-01-04 08:43:17 -07:00
negotiate(
2015-01-05 05:28:48 -07:00
html = repo enterableById id map {
case None => tournamentNotFound
2015-01-05 05:28:48 -07:00
case Some(tour) =>
2015-06-11 16:54:51 -06:00
env.api.join(tour.id, me)
2015-01-05 05:28:48 -07:00
Redirect(routes.Tournament.show(tour.id))
2015-01-04 08:43:17 -07:00
},
api = _ => OptionFuOk(repo enterableById id) { tour =>
2015-06-11 16:54:51 -06:00
env.api.join(tour.id, me)
2015-01-04 08:43:17 -07:00
fuccess(Json.obj("ok" -> true))
}
2015-01-04 08:43:17 -07:00
)
2013-05-12 09:02:45 -06:00
}
}
2014-02-17 02:12:19 -07:00
def withdraw(id: String) = Auth { implicit ctx =>
me =>
2015-01-04 08:43:17 -07:00
OptionResult(repo byId id) { tour =>
env.api.withdraw(tour.id, me.id)
2015-01-18 10:40:22 -07:00
if (HTTPRequest.isXhr(ctx.req)) Ok(Json.obj("ok" -> true)) as JSON
else Redirect(routes.Tournament.show(tour.id))
2013-05-12 09:02:45 -06:00
}
}
2014-02-17 02:12:19 -07:00
def form = Auth { implicit ctx =>
me =>
NoLame {
2013-05-12 09:02:45 -06:00
Ok(html.tournament.form(env.forms.create, env.forms)).fuccess
}
}
2014-02-17 02:12:19 -07:00
def create = AuthBody { implicit ctx =>
implicit me =>
NoLame {
2013-05-12 09:02:45 -06:00
implicit val req = ctx.body
env.forms.create.bindFromRequest.fold(
2014-02-17 02:12:19 -07:00
err => BadRequest(html.tournament.form(err, env.forms)).fuccess,
setup => env.api.createTournament(setup, me) map { tour =>
2013-05-12 09:02:45 -06:00
Redirect(routes.Tournament.show(tour.id))
})
}
}
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>
(getInt("version") |@| get("sri")).tupled ?? {
2014-02-17 02:12:19 -07:00
case (version, uid) => env.socketHandler.join(id, version, uid, ctx.me)
}
2013-05-12 09:02:45 -06:00
}
2014-02-01 01:57:40 -07:00
private def chatOf(tour: lila.tournament.Tournament)(implicit ctx: Context) =
2014-02-01 02:05:27 -07:00
ctx.isAuth ?? {
Env.chat.api.userChat find tour.id map (_.forUser(ctx.me).some)
2014-02-01 02:05:27 -07:00
}
2013-05-07 17:44:26 -06:00
}