eval cache server-side put

This commit is contained in:
Thibault Duplessis 2017-01-31 13:28:06 +01:00
parent a8eeec073a
commit 90deced233
5 changed files with 63 additions and 7 deletions

View file

@ -50,5 +50,6 @@ object BSONHandlers {
}
implicit val evalHandler = Macros.handler[Eval]
implicit val trustedEvalHandler = Macros.handler[TrustedEval]
implicit val entryHandler = Macros.handler[EvalCacheEntry]
}

View file

@ -22,7 +22,7 @@ final class EvalCacheApi(coll: Coll) {
private def put(input: Input): Funit =
getEntry(input.id) map {
_.fold(input.entry)(_ add input.eval)
_.fold(input entry Trust(1))(_ add input.eval)
} flatMap { entry =>
coll.update($id(entry.id), entry, upsert = true).void
}

View file

@ -7,7 +7,7 @@ import lila.tree.Eval.{ Score }
case class EvalCacheEntry(
_id: EvalCacheEntry.Id,
evals: List[EvalCacheEntry.Eval]) {
evals: List[EvalCacheEntry.TrustedEval]) {
import EvalCacheEntry._
@ -16,9 +16,9 @@ case class EvalCacheEntry(
def fen = _id.fen
def multiPv = _id.multiPv
def bestEval: Option[Eval] = evals.headOption
def bestEval: Option[Eval] = evals.headOption.map(_.eval)
def add(eval: Eval) = copy(evals = eval :: evals)
def add(eval: Eval) = copy(evals = TrustedEval(Trust(1), eval) :: evals)
}
object EvalCacheEntry {
@ -29,6 +29,8 @@ object EvalCacheEntry {
val MAX_PV_SIZE = 8
val MAX_MULTI_PV = 5
case class TrustedEval(trust: Trust, eval: Eval)
case class Eval(
score: Score,
pv: Pv,
@ -36,7 +38,6 @@ object EvalCacheEntry {
depth: Int,
engine: String,
by: lila.user.User.ID,
trust: Trust,
date: DateTime) {
def bestMove: Option[Uci] = pv.value.headOption
@ -78,7 +79,7 @@ object EvalCacheEntry {
}
case class Input(id: Id, eval: Eval) {
def entry = EvalCacheEntry(id, List(eval))
def entry(trust: Trust) = EvalCacheEntry(id, List(TrustedEval(trust, eval)))
}
object Input {

View file

@ -0,0 +1,54 @@
package lila.evalCache
import org.joda.time.DateTime
import play.api.libs.json.JsObject
import chess.format.Uci
import EvalCacheEntry._
import lila.common.PimpedJson._
import lila.socket.Handler.Controller
import lila.tree.Eval._
import lila.user.User
final class SocketHandler(api: EvalCacheApi) {
def controller(user: User): Controller =
if (canPut(user)) makeController(user)
else lila.socket.Handler.emptyController
private def makeController(user: User): Controller = {
case ("evalPut", o) => parsePut(user, o) foreach api.put
}
private def parsePut(user: User, o: JsObject): Option[Input.Candidate] = for {
d <- o obj "d"
variant = chess.variant.Variant orDefault ~d.str("variant")
if variant.standard
fen <- d str "fen"
multiPv <- d int "multiPv"
cp = d int "cp" map Cp.apply
mate = d int "mate" map Mate.apply
score <- cp.map(Score.cp) orElse mate.map(Score.mate)
nodes <- d int "nodes"
depth <- d int "depth"
engine <- d str "engine"
pvStr <- d str "pv"
pv <- pvStr.split(' ').take(EvalCacheEntry.MAX_PV_SIZE).toList.foldLeft(List.empty[Uci].some) {
case (Some(ucis), str) => Uci(str) map (_ :: ucis)
case _ => None
}.map(_.reverse)
} yield Input.Candidate(
fen,
multiPv,
Eval(
score = score,
pv = Pv(pv),
nodes = nodes,
depth = depth,
engine = engine,
by = user.id,
date = DateTime.now))
private def canPut(user: User) = true
}

View file

@ -254,7 +254,7 @@ object ApplicationBuild extends Build {
libraryDependencies ++= provided(play.api, reactivemongo.driver)
)
lazy val evalCache = project("evalCache", Seq(common, db, user, tree)).settings(
lazy val evalCache = project("evalCache", Seq(common, db, user, socket, tree)).settings(
libraryDependencies ++= provided(play.api, reactivemongo.driver)
)