tournament api join feedback - closes #2481

This commit is contained in:
Thibault Duplessis 2017-01-09 09:44:13 +01:00
parent 3130bc9d31
commit 608c41f976
4 changed files with 92 additions and 86 deletions

View file

@ -37,10 +37,12 @@ private[controllers] trait LilaController
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] =
negotiate(
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

View file

@ -126,8 +126,7 @@ object Tournament extends LilaController {
}
}
def join(id: String) = AuthBody(BodyParsers.parse.json) { implicit ctx =>
implicit me =>
def join(id: String) = AuthBody(BodyParsers.parse.json) { implicit ctx => implicit me =>
NoLame {
val password = ctx.body.body.\("p").asOpt[String]
negotiate(
@ -137,16 +136,16 @@ object Tournament extends LilaController {
env.api.join(tour.id, me, password)
Redirect(routes.Tournament.show(tour.id))
},
api = _ => OptionFuOk(repo enterableById id) { tour =>
env.api.join(tour.id, me, password)
fuccess(Json.obj("ok" -> true))
api = _ => OptionFuResult(repo enterableById id) { tour =>
env.api.joinWithResult(tour.id, me, password) map { result =>
if (result) Ok(jsonOkBody)
else BadRequest(Json.obj("joined" -> false))
}
)
})
}
}
def withdraw(id: String) = Auth { implicit ctx =>
me =>
def withdraw(id: String) = Auth { implicit ctx => me =>
OptionResult(repo byId id) { tour =>
env.api.withdraw(tour.id, me.id)
if (HTTPRequest.isXhr(ctx.req)) Ok(Json.obj("ok" -> true)) as JSON
@ -154,8 +153,7 @@ object Tournament extends LilaController {
}
}
def terminate(id: String) = Secure(_.TerminateTournament) { implicit ctx =>
me =>
def terminate(id: String) = Secure(_.TerminateTournament) { implicit ctx => me =>
OptionResult(repo startedById id) { tour =>
env.api finish tour
Env.mod.logApi.terminateTournament(me.id, tour.fullName)
@ -163,20 +161,18 @@ object Tournament extends LilaController {
}
}
def form = Auth { implicit ctx =>
me =>
def form = Auth { implicit ctx => me =>
NoLame {
Ok(html.tournament.form(env.forms.create, env.forms)).fuccess
}
}
def create = AuthBody { implicit ctx =>
implicit me =>
def create = AuthBody { implicit ctx => implicit me =>
NoLame {
import play.api.i18n.Messages.Implicits._
import play.api.Play.current
implicit val req = ctx.body
negotiate (
negotiate(
html = env.forms.create.bindFromRequest.fold(
err => BadRequest(html.tournament.form(err, env.forms)).fuccess,
setup => env.api.createTournament(setup, me) map { tour =>
@ -191,8 +187,7 @@ object Tournament extends LilaController {
}
}
def limitedInvitation = Auth { implicit ctx =>
me =>
def limitedInvitation = Auth { implicit ctx => me =>
env.api.fetchVisibleTournaments.flatMap { tours =>
lila.tournament.TournamentInviter.findNextFor(me, tours, env.verify.canEnter(me))
} map {

View file

@ -4,7 +4,7 @@ import scala.concurrent.duration._
import scala.concurrent.Future
import akka.actor.ActorRef
import akka.pattern.{ ask, after }
import akka.pattern.ask
import chess.{ Color, MoveOrDrop, Status, Situation }
import chess.format.FEN
import makeTimeout.large

View file

@ -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) }
}
}