change Vote to ratio not enabled

This commit is contained in:
James Clarke 2016-12-12 05:48:43 +11:00
parent 1c7fb95e06
commit fa58b9db8e
7 changed files with 28 additions and 16 deletions

View file

@ -7,7 +7,8 @@ puzzles.find().forEach(function(p) {
_id: p._id
}, {
$set: {
"vote.enabled": (p.vote.up * 3 > p.vote.down) || (p.vote.up + p.vote.down < 30)
"vote.ratio": Math.floor(100*(p.vote.up - p.vote.down)/(p.vote.up + p.vote.down)),
"vote.nb": (p.vote.up + p.vote.down)
}
});
modified += 1;

View file

@ -1,28 +1,30 @@
package lila.puzzle
case class AggregateVote(up: Int, down: Int, enabled: Boolean) {
case class AggregateVote(up: Int, down: Int, nb: Int, ratio: Int) {
def add(v: Boolean) = copy(
up = up + v.fold(1, 0),
down = down + v.fold(0, 1)
).computeEnabled
).computeNbAndRatio
def change(from: Boolean, to: Boolean) = if (from == to) this else copy(
up = up + to.fold(1, -1),
down = down + to.fold(-1, 1)
).computeEnabled
).computeNbAndRatio
def count = up + down
def sum = up - down
def computeEnabled = copy(enabled = count < 30 || up * 3 > down)
def computeNbAndRatio = copy(
ratio = 100*(up - down)/(up + down),
nb = up + down)
}
object AggregateVote {
val default = AggregateVote(1, 0, true)
val disable = AggregateVote(0, 9000, false).computeEnabled
val default = AggregateVote(1, 0, 1, 100)
val disable = AggregateVote(0, 9000, 9000, -100).computeNbAndRatio
import reactivemongo.bson.Macros
implicit val aggregatevoteBSONHandler = Macros.handler[AggregateVote]

View file

@ -51,8 +51,8 @@ private[puzzle] final class Daily(
).uno[Puzzle]
private def findNew = coll.find(
$doc(F.day $exists false, F.voteEnabled -> true)
).uno[Puzzle] flatMap {
$doc(F.day $exists false, F.voteNb $gte 200)
).sort($doc(F.voteRatio -> -1)).uno[Puzzle] flatMap {
case Some(puzzle) => coll.update(
$id(puzzle.id),
$set(F.day -> DateTime.now)

View file

@ -32,7 +32,7 @@ final class JsonView(
"gameId" -> puzzle.gameId,
"lines" -> lila.puzzle.Line.toJson(puzzle.lines),
"branch" -> (!isMobileApi).option(makeBranch(puzzle)),
"enabled" -> puzzle.vote.enabled,
"enabled" -> puzzle.enabled,
"vote" -> puzzle.vote.sum
).noNull,
"mode" -> mode,

View file

@ -27,6 +27,9 @@ case class Puzzle(
}
} | 0
// (1 - 3)/(1 + 3) = -0.5
def enabled = vote.ratio > -50 || vote.nb < 30
def withVote(f: AggregateVote => AggregateVote) = copy(vote = f(vote))
def initialMove: Uci.Move = history.lastOption flatMap Uci.Move.apply err s"Bad initial move $this"
@ -57,7 +60,7 @@ object Puzzle {
color = color,
date = DateTime.now,
perf = Perf.default,
vote = AggregateVote(0, 0, true),
vote = AggregateVote(1, 0, 1, 1),
attempts = 0,
mate = mate)
@ -103,7 +106,8 @@ object Puzzle {
val perf = "perf"
val rating = s"$perf.gl.r"
val vote = "vote"
val voteEnabled = s"$vote.enabled"
val voteNb = s"$vote.nb"
val voteRatio = s"$vote.ratio"
val day = "day"
val attempts = "attempts"
val mate = "mate"

View file

@ -53,7 +53,7 @@ private[puzzle] final class PuzzleApi(
puzzleColl.exists($doc(
F.id -> $gte(puzzleIdMin),
F.fen.$regex(fenStart.replace("/", "\\/"), ""),
F.voteEnabled -> true
F.voteRatio -> $gte(-0.5)
)) flatMap {
case false => puzzleColl insert p inject id
case _ => fufail(s"Duplicate puzzle $fenStart")
@ -62,6 +62,7 @@ private[puzzle] final class PuzzleApi(
def export(nb: Int): Fu[List[Puzzle]] = List(true, false).map { mate =>
puzzleColl.find($doc(F.mate -> mate))
.sort($doc(F.voteRatio -> -1))
.cursor[Puzzle]().gather[List](nb / 2)
}.sequenceFu.map(_.flatten)

View file

@ -5,6 +5,7 @@ import scala.util.Random
import lila.db.dsl._
import lila.user.User
import Puzzle.{BSONFields => F}
private[puzzle] final class Selector(
puzzleColl: Coll,
@ -74,13 +75,16 @@ private[puzzle] final class Selector(
step: Int,
idRange: Range): Fu[Option[Puzzle]] =
puzzleColl.find($doc(
Puzzle.BSONFields.id $gt
F.id $gt
idRange.min $lt
idRange.max,
Puzzle.BSONFields.rating $gt
F.rating $gt
(rating - tolerance) $lt
(rating + tolerance),
Puzzle.BSONFields.voteEnabled -> true
$or(
F.voteRatio $gt -50,
F.voteNb $lt 30
)
)).uno[Puzzle] flatMap {
case None if (tolerance + step) <= toleranceMax =>
tryRange(rating, tolerance + step, step,