Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions modules/study/src/main/StudyPgnImport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import lila.tree.{ Branch, Branches, ImportResult, Root, Clock }
object StudyPgnImport:

case class Context(
currentGame: chess.Game,
currentPosition: chess.Position,
clocks: ByColor[Option[Clock]],
timeControl: Option[TournamentClock]
timeControl: Option[TournamentClock],
ply: Ply
)

def result(pgn: PgnStr, contributors: List[LightUser]): Either[ErrorStr, Result] =
lila.tree.parseImport(pgn).map(result(_, contributors))

def result(importResult: ImportResult, contributors: List[LightUser]): Result =
import importResult.*
import importResult.{ replay, initialFen, parsed }
val annotator = findAnnotator(parsed, contributors)

val timeControl = parsed.tags.timeControl
Expand All @@ -29,23 +30,27 @@ object StudyPgnImport:
case (shapes, _, _, comments) =>
val root = Root(
ply = replay.setup.ply,
fen = initialFen | game.position.variant.initialFen,
fen = initialFen | replay.setup.position.variant.initialFen,
check = replay.setup.position.check,
shapes = shapes,
comments = comments,
glyphs = Glyphs.empty,
clock = clock,
crazyData = replay.setup.position.crazyData,
children = parsed.tree.fold(Branches.empty):
makeBranches(Context(replay.setup, ByColor.fill(clock), timeControl), _, annotator)
makeBranches(
Context(replay.setup.position, ByColor.fill(clock), timeControl, replay.setup.ply),
_,
annotator
)
)

val ending = importResult.result.map: res =>
Ending(
status = res.status,
points = res.points,
resultText = chess.Outcome.showPoints(res.points.some),
statusText = lila.tree.StatusText(res.status, res.winner, game.position.variant)
statusText = lila.tree.StatusText(res.status, res.winner, replay.setup.position.variant)
)

val commented =
Expand All @@ -57,7 +62,7 @@ object StudyPgnImport:

Result(
root = commented,
variant = game.position.variant,
variant = replay.setup.position.variant,
tags = PgnTags
.withRelevantTags(parsed.tags, Set(Tag.WhiteClock, Tag.BlackClock)),
ending = ending
Expand Down Expand Up @@ -129,37 +134,39 @@ object StudyPgnImport:
): Option[Branch] =
try
node.value
.san(context.currentGame.position)
.san(context.currentPosition)
.fold(
_ => none, // illegal move; stop here.
moveOrDrop =>
val game = moveOrDrop.applyGame(context.currentGame)
val position = moveOrDrop.after
val currentPly = context.ply.next
val uci = moveOrDrop.toUci
val sanStr = moveOrDrop.toSanStr
val (shapes, clock, emt, comments) = parseComments(node.value.metas.comments, annotator)
val mover = !game.ply.turn
val mover = !position.color
val computedClock: Option[Clock] = clock
.map(Clock(_, trust = true.some))
.orElse:
(context.clocks(mover), emt).mapN(guessNewClockState(_, game.ply, context.timeControl, _))
(context.clocks(mover), emt).mapN(guessNewClockState(_, currentPly, context.timeControl, _))
.filter(_.positive)
Branch(
id = UciCharPair(uci),
ply = game.ply,
ply = currentPly,
move = Uci.WithSan(uci, sanStr),
fen = Fen.write(game),
check = game.position.check,
fen = Fen.write(position, currentPly.fullMoveNumber),
check = position.check,
shapes = shapes,
comments = comments,
glyphs = node.value.metas.glyphs,
clock = computedClock,
crazyData = game.position.crazyData,
crazyData = position.crazyData,
children = node.child.fold(Branches.empty):
makeBranches(
Context(
game,
position,
context.clocks.update(mover, _ => computedClock),
context.timeControl
context.timeControl,
currentPly
),
_,
annotator
Expand Down
25 changes: 13 additions & 12 deletions modules/study/src/main/StudyPgnImportNew.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ object StudyPgnImportNew:
case (shapes, _, _, comments) =>
val tc = parsedPgn.tags.timeControl
val clock = tc.map(_.limit).map(Clock(_, true.some))
val setup = Context(replay.setup, ByColor.fill(clock), tc)
val setup = Context(replay.setup.position, ByColor.fill(clock), tc, replay.setup.ply)
val root: NewRoot =
NewRoot(
Metas(
ply = replay.setup.ply,
fen = initialFen | game.position.variant.initialFen,
fen = initialFen | replay.setup.position.variant.initialFen,
check = replay.setup.position.check,
dests = None,
drops = None,
Expand All @@ -54,7 +54,7 @@ object StudyPgnImportNew:
status = res.status,
points = res.points,
resultText = chess.Outcome.showPoints(res.points.some),
statusText = lila.tree.StatusText(res.status, res.winner, game.position.variant)
statusText = lila.tree.StatusText(res.status, res.winner, replay.state.position.variant)
)

val commented =
Expand Down Expand Up @@ -88,19 +88,20 @@ object StudyPgnImportNew:
annotator: Option[Comment.Author]
): (Context, Option[NewBranch]) =
data
.san(context.currentGame.position)
.san(context.currentPosition)
.map(moveOrDrop =>
val game = moveOrDrop.applyGame(context.currentGame)
val game = moveOrDrop.after
val currentPly = context.ply.next
val uci = moveOrDrop.toUci
val id = UciCharPair(uci)
val sanStr = moveOrDrop.toSanStr
val (shapes, clock, emt, comments) = StudyPgnImport.parseComments(data.metas.comments, annotator)
val mover = !game.ply.turn
val mover = !game.color
val computedClock: Option[Clock] = clock
.map(Clock(_, trust = true.some))
.orElse:
(context.clocks(mover), emt)
.mapN(StudyPgnImport.guessNewClockState(_, game.ply, context.timeControl, _))
.mapN(StudyPgnImport.guessNewClockState(_, currentPly, context.timeControl, _))
.filter(_.positive)
val newBranch =
NewBranch(
Expand All @@ -109,9 +110,9 @@ object StudyPgnImportNew:
comp = false,
forceVariation = false,
Metas(
ply = game.ply,
fen = Fen.write(game),
check = game.position.check,
ply = currentPly,
fen = Fen.write(game, currentPly.fullMoveNumber),
check = game.check,
dests = None,
drops = None,
eval = None,
Expand All @@ -121,11 +122,11 @@ object StudyPgnImportNew:
glyphs = data.metas.glyphs,
opening = None,
clock = computedClock,
crazyData = game.position.crazyData
crazyData = game.crazyData
)
)

(Context(game, context.clocks, context.timeControl), newBranch.some)
(Context(game, context.clocks, context.timeControl, currentPly), newBranch.some)
)
.toOption
.match
Expand Down