From b7400ac50cfce6978d29aefc065d9066f35753de Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Fri, 12 Apr 2024 08:31:51 +0700 Subject: [PATCH] Use emt when missing clock in NewPgnImport --- modules/study/src/main/NewPgnImport.scala | 36 ++++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/modules/study/src/main/NewPgnImport.scala b/modules/study/src/main/NewPgnImport.scala index 2608f3af4607b..c211cb8ee708c 100644 --- a/modules/study/src/main/NewPgnImport.scala +++ b/modules/study/src/main/NewPgnImport.scala @@ -34,6 +34,8 @@ object NewPgnImport: val annotator = PgnImport.findAnnotator(parsedPgn, contributors) PgnImport.parseComments(parsedPgn.initialPosition.comments, annotator) match case (shapes, _, _, comments) => + val clock = parsedPgn.tags.clockConfig.map(_.limit) + val setup = Context(replay.setup, clock) val root: NewRoot = NewRoot( Metas( @@ -49,9 +51,9 @@ object NewPgnImport: glyphs = Glyphs.empty, opening = None, crazyData = replay.setup.situation.board.crazyData, - clock = parsedPgn.tags.clockConfig.map(_.limit) + clock = clock ), - parsedPgn.tree.flatMap(makeTree(replay.setup, _, annotator)) + parsedPgn.tree.flatMap(makeTree(setup, _, annotator)) ) val end: Option[PgnImport.End] = (game.finished.option(game.status)).map { status => PgnImport.End( @@ -77,28 +79,33 @@ object NewPgnImport: ) } + case class Context( + game: chess.Game, + clock: Option[Centis] + ) + private def makeTree( - setup: chess.Game, + context: Context, node: ParsedPgnTree, annotator: Option[Comment.Author] ): Option[PgnNode[NewBranch]] = - node.mapAccumlOption_(setup): (setup, data) => - transform(setup, data, annotator) + node.mapAccumlOption_(context): (context, data) => + transform(context, data, annotator) private def transform( - context: chess.Game, + context: Context, data: PgnNodeData, annotator: Option[Comment.Author] - ): (chess.Game, Option[NewBranch]) = + ): (Context, Option[NewBranch]) = data - .san(context.situation) + .san(context.game.situation) .map(moveOrDrop => - val game = moveOrDrop.applyGame(context) + val game = moveOrDrop.applyGame(context.game) val uci = moveOrDrop.toUci val id = UciCharPair(uci) val sanStr = moveOrDrop.toSanStr ( - game, + Context(game, None), // TODO PgnImport.parseComments(data.metas.comments, annotator) match case (shapes, clock, emt, comments) => NewBranch( @@ -118,12 +125,13 @@ object NewPgnImport: gamebook = None, glyphs = data.metas.glyphs, opening = None, - clock = clock, + clock = clock.orElse((context.clock, emt).mapN(_ + _)), crazyData = game.situation.board.crazyData ) ).some ) ) - .toOption match - case Some(branch) => branch - case None => (context, None) + .toOption + .match + case Some(branch) => branch + case None => (context, None)