some dest encoding refactoring

pull/4786/head
Thibault Duplessis 2018-12-15 22:25:48 +08:00
parent 595b69d9f1
commit f83507a86c
4 changed files with 22 additions and 13 deletions

View File

@ -157,7 +157,7 @@ object Event {
def newJson(moves: Map[Pos, List[Pos]]) =
if (moves.isEmpty) JsNull
else {
val sb = new java.lang.StringBuilder(64)
val sb = new java.lang.StringBuilder(128)
var first = true
moves foreach {
case (orig, dests) =>

View File

@ -5,7 +5,7 @@ import play.api.libs.json._
import chess.format.FEN
import chess.opening._
import chess.variant.Variant
import lila.tree.Node.openingWriter
import lila.tree.Node.{ openingWriter, destString }
case class AnaDests(
variant: Variant,
@ -21,11 +21,7 @@ case class AnaDests(
if (isInitial) AnaDests.initialDests
else {
val sit = chess.Game(variant.some, fen.value.some).situation
sit.playable(false) ?? {
sit.destinations map {
case (orig, dests) => s"${orig.piotr}${dests.map(_.piotr).mkString}"
} mkString " "
}
sit.playable(false) ?? destString(sit.destinations)
}
lazy val opening = Variant.openingSensibleVariants(variant) ?? {

View File

@ -264,11 +264,7 @@ object Node {
.add("glyphs", glyphs.nonEmpty)
.add("shapes", if (shapes.list.nonEmpty) Some(shapes.list) else None)
.add("opening", opening)
.add("dests", dests.map {
_.map {
case (orig, dests) => s"${orig.piotr}${dests.map(_.piotr).mkString}"
}.mkString(" ")
})
.add("dests", dests)
.add("drops", drops.map { drops =>
JsString(drops.map(_.key).mkString)
})
@ -284,6 +280,23 @@ object Node {
}
}
def destString(dests: Map[Pos, List[Pos]]): String = {
val sb = new java.lang.StringBuilder(64)
var first = true
dests foreach {
case (orig, dests) =>
if (first) first = false
else sb append " "
sb append orig.piotr
dests foreach { sb append _.piotr }
}
sb.toString
}
implicit val destsJsonWriter: Writes[Map[Pos, List[Pos]]] = Writes { dests =>
JsString(destString(dests))
}
implicit val defaultNodeJsonWriter: Writes[Node] =
makeNodeJsonWriter(alwaysChildren = true)

View File

@ -23,7 +23,7 @@ export function readDests(lines?: string): Dests | null {
if (typeof lines === 'undefined') return null;
const dests: Dests = {};
if (lines) lines.split(' ').forEach(line => {
dests[piotr[line[0]]] = line.split('').slice(1).map(c => piotr[c] as Key)
dests[piotr[line[0]]] = line.slice(1).split('').map(c => piotr[c] as Key)
});
return dests;
}