implement /tournament/featured API endpoint - closes #3791

It uses the same algorithm already in place for the website homepage,
selecting best upcoming and ongoing tournaments for the user tastes,
and highlighting official tournaments.
pull/3805/head
Thibault Duplessis 2017-11-13 18:49:38 -05:00
parent f5fe9a08b2
commit 95c73b6825
5 changed files with 23 additions and 7 deletions

View File

@ -220,4 +220,14 @@ object Tournament extends LilaController {
env.socketHandler.join(id, uid, ctx.me)
}
}
def featured = Open { implicit ctx =>
negotiate(
html = notFound,
api = _ =>
Env.tournament.cached.promotable.get.nevermind map {
lila.tournament.Spotlight.select(_, ctx.me, 4)
} flatMap env.scheduleJsonView.featured map { Ok(_) }
)
}
}

View File

@ -18,7 +18,7 @@
@events.map { e =>
@event.homepageSpotlight(e)
}
@lila.tournament.Spotlight.select(tours, ctx.me).map { tour =>
@lila.tournament.Spotlight.select(tours, ctx.me, 3).map { tour =>
@tournament.homepageSpotlight(tour)
}
@simuls.find(_.spotlightable).take(2).map { s =>

View File

@ -224,6 +224,7 @@ GET /$gameId<\w{8}>/$color<white|black>/text controllers.Round.watcherText(g
# Tournament
GET /tournament controllers.Tournament.home(page: Int ?= 1)
GET /tournament/featured controllers.Tournament.featured
GET /tournament/new controllers.Tournament.form
POST /tournament/new controllers.Tournament.create
GET /tournament/$id<\w{8}> controllers.Tournament.show(id: String)

View File

@ -19,9 +19,14 @@ final class ScheduleJsonView(lightUser: LightUser.Getter) {
"finished" -> finished
)
def featured(tournaments: List[Tournament]): Fu[JsObject] =
tournaments.map(tournamentJson).sequenceFu map { objs =>
Json.obj("featured" -> objs)
}
private def tournamentJson(tour: Tournament): Fu[JsObject] = for {
owner <- tour.nonLichessCreatedBy.??(lightUser)
winner <- tour.winnerId.??(lightUser)
owner <- tour.nonLichessCreatedBy ?? lightUser
winner <- tour.winnerId ?? lightUser
} yield Json.obj(
"id" -> tour.id,
"createdBy" -> tour.createdBy,

View File

@ -16,11 +16,11 @@ object Spotlight {
import Schedule.Freq._
def select(tours: List[Tournament], user: Option[User]): List[Tournament] =
user.fold(sort(tours) take 3) { select(tours, _) }
def select(tours: List[Tournament], user: Option[User], max: Int): List[Tournament] =
user.fold(sort(tours) take max) { select(tours, _, max) }
def select(tours: List[Tournament], user: User): List[Tournament] =
sort(tours.filter { select(_, user) }) take 3
def select(tours: List[Tournament], user: User, max: Int): List[Tournament] =
sort(tours.filter { select(_, user) }) take max
private def sort(tours: List[Tournament]) = tours.sortBy { t =>
-(t.schedule.??(_.freq.importance))