Skip to content

Commit

Permalink
Merge pull request #14913 from lenguyenthanh/takebacker
Browse files Browse the repository at this point in the history
Random Scala tweaks
  • Loading branch information
ornicar committed Mar 19, 2024
2 parents 8f0c9d7 + 9c728a3 commit c9389b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
13 changes: 7 additions & 6 deletions modules/game/src/main/UciMemo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
31 changes: 16 additions & 15 deletions modules/round/src/main/Takebacker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c9389b8

Please sign in to comment.