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

@ -58,27 +58,27 @@ object Tournament extends LilaController {
val page = getInt("page")
negotiate(
html = repo byId id flatMap {
_.fold(tournamentNotFound.fuccess) { tour =>
(env.api.verdicts(tour, ctx.me) zip
env.version(tour.id) zip {
ctx.noKid ?? Env.chat.api.userChat.findMine(tour.id, ctx.me).map(some)
}).flatMap {
case ((verdicts, version), chat) => env.jsonView(tour, page, ctx.me, none, version.some) map {
html.tournament.show(tour, verdicts, _, chat)
}
}.map { Ok(_) }.mon(_.http.response.tournament.show.website)
}
},
_.fold(tournamentNotFound.fuccess) { tour =>
(env.api.verdicts(tour, ctx.me) zip
env.version(tour.id) zip {
ctx.noKid ?? Env.chat.api.userChat.findMine(tour.id, ctx.me).map(some)
}).flatMap {
case ((verdicts, version), chat) => env.jsonView(tour, page, ctx.me, none, version.some) map {
html.tournament.show(tour, verdicts, _, chat)
}
}.map { Ok(_) }.mon(_.http.response.tournament.show.website)
}
},
api = _ => repo byId id flatMap {
case None => NotFound(jsonError("No such tournament")).fuccess
case Some(tour) => {
get("playerInfo").?? { env.api.playerInfo(tour.id, _) } zip
getBool("socketVersion").??(env version tour.id map some) flatMap {
case (playerInfoExt, socketVersion) =>
env.jsonView(tour, page, ctx.me, playerInfoExt, socketVersion)
} map { Ok(_) }
}.mon(_.http.response.tournament.show.mobile)
} map (_ as JSON)
case None => NotFound(jsonError("No such tournament")).fuccess
case Some(tour) => {
get("playerInfo").?? { env.api.playerInfo(tour.id, _) } zip
getBool("socketVersion").??(env version tour.id map some) flatMap {
case (playerInfoExt, socketVersion) =>
env.jsonView(tour, page, ctx.me, playerInfoExt, socketVersion)
} map { Ok(_) }
}.mon(_.http.response.tournament.show.mobile)
} map (_ as JSON)
) map NoCache
}
@ -126,79 +126,74 @@ object Tournament extends LilaController {
}
}
def join(id: String) = AuthBody(BodyParsers.parse.json) { implicit ctx =>
implicit me =>
NoLame {
val password = ctx.body.body.\("p").asOpt[String]
negotiate(
html = repo enterableById id map {
case None => tournamentNotFound
case Some(tour) =>
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))
}
)
}
def join(id: String) = AuthBody(BodyParsers.parse.json) { implicit ctx => implicit me =>
NoLame {
val password = ctx.body.body.\("p").asOpt[String]
negotiate(
html = repo enterableById id map {
case None => tournamentNotFound
case Some(tour) =>
env.api.join(tour.id, me, password)
Redirect(routes.Tournament.show(tour.id))
},
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 =>
OptionResult(repo byId id) { tour =>
env.api.withdraw(tour.id, me.id)
if (HTTPRequest.isXhr(ctx.req)) Ok(Json.obj("ok" -> true)) as JSON
else Redirect(routes.Tournament.show(tour.id))
}
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
else Redirect(routes.Tournament.show(tour.id))
}
}
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)
Redirect(routes.Tournament show tour.id)
}
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)
Redirect(routes.Tournament show tour.id)
}
}
def form = Auth { implicit ctx =>
me =>
NoLame {
Ok(html.tournament.form(env.forms.create, env.forms)).fuccess
}
def form = Auth { implicit ctx => me =>
NoLame {
Ok(html.tournament.form(env.forms.create, env.forms)).fuccess
}
}
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 (
html = 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))
}),
api = _ => env.forms.create.bindFromRequest.fold(
err => BadRequest(errorsAsJson(err)).fuccess,
setup => env.api.createTournament(setup, me) map { tour =>
Ok(Json.obj("id" -> tour.id))
})
)
}
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(
html = 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))
}),
api = _ => env.forms.create.bindFromRequest.fold(
err => BadRequest(errorsAsJson(err)).fuccess,
setup => env.api.createTournament(setup, me) map { tour =>
Ok(Json.obj("id" -> tour.id))
})
)
}
}
def limitedInvitation = Auth { implicit ctx =>
me =>
env.api.fetchVisibleTournaments.flatMap { tours =>
lila.tournament.TournamentInviter.findNextFor(me, tours, env.verify.canEnter(me))
} map {
case None => Redirect(routes.Tournament.home(1))
case Some(t) => Redirect(routes.Tournament.show(t.id))
}
def limitedInvitation = Auth { implicit ctx => me =>
env.api.fetchVisibleTournaments.flatMap { tours =>
lila.tournament.TournamentInviter.findNextFor(me, tours, env.verify.canEnter(me))
} map {
case None => Redirect(routes.Tournament.home(1))
case Some(t) => Redirect(routes.Tournament.show(t.id))
}
}
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>

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