publish takeback offers on the board API

pull/9644/head
Thibault Duplessis 2021-08-24 17:25:26 +02:00
parent bf45c394fd
commit c0c4a81fa3
4 changed files with 32 additions and 18 deletions

View File

@ -58,6 +58,8 @@ final class BotJsonView(
"bdraw" -> game.blackPlayer.isOfferingDraw,
"status" -> game.status.name
)
.add("wback" -> game.whitePlayer.isProposingTakeback)
.add("bback" -> game.blackPlayer.isProposingTakeback)
.add("winner" -> game.winnerColor)
.add("rematch" -> rematches.of(game.id))
}

View File

@ -9,7 +9,14 @@ import scala.concurrent.duration._
import lila.chat.Chat
import lila.chat.UserLine
import lila.common.Bus
import lila.game.actorApi.{ AbortedBy, BoardDrawOffer, BoardTakeback, FinishGame, MoveGameEvent }
import lila.game.actorApi.{
AbortedBy,
BoardDrawOffer,
BoardTakeback,
BoardTakebackOffer,
FinishGame,
MoveGameEvent
}
import lila.game.{ Game, Pov }
import lila.hub.actorApi.map.Tell
import lila.round.actorApi.BotConnected
@ -100,10 +107,11 @@ final class GameStateStream(
case MoveGameEvent(g, _, _) if g.id == id && !g.finished => pushState(g).unit
case lila.chat.actorApi.ChatLine(chatId, UserLine(username, _, _, text, false, false)) =>
pushChatLine(username, text, chatId.value.lengthIs == Game.gameIdSize).unit
case FinishGame(g, _, _) if g.id == id => onGameOver(g.some).unit
case AbortedBy(pov) if pov.gameId == id => onGameOver(pov.game.some).unit
case BoardTakeback(pov) if pov.gameId == id => pushState(pov.game).unit
case BoardDrawOffer(g) if g.id == id => pushState(game).unit
case FinishGame(g, _, _) if g.id == id => onGameOver(g.some).unit
case AbortedBy(pov) if pov.gameId == id => onGameOver(pov.game.some).unit
case BoardDrawOffer(g) if g.id == id => pushState(g).unit
case BoardTakebackOffer(g) if g.id == id => pushState(g).unit
case BoardTakeback(g) if g.id == id => pushState(g).unit
case SetOnline =>
onlineApiUsers.setOnline(user.id)
context.system.scheduler

View File

@ -35,7 +35,12 @@ object BoardDrawOffer {
def makeChan(gameId: Game.ID) = s"boardDrawOffer:$gameId"
}
case class BoardTakeback(pov: Pov)
case class BoardTakeback(game: Game)
object BoardTakeback {
def makeChan(gameId: Game.ID) = s"boardTakeback:$gameId"
}
case class BoardTakebackOffer(game: Game)
object BoardTakebackOffer {
def makeChan = BoardTakeback.makeChan _
}

View File

@ -36,10 +36,9 @@ final private class Takebacker(
case Pov(game, color) if (game playerCanProposeTakeback color) && situation.offerable =>
{
messenger.system(game, trans.takebackPropositionSent.txt())
val progress = Progress(game) map { g =>
g.updatePlayer(color, _ proposeTakeback g.turns)
}
proxy.save(progress) >>- publishTakebackOffer(pov) inject
val newGame = game.updatePlayer(color, _ proposeTakeback game.turns)
proxy.save(Progress(newGame)) >>-
publishTakebackOffer(newGame) inject
List(Event.TakebackOffers(color.white, color.black))
} dmap (_ -> situation)
case _ => fufail(ClientError("[takebacker] invalid yes " + pov))
@ -84,13 +83,6 @@ final private class Takebacker(
}
}
private def publishTakebackOffer(pov: Pov): Unit =
if (pov.game.isCorrespondence && pov.game.nonAi && pov.player.hasUser)
Bus.publish(
lila.hub.actorApi.round.CorresTakebackOfferEvent(pov.gameId),
"offerEventCorres"
)
private def IfAllowed[A](game: Game)(f: => Fu[A]): Fu[A] =
if (!game.playable) fufail(ClientError("[takebacker] game is over " + game.id))
else if (game.isMandatory) fufail(ClientError("[takebacker] game disallows it " + game.id))
@ -125,13 +117,20 @@ final private class Takebacker(
proxy.save(p2) inject p2.events
}
private def publishTakebackOffer(game: Game): Unit =
if (lila.game.Game.isBoardCompatible(game))
Bus.publish(
lila.game.actorApi.BoardDrawOffer(game),
lila.game.actorApi.BoardDrawOffer makeChan game.id
)
private def publishTakeback(prevPov: Pov)(implicit proxy: GameProxy): Unit =
if (lila.game.Game.isBoardCompatible(prevPov.game))
proxy
.withPov(prevPov.color) { p =>
fuccess(
Bus.publish(
lila.game.actorApi.BoardTakeback(p),
lila.game.actorApi.BoardTakeback(p.game),
lila.game.actorApi.BoardTakeback makeChan prevPov.gameId
)
)