-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #605 from lichess-org/tournament-clock
Tournament clock
- Loading branch information
Showing
5 changed files
with
79 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package chess | ||
|
||
import cats.syntax.all.* | ||
|
||
import Clock.{ LimitSeconds, IncrementSeconds } | ||
|
||
case class TournamentClock(limitSeconds: LimitSeconds, incrementSeconds: IncrementSeconds): | ||
|
||
def toClockConfig: Option[Clock.Config] = | ||
Clock.Config(limitSeconds, incrementSeconds).some | ||
|
||
object TournamentClock: | ||
|
||
object parse: | ||
|
||
private val cleanRegex = "(move|minutes|minute|min|m|seconds|second|sec|s|'|\"|/)".r | ||
|
||
private def make(a: Int, b: Int) = | ||
val limit = LimitSeconds(if a > 180 then a else a * 60) | ||
TournamentClock(limit, IncrementSeconds(b)) | ||
|
||
def apply(str: String): Option[TournamentClock] = | ||
cleanRegex | ||
.replaceAllIn(str.toLowerCase, "") | ||
.replace(" ", "") | ||
.split('+') match | ||
case Array(a) => a.toIntOption.map(make(_, 0)) | ||
case Array(a, b) => (a.toIntOption, b.toIntOption).mapN(make) | ||
case _ => none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package chess | ||
|
||
import Clock.* | ||
|
||
class TournamentClockTest extends ChessTest: | ||
|
||
import TournamentClock.parse | ||
|
||
def someClock(minutes: Int, seconds: Int) = Some: | ||
TournamentClock(LimitSeconds(minutes * 60), IncrementSeconds(seconds)) | ||
|
||
test("parse"): | ||
assertEquals(parse(""), None) | ||
assertEquals(parse("nope"), None) | ||
|
||
assertEquals(parse("5+5"), someClock(5, 5)) | ||
assertEquals(parse("5+0"), someClock(5, 0)) | ||
assertEquals(parse("30+40"), someClock(30, 40)) | ||
|
||
assertEquals(parse("15m + 10s"), someClock(15, 10)) | ||
assertEquals(parse("15 m + 10 s"), someClock(15, 10)) | ||
assertEquals(parse("15min + 10sec"), someClock(15, 10)) | ||
assertEquals(parse("15m + 10 sec"), someClock(15, 10)) | ||
assertEquals(parse("15 min + 10 sec"), someClock(15, 10)) | ||
assertEquals(parse("15 min + 10 s"), someClock(15, 10)) | ||
assertEquals(parse("15 minutes + 10 seconds"), someClock(15, 10)) | ||
assertEquals(parse(" 15 MiNUTes+10SECOnds "), someClock(15, 10)) | ||
|
||
assertEquals(parse("15 min + 10 sec / move"), someClock(15, 10)) | ||
assertEquals(parse("15 min + 10 s / move"), someClock(15, 10)) | ||
assertEquals(parse("15 min + 10 seconds / move"), someClock(15, 10)) | ||
assertEquals(parse("15 minutes + 10 seconds / move"), someClock(15, 10)) | ||
|
||
assertEquals(parse("90 min + 30 sec / move"), someClock(90, 30)) | ||
|
||
assertEquals(parse("120' + 12\""), someClock(120, 12)) | ||
assertEquals(parse("120' + 12\"/move"), someClock(120, 12)) | ||
assertEquals(parse("120' + 12\" / move"), someClock(120, 12)) | ||
assertEquals(parse("7200+12"), someClock(120, 12)) | ||
|
||
assertEquals(parse("3600"), someClock(60, 0)) | ||
assertEquals(parse("60"), someClock(60, 0)) | ||
assertEquals(parse("180"), someClock(180, 0)) | ||
assertEquals(parse("240"), someClock(4, 0)) | ||
|
||
// we're not there yet | ||
// assertEquals(parse("90 min / 40 moves + 30 min + 30 sec / move"), ???) |