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 }
|
2015-08-18 18:05:26 -06:00
|
|
|
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 =>
|
2015-08-18 10:03:07 -06:00
|
|
|
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 =>
|
2015-06-14 05:23:40 -06:00
|
|
|
val page = getInt("page")
|
2015-06-14 04:27:43 -06:00
|
|
|
negotiate(
|
|
|
|
html = repo byId id flatMap {
|
|
|
|
_.fold(tournamentNotFound.fuccess) { tour =>
|
2015-06-14 05:23:40 -06:00
|
|
|
env.version(tour.id) zip env.jsonView(tour, page, ctx.userId) zip chatOf(tour) map {
|
2015-06-14 04:27:43 -06:00
|
|
|
case ((version, data), chat) => html.tournament.show(tour, version, data, chat)
|
|
|
|
}
|
2015-06-13 14:34:13 -06:00
|
|
|
}
|
2015-06-14 04:27:43 -06:00
|
|
|
},
|
|
|
|
api = _ => repo byId id flatMap {
|
|
|
|
case None => NotFound(Json.obj("error" -> "No such tournament")).fuccess
|
2015-06-14 05:23:40 -06:00
|
|
|
case Some(tour) => env.jsonView(tour, page, ctx.userId) map { Ok(_) }
|
2015-06-14 04:27:43 -06:00
|
|
|
} map (_ as JSON)
|
2015-06-19 09:36:31 -06:00
|
|
|
) map NoCache
|
2015-06-13 14:34:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 14:52:04 -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
|
2015-05-13 14:52:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 18:05:26 -06:00
|
|
|
def userGameNbMini(id: String, user: String, nb: Int) = Open { implicit ctx =>
|
|
|
|
withUserGameNb(id, user, nb) { pov =>
|
2015-08-25 06:23:49 -06:00
|
|
|
Ok(html.tournament.miniGame(pov))
|
2015-08-18 18:05:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-10-02 13:31:25 -06:00
|
|
|
_.flatMap { Pov.ofUserId(_, userId) }.fold(Redirect(routes.Tournament show id))(withPov)
|
2015-08-18 18:05:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 09:50:05 -06:00
|
|
|
def join(id: String) = Auth { implicit ctx =>
|
2014-02-17 02:12:19 -07:00
|
|
|
implicit me =>
|
2015-07-26 04:26:33 -06:00
|
|
|
NoLame {
|
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-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))
|
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 =>
|
2015-06-12 07:15:35 -06:00
|
|
|
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
|
2015-05-21 22:06:54 -06:00
|
|
|
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 =>
|
2015-07-26 04:26:33 -06:00
|
|
|
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 =>
|
2015-07-26 04:26:33 -06:00
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|