diff --git a/app/controllers/Opening.scala b/app/controllers/Opening.scala index c9c31a958aa6..471e02420641 100644 --- a/app/controllers/Opening.scala +++ b/app/controllers/Opening.scala @@ -49,7 +49,7 @@ final class Opening(env: Env) extends LilaController(env): s"${routes.Opening.byKeyAndMoves(query.key, page.query.pgnUnderscored)}?r=1" else Ok.async: - page.query.exactOpening.so(env.puzzle.opening.getClosestTo).map { puzzle => + page.query.exactOpening.so(env.puzzle.opening.getClosestTo(_)).map { puzzle => val puzzleKey = puzzle.map(_.fold(_.family.key.value, _.opening.key.value)) views.opening.ui.show(page, puzzleKey) } diff --git a/modules/api/src/main/RoundApi.scala b/modules/api/src/main/RoundApi.scala index 44a95dfe28db..c576d4614c6e 100644 --- a/modules/api/src/main/RoundApi.scala +++ b/modules/api/src/main/RoundApi.scala @@ -127,13 +127,9 @@ final private[api] class RoundApi( tourApi.gameView.analysis(pov.game), pov.game.simulId.so(simulApi.find), swissApi.gameView(pov), - ctx.me - .ifTrue(ctx.isMobileApi) - .so: - noteApi.get(pov.gameId, _) - , + ctx.me.ifTrue(ctx.isMobileApi).so(noteApi.get(pov.gameId, _)), owner.so(forecastApi.loadForDisplay(pov)), - withFlags.puzzles.so(pov.game.opening.map(_.opening)).so(puzzleOpeningApi.getClosestTo), + withFlags.puzzles.so(pov.game.opening.map(_.opening)).so(puzzleOpeningApi.getClosestTo(_, true)), bookmarkApi.exists(pov.game, ctx.me) ).mapN: (json, tour, simul, swiss, note, fco, puzzleOpening, bookmarked) => ( @@ -160,8 +156,8 @@ final private[api] class RoundApi( owner .so(forecastApi.loadForDisplay(pov)) .map: fco => - withForecast(pov, owner, fco) { - withTree(pov, analysis = none, initialFen, ExportOptions(opening = true)) { + withForecast(pov, owner, fco): + withTree(pov, analysis = none, initialFen, ExportOptions(opening = true)): jsonView.userAnalysisJson( pov, pref, @@ -169,8 +165,6 @@ final private[api] class RoundApi( orientation, owner = owner ) - } - } .flatMap(externalEngineApi.withExternalEngines) private def withTree( diff --git a/modules/puzzle/src/main/PuzzleOpening.scala b/modules/puzzle/src/main/PuzzleOpening.scala index 4bc50a274a35..cd80413cf27a 100644 --- a/modules/puzzle/src/main/PuzzleOpening.scala +++ b/modules/puzzle/src/main/PuzzleOpening.scala @@ -54,7 +54,7 @@ final class PuzzleOpeningApi( gameRepo: lila.core.game.GameRepo, cacheApi: CacheApi, mongoCache: MongoCache.Api -)(using Executor, akka.stream.Materializer): +)(using Executor, akka.stream.Materializer, Scheduler): import BsonHandlers.given import SimpleOpening.* import PuzzleOpening.* @@ -94,10 +94,11 @@ final class PuzzleOpeningApi( } def getClosestTo( - opening: Opening + opening: Opening, + quickOrNone: Boolean = false ): Fu[Option[Either[PuzzleOpening.FamilyWithCount, PuzzleOpening.WithCount]]] = SimpleOpening(opening).so: lilaOp => - collection.map: coll => + (if quickOrNone then collectionQuickly else collection.map(some)).mapz: coll => coll.openingMap .get(lilaOp.key) .map(Right.apply) @@ -112,6 +113,9 @@ final class PuzzleOpeningApi( def collection: Fu[PuzzleOpeningCollection] = collectionCache.get {} + def collectionQuickly: Fu[Option[PuzzleOpeningCollection]] = + collection.map(some).withTimeoutDefault(20.millis, none) + def count(key: Either[LilaOpeningFamily.Key, SimpleOpening.Key]): Fu[Int] = collection.dmap: coll => key.fold(f => coll.familyMap.get(f).so(_.count), o => coll.openingMap.get(o).so(_.count))