code tweaks

This commit is contained in:
Thibault Duplessis 2017-02-01 12:49:25 +01:00
parent 9be5421401
commit a4290783da
3 changed files with 12 additions and 25 deletions

View file

@ -57,12 +57,15 @@ object Info {
def start(ply: Int) = Info(ply, Eval.initial, Nil)
private def strCp(s: String) = parseIntOption(s) map Cp.apply
private def strMate(s: String) = parseIntOption(s) map Mate.apply
private def decode(ply: Int, str: String): Option[Info] = str.split(separator) match {
case Array() => Info(ply, Eval.empty).some
case Array(cp) => Info(ply, Eval(Cp(cp), None, None)).some
case Array(cp, ma) => Info(ply, Eval(Cp(cp), Mate(ma), None)).some
case Array(cp, ma, va) => Info(ply, Eval(Cp(cp), Mate(ma), None), va.split(' ').toList).some
case Array(cp, ma, va, be) => Info(ply, Eval(Cp(cp), Mate(ma), Uci.Move piotr be), va.split(' ').toList).some
case Array(cp) => Info(ply, Eval(strCp(cp), None, None)).some
case Array(cp, ma) => Info(ply, Eval(strCp(cp), strMate(ma), None)).some
case Array(cp, ma, va) => Info(ply, Eval(strCp(cp), strMate(ma), None), va.split(' ').toList).some
case Array(cp, ma, va, be) => Info(ply, Eval(strCp(cp), strMate(ma), Uci.Move piotr be), va.split(' ').toList).some
case _ => none
}

View file

@ -109,9 +109,7 @@ private[study] final class SocketHandler(
val json = Json.obj(
"dests" -> res.dests,
"path" -> res.path
) ++ res.opening.?? { o =>
Json.obj("opening" -> o)
}
).add("opening", res.opening)
res.multiPv match {
case None => member push makeMessage("dests", json)
case Some(multiPv) =>
@ -119,10 +117,10 @@ private[study] final class SocketHandler(
evalCache.getEval(
fen = lila.evalCache.EvalCacheEntry.SmallFen make fen,
multiPv = multiPv.atMost(res.dests.count(' ' ==) + 1)
).thenPp foreach { eval =>
member push makeMessage("dests", json ++ eval.?? { e =>
Json.obj("eval" -> lila.evalCache.JsonHandlers.writeEval(e, fen))
})
) foreach { eval =>
member push makeMessage(
"dests",
json.add("eval", eval.map { lila.evalCache.JsonHandlers.writeEval(_, fen) }))
}
}
}

View file

@ -63,8 +63,6 @@ object Eval {
val CEILING = 1000
val initial = Cp(15)
def apply(str: String): Option[Cp] = parseIntOption(str) map Cp.apply
}
case class Mate(value: Int) extends AnyVal {
@ -83,11 +81,6 @@ object Eval {
def signum: Int = Math.signum(value).toInt
}
object Mate {
def apply(str: String): Option[Mate] = parseIntOption(str) map Mate.apply
}
val initial = Eval(Some(Cp.initial), None, None)
val empty = Eval(None, None, None)
@ -108,11 +101,4 @@ object Eval {
implicit val evalWrites = Json.writes[Eval]
}
private def parseIntOption(str: String): Option[Int] = try {
Some(java.lang.Integer.parseInt(str))
}
catch {
case e: NumberFormatException => None
}
}