Skip to content

Commit

Permalink
better type rating conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 26, 2023
1 parent 0534f15 commit 1929ca0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
12 changes: 6 additions & 6 deletions modules/simul/src/main/SimulCondition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object SimulCondition:

case class WithVerdict(condition: SimulCondition, verdict: Verdict)

case class MaxRating(perf: PerfType, rating: Int) extends SimulCondition:
case class MaxRating(perf: PerfType, rating: IntRating) extends SimulCondition:
def verify(user: User, getMaxRating: GetMaxRating)(using Executor): Fu[Verdict] =
if (user.perfs(perf).provisional.yes) fuccess(Refused { lang =>
given Lang = lang
Expand All @@ -44,7 +44,7 @@ object SimulCondition:

def name(using lang: Lang) = trans.ratedLessThanInPerf.txt(rating, perf.trans)

case class MinRating(perf: PerfType, rating: Int) extends SimulCondition:
case class MinRating(perf: PerfType, rating: IntRating) extends SimulCondition:
def verify(user: User): Fu[Verdict] =
if (user.perfs(perf).provisional.yes) fuccess(Refused { lang =>
given Lang = lang
Expand Down Expand Up @@ -124,8 +124,8 @@ object SimulCondition:
PerfType.nonPuzzle.map { pt =>
pt.key -> pt.trans
}
case class RatingSetup(perf: Option[Perf.Key], rating: Option[Int]):
def convert[A](f: (PerfType, Int) => A): Option[A] = for
case class RatingSetup(perf: Option[Perf.Key], rating: Option[IntRating]):
def convert[A](f: (PerfType, IntRating) => A): Option[A] = for
perf <- perf
perfType <- PerfType(perf)
rating <- rating
Expand All @@ -137,15 +137,15 @@ object SimulCondition:
options(maxRatings, "Max rating of %d").toList.map { case (k, v) => k.toString -> v }
val maxRating = mapping(
"perf" -> optional(of[Perf.Key].verifying(perfKeys.contains)),
"rating" -> optional(numberIn(maxRatings))
"rating" -> optional(numberIn(maxRatings).into[IntRating])
)(RatingSetup.apply)(unapply)
val minRatings = List(1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600)
val minRatingChoices = ("", "No restriction") ::
options(minRatings, "Min rating of %d").toList.map { case (k, v) => k.toString -> v }
val minRating = mapping(
"perf" -> optional(of[Perf.Key].verifying(perfKeys.contains)),
"rating" -> optional(numberIn(minRatings))
"rating" -> optional(numberIn(minRatings).into[IntRating])
)(RatingSetup.apply)(unapply)
def all(teams: Set[TeamId]) =
mapping(
Expand Down
13 changes: 6 additions & 7 deletions modules/swiss/src/main/SwissCondition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object SwissCondition:
def name(perf: PerfType)(using lang: Lang) =
trans.moreThanNbPerfRatedGames.pluralTxt(nb, nb, perf.trans)

case class MaxRating(rating: Int) extends SwissCondition:
case class MaxRating(rating: IntRating) extends SwissCondition:

def apply(perf: PerfType, getMaxRating: GetMaxRating)(
user: User
Expand Down Expand Up @@ -84,7 +84,7 @@ object SwissCondition:

def name(perf: PerfType)(using lang: Lang) = trans.ratedLessThanInPerf.txt(rating, perf.trans)

case class MinRating(rating: Int) extends SwissCondition with FlatCond:
case class MinRating(rating: IntRating) extends SwissCondition with FlatCond:

def apply(user: User, perf: PerfType) =
if (user.perfs(perf).provisional.yes) Refused { lang =>
Expand Down Expand Up @@ -193,28 +193,27 @@ object SwissCondition:
pt.key -> pt.trans
}
val nbRatedGames = Vector(0, 5, 10, 15, 20, 30, 40, 50, 75, 100, 150, 200)
val nbRatedGameChoices = options(nbRatedGames, "%d rated game{s}") map {
val nbRatedGameChoices = options(nbRatedGames, "%d rated game{s}").map:
case (0, _) => (0, "No restriction")
case x => x
}
val nbRatedGame = mapping(
"nb" -> number(min = 0, max = ~nbRatedGames.lastOption)
)(NbRatedGame.apply)(_.nb.some)
case class RatingSetup(rating: Option[Int]):
case class RatingSetup(rating: Option[IntRating]):
def actualRating = rating.filter(r => r > 600 && r < 3000)
val maxRatings =
List(2200, 2100, 2000, 1900, 1800, 1700, 1600, 1500, 1400, 1300, 1200, 1100, 1000, 900, 800)
val maxRatingChoices = ("", "No restriction") ::
options(maxRatings, "Max rating of %d").toList.map { case (k, v) => k.toString -> v }
val maxRating = mapping(
"rating" -> optional(numberIn(maxRatings))
"rating" -> optional(numberIn(maxRatings).into[IntRating])
)(RatingSetup.apply)(_.rating.some)
val minRatings = List(1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600)
val minRatingChoices = ("", "No restriction") ::
options(minRatings, "Min rating of %d").toList.map { case (k, v) => k.toString -> v }
val minRating = mapping(
"rating" -> optional(numberIn(minRatings))
"rating" -> optional(numberIn(minRatings).into[IntRating])
)(RatingSetup.apply)(_.rating.some)
def all =
mapping(
Expand Down
19 changes: 11 additions & 8 deletions modules/tournament/src/main/Condition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ object Condition:

abstract trait RatingCondition:
val perf: PerfType
val rating: Int
val rating: IntRating

case class MaxRating(perf: PerfType, rating: Int) extends Condition with RatingCondition:
case class MaxRating(perf: PerfType, rating: IntRating) extends Condition with RatingCondition:

def apply(
getMaxRating: GetMaxRating
Expand All @@ -88,7 +88,10 @@ object Condition:

def name(using lang: Lang) = trans.ratedLessThanInPerf.txt(rating, perf.trans)

case class MinRating(perf: PerfType, rating: Int) extends Condition with RatingCondition with FlatCond:
case class MinRating(perf: PerfType, rating: IntRating)
extends Condition
with RatingCondition
with FlatCond:

def apply(user: User) =
if (user.perfs(perf).provisional.yes) Refused { lang =>
Expand Down Expand Up @@ -273,29 +276,29 @@ object Condition:
)
object NbRatedGameSetup:
def apply(x: NbRatedGame): NbRatedGameSetup = NbRatedGameSetup(x.perf.map(_.key), x.nb)
case class RatingSetup(perf: Option[Perf.Key], rating: Option[Int]):
case class RatingSetup(perf: Option[Perf.Key], rating: Option[IntRating]):
def actualRating = rating.filter(r => r > 600 && r < 3000)
def convert[A](tourPerf: PerfType)(f: (PerfType, Int) => A): Option[A] =
def convert[A](tourPerf: PerfType)(f: (PerfType, IntRating) => A): Option[A] =
actualRating map { r =>
f(perf.flatMap(PerfType.apply) | tourPerf, r)
}
object RatingSetup:
def apply(v: (Option[PerfType], Option[Int])): RatingSetup = RatingSetup(v._1.map(_.key), v._2)
def apply(v: (Option[PerfType], Option[IntRating])): RatingSetup = RatingSetup(v._1.map(_.key), v._2)
val maxRatings =
List(2200, 2100, 2000, 1900, 1800, 1700, 1600, 1500, 1400, 1300, 1200, 1100, 1000, 900, 800)
val maxRatingChoices = ("", "No restriction") ::
options(maxRatings, "Max rating of %d").toList.map { case (k, v) => k.toString -> v }
val maxRating = mapping(
"perf" -> optional(of[Perf.Key].verifying(perfKeys.contains)),
"rating" -> optional(numberIn(maxRatings))
"rating" -> optional(numberIn(maxRatings).into[IntRating])
)(RatingSetup.apply)(unapply)
val minRatings = List(1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600)
val minRatingChoices = ("", "No restriction") ::
options(minRatings, "Min rating of %d").toList.map { case (k, v) => k.toString -> v }
val minRating = mapping(
"perf" -> optional(of[Perf.Key].verifying(perfKeys.contains)),
"rating" -> optional(numberIn(minRatings))
"rating" -> optional(numberIn(minRatings).into[IntRating])
)(RatingSetup.apply)(unapply)
def teamMember(leaderTeams: List[LeaderTeam]) =
mapping(
Expand Down
11 changes: 6 additions & 5 deletions modules/tournament/src/main/Schedule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,17 @@ object Schedule:

case _ => 0

val minRating = (s.freq, s.variant) match
case (Weekend, chess.variant.Crazyhouse) => 2100
case (Weekend, _) => 2200
case _ => 0
val minRating = IntRating:
(s.freq, s.variant) match
case (Weekend, chess.variant.Crazyhouse) => 2100
case (Weekend, _) => 2200
case _ => 0

Condition.All(
nbRatedGame = nbRatedGame.some.filter(0 <).map {
Condition.NbRatedGame(s.perfType.some, _)
},
minRating = minRating.some.filter(0 <).map {
minRating = minRating.some.filter(_ > 0).map {
Condition.MinRating(s.perfType, _)
},
maxRating = none,
Expand Down
2 changes: 1 addition & 1 deletion modules/tournament/src/main/TournamentScheduler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ Thank you all, you rock!""",
case 1 => SuperBlitz
case 2 => Blitz
case _ => Rapid
List(1300, 1500, 1700, 2000).zipWithIndex.flatMap { (rating, hourDelay) =>
List(1300, 1500, 1700, 2000).map(IntRating(_)).zipWithIndex.flatMap { (rating, hourDelay) =>
import chess.Clock
val perf = Schedule.Speed toPerfType speed
val conditions = Condition.All(
Expand Down

0 comments on commit 1929ca0

Please sign in to comment.