Skip to content

Commit

Permalink
opaque type AnyTour
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 19, 2022
1 parent dad9fe4 commit 1b543d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions app/mashup/TeamInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ case class TeamInfo(
def userIds = forum.??(_.flatMap(_.userId))

object TeamInfo:
case class AnyTour(any: Either[Tournament, Swiss]) extends AnyVal:
def isEnterable = any.fold(_.isEnterable, _.isEnterable)
def startsAt = any.fold(_.startsAt, _.startsAt)
def isNowOrSoon = any.fold(_.isNowOrSoon, _.isNowOrSoon)
def nbPlayers = any.fold(_.nbPlayers, _.nbPlayers)
def anyTour(tour: Tournament) = AnyTour(Left(tour))
def anyTour(swiss: Swiss) = AnyTour(Right(swiss))
opaque type AnyTour = Either[Tournament, Swiss]
object AnyTour extends TotalWrapper[AnyTour, Either[Tournament, Swiss]]:
extension (e: AnyTour)
def isEnterable = e.fold(_.isEnterable, _.isEnterable)
def startsAt = e.fold(_.startsAt, _.startsAt)
def isNowOrSoon = e.fold(_.isNowOrSoon, _.isNowOrSoon)
def nbPlayers = e.fold(_.nbPlayers, _.nbPlayers)
def apply(tour: Tournament): AnyTour = Left(tour)
def apply(swiss: Swiss): AnyTour = Right(swiss)

case class PastAndNext(past: List[AnyTour], next: List[AnyTour]):
def nonEmpty = past.nonEmpty || next.nonEmpty
Expand Down Expand Up @@ -71,10 +73,10 @@ final class TeamInfoApi(
case (tours, swisses) =>
PastAndNext(
past = {
tours.past.map(anyTour) ::: swisses.past.map(anyTour)
tours.past.map(AnyTour(_)) ::: swisses.past.map(AnyTour(_))
}.sortBy(-_.startsAt.getSeconds),
next = {
tours.next.map(anyTour) ::: swisses.next.map(anyTour)
tours.next.map(AnyTour(_)) ::: swisses.next.map(AnyTour(_))
}.sortBy(_.startsAt.getSeconds)
)
}
6 changes: 3 additions & 3 deletions app/views/team/tournaments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ object tournaments:
"soon" -> any.isNowOrSoon
)
)(
td(cls := "icon")(iconTag(any.any.fold(tournamentIconChar, views.html.swiss.bits.iconChar))),
td(cls := "icon")(iconTag(any.value.fold(tournamentIconChar, views.html.swiss.bits.iconChar))),
td(cls := "header")(
any.any.fold(
any.value.fold(
t =>
a(href := routes.Tournament.show(t.id))(
span(cls := "name")(t.name()),
Expand Down Expand Up @@ -91,7 +91,7 @@ object tournaments:
)
),
td(cls := "infos")(
any.any.fold(
any.value.fold(
t =>
frag(
t.teamBattle map { battle =>
Expand Down

0 comments on commit 1b543d4

Please sign in to comment.