Skip to content

Commit

Permalink
filter broadcast source games by round
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jun 18, 2024
1 parent cac60a4 commit 7cc6a9b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions modules/relay/src/main/RelayFetch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ final private class RelayFetch(
if !rt.round.sync.playing then fuccess(updating(_.withSync(_.play(rt.tour.official))))
else
fetchGames(rt)
.map(RelayGame.filter(rt.round.sync.onlyRound))
.map(games => rt.tour.players.fold(games)(_.update(games)))
.flatMap(fidePlayers.enrichGames(rt.tour))
.map(games => rt.tour.teams.fold(games)(_.update(games)))
Expand Down
4 changes: 4 additions & 0 deletions modules/relay/src/main/RelayGame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ private object RelayGame:
,
mul => RelayFetch.multiPgnToGames(mul).fold(e => throw e, identity)
)

def filter(onlyRound: Option[Int])(games: RelayGames): RelayGames =
onlyRound.fold(games): round =>
games.filter(_.tags.roundNumber.has(round))
1 change: 1 addition & 0 deletions modules/relay/src/main/RelayRound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ object RelayRound:
nextAt: Option[Instant], // when to run next sync
period: Option[Seconds], // override time between two sync (rare)
delay: Option[Seconds], // add delay between the source and the study
onlyRound: Option[Int], // only keep games with [Round "x"]
log: SyncLog
):
def hasUpstream = upstream.isDefined
Expand Down
15 changes: 9 additions & 6 deletions modules/relay/src/main/RelayRoundForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ final class RelayRoundForm(using mode: Mode):
"startsAt" -> optional(ISOInstantOrTimestamp.mapping),
"finished" -> optional(boolean),
"period" -> optional(number(min = 2, max = 60).into[Seconds]),
"delay" -> optional(
number(min = 0, max = RelayDelay.maxSeconds.value).into[Seconds]
) // don't increase the max
"delay" -> optional(number(min = 0, max = RelayDelay.maxSeconds.value).into[Seconds]),
"onlyRound" -> optional(number(min = 1, max = 999))
)(Data.apply)(unapply)
.verifying("This source requires a round number. See the new form field below.", !_.roundMissing)

Expand All @@ -54,7 +53,7 @@ object RelayRoundForm:
val prevs: Option[(RelayRound, RelayRound)] = rounds.reverse match
case a :: b :: _ => (a, b).some
case _ => none
val prev: Option[RelayRound] = prevs.map(_._1)
val prev: Option[RelayRound] = rounds.lastOption
val roundNumberRegex = """([^\d]*)(\d{1,2})([^\d]*)""".r
val roundNumberIn: String => Option[Int] =
case roundNumberRegex(_, n, _) => n.toIntOption
Expand Down Expand Up @@ -86,7 +85,8 @@ object RelayRoundForm:
syncUrlRound = (prevs.isEmpty || nextUrl.isDefined).option(nextNumber),
startsAt = guessDate,
period = prev.flatMap(_.sync.period),
delay = prev.flatMap(_.sync.delay)
delay = prev.flatMap(_.sync.delay),
onlyRound = prev.flatMap(_.sync.onlyRound).map(_ + 1)
)

case class GameIds(ids: List[GameId])
Expand Down Expand Up @@ -143,7 +143,8 @@ object RelayRoundForm:
startsAt: Option[Instant] = None,
finished: Option[Boolean] = None,
period: Option[Seconds] = None,
delay: Option[Seconds] = None
delay: Option[Seconds] = None,
onlyRound: Option[Int] = None
):

def requiresRound = syncUrl.exists(RelayRound.Sync.UpstreamUrl.LccRegex.matches)
Expand Down Expand Up @@ -176,6 +177,7 @@ object RelayRoundForm:
nextAt = none,
period = period.ifTrue(Granter.ofUser(_.StudyAdmin)(user)),
delay = delay,
onlyRound = onlyRound,
log = SyncLog.empty
)

Expand Down Expand Up @@ -207,5 +209,6 @@ object RelayRoundForm:
startsAt = relay.startsAt,
finished = relay.finished.option(true),
period = relay.sync.period,
onlyRound = relay.sync.onlyRound,
delay = relay.sync.delay
)
10 changes: 10 additions & 0 deletions modules/relay/src/main/ui/FormUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ final class FormUi(helpers: Helpers, ui: RelayUi, tourUi: RelayTourUi):
)(form3.input(_, typ = "number"))
)
),
form3.split(
form3.group(
form("onlyRound"),
raw("Filter games by round number"),
help = frag(
"Optional, only keep games from the source that match a round number."
).some,
half = true
)(form3.input(_, typ = "number"))
),
form3.actions(
a(href := routes.RelayTour.show(t.slug, t.id))(trans.site.cancel()),
form3.submit(trans.site.apply())
Expand Down

0 comments on commit 7cc6a9b

Please sign in to comment.