tournament api join feedback - closes #2481
This commit is contained in:
parent
3130bc9d31
commit
608c41f976
|
@ -37,10 +37,12 @@ private[controllers] trait LilaController
|
||||||
|
|
||||||
protected implicit def LilaHtmlToResult(content: Html): Result = Ok(content)
|
protected implicit def LilaHtmlToResult(content: Html): Result = Ok(content)
|
||||||
|
|
||||||
|
protected val jsonOkBody = Json.obj("ok" -> true)
|
||||||
|
|
||||||
protected implicit def LilaFunitToResult(funit: Funit)(implicit ctx: Context): Fu[Result] =
|
protected implicit def LilaFunitToResult(funit: Funit)(implicit ctx: Context): Fu[Result] =
|
||||||
negotiate(
|
negotiate(
|
||||||
html = fuccess(Ok("ok")),
|
html = fuccess(Ok("ok")),
|
||||||
api = _ => fuccess(Ok(Json.obj("ok" -> true)) as JSON))
|
api = _ => fuccess(Ok(jsonOkBody) as JSON))
|
||||||
|
|
||||||
implicit def lang(implicit req: RequestHeader) = Env.i18n.pool lang req
|
implicit def lang(implicit req: RequestHeader) = Env.i18n.pool lang req
|
||||||
|
|
||||||
|
|
|
@ -58,27 +58,27 @@ object Tournament extends LilaController {
|
||||||
val page = getInt("page")
|
val page = getInt("page")
|
||||||
negotiate(
|
negotiate(
|
||||||
html = repo byId id flatMap {
|
html = repo byId id flatMap {
|
||||||
_.fold(tournamentNotFound.fuccess) { tour =>
|
_.fold(tournamentNotFound.fuccess) { tour =>
|
||||||
(env.api.verdicts(tour, ctx.me) zip
|
(env.api.verdicts(tour, ctx.me) zip
|
||||||
env.version(tour.id) zip {
|
env.version(tour.id) zip {
|
||||||
ctx.noKid ?? Env.chat.api.userChat.findMine(tour.id, ctx.me).map(some)
|
ctx.noKid ?? Env.chat.api.userChat.findMine(tour.id, ctx.me).map(some)
|
||||||
}).flatMap {
|
}).flatMap {
|
||||||
case ((verdicts, version), chat) => env.jsonView(tour, page, ctx.me, none, version.some) map {
|
case ((verdicts, version), chat) => env.jsonView(tour, page, ctx.me, none, version.some) map {
|
||||||
html.tournament.show(tour, verdicts, _, chat)
|
html.tournament.show(tour, verdicts, _, chat)
|
||||||
}
|
}
|
||||||
}.map { Ok(_) }.mon(_.http.response.tournament.show.website)
|
}.map { Ok(_) }.mon(_.http.response.tournament.show.website)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
api = _ => repo byId id flatMap {
|
api = _ => repo byId id flatMap {
|
||||||
case None => NotFound(jsonError("No such tournament")).fuccess
|
case None => NotFound(jsonError("No such tournament")).fuccess
|
||||||
case Some(tour) => {
|
case Some(tour) => {
|
||||||
get("playerInfo").?? { env.api.playerInfo(tour.id, _) } zip
|
get("playerInfo").?? { env.api.playerInfo(tour.id, _) } zip
|
||||||
getBool("socketVersion").??(env version tour.id map some) flatMap {
|
getBool("socketVersion").??(env version tour.id map some) flatMap {
|
||||||
case (playerInfoExt, socketVersion) =>
|
case (playerInfoExt, socketVersion) =>
|
||||||
env.jsonView(tour, page, ctx.me, playerInfoExt, socketVersion)
|
env.jsonView(tour, page, ctx.me, playerInfoExt, socketVersion)
|
||||||
} map { Ok(_) }
|
} map { Ok(_) }
|
||||||
}.mon(_.http.response.tournament.show.mobile)
|
}.mon(_.http.response.tournament.show.mobile)
|
||||||
} map (_ as JSON)
|
} map (_ as JSON)
|
||||||
) map NoCache
|
) map NoCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,79 +126,74 @@ object Tournament extends LilaController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def join(id: String) = AuthBody(BodyParsers.parse.json) { implicit ctx =>
|
def join(id: String) = AuthBody(BodyParsers.parse.json) { implicit ctx => implicit me =>
|
||||||
implicit me =>
|
NoLame {
|
||||||
NoLame {
|
val password = ctx.body.body.\("p").asOpt[String]
|
||||||
val password = ctx.body.body.\("p").asOpt[String]
|
negotiate(
|
||||||
negotiate(
|
html = repo enterableById id map {
|
||||||
html = repo enterableById id map {
|
case None => tournamentNotFound
|
||||||
case None => tournamentNotFound
|
case Some(tour) =>
|
||||||
case Some(tour) =>
|
env.api.join(tour.id, me, password)
|
||||||
env.api.join(tour.id, me, password)
|
Redirect(routes.Tournament.show(tour.id))
|
||||||
Redirect(routes.Tournament.show(tour.id))
|
},
|
||||||
},
|
api = _ => OptionFuResult(repo enterableById id) { tour =>
|
||||||
api = _ => OptionFuOk(repo enterableById id) { tour =>
|
env.api.joinWithResult(tour.id, me, password) map { result =>
|
||||||
env.api.join(tour.id, me, password)
|
if (result) Ok(jsonOkBody)
|
||||||
fuccess(Json.obj("ok" -> true))
|
else BadRequest(Json.obj("joined" -> false))
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def withdraw(id: String) = Auth { implicit ctx =>
|
def withdraw(id: String) = Auth { implicit ctx => me =>
|
||||||
me =>
|
OptionResult(repo byId id) { tour =>
|
||||||
OptionResult(repo byId id) { tour =>
|
env.api.withdraw(tour.id, me.id)
|
||||||
env.api.withdraw(tour.id, me.id)
|
if (HTTPRequest.isXhr(ctx.req)) Ok(Json.obj("ok" -> true)) as JSON
|
||||||
if (HTTPRequest.isXhr(ctx.req)) Ok(Json.obj("ok" -> true)) as JSON
|
else Redirect(routes.Tournament.show(tour.id))
|
||||||
else Redirect(routes.Tournament.show(tour.id))
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def terminate(id: String) = Secure(_.TerminateTournament) { implicit ctx =>
|
def terminate(id: String) = Secure(_.TerminateTournament) { implicit ctx => me =>
|
||||||
me =>
|
OptionResult(repo startedById id) { tour =>
|
||||||
OptionResult(repo startedById id) { tour =>
|
env.api finish tour
|
||||||
env.api finish tour
|
Env.mod.logApi.terminateTournament(me.id, tour.fullName)
|
||||||
Env.mod.logApi.terminateTournament(me.id, tour.fullName)
|
Redirect(routes.Tournament show tour.id)
|
||||||
Redirect(routes.Tournament show tour.id)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def form = Auth { implicit ctx =>
|
def form = Auth { implicit ctx => me =>
|
||||||
me =>
|
NoLame {
|
||||||
NoLame {
|
Ok(html.tournament.form(env.forms.create, env.forms)).fuccess
|
||||||
Ok(html.tournament.form(env.forms.create, env.forms)).fuccess
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def create = AuthBody { implicit ctx =>
|
def create = AuthBody { implicit ctx => implicit me =>
|
||||||
implicit me =>
|
NoLame {
|
||||||
NoLame {
|
import play.api.i18n.Messages.Implicits._
|
||||||
import play.api.i18n.Messages.Implicits._
|
import play.api.Play.current
|
||||||
import play.api.Play.current
|
implicit val req = ctx.body
|
||||||
implicit val req = ctx.body
|
negotiate(
|
||||||
negotiate (
|
html = env.forms.create.bindFromRequest.fold(
|
||||||
html = env.forms.create.bindFromRequest.fold(
|
err => BadRequest(html.tournament.form(err, env.forms)).fuccess,
|
||||||
err => BadRequest(html.tournament.form(err, env.forms)).fuccess,
|
setup => env.api.createTournament(setup, me) map { tour =>
|
||||||
setup => env.api.createTournament(setup, me) map { tour =>
|
Redirect(routes.Tournament.show(tour.id))
|
||||||
Redirect(routes.Tournament.show(tour.id))
|
}),
|
||||||
}),
|
api = _ => env.forms.create.bindFromRequest.fold(
|
||||||
api = _ => env.forms.create.bindFromRequest.fold(
|
err => BadRequest(errorsAsJson(err)).fuccess,
|
||||||
err => BadRequest(errorsAsJson(err)).fuccess,
|
setup => env.api.createTournament(setup, me) map { tour =>
|
||||||
setup => env.api.createTournament(setup, me) map { tour =>
|
Ok(Json.obj("id" -> tour.id))
|
||||||
Ok(Json.obj("id" -> tour.id))
|
})
|
||||||
})
|
)
|
||||||
)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def limitedInvitation = Auth { implicit ctx =>
|
def limitedInvitation = Auth { implicit ctx => me =>
|
||||||
me =>
|
env.api.fetchVisibleTournaments.flatMap { tours =>
|
||||||
env.api.fetchVisibleTournaments.flatMap { tours =>
|
lila.tournament.TournamentInviter.findNextFor(me, tours, env.verify.canEnter(me))
|
||||||
lila.tournament.TournamentInviter.findNextFor(me, tours, env.verify.canEnter(me))
|
} map {
|
||||||
} map {
|
case None => Redirect(routes.Tournament.home(1))
|
||||||
case None => Redirect(routes.Tournament.home(1))
|
case Some(t) => Redirect(routes.Tournament.show(t.id))
|
||||||
case Some(t) => Redirect(routes.Tournament.show(t.id))
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>
|
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>
|
||||||
|
|
|
@ -4,7 +4,7 @@ import scala.concurrent.duration._
|
||||||
import scala.concurrent.Future
|
import scala.concurrent.Future
|
||||||
|
|
||||||
import akka.actor.ActorRef
|
import akka.actor.ActorRef
|
||||||
import akka.pattern.{ ask, after }
|
import akka.pattern.ask
|
||||||
import chess.{ Color, MoveOrDrop, Status, Situation }
|
import chess.{ Color, MoveOrDrop, Status, Situation }
|
||||||
import chess.format.FEN
|
import chess.format.FEN
|
||||||
import makeTimeout.large
|
import makeTimeout.large
|
||||||
|
|
|
@ -190,7 +190,16 @@ final class TournamentApi(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else fuccess(socketReload(tour.id))
|
}
|
||||||
|
else fuccess(socketReload(tour.id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def joinWithResult(tourId: String, me: User, p: Option[String]): Fu[Boolean] = {
|
||||||
|
join(tourId, me, p)
|
||||||
|
// atrocious hack, because joining is fire and forget
|
||||||
|
akka.pattern.after(500 millis, system.scheduler) {
|
||||||
|
PlayerRepo.find(tourId, me.id) map { _ ?? (_.active) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue