diff --git a/src/main/scala/evalCache/EvalCacheApi.scala b/src/main/scala/evalCache/EvalCacheApi.scala index 080c5c83..57f5a321 100644 --- a/src/main/scala/evalCache/EvalCacheApi.scala +++ b/src/main/scala/evalCache/EvalCacheApi.scala @@ -38,16 +38,18 @@ final class EvalCacheApi(mongo: Mongo)(using if e.up then upgrade.register(sri, e) def getMulti(sri: Sri, e: EvalGetMulti, emit: Emit[ClientIn]): Unit = - e.fens.foreach: fen => - getEntry(Id.make(e.variant, fen)) - .map: - _.flatMap(_.makeBestSinglePvEval) - .map(monitorRequest(fen, Monitor.evalCache.multi)) - .foreach: - _.foreach: eval => - emit: - ClientIn.EvalHitMulti: - EvalCacheJsonHandlers.writeMultiHit(eval, fen) + e.fens + .traverse: fen => + getEntry(Id.make(e.variant, fen)) + .map: + _.flatMap(_.makeBestSinglePvEval).map(fen -> _) + .map(monitorRequest(fen, Monitor.evalCache.multi)) + .map(_.flatten) + .foreach: evals => + if evals.nonEmpty then + emit: + ClientIn.EvalHitMulti: + EvalCacheJsonHandlers.writeMultiHit(evals) multi.register(sri, e) def put(sri: Sri, user: User.Id, e: EvalPut): Unit = diff --git a/src/main/scala/evalCache/EvalCacheJsonHandlers.scala b/src/main/scala/evalCache/EvalCacheJsonHandlers.scala index 9395b299..c0f4a523 100644 --- a/src/main/scala/evalCache/EvalCacheJsonHandlers.scala +++ b/src/main/scala/evalCache/EvalCacheJsonHandlers.scala @@ -40,11 +40,15 @@ object EvalCacheJsonHandlers: "pvs" -> JsArray(e.pvs.toList.map(writePv)) ) - def writeMultiHit(e: Eval, fen: Fen.Epd) = Json + def writeMultiHit(fen: Fen.Epd, e: Eval): JsObject = Json .obj("fen" -> fen, "depth" -> e.depth) .add("cp" -> e.bestPv.score.cp) .add("mate" -> e.bestPv.score.mate) + def writeMultiHit(evals: List[(Fen.Epd, Eval)]): JsObject = evals match + case List(single) => writeMultiHit.tupled(single) + case many => Json.obj("multi" -> many.map(writeMultiHit.tupled)) + private def writePv(pv: Pv) = Json .obj("moves" -> pv.moves.value.toList.map(_.uci).mkString(" ")) .add("cp" -> pv.score.cp) diff --git a/src/main/scala/evalCache/EvalCacheMulti.scala b/src/main/scala/evalCache/EvalCacheMulti.scala index 2d7d55d9..ae290dff 100644 --- a/src/main/scala/evalCache/EvalCacheMulti.scala +++ b/src/main/scala/evalCache/EvalCacheMulti.scala @@ -45,7 +45,7 @@ final private class EvalCacheMulti(using val sris = oldEval.sris.filter(_ != fromSri) if sris.nonEmpty then val hit = EvalHitMulti: - EvalCacheJsonHandlers.writeMultiHit(input.eval, input.fen) + EvalCacheJsonHandlers.writeMultiHit(input.fen, input.eval) sris.foreach: sri => Bus.publish(_ sri sri, hit) upgradeMon.count.increment(sris.size)