Skip to content

Commit

Permalink
fix time control parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 31, 2024
1 parent 75b5a36 commit 4c8d1a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 7 additions & 7 deletions core/src/main/scala/TournamentClock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ object TournamentClock:

object parse:

private val cleanRegex = "(move|minutes|minute|min|m|seconds|second|sec|s|'|\"|/)".r
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
.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
2 changes: 2 additions & 0 deletions test-kit/src/test/scala/TournamentClockTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ class TournamentClockTest extends ChessTest:
assertEquals(parse("180"), someClock(180, 0))
assertEquals(parse("240"), someClock(4, 0))

assertEquals(parse("120 min / 40 moves + 30 min"), None)

// we're not there yet
// assertEquals(parse("90 min / 40 moves + 30 min + 30 sec / move"), ???)

0 comments on commit 4c8d1a0

Please sign in to comment.