lila/modules/puzzle/src/main/PuzzleBatch.scala

59 lines
1.7 KiB
Scala
Raw Permalink Normal View History

package lila.puzzle
import scala.concurrent.ExecutionContext
import lila.db.dsl._
2021-04-10 01:25:15 -06:00
import lila.user.User
// mobile app BC
final class PuzzleBatch(colls: PuzzleColls, anonApi: PuzzleAnon, pathApi: PuzzlePathApi)(implicit
ec: ExecutionContext
) {
import BsonHandlers._
def nextFor(user: Option[User], nb: Int): Fu[Vector[Puzzle]] = (nb > 0) ?? {
user match {
case None => anonApi.getBatchFor(nb)
case Some(user) =>
2020-12-15 02:15:03 -07:00
{
val tier =
2021-01-14 14:12:01 -07:00
if (user.perfs.puzzle.nb > 5000) PuzzleTier.Good
2020-12-15 02:15:03 -07:00
else PuzzleTier.Top
pathApi.nextFor(
user,
PuzzleTheme.mix.key,
2021-01-14 14:12:01 -07:00
tier,
2020-12-15 02:15:03 -07:00
PuzzleDifficulty.Normal,
Set.empty
) orFail
s"No puzzle path for ${user.id} $tier" flatMap { pathId =>
colls.path {
_.aggregateList(nb) { framework =>
import framework._
Match($id(pathId)) -> List(
Project($doc("puzzleId" -> "$ids", "_id" -> false)),
Unwind("puzzleId"),
Sample(nb),
PipelineOperator(
2021-10-04 00:02:26 -06:00
$lookup.simple(
from = colls.puzzle,
local = "puzzleId",
foreign = "_id",
as = "puzzle"
2020-12-15 02:15:03 -07:00
)
),
PipelineOperator(
2021-10-04 00:02:26 -06:00
$doc("$replaceWith" -> $doc("$arrayElemAt" -> $arr("$puzzle", 0)))
)
)
2020-12-15 02:15:03 -07:00
}.map {
_.view.flatMap(PuzzleBSONReader.readOpt).toVector
}
}
}
2020-12-15 02:15:03 -07:00
}.mon(_.puzzle.selector.user.batch(nb = nb))
}
}
}