Skip to content

Commit

Permalink
Add emt field to Study & parse it
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Apr 10, 2024
1 parent a61452a commit 764d9a5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
6 changes: 3 additions & 3 deletions modules/study/src/main/BSONHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ object BSONHandlers:
def reads(r: Reader) =
val brush = r.str("b")
r.getO[Square]("p")
.map { pos =>
Shape.Circle(brush, pos)
}
.map(pos => Shape.Circle(brush, pos))
.getOrElse(Shape.Arrow(brush, r.get[Square]("o"), r.get[Square]("d")))
def writes(w: Writer, t: Shape) =
t match
Expand Down Expand Up @@ -140,6 +138,7 @@ object BSONHandlers:
glyphs = doc.getAsOpt[Glyphs](F.glyphs).getOrElse(Glyphs.empty)
eval = doc.getAsOpt[Score](F.score).map(_.eval)
clock = doc.getAsOpt[Centis](F.clock)
emt = doc.getAsOpt[Centis](F.emt)
crazyData = doc.getAsOpt[Crazyhouse.Data](F.crazy)
forceVariation = ~doc.getAsOpt[Boolean](F.forceVariation)
yield Branch(
Expand Down Expand Up @@ -175,6 +174,7 @@ object BSONHandlers:
glyphs = doc.getAsOpt[Glyphs](F.glyphs).getOrElse(Glyphs.empty)
eval = doc.getAsOpt[Score](F.score).map(_.eval)
clock = doc.getAsOpt[Centis](F.clock)
emt = doc.getAsOpt[Centis](F.emt)
crazyData = doc.getAsOpt[Crazyhouse.Data](F.crazy)
forceVariation = ~doc.getAsOpt[Boolean](F.forceVariation)
yield NewBranch(
Expand Down
16 changes: 13 additions & 3 deletions modules/study/src/main/CommentParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ private[study] object CommentParser:
private val arrowsRemoveRegex = """\[\%cal[\s\r\n]++((?:\w{5}[,\s]*+)++)\]""".r
private val clockRegex = """(?s)\[\%clk[\s\r\n]++([\d:\.]++)\]""".r.unanchored
private val clockRemoveRegex = """\[\%clk[\s\r\n]++[\d:\.]++\]""".r
private val emtRegex = """(?s)\[\%emt[\s\r\n]++([\d:\.]++)\]""".r.unanchored
private val emtRemoveRegex = """\[\%emt[\s\r\n]++[\d:\.]++\]""".r
private val tcecClockRegex = """(?s)tl=([\d:\.]++)""".r.unanchored
private val tcecClockRemoveRegex = """tl=[\d:\.]++""".r

case class ParsedComment(
shapes: Shapes,
clock: Option[Centis],
emt: Option[Centis],
comment: String
)

def apply(comment: ChessComment): ParsedComment =
parseShapes(comment.value) match
case (shapes, c2) =>
parseClock(c2) match
case (clock, c3) => ParsedComment(shapes, clock, c3)
case (clock, c3) =>
parseEmt(c3) match
case (emt, c4) => ParsedComment(shapes, clock, emt, c4)

private type ClockAndComment = (Option[Centis], String)
private type CentisAndComment = (Option[Centis], String)

private def readCentis(hours: String, minutes: String, seconds: String): Option[Centis] =
for
Expand All @@ -52,12 +57,17 @@ private[study] object CommentParser:
readCentis(hours, minutes, seconds)
case _ => none

private def parseClock(comment: String): ClockAndComment =
private def parseClock(comment: String): CentisAndComment =
comment match
case clockRegex(str) => readCentis(str) -> clockRemoveRegex.replaceAllIn(comment, "").trim
case tcecClockRegex(str) => readCentis(str) -> tcecClockRemoveRegex.replaceAllIn(comment, "").trim
case _ => None -> comment

private def parseEmt(comment: String): CentisAndComment =
comment match
case emtRegex(str) => readCentis(str) -> emtRemoveRegex.replaceAllIn(comment, "").trim
case _ => None -> comment

private type ShapesAndComment = (Shapes, String)

private def parseShapes(comment: String): ShapesAndComment =
Expand Down
8 changes: 5 additions & 3 deletions modules/study/src/main/NewPgnImport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object NewPgnImport:
case Preprocessed(game, replay, initialFen, parsedPgn) =>
val annotator = PgnImport.findAnnotator(parsedPgn, contributors)
PgnImport.parseComments(parsedPgn.initialPosition.comments, annotator) match
case (shapes, _, comments) =>
case (shapes, _, _, comments) =>
val root: NewRoot =
NewRoot(
Metas(
Expand All @@ -49,7 +49,8 @@ object NewPgnImport:
glyphs = Glyphs.empty,
opening = None,
crazyData = replay.setup.situation.board.crazyData,
clock = parsedPgn.tags.clockConfig.map(_.limit)
clock = parsedPgn.tags.clockConfig.map(_.limit),
emt = None
),
parsedPgn.tree.flatMap(makeTree(replay.setup, _, annotator))
)
Expand Down Expand Up @@ -100,7 +101,7 @@ object NewPgnImport:
(
game,
PgnImport.parseComments(data.metas.comments, annotator) match
case (shapes, clock, comments) =>
case (shapes, clock, emt, comments) =>
NewBranch(
id = id,
move = Uci.WithSan(uci, sanStr),
Expand All @@ -119,6 +120,7 @@ object NewPgnImport:
glyphs = data.metas.glyphs,
opening = None,
clock = clock,
emt = emt,
crazyData = game.situation.board.crazyData
)
).some
Expand Down
1 change: 1 addition & 0 deletions modules/study/src/main/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ object Node:
val clock = "l"
val crazy = "z"
val forceVariation = "fv"
val emt = "em"
32 changes: 18 additions & 14 deletions modules/study/src/main/PgnImport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object PgnImport:
case Preprocessed(game, replay, initialFen, parsedPgn) =>
val annotator = findAnnotator(parsedPgn, contributors)
parseComments(parsedPgn.initialPosition.comments, annotator) match
case (shapes, _, comments) =>
case (shapes, _, _, comments) =>
val root = Root(
ply = replay.setup.ply,
fen = initialFen | game.variant.initialFen,
Expand All @@ -40,6 +40,7 @@ object PgnImport:
comments = comments,
glyphs = Glyphs.empty,
clock = parsedPgn.tags.clockConfig.map(_.limit),
emt = none,
crazyData = replay.setup.situation.board.crazyData,
children = parsedPgn.tree.fold(Branches.empty)(makeBranches(replay.setup, _, annotator))
)
Expand Down Expand Up @@ -88,19 +89,21 @@ object PgnImport:
def parseComments(
comments: List[ChessComment],
annotator: Option[Comment.Author]
): (Shapes, Option[Centis], Comments) =
comments.foldLeft((Shapes(Nil), none[Centis], Comments(Nil))) { case ((shapes, clock, comments), txt) =>
CommentParser(txt) match
case CommentParser.ParsedComment(s, c, str) =>
(
(shapes ++ s),
c.orElse(clock),
(str.trim match
case "" => comments
case com =>
comments + Comment(Comment.Id.make, Comment.Text(com), annotator | Comment.Author.Lichess)
): (Shapes, Option[Centis], Option[Centis], Comments) =
comments.foldLeft((Shapes(Nil), none[Centis], none[Centis], Comments(Nil))) {
case ((shapes, clock, emt, comments), txt) =>
CommentParser(txt) match
case CommentParser.ParsedComment(s, c, e, str) =>
(
(shapes ++ s),
c.orElse(clock),
e.orElse(emt),
(str.trim match
case "" => comments
case com =>
comments + Comment(Comment.Id.make, Comment.Text(com), annotator | Comment.Author.Lichess)
)
)
)
}

private def makeBranches(
Expand Down Expand Up @@ -128,7 +131,7 @@ object PgnImport:
val uci = moveOrDrop.toUci
val sanStr = moveOrDrop.toSanStr
parseComments(node.value.metas.comments, annotator) match
case (shapes, clock, comments) =>
case (shapes, clock, emt, comments) =>
Branch(
id = UciCharPair(uci),
ply = game.ply,
Expand All @@ -139,6 +142,7 @@ object PgnImport:
comments = comments,
glyphs = node.value.metas.glyphs,
clock = clock,
emt = emt,
crazyData = game.situation.board.crazyData,
children = node.child.fold(Branches.empty)(makeBranches(game, _, annotator))
).some
Expand Down
3 changes: 3 additions & 0 deletions modules/tree/src/main/newTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ case class Metas(
glyphs: Glyphs = Glyphs.empty,
opening: Option[Opening] = None,
clock: Option[Centis] = None,
emt: Option[Centis] = None,
crazyData: Option[Crazyhouse.Data] = None
// TODO, add support for variationComments
):
Expand Down Expand Up @@ -79,6 +80,7 @@ case class NewBranch(
glyphs,
opening,
clock,
emt,
crazyData
}
override def toString = s"$ply, $id, ${move.uci}"
Expand Down Expand Up @@ -167,6 +169,7 @@ object NewTree:
node.glyphs,
node.opening,
node.clock,
node.emt,
node.crazyData
)

Expand Down
3 changes: 3 additions & 0 deletions modules/tree/src/main/tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ sealed trait Node:
def addChild(branch: Branch): Node
def dropFirstChild: Node
def clock: Option[Centis]
def emt: Option[Centis]
def forceVariation: Boolean

// implementation dependent
Expand Down Expand Up @@ -191,6 +192,7 @@ case class Root(
children: Branches = Branches.empty,
opening: Option[Opening] = None,
clock: Option[Centis] = None, // clock state at game start, assumed same for both players
emt: Option[Centis] = None,
crazyData: Option[Crazyhouse.Data]
) extends Node:

Expand Down Expand Up @@ -321,6 +323,7 @@ case class Branch(
opening: Option[Opening] = None,
comp: Boolean = false,
clock: Option[Centis] = None, // clock state after the move is played, and the increment applied
emt: Option[Centis] = None, // estimated move time
crazyData: Option[Crazyhouse.Data],
forceVariation: Boolean = false // cannot be mainline
) extends Node:
Expand Down

0 comments on commit 764d9a5

Please sign in to comment.