Skip to content

Commit

Permalink
broadcast lcc form convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jun 20, 2024
1 parent efa8edc commit 8ad5a1b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion modules/relay/src/main/RelayRound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ object RelayRound:
def formUrl = s"$viewUrl $round"
object UpstreamLcc:
private val idRegex = """.*view\.livechesscloud\.com/?#?([0-9a-f\-]+)""".r
def findId(url: UpstreamUrl): Option[String] = url.url match
def findId(url: String): Option[String] = url match
case idRegex(id) => id.some
case _ => none
def find(url: String): Option[UpstreamLcc] = url.split(' ').map(_.trim).filter(_.nonEmpty) match
Expand Down
7 changes: 5 additions & 2 deletions modules/relay/src/main/RelayRoundForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ final class RelayRoundForm(using mode: Mode):
)

val lccMapping = mapping(
"id" -> cleanText(minLength = 10, maxLength = 40),
"id" -> cleanText(minLength = 10, maxLength = 100).transform(
str => Sync.UpstreamLcc.findId(str).getOrElse(str),
identity
),
"round" -> number(min = 1, max = 999)
)(Sync.UpstreamLcc.apply)(unapply)

Expand Down Expand Up @@ -214,7 +217,7 @@ object RelayRoundForm:
.map:
case url: Sync.UpstreamUrl =>
val foundLcc = for
lccId <- Sync.UpstreamLcc.findId(url)
lccId <- Sync.UpstreamLcc.findId(url.url)
round <- roundNumberIn(name.value)
yield Sync.UpstreamLcc(lccId, round)
foundLcc | url
Expand Down
72 changes: 41 additions & 31 deletions modules/relay/src/main/ui/FormUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ final class FormUi(helpers: Helpers, ui: RelayUi, tourUi: RelayTourUi):
)
),
standardFlash,
inner(form, routes.RelayRound.create(nav.tour.id), nav.tour, create = true)
inner(form, routes.RelayRound.create(nav.tour.id), nav.tour, round = none)
)

def edit(r: RelayRound, form: Form[RelayRoundForm.Data], nav: FormNavigation)(using Context) =
Expand All @@ -108,7 +108,7 @@ final class FormUi(helpers: Helpers, ui: RelayUi, tourUi: RelayTourUi):
frag(
boxTop(h1(a(href := rt.path)(rt.fullName))),
standardFlash,
inner(form, routes.RelayRound.update(r.id), nav.tour, create = false),
inner(form, routes.RelayRound.update(r.id), nav.tour, round = r.some),
div(cls := "relay-form__actions")(
postForm(action := routes.RelayRound.reset(r.id))(
submitButton(
Expand All @@ -126,14 +126,17 @@ final class FormUi(helpers: Helpers, ui: RelayUi, tourUi: RelayTourUi):
)
)

private def inner(form: Form[RelayRoundForm.Data], url: play.api.mvc.Call, t: RelayTour, create: Boolean)(
using ctx: Context
) =
private def inner(
form: Form[RelayRoundForm.Data],
url: play.api.mvc.Call,
t: RelayTour,
round: Option[RelayRound]
)(using ctx: Context) =
postForm(cls := "form3", action := url)(
(!Granter.opt(_.StudyAdmin)).option:
div(cls := "form-group")(
div(cls := "form-group")(ui.howToUse),
(create && t.createdAt.isBefore(nowInstant.minusMinutes(1))).option:
(round.isEmpty && t.createdAt.isBefore(nowInstant.minusMinutes(1))).option:
p(dataIcon := Icon.InfoCircle, cls := "text"):
trb.theNewRoundHelp()
)
Expand Down Expand Up @@ -163,34 +166,41 @@ final class FormUi(helpers: Helpers, ui: RelayUi, tourUi: RelayTourUi):
trb.sourceSingleUrl(),
help = trb.sourceUrlHelp().some
)(form3.input(_))(cls := "relay-form__sync relay-form__sync-url"),
div(cls := "relay-form__sync relay-form__sync-lcc none")(
(!Granter.opt(_.Relay)).option(
flashMessage("box")(
p(strong("Please use the ", a(href := broadcasterUrl)("Lichess Broadcaster App"))),
p(
"LiveChessCloud support is deprecated and will be removed soon.",
br,
"If you need help, please contact us at [email protected]."
div(cls := "relay-form__sync relay-form__sync-lcc none"):
val lccUrl = round
.flatMap(_.sync.upstream)
.collect:
case lcc: RelayRound.Sync.UpstreamLcc => lcc.viewUrl
frag(
(!Granter.opt(_.Relay)).option(
flashMessage("box")(
p(strong("Please use the ", a(href := broadcasterUrl)("Lichess Broadcaster App"))),
p(
"LiveChessCloud support is deprecated and will be removed soon.",
br,
"If you need help, please contact us at [email protected]."
)
)
),
lccUrl.map(url => div(cls := "form-group")(a(href := url, targetBlank)(url))),
form3.split(
form3.group(
form("syncLcc.id"),
"Tournament ID",
help = frag(
"From the LCC page URL. The ID looks like this: ",
pre("f1943ec6-4992-45d9-969d-a0aff688b404")
).some,
half = true
)(form3.input(_)),
form3.group(
form("syncLcc.round"),
trb.roundNumber(),
half = true
)(form3.input(_, typ = "number"))
)
),
form3.split(
form3.group(
form("syncLcc.id"),
"Tournament ID",
help = frag(
"From the LCC page URL. The ID looks like this: ",
pre("f1943ec6-4992-45d9-969d-a0aff688b404")
).some,
half = true
)(form3.input(_)),
form3.group(
form("syncLcc.round"),
trb.roundNumber(),
half = true
)(form3.input(_, typ = "number"))
)
),
,
form3.group(
form("syncUrls"),
"Multiple source URLs, one per line.",
Expand Down

0 comments on commit 8ad5a1b

Please sign in to comment.