Skip to content

Commit 75b5a36

Browse files
committed
type safety for pgn move time, using scalalib.model.Seconds
1 parent 55ce6ab commit 75b5a36

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

core/src/main/scala/format/pgn/Pgn.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package format.pgn
33

44
import cats.syntax.all.*
55
import monocle.syntax.all.*
6+
import scalalib.model.Seconds
67

78
type PgnTree = Node[Move]
89

@@ -56,17 +57,17 @@ case class Move(
5657
glyphs: Glyphs = Glyphs.empty,
5758
opening: Option[String] = None,
5859
result: Option[String] = None,
59-
secondsLeft: Option[Int] = None, // %clk clock in seconds for the move player, after the move
60-
moveTime: Option[Int] = None, // %emt estimated move time in seconds
60+
timeLeft: Option[Seconds] = None, // %clk clock in seconds for the move player, after the move
61+
moveTime: Option[Seconds] = None, // %emt estimated move time in seconds
6162
variationComments: List[Comment] = Nil
6263
):
6364

6465
private def clockString: Option[String] = List(
65-
secondsLeft.map(seconds => "[%clk " + Move.formatPgnSeconds(seconds) + "]"),
66+
timeLeft.map(seconds => "[%clk " + Move.formatPgnSeconds(seconds) + "]"),
6667
moveTime.map(seconds => "[%emt " + Move.formatPgnSeconds(seconds) + "]")
6768
).flatten.some.filter(_.nonEmpty).map(_.mkString(" "))
6869

69-
def hasComment = comments.nonEmpty || secondsLeft.isDefined || moveTime.isDefined
70+
def hasComment = comments.nonEmpty || timeLeft.isDefined || moveTime.isDefined
7071

7172
private def nonEmpty = hasComment || opening.isDefined || result.isDefined
7273

@@ -99,6 +100,6 @@ object Move:
99100
private def noDoubleLineBreak(txt: String) =
100101
noDoubleLineBreakRegex.replaceAllIn(txt, "\n")
101102

102-
def formatPgnSeconds(t: Int): String =
103-
val d = java.time.Duration.ofSeconds(t)
103+
def formatPgnSeconds(t: Seconds): String =
104+
val d = java.time.Duration.ofSeconds(t.value)
104105
f"${d.toHours}:${d.toMinutesPart}%02d:${d.toSecondsPart}%02d"

test-kit/src/main/scala/chess/ChessTreeArbitraries.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chess
33
import cats.syntax.all.*
44
import chess.format.pgn.{ Comment, Glyphs, InitialComments, Move as PgnMove, Pgn, Tags }
55
import org.scalacheck.Gen
6+
import scalalib.model.Seconds
67

78
case class GameTree[A](init: Situation, ply: Ply, tree: Option[Node[A]])
89
case class WithMove[A](move: Move, data: A)
@@ -88,7 +89,7 @@ object ChessTreeArbitraries:
8889
comments <- genComments(5)
8990
glyphs <- Gen.someOf(Glyphs.all).map(xs => Glyphs.fromList(xs.toList))
9091
clock <- Gen.posNum[Int]
91-
yield WithMove(move, PgnMove(move.san, comments, glyphs, secondsLeft = clock.some))
92+
yield WithMove(move, PgnMove(move.san, comments, glyphs, timeLeft = Seconds(clock).some))
9293

9394
def genNode[A: Generator](value: A, variations: List[A] = Nil): Gen[Node[A]] =
9495
value.next.flatMap: nextSeeds =>

test-kit/src/test/scala/format/pgn/TimeFormatTest.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package chess
22
package format.pgn
33

44
import scalalib.time.*
5+
import scalalib.model.Seconds
56

67
class TimeFormatTest extends ChessTest:
78

89
test("format seconds"):
9-
assertEquals(Move.formatPgnSeconds(0), "0:00:00")
10-
assertEquals(Move.formatPgnSeconds(9), "0:00:09")
11-
assertEquals(Move.formatPgnSeconds(60), "0:01:00")
12-
assertEquals(Move.formatPgnSeconds(79835), "22:10:35")
13-
assertEquals(Move.formatPgnSeconds(979835), "272:10:35")
10+
def f(s: Int) = Move.formatPgnSeconds(Seconds(s))
11+
assertEquals(f(0), "0:00:00")
12+
assertEquals(f(9), "0:00:09")
13+
assertEquals(f(60), "0:01:00")
14+
assertEquals(f(79835), "22:10:35")
15+
assertEquals(f(979835), "272:10:35")
1416

1517
test("format PGN tags"):
1618
assertEquals(Tag.UTCDate.format.format(millisToDateTime(1680424483730L)), "2023.04.02")

0 commit comments

Comments
 (0)