ignore invalid chapter nodes

pull/8091/head
Thibault Duplessis 2021-02-03 08:55:37 +01:00
parent cc9250d7ea
commit 697f1f3044
2 changed files with 40 additions and 25 deletions

View File

@ -158,24 +158,37 @@ object BSONHandlers {
)
}
def readNode(doc: Bdoc, id: UciCharPair): Node = {
import Node.BsonFields._
val r = new Reader(doc)
Node(
id = id,
ply = r int ply,
move = WithSan(r.get[Uci](uci), r.str(san)),
fen = r.get[FEN](fen),
check = r boolD check,
shapes = r.getO[Shapes](shapes) | Shapes.empty,
comments = r.getO[Comments](comments) | Comments.empty,
gamebook = r.getO[Gamebook](gamebook),
glyphs = r.getO[Glyphs](glyphs) | Glyphs.empty,
score = r.getO[Score](score),
crazyData = r.getO[Crazyhouse.Data](crazy),
clock = r.getO[Centis](clock),
children = Node.emptyChildren,
forceVariation = r boolD forceVariation
def readNode(doc: Bdoc, id: UciCharPair): Option[Node] = {
import Node.{ BsonFields => F }
for {
ply <- doc.getAsOpt[Int](F.ply)
uci <- doc.getAsOpt[Uci](F.uci)
san <- doc.getAsOpt[String](F.san)
fen <- doc.getAsOpt[FEN](F.fen)
check = ~doc.getAsOpt[Boolean](F.check)
shapes = doc.getAsOpt[Shapes](F.shapes) getOrElse Shapes.empty
comments = doc.getAsOpt[Comments](F.comments) getOrElse Comments.empty
gamebook = doc.getAsOpt[Gamebook](F.gamebook)
glyphs = doc.getAsOpt[Glyphs](F.glyphs) getOrElse Glyphs.empty
score = doc.getAsOpt[Score](F.score)
clock = doc.getAsOpt[Centis](F.clock)
crazy = doc.getAsOpt[Crazyhouse.Data](F.crazy)
forceVariation = ~doc.getAsOpt[Boolean](F.forceVariation)
} yield Node(
id,
ply,
WithSan(uci, san),
fen,
check,
shapes,
comments,
gamebook,
glyphs,
score,
clock,
crazy,
Node.emptyChildren,
forceVariation
)
}

View File

@ -13,9 +13,10 @@ private object StudyFlatTree {
private case class FlatNode(path: Path, data: Bdoc) {
val depth = path.ids.size
def toNodeWithChildren(children: Option[Children]): Node = {
readNode(data, path.ids.last)
}.copy(children = children | Node.emptyChildren)
def toNodeWithChildren(children: Option[Children]): Option[Node] =
readNode(data, path.ids.last) map {
_.copy(children = children | Node.emptyChildren)
}
}
object reader {
@ -41,10 +42,11 @@ private object StudyFlatTree {
// assumes that node has a greater depth than roots (sort beforehand)
private def update(roots: Map[Path, Children], flat: FlatNode): Map[Path, Children] = {
val node = flat.toNodeWithChildren(roots get flat.path)
roots.removed(flat.path).updatedWith(flat.path.parent) {
case None => Children(Vector(node)).some
case Some(siblings) => siblings.addNode(node).some
flat.toNodeWithChildren(roots get flat.path).fold(roots) { node =>
roots.removed(flat.path).updatedWith(flat.path.parent) {
case None => Children(Vector(node)).some
case Some(siblings) => siblings.addNode(node).some
}
}
}
}