Skip to content

Commit

Permalink
fix broadcast tour visual tier sort
Browse files Browse the repository at this point in the history
compute the visual tier before sorting the tournaments
to fix display order on /broadcast
  • Loading branch information
ornicar committed Oct 28, 2024
1 parent 70297b5 commit 13bfef8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
19 changes: 17 additions & 2 deletions modules/relay/src/main/RelayListing.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lila.relay

import reactivemongo.api.bson.*
import monocle.syntax.all.*

import lila.db.dsl.{ *, given }
import lila.relay.RelayTour.ActiveWithSomeRounds
Expand Down Expand Up @@ -128,8 +129,9 @@ final class RelayListing(
doc <- docs
tour <- doc.asOpt[RelayTour]
round <- doc.getAsOpt[RelayRound]("round")
group = RelayListing.group.readFromOne(doc)
yield (tour, round, group)
group = RelayListing.group.readFromOne(doc)
reTiered = decreaseTierOfDistantNextRound(tour, round)
yield (reTiered, round, group)
sorted = tours.sortBy: (tour, round, _) =>
(
!round.hasStarted, // ongoing tournaments first
Expand All @@ -150,6 +152,19 @@ final class RelayListing(
tr.display.hasStarted || tr.display.startsAtTime.exists(_.isBefore(nowInstant.plusMinutes(30)))
active

private def decreaseTierOfDistantNextRound(tour: RelayTour, round: RelayRound): RelayTour =
import RelayTour.Tier.*
val visualTier = for
tier <- tour.tier
nextAt <- round.startsAtTime
days = scalalib.time.daysBetween(nowInstant.withTimeAtStartOfDay, nextAt)
yield
if tier == BEST && days > 10 then NORMAL
else if tier == BEST && days > 5 then HIGH
else if tier == HIGH && days > 5 then NORMAL
else tier
tour.copy(tier = visualTier.orElse(tour.tier))

val upcoming = cacheApi.unit[List[RelayTour.WithLastRound]]:
_.refreshAfterWrite(14 seconds).buildAsyncFuture: _ =>
val max = 64
Expand Down
21 changes: 2 additions & 19 deletions modules/relay/src/main/ui/RelayTourUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ui

import java.time.{ Month, YearMonth }
import scalalib.paginator.Paginator
import monocle.syntax.all.*

import lila.core.LightUser
import lila.relay.RelayTour.{ WithLastRound, WithFirstRound }
Expand All @@ -21,9 +20,8 @@ final class RelayTourUi(helpers: Helpers, ui: RelayUi):
upcoming: List[WithLastRound],
past: Seq[WithLastRound]
)(using Context) =
val reTiered = decreaseTierOfDistantNextRound(active)
def nonEmptyTier(selector: RelayTour.Tier.Selector, tier: String) =
val selected = reTiered.filter(_.tour.tierIs(selector))
val selected = active.filter(_.tour.tierIs(selector))
selected.nonEmpty.option(st.section(cls := s"relay-cards relay-cards--tier-$tier"):
selected.map:
card.render(_, live = _.display.hasStarted)
Expand All @@ -35,7 +33,7 @@ final class RelayTourUi(helpers: Helpers, ui: RelayUi):
pageMenu("index"),
div(cls := "page-menu__content box box-pad")(
boxTop(h1(trc.liveBroadcasts()), searchForm("")),
Granter.opt(_.StudyAdmin).option(adminIndex(reTiered)),
Granter.opt(_.StudyAdmin).option(adminIndex(active)),
nonEmptyTier(_.BEST, "best"),
nonEmptyTier(_.HIGH, "high"),
nonEmptyTier(_.NORMAL, "normal"),
Expand Down Expand Up @@ -70,21 +68,6 @@ final class RelayTourUi(helpers: Helpers, ui: RelayUi):
card.render(tr.copy(link = tr.display), live = _.display.hasStarted, errors = errors.take(5))
)

private def decreaseTierOfDistantNextRound(active: List[RelayTour.ActiveWithSomeRounds]) =
val now = nowInstant.withTimeAtStartOfDay
active.map: a =>
import RelayTour.Tier.*
val visualTier = for
tier <- a.tour.tier
nextAt <- a.display.startsAtTime
days = scalalib.time.daysBetween(now, nextAt)
yield
if tier == BEST && days > 10 then NORMAL
else if tier == BEST && days > 5 then HIGH
else if tier == HIGH && days > 5 then NORMAL
else tier
a.focus(_.tour.tier).replace(visualTier.orElse(a.tour.tier))

private def listLayout(title: String, menu: Tag)(body: Modifier*)(using Context) =
Page(title)
.css("bits.relay.index")
Expand Down

0 comments on commit 13bfef8

Please sign in to comment.