Skip to content

Commit

Permalink
integrate TournamentClock
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 29, 2024
1 parent e9a6e3a commit d394e12
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 15 deletions.
11 changes: 0 additions & 11 deletions core/src/main/scala/Clock.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package chess

import cats.syntax.option.none
import chess.Clock.Config

import java.text.DecimalFormat
Expand Down Expand Up @@ -207,16 +206,6 @@ object Clock:
if limitSeconds == 0 then increment.atLeast(Centis(300))
else limit

// [TimeControl "600+2"] -> 10+2
def readPgnConfig(str: String): Option[Config] =
str.split('+') match
case Array(initStr, incStr) =>
for
init <- initStr.toIntOption
inc <- incStr.toIntOption
yield Config(init, inc)
case _ => none

def apply(limit: LimitSeconds, increment: IncrementSeconds): Clock = apply(Config(limit, increment))

def apply(config: Config): Clock =
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/TournamentClock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import cats.syntax.all.*

import Clock.{ LimitSeconds, IncrementSeconds }

case class TournamentClock(limitSeconds: LimitSeconds, incrementSeconds: IncrementSeconds)
case class TournamentClock(limitSeconds: LimitSeconds, incrementSeconds: IncrementSeconds):

def toClockConfig: Option[Clock.Config] =
Clock.Config(limitSeconds, incrementSeconds).some

object TournamentClock:

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/format/pgn/Reader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ object Reader:

private def makeGame(tags: Tags) =
val g = Game(variantOption = tags.variant, fen = tags.fen)
g.copy(startedAtPly = g.ply, clock = tags.clockConfig.map(Clock.apply))
g.copy(startedAtPly = g.ply, clock = tags.clockConfig.flatMap(_.toClockConfig).map(Clock.apply))
4 changes: 2 additions & 2 deletions core/src/main/scala/format/pgn/Tag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ case class Tags(value: List[Tag]) extends AnyVal:
val name = which(Tag)
value.find(_.name == name).map(_.value)

def clockConfig: Option[Clock.Config] =
def clockConfig: Option[TournamentClock] =
value
.collectFirst { case Tag(Tag.TimeControl, str) => str }
.flatMap(Clock.readPgnConfig)
.flatMap(TournamentClock.parse.apply)

def variant: Option[chess.variant.Variant] =
apply(_.Variant)
Expand Down

0 comments on commit d394e12

Please sign in to comment.