/api/bot|board/game/id/claim-victory endpoint

to claim victory when the opponent has left the game
pull/9566/head
Thibault Duplessis 2021-08-12 15:16:38 +02:00
parent 3b07f60d85
commit 226f094f44
4 changed files with 21 additions and 2 deletions

View File

@ -107,6 +107,10 @@ final class PlayApi(
as(id, me) { pov =>
fuccess(env.bot.player.setDraw(pov, lila.common.Form.trueish(bool))) pipe toResult
}
case Array("game", id, "claim-victory") =>
as(id, me) { pov =>
env.bot.player.claimVictory(pov) pipe toResult
}
case _ => notFoundJson("No such command")
}
}

View File

@ -9,7 +9,7 @@ import lila.game.Game.PlayerId
import lila.game.{ Game, GameRepo, Pov }
import lila.hub.actorApi.map.Tell
import lila.hub.actorApi.round.{ Abort, BotPlay, RematchNo, RematchYes, Resign }
import lila.round.actorApi.round.{ DrawNo, DrawYes }
import lila.round.actorApi.round.{ DrawNo, DrawYes, ResignForce }
import lila.user.User
final class BotPlayer(
@ -98,4 +98,17 @@ final class BotPlayer(
def setDraw(pov: Pov, v: Boolean): Unit =
if (v) offerDraw(pov) else declineDraw(pov)
def claimVictory(pov: Pov): Funit =
pov.mightClaimWin ?? {
tellRound(pov.gameId, ResignForce(pov.typedPlayerId))
lila.common.Future.delay(500 millis) {
gameRepo.finished(pov.gameId) map {
_.exists(_.winner.map(_.id) has pov.playerId)
}
}
} flatMap {
case true => funit
case _ => clientError("You cannot claim the win on this game")
}
}

View File

@ -43,6 +43,8 @@ case class Pov(game: Game, color: Color) {
def forecastable = game.forecastable && game.turnColor != color
def mightClaimWin = game.resignable && !game.hasAi && game.hasClock && !isMyTurn
override def toString = ref.toString
}

View File

@ -277,7 +277,7 @@ final private[round] class RoundAsyncActor(
case ResignForce(playerId) =>
handle(playerId) { pov =>
(pov.game.resignable && !pov.game.hasAi && pov.game.hasClock && !pov.isMyTurn) ?? {
pov.mightClaimWin ?? {
getPlayer(!pov.color).isLongGone flatMap {
case true =>
finisher.rageQuit(