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
|
2015-01-22 06:39:17 -07:00
|
|
|
import lila.game.{ Pov, GameRepo }
|
2014-07-23 15:08:59 -06:00
|
|
|
import lila.tournament.{ System, 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())
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
val home = Open { implicit ctx =>
|
2014-04-10 15:01:45 -06:00
|
|
|
fetchTournaments zip repo.scheduled zip UserRepo.allSortToints(10) map {
|
|
|
|
case ((((created, started), finished), scheduled), leaderboard) =>
|
|
|
|
Ok(html.tournament.home(created, started, finished, scheduled, leaderboard))
|
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
|
|
|
|
case "swiss" => System.Swiss.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
|
|
|
val homeReload = Open { implicit ctx =>
|
2014-04-10 15:01:45 -06:00
|
|
|
fetchTournaments map {
|
2014-02-17 02:12:19 -07:00
|
|
|
case ((created, started), finished) =>
|
2013-05-12 09:02:45 -06:00
|
|
|
Ok(html.tournament.homeInner(created, started, finished))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 15:01:45 -06:00
|
|
|
private def fetchTournaments =
|
2015-01-18 09:55:07 -07:00
|
|
|
env allCreatedSorted true zip repo.publicStarted zip repo.finished(10)
|
2013-05-12 09:02:45 -06:00
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def show(id: String) = Open { implicit ctx =>
|
2013-05-12 09:02:45 -06:00
|
|
|
repo byId id flatMap {
|
2015-01-04 11:28:30 -07:00
|
|
|
_.fold(tournamentNotFound.fuccess) { tour =>
|
2015-01-04 16:51:42 -07:00
|
|
|
env.version(tour.id) zip
|
|
|
|
env.jsonView(tour) zip
|
|
|
|
chatOf(tour) map {
|
|
|
|
case ((version, data), chat) => html.tournament.show(tour, version, data, chat)
|
|
|
|
}
|
2013-05-12 09:02:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def join(id: String) = AuthBody { implicit ctx =>
|
|
|
|
implicit me =>
|
2013-05-12 09:02:45 -06:00
|
|
|
NoEngine {
|
2015-01-04 08:43:17 -07:00
|
|
|
negotiate(
|
2015-01-05 05:28:48 -07:00
|
|
|
html = repo enterableById id map {
|
2015-01-05 06:29:52 -07:00
|
|
|
case None => tournamentNotFound
|
2015-01-05 05:28:48 -07:00
|
|
|
case Some(tour) =>
|
2015-01-05 06:29:52 -07:00
|
|
|
env.api.join(tour, 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-01-05 06:29:52 -07:00
|
|
|
env.api.join(tour, me)
|
2015-01-04 08:43:17 -07:00
|
|
|
fuccess(Json.obj("ok" -> true))
|
2014-05-10 15:27:06 -06:00
|
|
|
}
|
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 =>
|
2014-05-10 15:27:06 -06:00
|
|
|
env.api.withdraw(tour, 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).url)
|
2013-05-12 09:02:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 16:18:39 -07:00
|
|
|
def berserk(id: String) = Auth { implicit ctx =>
|
|
|
|
me =>
|
|
|
|
OptionResult(repo startedById id) { tour =>
|
|
|
|
env.api.berserk(tour, me.id)
|
|
|
|
Ok(Json.obj("ok" -> true)) as JSON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def earlyStart(id: String) = Auth { implicit ctx =>
|
|
|
|
implicit me =>
|
2015-01-04 16:51:42 -07:00
|
|
|
OptionResult(repo.createdByIdAndCreator(id, me.id)) { tour =>
|
2014-05-10 15:27:06 -06:00
|
|
|
env.api startIfReady tour
|
2015-01-04 16:51:42 -07:00
|
|
|
Ok(Json.obj("ok" -> true)) as JSON
|
2013-05-12 09:02:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:12:19 -07:00
|
|
|
def form = Auth { implicit ctx =>
|
|
|
|
me =>
|
2013-05-12 09:02:45 -06:00
|
|
|
NoEngine {
|
|
|
|
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 =>
|
2013-05-12 09:02:45 -06:00
|
|
|
NoEngine {
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 06:39:17 -07:00
|
|
|
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)
|
2015-01-22 06:39:17 -07:00
|
|
|
}
|
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 ?? {
|
2014-02-01 02:19:47 -07:00
|
|
|
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
|
|
|
}
|