Skip to content

Commit

Permalink
refactor game moretime, allow bypassing some conditions with API call
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Oct 22, 2024
1 parent dec1fc8 commit e9f1c88
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 33 deletions.
5 changes: 0 additions & 5 deletions modules/core/src/main/game/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ case class Game(

def boosted = rated && finished && bothPlayersHaveMoved && playedTurns < 10

def moretimeable(color: Color) =
playable && canTakebackOrAddTime && !hasRule(_.noGiveTime) && {
clock.exists(_.moretimeable(color)) || correspondenceClock.exists(_.moretimeable(color))
}

def abortable = status == Status.Started && playedTurns < 2 && nonMandatory
def abortableByUser = abortable && !hasRule(_.noAbort)

Expand Down
2 changes: 1 addition & 1 deletion modules/round/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class JsonView(
flags: ExportOptions
): Fu[JsObject] = for
takebackable <- takebacker.isAllowedIn(pov.game, Preload(prefs))
moretimeable <- moretimer.isAllowedIn(pov.game, Preload(prefs), byAdmin = false)
moretimeable <- moretimer.isAllowedIn(pov.game, Preload(prefs), force = false)
socket <- getSocketStatus(pov.game)
pref = prefs(pov.color)
yield
Expand Down
41 changes: 15 additions & 26 deletions modules/round/src/main/Moretimer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ final class Moretimer(

// pov of the player giving more time
def apply(pov: Pov, duration: FiniteDuration, force: Boolean): Fu[Option[Progress]] =
IfAllowed(pov.game, Preload.none, force):
(pov.game
.moretimeable(!pov.color))
.so:
if pov.game.hasClock
then give(pov.game, List(!pov.color), duration).some
else
pov.game.hasCorrespondenceClock.option:
messenger.volatile(pov.game, s"${!pov.color} gets more time")
val p = correspondenceGiveTime(pov.game)
p.game.correspondenceClock.map(Event.CorrespondenceClock.apply).fold(p)(p + _)
isAllowedIn(pov.game, Preload.none, force).map:
case false =>
logger.warn(s"[moretimer] not allowed on ${pov.game.id}")
none
case true =>
if pov.game.clock.exists(_.moretimeable(!pov.color))
then give(pov.game, List(!pov.color), duration).some
else if pov.game.correspondenceClock.exists(_.moretimeable(!pov.color))
then
messenger.volatile(pov.game, s"${!pov.color} gets more time")
val p = Progress(pov.game, pov.game.copy(movedAt = nowInstant))
p.game.correspondenceClock.map(Event.CorrespondenceClock.apply).foldLeft(p)(_ + _).some
else none

TODO.duplicated(code)
def isAllowedIn(game: Game, prefs: Preload[ByColor[Pref]], force: Boolean): Fu[Boolean] =
(game.canTakebackOrAddTime && game.playable).so:
(game.playable && !game.isUnlimited && game.canTakebackOrAddTime).so:
if force then fuccess(true)
else (!game.metadata.hasRule(_.noGiveTime)).so(isAllowedByPrefs(game, prefs))

private def correspondenceGiveTime(g: Game) = Progress(g, g.copy(movedAt = nowInstant))
else (!game.hasRule(_.noGiveTime)).so(isAllowedByPrefs(game, prefs))

private[round] def give(game: Game, colors: List[Color], unchecked: FiniteDuration): Progress =
game.clock.fold(Progress(game)): clock =>
Expand All @@ -58,13 +57,3 @@ final class Moretimer(
.dmap:
_.forall: p =>
p.moretime == Pref.Moretime.ALWAYS || (p.moretime == Pref.Moretime.CASUAL && game.casual)

private def IfAllowed[A](game: Game, prefs: Preload[ByColor[Pref]], force: Boolean)(f: => A): Fu[A] =
if !game.playable then fufail(ClientError("[moretimer] game is over"))
else if !game.canTakebackOrAddTime then fufail(ClientError("[moretimer] Can't add time to this game"))
else if !force && game.metadata.hasRule(_.noGiveTime) then
fufail(ClientError("[moretimer] game rules disallows it"))
else
isAllowedByPrefs(game, prefs).flatMap:
if _ then fuccess(f)
else fufail(ClientError("[moretimer] disallowed by preferences " + game.id))
2 changes: 1 addition & 1 deletion modules/round/src/main/RoundMobile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class RoundMobile(
users <- game.userIdPair.traverse(_.so(lightUserGet))
prefs <- prefApi.byId(game.userIdPair)
takebackable <- takebacker.isAllowedIn(game, Preload(prefs))
moretimeable <- moretimer.isAllowedIn(game, Preload(prefs), byAdmin = false)
moretimeable <- moretimer.isAllowedIn(game, Preload(prefs), force = false)
chat <- use.chat.so(getPlayerChat(game, myPlayer.exists(_.hasUser)))
chatLines <- chat.map(_.chat).soFu(lila.chat.JsonView.asyncLines)
bookmarked <- use.bookmark.so(bookmarkExists(game, myPlayer.flatMap(_.userId)))
Expand Down

0 comments on commit e9f1c88

Please sign in to comment.