Skip to content

Commit

Permalink
Traverse sans only once in gameMoveWhileValid
Browse files Browse the repository at this point in the history
- Fix reversed order when error happen
- Add gameMoveWhileValidReverse because in some case we need
games in reversed order, for example: https://github.com/lichess-org/lila/blob/27f1db88d269c17a04ccdbdfa67ed9cda6bd4514/modules/study/src/main/ServerEval.scala#L147
  • Loading branch information
lenguyenthanh committed Oct 8, 2023
1 parent 6941555 commit 1e9d9d6
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/main/scala/Replay.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ object Replay:
): (Game, List[(Game, Uci.WithSan)], Option[ErrorStr]) =
val init = makeGame(variant, initialFen.some)
val emptyGames = List.empty[(Game, Uci.WithSan)]
(for
moves <- Parser.moves(sans).leftMap(err => (init, emptyGames, err.some))
games <- moves.value
.zip(sans)
.foldM(emptyGames):
case (games, (san, sanStr)) =>
val game = games.headOption.fold(init)(_._1)
san(game.situation)
.leftMap(err => (init, games, err.some))
.map(m => (m.applyGame(game), Uci.WithSan(m.toUci, sanStr)) :: games)
yield (init, games.reverse, none)).merge
sans
.foldM(emptyGames):
case (games, str) =>
Parser
.sanOnly(str)
.flatMap: san =>
val game = games.headOption.fold(init)(_._1)
san(game.situation)
.map(m => (m.applyGame(game), Uci.WithSan(m.toUci, str)) :: games)
.leftMap(err => (init, games.reverse, err.some))
.map(gs => (init, gs.reverse, none))
.merge

private def computeSituations[M](
sit: Situation,
Expand Down

0 comments on commit 1e9d9d6

Please sign in to comment.