lila/app/controllers/Tournament.scala

197 lines
6.5 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
import play.api.libs.json.JsValue
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._
2013-05-12 09:02:45 -06:00
import lila.game.{ Pov, GameRepo }
import lila.tournament.{ TournamentRepo, Created, Started, Finished, Tournament Tourney }
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())
protected def TourOptionFuRedirect[A](fua: Fu[Option[A]])(op: A Fu[Call])(implicit ctx: Context) =
fua flatMap {
_.fold(tournamentNotFound(ctx).fuccess)(a op(a) map { b Redirect(b) })
}
2013-12-29 16:21:10 -07:00
val home = Open { implicit ctx
2013-05-12 09:02:45 -06:00
tournaments zip UserRepo.allSortToints(10) map {
case (((created, started), finished), leaderboard)
Ok(html.tournament.home(created, started, finished, leaderboard))
}
}
val faq = Open { implicit ctx Ok(html.tournament.faqPage()).fuccess }
val homeReload = Open { implicit ctx
tournaments map {
case ((created, started), finished)
Ok(html.tournament.homeInner(created, started, finished))
}
}
private def tournaments =
repo.created zip repo.started zip repo.finished(20)
2014-01-28 02:57:59 -07:00
def show(id: String) = Open { implicit ctx
2013-05-12 09:02:45 -06:00
repo byId id flatMap {
_ match {
case Some(tour: Created) showCreated(tour) map { Ok(_) }
case Some(tour: Started) showStarted(tour) map { Ok(_) }
case Some(tour: Finished) showFinished(tour) map { Ok(_) }
2013-05-12 16:48:48 -06:00
case _ tournamentNotFound.fuccess
2013-05-12 09:02:45 -06:00
}
}
}
private def showCreated(tour: Created)(implicit ctx: Context) =
2014-02-01 01:57:40 -07:00
env.version(tour.id) zip chatOf(tour) map {
case (version, chat) html.tournament.show.created(tour, version, chat)
}
2013-05-12 09:02:45 -06:00
private def showStarted(tour: Started)(implicit ctx: Context) =
env.version(tour.id) zip
2014-02-01 01:57:40 -07:00
chatOf(tour) zip
2013-05-12 09:02:45 -06:00
GameRepo.games(tour recentGameIds 4) zip
tour.userCurrentPov(ctx.me).??(GameRepo.pov) map {
2014-02-01 01:57:40 -07:00
case (((version, chat), games), pov)
html.tournament.show.started(tour, version, chat, games, pov)
2013-05-12 09:02:45 -06:00
}
private def showFinished(tour: Finished)(implicit ctx: Context) =
2014-02-01 01:57:40 -07:00
env.version(tour.id) zip
chatOf(tour) zip
GameRepo.games(tour recentGameIds 4) map {
case ((version, chat), games)
html.tournament.show.finished(tour, version, chat, games)
2013-05-12 09:02:45 -06:00
}
def join(id: String) = AuthBody { implicit ctx
implicit me
NoEngine {
2013-05-12 16:48:48 -06:00
TourOptionFuRedirect(repo createdById id) { tour
2013-07-30 15:02:12 -06:00
tour.hasPassword.fold(
fuccess(routes.Tournament.joinPassword(id)),
env.api.join(tour, me, none).fold(
err routes.Tournament.home(),
_ routes.Tournament.show(tour.id)
)
2013-05-12 09:02:45 -06:00
)
}
}
}
2013-07-30 15:02:12 -06:00
def joinPasswordForm(id: String) = Auth { implicit ctx
implicit me NoEngine {
repo createdById id flatMap {
_.fold(tournamentNotFound(ctx).fuccess) { tour
renderJoinPassword(tour, env.forms.joinPassword) map { Ok(_) }
}
}
}
}
def joinPassword(id: String) = AuthBody { implicit ctx
2013-05-12 09:02:45 -06:00
implicit me
2013-07-30 15:02:12 -06:00
NoEngine {
implicit val req = ctx.body
repo createdById id flatMap {
_.fold(tournamentNotFound(ctx).fuccess) { tour
env.forms.joinPassword.bindFromRequest.fold(
err renderJoinPassword(tour, err) map { BadRequest(_) },
password env.api.join(tour, me, password.some) flatFold (
_ renderJoinPassword(tour, env.forms.joinPassword) map { BadRequest(_) },
_ fuccess(Redirect(routes.Tournament.show(tour.id)))
)
2013-07-30 15:02:12 -06:00
)
}
}
}
}
private def renderJoinPassword(tour: Created, form: Form[_])(implicit ctx: Context) =
env version tour.id map { html.tournament.joinPassword(tour, form, _) }
2013-07-30 15:02:12 -06:00
def withdraw(id: String) = Auth { implicit ctx
me
2013-05-12 16:48:48 -06:00
TourOptionFuRedirect(repo byId id) { tour
2013-05-12 09:02:45 -06:00
env.api.withdraw(tour, me.id) inject routes.Tournament.show(tour.id)
}
}
def earlyStart(id: String) = Auth { implicit ctx
implicit me
2013-05-12 16:48:48 -06:00
TourOptionFuRedirect(repo.createdByIdAndCreator(id, me.id)) { tour
2013-05-12 09:02:45 -06:00
~env.api.earlyStart(tour) inject routes.Tournament.show(tour.id)
}
}
def reload(id: String) = Open { implicit ctx
OptionFuOk(repo byId id) {
case tour: Created reloadCreated(tour)
case tour: Started reloadStarted(tour)
case tour: Finished reloadFinished(tour)
}
}
private def reloadCreated(tour: Created)(implicit ctx: Context) = fuccess {
html.tournament.show.inner(none)(html.tournament.show.createdInner(tour))
}
private def reloadStarted(tour: Started)(implicit ctx: Context) =
GameRepo.games(tour recentGameIds 4) zip
tour.userCurrentPov(ctx.me).??(GameRepo.pov) map {
2013-05-12 09:02:45 -06:00
case (games, pov) {
val pairings = html.tournament.pairings(tour)
val inner = html.tournament.show.startedInner(tour, games, pov)
html.tournament.show.inner(pairings.some)(inner)
}
}
private def reloadFinished(tour: Finished)(implicit ctx: Context) =
GameRepo games (tour recentGameIds 4) map { games
val pairings = html.tournament.pairings(tour)
val inner = html.tournament.show.finishedInner(tour, games)
html.tournament.show.inner(pairings.some)(inner)
}
def form = Auth { implicit ctx
me
NoEngine {
Ok(html.tournament.form(env.forms.create, env.forms)).fuccess
}
}
def create = AuthBody { implicit ctx
implicit me
NoEngine {
implicit val req = ctx.body
env.forms.create.bindFromRequest.fold(
err BadRequest(html.tournament.form(err, env.forms)).fuccess,
setup env.api.createTournament(setup, me) map { tour
Redirect(routes.Tournament.show(tour.id))
})
}
}
def websocket(id: String) = Socket[JsValue] { implicit ctx
~(getInt("version") |@| get("sri") apply {
case (version, uid) env.socketHandler.join(id, version, uid, ctx.me)
})
}
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
}