fix broken puzzle vote percent code

This commit is contained in:
James Clarke 2016-12-10 22:26:06 +11:00
parent 96bc297ff7
commit 8bf63b9fb8
5 changed files with 14 additions and 14 deletions

View file

@ -5,7 +5,9 @@ puzzles.find({}).forEach(function(p) {
_id: p._id
}, {
$set: {
voteDisabled: (p.vote.up * 9 < p.vote.down) && (p.vote.up + p.vote.down > 50)
vote: {
enabled: (p.vote.up * 3 > p.vote.down) || (p.vote.up + p.vote.down < 50)
}
}
});
});

View file

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

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.enabled,
"enabled" -> puzzle.vote.enabled,
"vote" -> puzzle.vote.sum
).noNull,
"mode" -> mode,

View file

@ -31,8 +31,6 @@ case class Puzzle(
def initialMove: Uci.Move = history.lastOption flatMap Uci.Move.apply err s"Bad initial move $this"
def enabled = vote.sum > -9000
def fenAfterInitialMove: Option[String] = {
for {
sit1 <- Forsyth << fen
@ -59,7 +57,7 @@ object Puzzle {
color = color,
date = DateTime.now,
perf = Perf.default,
vote = AggregateVote(0, 0, 0),
vote = AggregateVote(0, 0, true),
attempts = 0,
mate = mate)
@ -106,7 +104,7 @@ object Puzzle {
val rating = s"$perf.gl.r"
val vote = "vote"
val voteSum = s"$vote.sum"
val voteDisabled = s"$vote.disabled"
val voteEnabled = s"$vote.enabled"
val attempts = "attempts"
val mate = "mate"
}

View file

@ -80,7 +80,7 @@ private[puzzle] final class Selector(
Puzzle.BSONFields.rating $gt
(rating - tolerance) $lt
(rating + tolerance),
Puzzle.BSONFields.voteDisabled -> false
Puzzle.BSONFields.voteEnabled -> true
)).uno[Puzzle] flatMap {
case None if (tolerance + step) <= toleranceMax =>
tryRange(rating, tolerance + step, step,