From 9c728a39a0ca2e9d70235cfee852862947b11369 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Sun, 17 Mar 2024 16:24:57 +0700 Subject: [PATCH] Scala tweaks --- modules/game/src/main/UciMemo.scala | 13 ++++++----- modules/round/src/main/Takebacker.scala | 31 +++++++++++++------------ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/modules/game/src/main/UciMemo.scala b/modules/game/src/main/UciMemo.scala index f2258854a45a..26fc6af2c877 100644 --- a/modules/game/src/main/UciMemo.scala +++ b/modules/game/src/main/UciMemo.scala @@ -8,7 +8,7 @@ final class UciMemo(gameRepo: GameRepo)(using Executor): type UciVector = Vector[String] private val cache: Cache[GameId, UciVector] = lila.memo.CacheApi.scaffeineNoScheduler - .expireAfterAccess(5 minutes) + .expireAfterAccess(5.minutes) .build[GameId, UciVector]() private val hardLimit = 300 @@ -24,11 +24,12 @@ final class UciMemo(gameRepo: GameRepo)(using Executor): cache.put(game.id, uciMoves.toVector) def get(game: Game, max: Int = hardLimit): Fu[UciVector] = - cache.getIfPresent(game.id).filter { moves => - moves.size.atMost(max) == game.sans.size.atMost(max) - } match - case Some(moves) => fuccess(moves) - case _ => compute(game, max).addEffect { set(game, _) } + cache + .getIfPresent(game.id) + .filter(_.size.atMost(max) == game.sans.size.atMost(max)) + .match + case Some(moves) => fuccess(moves) + case _ => compute(game, max).addEffect(set(game, _)) def drop(game: Game, nb: Int) = val current = ~cache.getIfPresent(game.id) diff --git a/modules/round/src/main/Takebacker.scala b/modules/round/src/main/Takebacker.scala index 48e0b941cca8..6770e3082a11 100644 --- a/modules/round/src/main/Takebacker.scala +++ b/modules/round/src/main/Takebacker.scala @@ -95,21 +95,22 @@ final private class Takebacker( if _ then f else fufail(ClientError("[takebacker] disallowed by preferences " + game.id)) - private def single(game: Game)(using GameProxy): Fu[Events] = for - fen <- gameRepo.initialFen(game) - progress <- Rewind(game, fen).toFuture - _ <- fuccess { uciMemo.drop(game, 1) } - events <- saveAndNotify(progress) - yield events - - private def double(game: Game)(using GameProxy): Fu[Events] = for - fen <- gameRepo.initialFen(game) - prog1 <- Rewind(game, fen).toFuture - prog2 <- Rewind(prog1.game, fen).toFuture.dmap: progress => - prog1.withGame(progress.game) - _ <- fuccess { uciMemo.drop(game, 2) } - events <- saveAndNotify(prog2) - yield events + private def single(game: Game)(using GameProxy): Fu[Events] = + for + fen <- gameRepo.initialFen(game) + progress <- Rewind(game, fen).toFuture + _ <- fuccess(uciMemo.drop(game, 1)) + events <- saveAndNotify(progress) + yield events + + private def double(game: Game)(using GameProxy): Fu[Events] = + for + fen <- gameRepo.initialFen(game) + prog1 <- Rewind(game, fen).toFuture + prog2 <- Rewind(prog1.game, fen).toFuture.dmap(progress => prog1.withGame(progress.game)) + _ <- fuccess(uciMemo.drop(game, 2)) + events <- saveAndNotify(prog2) + yield events private def saveAndNotify(p1: Progress)(using proxy: GameProxy): Fu[Events] = val p2 = p1 + Event.Reload