2013-05-18 09:26:37 -06:00
|
|
|
package lila.round
|
|
|
|
|
2013-12-05 16:53:18 -07:00
|
|
|
import lila.game.{ GameRepo, Game, UciMemo, Pov, Rewind, Event, Progress }
|
2014-05-01 06:08:54 -06:00
|
|
|
import lila.pref.{ Pref, PrefApi }
|
2013-05-18 09:26:37 -06:00
|
|
|
|
2014-05-01 06:08:54 -06:00
|
|
|
private[round] final class Takebacker(
|
|
|
|
messenger: Messenger,
|
|
|
|
uciMemo: UciMemo,
|
2017-09-04 13:33:36 -06:00
|
|
|
prefApi: PrefApi,
|
|
|
|
bus: lila.common.Bus
|
2017-02-14 08:34:07 -07:00
|
|
|
) {
|
2013-05-18 09:26:37 -06:00
|
|
|
|
2016-06-21 03:52:42 -06:00
|
|
|
def yes(situation: Round.TakebackSituation)(pov: Pov)(implicit proxy: GameProxy): Fu[(Events, Round.TakebackSituation)] = IfAllowed(pov.game) {
|
2014-05-01 06:08:54 -06:00
|
|
|
pov match {
|
2016-06-21 03:52:42 -06:00
|
|
|
case Pov(game, _) if pov.opponent.isProposingTakeback => {
|
2015-03-26 04:04:40 -06:00
|
|
|
if (pov.opponent.proposeTakebackAt == pov.game.turns) single(game)
|
|
|
|
else double(game)
|
2016-06-21 03:52:42 -06:00
|
|
|
} map (_ -> situation.reset)
|
|
|
|
case Pov(game, _) if pov.opponent.isAi => double(game) map (_ -> situation)
|
|
|
|
case Pov(game, color) if (game playerCanProposeTakeback color) && situation.offerable => {
|
2014-05-01 06:08:54 -06:00
|
|
|
messenger.system(game, _.takebackPropositionSent)
|
2015-03-26 04:04:40 -06:00
|
|
|
val progress = Progress(game) map { g =>
|
|
|
|
g.updatePlayer(color, _ proposeTakeback g.turns)
|
|
|
|
}
|
2017-09-04 13:40:24 -06:00
|
|
|
proxy.save(progress) >>- publishTakebackOffer(pov) inject
|
2017-09-04 13:33:36 -06:00
|
|
|
List(Event.TakebackOffers(color.white, color.black))
|
2016-06-21 03:52:42 -06:00
|
|
|
} map (_ -> situation)
|
2016-03-19 23:15:30 -06:00
|
|
|
case _ => fufail(ClientError("[takebacker] invalid yes " + pov))
|
2014-05-01 06:08:54 -06:00
|
|
|
}
|
2013-05-18 09:26:37 -06:00
|
|
|
}
|
2014-05-01 06:08:54 -06:00
|
|
|
|
2016-06-21 03:52:42 -06:00
|
|
|
def no(situation: Round.TakebackSituation)(pov: Pov)(implicit proxy: GameProxy): Fu[(Events, Round.TakebackSituation)] = pov match {
|
2016-05-20 01:12:47 -06:00
|
|
|
case Pov(game, color) if pov.player.isProposingTakeback => proxy.save {
|
|
|
|
messenger.system(game, _.takebackPropositionCanceled)
|
|
|
|
Progress(game) map { g => g.updatePlayer(color, _.removeTakebackProposition) }
|
2016-06-21 03:52:42 -06:00
|
|
|
} inject {
|
2016-06-21 04:12:20 -06:00
|
|
|
List(Event.TakebackOffers(false, false)) -> situation.decline
|
2016-06-21 03:52:42 -06:00
|
|
|
}
|
2016-05-20 01:12:47 -06:00
|
|
|
case Pov(game, color) if pov.opponent.isProposingTakeback => proxy.save {
|
|
|
|
messenger.system(game, _.takebackPropositionDeclined)
|
|
|
|
Progress(game) map { g => g.updatePlayer(!color, _.removeTakebackProposition) }
|
2016-06-21 03:52:42 -06:00
|
|
|
} inject {
|
|
|
|
List(Event.TakebackOffers(false, false)) -> situation.decline
|
|
|
|
}
|
2016-05-20 01:12:47 -06:00
|
|
|
case _ => fufail(ClientError("[takebacker] invalid no " + pov))
|
2013-05-18 09:26:37 -06:00
|
|
|
}
|
|
|
|
|
2014-05-01 06:08:54 -06:00
|
|
|
def isAllowedByPrefs(game: Game): Fu[Boolean] =
|
2015-09-03 15:05:54 -06:00
|
|
|
if (game.hasAi) fuccess(true)
|
|
|
|
else game.userIds.map { userId =>
|
2014-05-01 06:08:54 -06:00
|
|
|
prefApi.getPref(userId, (p: Pref) => p.takeback)
|
|
|
|
}.sequenceFu map {
|
|
|
|
_.forall { p =>
|
|
|
|
p == Pref.Takeback.ALWAYS || (p == Pref.Takeback.CASUAL && game.casual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 13:40:24 -06:00
|
|
|
private def publishTakebackOffer(pov: Pov): Unit =
|
2017-09-04 13:33:36 -06:00
|
|
|
if (pov.game.isCorrespondence && pov.game.nonAi) pov.player.userId foreach { userId =>
|
|
|
|
bus.publish(
|
2017-09-04 13:40:24 -06:00
|
|
|
lila.hub.actorApi.round.CorresTakebackOfferEvent(pov.gameId),
|
|
|
|
'offerEventCorres
|
2017-09-04 13:33:36 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-06-21 03:52:42 -06:00
|
|
|
private def IfAllowed[A](game: Game)(f: => Fu[A]): Fu[A] =
|
2016-03-19 23:15:30 -06:00
|
|
|
if (!game.playable) fufail(ClientError("[takebacker] game is over " + game.id))
|
2015-08-12 05:53:20 -06:00
|
|
|
else isAllowedByPrefs(game) flatMap {
|
2016-03-19 23:15:30 -06:00
|
|
|
_.fold(f, fufail(ClientError("[takebacker] disallowed by preferences " + game.id)))
|
2014-05-01 06:08:54 -06:00
|
|
|
}
|
|
|
|
|
2016-03-22 07:06:48 -06:00
|
|
|
private def single(game: Game)(implicit proxy: GameProxy): Fu[Events] = for {
|
2015-01-14 08:32:05 -07:00
|
|
|
fen ← GameRepo initialFen game
|
2013-12-29 04:38:07 -07:00
|
|
|
progress ← Rewind(game, fen).future
|
|
|
|
_ ← fuccess { uciMemo.drop(game, 1) }
|
2016-03-22 06:56:01 -06:00
|
|
|
events ← saveAndNotify(progress)
|
2013-12-05 16:53:18 -07:00
|
|
|
} yield events
|
2013-05-18 09:26:37 -06:00
|
|
|
|
2016-03-22 07:06:48 -06:00
|
|
|
private def double(game: Game)(implicit proxy: GameProxy): Fu[Events] = for {
|
2015-01-14 08:32:05 -07:00
|
|
|
fen ← GameRepo initialFen game
|
2013-12-05 16:53:18 -07:00
|
|
|
prog1 ← Rewind(game, fen).future
|
2014-02-17 02:12:19 -07:00
|
|
|
prog2 ← Rewind(prog1.game, fen).future map { progress =>
|
2013-12-05 16:53:18 -07:00
|
|
|
prog1 withGame progress.game
|
2013-05-18 09:26:37 -06:00
|
|
|
}
|
2013-12-05 16:53:18 -07:00
|
|
|
_ ← fuccess { uciMemo.drop(game, 2) }
|
2016-03-22 06:56:01 -06:00
|
|
|
events ← saveAndNotify(prog2)
|
2013-12-05 16:53:18 -07:00
|
|
|
} yield events
|
2013-05-18 09:26:37 -06:00
|
|
|
|
2016-03-22 07:06:48 -06:00
|
|
|
private def saveAndNotify(p1: Progress)(implicit proxy: GameProxy): Fu[Events] = {
|
2013-05-18 09:26:37 -06:00
|
|
|
val p2 = p1 + Event.Reload
|
2014-02-01 00:54:03 -07:00
|
|
|
messenger.system(p2.game, _.takebackPropositionAccepted)
|
2016-03-22 07:06:48 -06:00
|
|
|
proxy.save(p2) inject p2.events
|
2013-05-18 09:26:37 -06:00
|
|
|
}
|
|
|
|
}
|