bootstrap tournament schedule API and bindings

This commit is contained in:
Thibault Duplessis 2015-06-13 23:11:09 +02:00
parent bef503e0ee
commit 5c50501b28
5 changed files with 84 additions and 10 deletions

View file

@ -8,7 +8,7 @@ import lila.api.Context
import lila.app._
import lila.common.HTTPRequest
import lila.game.{ Pov, GameRepo }
import lila.tournament.{ System, TournamentRepo, Tournament => Tourney }
import lila.tournament.{ System, TournamentRepo, Tournament => Tourney, VisibleTournaments }
import lila.user.UserRepo
import views._
@ -20,9 +20,9 @@ object Tournament extends LilaController {
private def tournamentNotFound(implicit ctx: Context) = NotFound(html.tournament.notFound())
val home = Open { implicit ctx =>
fetchTournaments zip repo.scheduledDedup zip UserRepo.allSortToints(10) map {
case ((((created, started), finished), scheduled), leaderboard) =>
Ok(html.tournament.home(created, started, finished, scheduled, leaderboard))
env.fetchVisibleTournaments() zip repo.scheduledDedup zip UserRepo.allSortToints(10) map {
case ((visible@VisibleTournaments(created, started, finished), scheduled), leaderboard) =>
Ok(html.tournament.home(created, started, finished, scheduled, leaderboard, env scheduleJsonView visible))
}
}
@ -35,15 +35,12 @@ object Tournament extends LilaController {
}
val homeReload = Open { implicit ctx =>
fetchTournaments map {
case ((created, started), finished) =>
env.fetchVisibleTournaments() map {
case VisibleTournaments(created, started, finished) =>
Ok(html.tournament.homeInner(created, started, finished))
}
}
private def fetchTournaments =
env allCreatedSorted true zip repo.publicStarted zip repo.finished(10)
def show(id: String) = Open { implicit ctx =>
repo byId id flatMap {
_.fold(tournamentNotFound.fuccess) { tour =>

View file

@ -1,4 +1,15 @@
@(createds: List[Tournament], starteds: List[Tournament], finisheds: List[Tournament], scheduled: List[Tournament], leaderboard: List[User])(implicit ctx: Context)
@(createds: List[Tournament], starteds: List[Tournament], finisheds: List[Tournament], scheduled: List[Tournament], leaderboard: List[User], json: play.api.libs.json.JsObject)(implicit ctx: Context)
@moreJs = {
@jsAt(s"compiled/lichess.tournament-schedule${isProd??(".min")}.js")
@embedJs {
LichessTournamentSchedule(document.getElementById('tournament_schedule'), {
data: @Html(J.stringify(json)),
i18n: @jsI18n(),
userId: @Html(ctx.userId.fold("null")(id => s""""$id""""))
});
}
}
@side = {
<div class="tournament_links">
@ -27,7 +38,9 @@
@tournament.layout(
title = trans.tournaments.str(),
moreJs = moreJs,
side = side.some) {
<div id="tournament_schedule"></div>
<div id="tournament_list" data-href="@routes.Tournament.homeReload()">
@tournament.homeInner(createds, starteds, finisheds)
</div>

View file

@ -75,6 +75,8 @@ final class Env(
lazy val jsonView = new JsonView(lightUser)
lazy val scheduleJsonView = new ScheduleJsonView(lightUser)
private val socketHub = system.actorOf(
Props(new lila.socket.SocketHubActor.Default[Socket] {
def mkActor(tournamentId: String) = new Socket(
@ -110,6 +112,11 @@ final class Env(
val promotable =
lila.memo.AsyncCache.single(TournamentRepo.promotable, timeToLive = CreatedCacheTtl)
val fetchVisibleTournaments: () => Fu[VisibleTournaments] = () =>
allCreatedSorted(true) zip TournamentRepo.publicStarted zip TournamentRepo.finished(10) map {
case ((created, started), finished) => VisibleTournaments(created, started, finished)
}
private lazy val autoPairing = new AutoPairing(
roundMap = roundMap,
system = system,

View file

@ -0,0 +1,52 @@
package lila.tournament
import play.api.libs.json._
import lila.common.LightUser
import lila.common.PimpedJson._
import lila.game.{ Game, GameRepo }
import lila.user.User
final class ScheduleJsonView(
getLightUser: String => Option[LightUser]) {
def apply(tournaments: VisibleTournaments) = Json.obj(
"created" -> tournaments.created.map(tournamentJson),
"started" -> tournaments.started.map(tournamentJson),
"finished" -> tournaments.finished.map(tournamentJson))
private def tournamentJson(tour: Tournament) = Json.obj(
"id" -> tour.id,
"createdBy" -> tour.createdBy,
"system" -> tour.system.toString.toLowerCase,
"minutes" -> tour.minutes,
"clock" -> clockJson(tour.clock),
"position" -> tour.position.some.filterNot(_.initial).map(positionJson),
"rated" -> tour.mode.rated,
"fullName" -> tour.fullName,
"nbPlayers" -> tour.nbPlayers,
"private" -> tour.`private`,
"variant" -> tour.variant.key,
"secondsToStart" -> tour.secondsToStart,
"startsAt" -> org.joda.time.format.ISODateTimeFormat.dateTime.print(tour.startsAt),
"schedule" -> tour.schedule.map(scheduleJson),
"winner" -> tour.winnerId.flatMap(getLightUser).map(userJson))
private def scheduleJson(s: Schedule) = Json.obj(
"freq" -> s.freq.name,
"speed" -> s.speed.name)
private def clockJson(c: TournamentClock) = Json.obj(
"limit" -> c.limit,
"increment" -> c.increment)
private def positionJson(s: chess.StartingPosition) = Json.obj(
"eco" -> s.eco,
"name" -> s.name,
"fen" -> s.fen)
private def userJson(u: LightUser) = Json.obj(
"id" -> u.id,
"name" -> u.name,
"title" -> u.title)
}

View file

@ -3,3 +3,8 @@ package lila.tournament
case class MiniStanding(
tour: Tournament,
standing: Option[RankedPlayers])
case class VisibleTournaments(
created: List[Tournament],
started: List[Tournament],
finished: List[Tournament])