Skip to content

Commit

Permalink
more enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jan 26, 2023
1 parent be642ed commit c479974
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 134 deletions.
10 changes: 5 additions & 5 deletions modules/analyse/src/main/Advice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ sealed trait Advice:

object Advice:

sealed abstract class Judgement(val glyph: Glyph, val name: String):
enum Judgement(val glyph: Glyph, val name: String):
case Inaccuracy extends Judgement(Glyph.MoveAssessment.dubious, "Inaccuracy")
case Mistake extends Judgement(Glyph.MoveAssessment.mistake, "Mistake")
case Blunder extends Judgement(Glyph.MoveAssessment.blunder, "Blunder")
override def toString = name
def isMistakeOrBlunder = this == Judgement.Mistake || this == Judgement.Blunder
object Judgement:
object Inaccuracy extends Judgement(Glyph.MoveAssessment.dubious, "Inaccuracy")
object Mistake extends Judgement(Glyph.MoveAssessment.mistake, "Mistake")
object Blunder extends Judgement(Glyph.MoveAssessment.blunder, "Blunder")
val all = List(Inaccuracy, Mistake, Blunder)
val all = values.toList

def apply(prev: Info, info: Info): Option[Advice] = CpAdvice(prev, info) orElse MateAdvice(prev, info)

Expand Down
8 changes: 4 additions & 4 deletions modules/challenge/src/main/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ object Challenge:
def increment = config.increment
def show = config.show

sealed abstract class ColorChoice(val trans: I18nKey)
enum ColorChoice(val trans: I18nKey):
case Random extends ColorChoice(I18nKeys.randomColor)
case White extends ColorChoice(I18nKeys.white)
case Black extends ColorChoice(I18nKeys.black)
object ColorChoice:
case object Random extends ColorChoice(I18nKeys.randomColor)
case object White extends ColorChoice(I18nKeys.white)
case object Black extends ColorChoice(I18nKeys.black)
def apply(c: Color) = c.fold[ColorChoice](White, Black)

case class Open(userIds: Option[(UserId, UserId)]):
Expand Down
21 changes: 10 additions & 11 deletions modules/fishnet/src/main/Analyser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,14 @@ final class Analyser(

object Analyser:

sealed abstract class Result(val error: Option[String]):
enum Result(val error: Option[String]):
def ok = error.isEmpty
object Result:
case object Ok extends Result(none)
case object NoGame extends Result("Game not found".some)
case object NoChapter extends Result("Chapter not found".some)
case object AlreadyAnalysed extends Result("This game is already analysed".some)
case object NotAnalysable extends Result("This game is not analysable".some)
case object ConcurrentAnalysis extends Result("You already have an ongoing requested analysis".some)
case object WeeklyLimit extends Result("You have reached the weekly analysis limit".some)
case object DailyLimit extends Result("You have reached the daily analysis limit".some)
case object DailyIpLimit extends Result("You have reached the daily analysis limit on this IP".some)
case Ok extends Result(none)
case NoGame extends Result("Game not found".some)
case NoChapter extends Result("Chapter not found".some)
case AlreadyAnalysed extends Result("This game is already analysed".some)
case NotAnalysable extends Result("This game is not analysable".some)
case ConcurrentAnalysis extends Result("You already have an ongoing requested analysis".some)
case WeeklyLimit extends Result("You have reached the weekly analysis limit".some)
case DailyLimit extends Result("You have reached the daily analysis limit".some)
case DailyIpLimit extends Result("You have reached the daily analysis limit on this IP".some)
15 changes: 7 additions & 8 deletions modules/hub/src/main/actorApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ package shutup:
case class RecordPrivateChat(chatId: String, userId: UserId, text: String)
case class RecordPublicChat(userId: UserId, text: String, source: PublicSource)

sealed abstract class PublicSource(val parentName: String)
object PublicSource:
case class Tournament(id: TourId) extends PublicSource("tournament")
case class Simul(id: SimulId) extends PublicSource("simul")
case class Study(id: StudyId) extends PublicSource("study")
case class Watcher(gameId: GameId) extends PublicSource("watcher")
case class Team(id: TeamId) extends PublicSource("team")
case class Swiss(id: SwissId) extends PublicSource("swiss")
enum PublicSource(val parentName: String):
case Tournament(id: TourId) extends PublicSource("tournament")
case Simul(id: SimulId) extends PublicSource("simul")
case Study(id: StudyId) extends PublicSource("study")
case Watcher(gameId: GameId) extends PublicSource("watcher")
case Team(id: TeamId) extends PublicSource("team")
case Swiss(id: SwissId) extends PublicSource("swiss")

package mod:
case class MarkCheater(userId: UserId, value: Boolean)
Expand Down
28 changes: 12 additions & 16 deletions modules/push/src/main/Stacking.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package lila.push

sealed abstract private class Stacking(val key: String, val message: String)
private enum Stacking(val key: String, val message: String):

private object Stacking:

case object GameFinish extends Stacking("gameFinish", "$[notif_count] games are over")
case object GameMove extends Stacking("gameMove", "It's your turn in $[notif_count] games")
case object GameTakebackOffer
extends Stacking("gameTakebackOffer", "Takeback offers in $[notif_count] games")
case object GameDrawOffer extends Stacking("gameDrawOffer", "Draw offers in $[notif_count] games")
case object PrivateMessage extends Stacking("privateMessage", "You have $[notif_count] new messages")
case object ChallengeCreate extends Stacking("challengeCreate", "You have $[notif_count] new challenges")
case object ChallengeAccept
extends Stacking("challengeAccept", "$[notif_count] players accepted your challenges")
case object TourSoon extends Stacking("tourSoon", "$[notif_count] tournaments are starting")
case object ForumMention extends Stacking("forumMention", "You have been mentioned $[notif_count] times")
case object StreamStart extends Stacking("streamStart", "$[notif_count] streamers streaming")
case object InvitedStudy extends Stacking("invitedStudy", "You have $[notif_count] study invites")
case GameFinish extends Stacking("gameFinish", "$[notif_count] games are over")
case GameMove extends Stacking("gameMove", "It's your turn in $[notif_count] games")
case GameTakebackOffer extends Stacking("gameTakebackOffer", "Takeback offers in $[notif_count] games")
case GameDrawOffer extends Stacking("gameDrawOffer", "Draw offers in $[notif_count] games")
case PrivateMessage extends Stacking("privateMessage", "You have $[notif_count] new messages")
case ChallengeCreate extends Stacking("challengeCreate", "You have $[notif_count] new challenges")
case ChallengeAccept extends Stacking("challengeAccept", "$[notif_count] players accepted your challenges")
case TourSoon extends Stacking("tourSoon", "$[notif_count] tournaments are starting")
case ForumMention extends Stacking("forumMention", "You have been mentioned $[notif_count] times")
case StreamStart extends Stacking("streamStart", "$[notif_count] streamers streaming")
case InvitedStudy extends Stacking("invitedStudy", "You have $[notif_count] study invites")
2 changes: 1 addition & 1 deletion modules/puzzle/src/main/DailyPuzzle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final private[puzzle] class DailyPuzzle(
import framework.*
val forbiddenThemes = List(PuzzleTheme.oneMove) :::
odds(2).??(List(PuzzleTheme.checkFirst))
Match(pathApi.select(PuzzleAngle.mix, PuzzleTier.Top, 2150 to 2300)) -> List(
Match(pathApi.select(PuzzleAngle.mix, PuzzleTier.top, 2150 to 2300)) -> List(
Sample(3),
Project($doc("ids" -> true, "_id" -> false)),
UnwindField("ids"),
Expand Down
6 changes: 3 additions & 3 deletions modules/puzzle/src/main/PuzzleAnon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ final class PuzzleAnon(
.buildAsyncFuture { angle =>
countApi byAngle angle flatMap { count =>
val tier =
if (count > 5000) PuzzleTier.Top
else if (count > 2000) PuzzleTier.Good
else PuzzleTier.All
if (count > 5000) PuzzleTier.top
else if (count > 2000) PuzzleTier.good
else PuzzleTier.all
val ratingRange: Range =
if (count > 9000) 1300 to 1600
else if (count > 5000) 1100 to 1800
Expand Down
4 changes: 2 additions & 2 deletions modules/puzzle/src/main/PuzzleBatch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ final class PuzzleBatch(colls: PuzzleColls, anonApi: PuzzleAnon, pathApi: Puzzle
def nextFor(user: Option[User], angle: PuzzleAngle, nb: Int): Fu[Vector[Puzzle]] = (nb > 0) ?? {
user.fold(anonApi.getBatchFor(angle, nb)) { user =>
val tier =
if (user.perfs.puzzle.nb > 5000) PuzzleTier.Good
else PuzzleTier.Top
if (user.perfs.puzzle.nb > 5000) PuzzleTier.good
else PuzzleTier.top
pathApi
.nextFor(user, angle, tier, PuzzleDifficulty.Normal, Set.empty)
.orFail(s"No puzzle path for ${user.id} $tier")
Expand Down
22 changes: 9 additions & 13 deletions modules/puzzle/src/main/PuzzleDifficulty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package lila.puzzle

import lila.i18n.{ I18nKey, I18nKeys as trans }

sealed abstract class PuzzleDifficulty(val ratingDelta: Int, val name: I18nKey):

enum PuzzleDifficulty(val ratingDelta: Int, val name: I18nKey):
lazy val key = toString.toLowerCase
case Easiest extends PuzzleDifficulty(-600, trans.puzzle.easiest)
case Easier extends PuzzleDifficulty(-300, trans.puzzle.easier)
case Normal extends PuzzleDifficulty(0, trans.puzzle.normal)
case Harder extends PuzzleDifficulty(300, trans.puzzle.harder)
case Hardest extends PuzzleDifficulty(600, trans.puzzle.hardest)

object PuzzleDifficulty:
case object Easiest extends PuzzleDifficulty(-600, trans.puzzle.easiest)
case object Easier extends PuzzleDifficulty(-300, trans.puzzle.easier)
case object Normal extends PuzzleDifficulty(0, trans.puzzle.normal)
case object Harder extends PuzzleDifficulty(300, trans.puzzle.harder)
case object Hardest extends PuzzleDifficulty(600, trans.puzzle.hardest)

val all = List(Easiest, Easier, Normal, Harder, Hardest)
val default: PuzzleDifficulty = Normal

val all = values.toList
val default: PuzzleDifficulty = Normal
def isExtreme(d: PuzzleDifficulty) = d == Easiest || d == Hardest

def find(str: String) = all.find(_.key == str)
def find(str: String) = all.find(_.key == str)
10 changes: 5 additions & 5 deletions modules/puzzle/src/main/PuzzlePath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final private class PuzzlePathApi(colls: PuzzleColls)(using Executor):
compromise: Int = 0
): Fu[Option[Id]] = {
val actualTier =
if (tier == PuzzleTier.Top && PuzzleDifficulty.isExtreme(difficulty)) PuzzleTier.Good
if (tier == PuzzleTier.top && PuzzleDifficulty.isExtreme(difficulty)) PuzzleTier.good
else tier
colls
.path {
Expand All @@ -52,10 +52,10 @@ final private class PuzzlePathApi(colls: PuzzleColls)(using Executor):
}
.flatMap {
case Some(path) => fuccess(path.some)
case _ if actualTier == PuzzleTier.Top =>
nextFor(user, angle, PuzzleTier.Good, difficulty, previousPaths)
case _ if actualTier == PuzzleTier.Good && compromise == 2 =>
nextFor(user, angle, PuzzleTier.All, difficulty, previousPaths, compromise = 1)
case _ if actualTier == PuzzleTier.top =>
nextFor(user, angle, PuzzleTier.good, difficulty, previousPaths)
case _ if actualTier == PuzzleTier.good && compromise == 2 =>
nextFor(user, angle, PuzzleTier.all, difficulty, previousPaths, compromise = 1)
case _ if compromise < 5 =>
nextFor(user, angle, actualTier, difficulty, previousPaths, compromise + 1)
case _ => fuccess(none)
Expand Down
2 changes: 1 addition & 1 deletion modules/puzzle/src/main/PuzzleSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ final class PuzzleSessionApi(

private def createSessionFor(user: User, angle: PuzzleAngle, settings: PuzzleSettings): Fu[PuzzleSession] =
pathApi
.nextFor(user, angle, PuzzleTier.Top, settings.difficulty, Set.empty)
.nextFor(user, angle, PuzzleTier.top, settings.difficulty, Set.empty)
.orFail(s"No puzzle path found for ${user.id}, angle: $angle")
.dmap(pathId => PuzzleSession(settings, pathId, 0))
2 changes: 1 addition & 1 deletion modules/puzzle/src/main/PuzzleStreak.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class PuzzleStreakApi(colls: PuzzleColls, cacheApi: CacheApi)(using Execut
Facet(
buckets.map { case (rating, nbPuzzles) =>
val (tier, samples, deviation) =
if (rating > 2300) (PuzzleTier.Good, 5, 110) else (PuzzleTier.Top, 1, 85)
if (rating > 2300) (PuzzleTier.good, 5, 110) else (PuzzleTier.top, 1, 85)
rating.toString -> List(
Match(
$doc(
Expand Down
21 changes: 8 additions & 13 deletions modules/puzzle/src/main/PuzzleTier.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package lila.puzzle

sealed abstract class PuzzleTier(val key: String):

enum PuzzleTier:
def key = toString
def stepDown = PuzzleTier stepDown this

override def toString = key
case top, good, all

object PuzzleTier:

case object Top extends PuzzleTier("top")
case object Good extends PuzzleTier("good")
case object All extends PuzzleTier("all")

def stepDown(tier: PuzzleTier): Option[PuzzleTier] =
if (tier == Top) Good.some
else if (tier == Good) All.some
if (tier == top) good.some
else if (tier == good) all.some
else none

def from(tier: String) =
if (tier == Top.key) Top
else if (tier == Good.key) Good
else All
if (tier == top.toString) top
else if (tier == good.toString) good
else all
19 changes: 6 additions & 13 deletions modules/shutup/src/main/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,12 @@ case class TextAnalysis(

lazy val critical = badWords.nonEmpty && Analyser.isCritical(text)

sealed abstract class TextType(
val key: String,
val rotation: Int,
val name: String
)

object TextType:

case object PublicForumMessage extends TextType("puf", 20, "Public forum message")
case object TeamForumMessage extends TextType("tef", 20, "Team forum message")
case object PrivateMessage extends TextType("prm", 20, "Private message")
case object PrivateChat extends TextType("prc", 40, "Private chat")
case object PublicChat extends TextType("puc", 60, "Public chat")
enum TextType(val key: String, val rotation: Int, val name: String):
case PublicForumMessage extends TextType("puf", 20, "Public forum message")
case TeamForumMessage extends TextType("tef", 20, "Team forum message")
case PrivateMessage extends TextType("prm", 20, "Private message")
case PrivateChat extends TextType("prc", 40, "Private chat")
case PublicChat extends TextType("puc", 60, "Public chat")

case class TextReport(textType: TextType, ratios: List[Double]):

Expand Down
2 changes: 1 addition & 1 deletion modules/storm/src/main/StormSelector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class StormSelector(colls: PuzzleColls, cacheApi: CacheApi)(using Executor
def apply: Fu[List[StormPuzzle]] = current.get {}

private val theme = lila.puzzle.PuzzleTheme.mix.key
private val tier = lila.puzzle.PuzzleTier.Good.key
private val tier = lila.puzzle.PuzzleTier.good.key
private val maxDeviation = 85

/* for path boundaries:
Expand Down
21 changes: 10 additions & 11 deletions modules/tournament/src/main/Schedule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,23 @@ object Schedule:
def apply(name: String) = all.find(_.name == name)
def byId(id: Int) = all.find(_.id == id)

sealed abstract class Speed(val id: Int):
enum Speed(val id: Int):
val name = Speed.this.toString
val key = lila.common.String lcfirst name
def trans(using lang: Lang): String = this match
case Speed.Rapid => I18nKeys.rapid.txt()
case Speed.Classical => I18nKeys.classical.txt()
case _ => name
case UltraBullet extends Speed(5)
case HyperBullet extends Speed(10)
case Bullet extends Speed(20)
case HippoBullet extends Speed(25)
case SuperBlitz extends Speed(30)
case Blitz extends Speed(40)
case Rapid extends Speed(50)
case Classical extends Speed(60)
object Speed:
case object UltraBullet extends Speed(5)
case object HyperBullet extends Speed(10)
case object Bullet extends Speed(20)
case object HippoBullet extends Speed(25)
case object SuperBlitz extends Speed(30)
case object Blitz extends Speed(40)
case object Rapid extends Speed(50)
case object Classical extends Speed(60)
val all: List[Speed] =
List(UltraBullet, HyperBullet, Bullet, HippoBullet, SuperBlitz, Blitz, Rapid, Classical)
val all = values.toList
val mostPopular: List[Speed] = List(Bullet, Blitz, Rapid, Classical)
def apply(key: String) = all.find(_.key == key) orElse all.find(_.key.toLowerCase == key.toLowerCase)
def byId(id: Int) = all find (_.id == id)
Expand Down
15 changes: 7 additions & 8 deletions modules/tournament/src/main/Tournament.scala
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,11 @@ object Tournament:

case class PastAndNext(past: List[Tournament], next: List[Tournament])

sealed abstract class JoinResult(val error: Option[String]):
enum JoinResult(val error: Option[String]):
def ok = error.isEmpty
object JoinResult:
case object Ok extends JoinResult(none)
case object WrongEntryCode extends JoinResult("Wrong entry code".some)
case object Paused extends JoinResult("Your pause is not over yet".some)
case object Verdicts extends JoinResult("Tournament restrictions".some)
case object MissingTeam extends JoinResult("Missing team".some)
case object Nope extends JoinResult("Couldn't join for some reason?".some)
case Ok extends JoinResult(none)
case WrongEntryCode extends JoinResult("Wrong entry code".some)
case Paused extends JoinResult("Your pause is not over yet".some)
case Verdicts extends JoinResult("Tournament restrictions".some)
case MissingTeam extends JoinResult("Missing team".some)
case Nope extends JoinResult("Couldn't join for some reason?".some)
Loading

0 comments on commit c479974

Please sign in to comment.