Skip to content

Commit

Permalink
Use emt when missing clock in PgnImport
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Apr 12, 2024
1 parent 38f6900 commit f9111ac
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions modules/study/src/main/PgnImport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ object PgnImport:
ImportData(pgn, analyse = none).preprocess(user = none).map {
case Preprocessed(game, replay, initialFen, parsedPgn) =>
val annotator = findAnnotator(parsedPgn, contributors)

val clock = parsedPgn.tags.clockConfig.map(_.limit)
parseComments(parsedPgn.initialPosition.comments, annotator) match
case (shapes, _, _, comments) =>
val root = Root(
Expand All @@ -39,9 +41,9 @@ object PgnImport:
shapes = shapes,
comments = comments,
glyphs = Glyphs.empty,
clock = parsedPgn.tags.clockConfig.map(_.limit),
clock = clock,
crazyData = replay.setup.situation.board.crazyData,
children = parsedPgn.tree.fold(Branches.empty)(makeBranches(replay.setup, _, annotator))
children = parsedPgn.tree.fold(Branches.empty)(makeBranches(replay.setup, clock, _, annotator))
)
val end: Option[End] = (game.finished
.option(game.status))
Expand Down Expand Up @@ -107,16 +109,19 @@ object PgnImport:

private def makeBranches(
prev: chess.Game,
prevClock: Option[Centis],
node: PgnNode[PgnNodeData],
annotator: Option[Comment.Author]
): Branches =
val variations = node.take(Node.MAX_PLIES).variations.flatMap(x => makeBranch(prev, x.toNode, annotator))
val variations =
node.take(Node.MAX_PLIES).variations.flatMap(x => makeBranch(prev, prevClock, x.toNode, annotator))
removeDuplicatedChildrenFirstNode(
Branches(makeBranch(prev, node, annotator).fold(variations)(_ +: variations))
Branches(makeBranch(prev, prevClock, node, annotator).fold(variations)(_ +: variations))
)

private def makeBranch(
prev: chess.Game,
prevClock: Option[Centis],
node: PgnNode[PgnNodeData],
annotator: Option[Comment.Author]
): Option[Branch] =
Expand All @@ -131,6 +136,7 @@ object PgnImport:
val sanStr = moveOrDrop.toSanStr
parseComments(node.value.metas.comments, annotator) match
case (shapes, clock, emt, comments) =>
val _clock = clock.orElse((prevClock, emt).mapN(_ + _))
Branch(
id = UciCharPair(uci),
ply = game.ply,
Expand All @@ -140,9 +146,9 @@ object PgnImport:
shapes = shapes,
comments = comments,
glyphs = node.value.metas.glyphs,
clock = clock,
clock = _clock,
crazyData = game.situation.board.crazyData,
children = node.child.fold(Branches.empty)(makeBranches(game, _, annotator))
children = node.child.fold(Branches.empty)(makeBranches(game, _clock, _, annotator))
).some
)
catch
Expand Down

0 comments on commit f9111ac

Please sign in to comment.