sequence puzzle voting

puzzle-dashboard
Thibault Duplessis 2020-12-29 22:16:12 +01:00
parent 97525249d6
commit 4da0b71df5
1 changed files with 24 additions and 14 deletions

View File

@ -13,7 +13,7 @@ final class PuzzleApi(
colls: PuzzleColls, colls: PuzzleColls,
trustApi: PuzzleTrustApi, trustApi: PuzzleTrustApi,
countApi: PuzzleCountApi countApi: PuzzleCountApi
)(implicit ec: scala.concurrent.ExecutionContext) { )(implicit ec: scala.concurrent.ExecutionContext, system: akka.actor.ActorSystem, mode: play.api.Mode) {
import Puzzle.{ BSONFields => F } import Puzzle.{ BSONFields => F }
import BsonHandlers._ import BsonHandlers._
@ -44,23 +44,33 @@ final class PuzzleApi(
object vote { object vote {
private val sequencer =
new lila.hub.DuctSequencers(
maxSize = 16,
expiration = 5 minutes,
timeout = 3 seconds,
name = "puzzle.vote"
)
def update(id: Puzzle.Id, user: User, vote: Boolean): Funit = def update(id: Puzzle.Id, user: User, vote: Boolean): Funit =
round sequencer(id.value) {
.find(user, id) round
.flatMap { .find(user, id)
_ ?? { prevRound => .flatMap {
trustApi.vote(user, prevRound, vote) flatMap { _ ?? { prevRound =>
_ ?? { weight => trustApi.vote(user, prevRound, vote) flatMap {
val voteValue = (if (vote) 1 else -1) * weight _ ?? { weight =>
lila.mon.puzzle.vote(vote, prevRound.win).increment() val voteValue = (if (vote) 1 else -1) * weight
updatePuzzle(id, voteValue, prevRound.vote) zip lila.mon.puzzle.vote(vote, prevRound.win).increment()
colls.round { updatePuzzle(id, voteValue, prevRound.vote) zip
_.updateField($id(prevRound.id), PuzzleRound.BSONFields.vote, voteValue) colls.round {
} void _.updateField($id(prevRound.id), PuzzleRound.BSONFields.vote, voteValue)
} void
}
} }
} }
} }
} }
private def updatePuzzle(puzzleId: Puzzle.Id, newVote: Int, prevVote: Option[Int]): Funit = private def updatePuzzle(puzzleId: Puzzle.Id, newVote: Int, prevVote: Option[Int]): Funit =
colls.puzzle { coll => colls.puzzle { coll =>