2020-02-24 15:24:33 -07:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import play.api.i18n.Lang
|
2021-07-25 01:52:40 -06:00
|
|
|
import play.api.mvc._
|
|
|
|
import scala.concurrent.duration._
|
2020-03-11 15:27:56 -06:00
|
|
|
import scala.util.chaining._
|
2020-02-24 15:24:33 -07:00
|
|
|
|
|
|
|
import lila.app._
|
|
|
|
import lila.game.Pov
|
|
|
|
import lila.user.{ User => UserModel }
|
|
|
|
|
|
|
|
// both bot & board APIs
|
|
|
|
final class PlayApi(
|
|
|
|
env: Env,
|
|
|
|
apiC: => Api
|
2021-07-25 01:52:40 -06:00
|
|
|
)(implicit
|
|
|
|
mat: akka.stream.Materializer
|
2020-02-24 15:24:33 -07:00
|
|
|
) extends LilaController(env) {
|
|
|
|
|
|
|
|
implicit private def autoReqLang(implicit req: RequestHeader) = reqLang(req)
|
|
|
|
|
|
|
|
// bot endpoints
|
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def botGameStream(id: String) =
|
|
|
|
Scoped(_.Bot.Play) { implicit req => me =>
|
|
|
|
WithPovAsBot(id, me) { impl.gameStream(me, _) }
|
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def botMove(id: String, uci: String, offeringDraw: Option[Boolean]) =
|
|
|
|
Scoped(_.Bot.Play) { _ => me =>
|
|
|
|
WithPovAsBot(id, me) { impl.move(me, _, uci, offeringDraw) }
|
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def botCommand(cmd: String) =
|
|
|
|
ScopedBody(_.Bot.Play) { implicit req => me =>
|
|
|
|
cmd.split('/') match {
|
|
|
|
case Array("account", "upgrade") =>
|
|
|
|
env.user.repo.isManaged(me.id) flatMap {
|
|
|
|
case true => notFoundJson()
|
|
|
|
case _ =>
|
2020-07-23 07:31:00 -06:00
|
|
|
env.tournament.api.withdrawAll(me) >>
|
|
|
|
env.team.cached.teamIdsList(me.id).flatMap { env.swiss.api.withdrawAll(me, _) } >>
|
|
|
|
env.user.repo.setBot(me) >>
|
2021-07-09 10:58:32 -06:00
|
|
|
env.pref.api.setBot(me) >>
|
|
|
|
env.streamer.api.delete(me) >>-
|
2020-05-30 09:00:04 -06:00
|
|
|
env.user.lightUserApi.invalidate(me.id) pipe
|
2020-09-21 01:28:28 -06:00
|
|
|
toResult recover { case lila.base.LilaInvalid(msg) =>
|
|
|
|
BadRequest(jsonError(msg))
|
|
|
|
}
|
2020-05-05 22:11:15 -06:00
|
|
|
}
|
|
|
|
case _ => impl.command(me, cmd)(WithPovAsBot)
|
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// board endpoints
|
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def boardGameStream(id: String) =
|
|
|
|
Scoped(_.Board.Play) { implicit req => me =>
|
|
|
|
WithPovAsBoard(id, me) { impl.gameStream(me, _) }
|
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def boardMove(id: String, uci: String, offeringDraw: Option[Boolean]) =
|
|
|
|
Scoped(_.Board.Play) { _ => me =>
|
2020-05-30 07:31:13 -06:00
|
|
|
WithPovAsBoard(id, me) {
|
|
|
|
impl.move(me, _, uci, offeringDraw)
|
2020-05-05 22:11:15 -06:00
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
|
|
|
|
2021-08-27 06:00:52 -06:00
|
|
|
def boardCommandPost(cmd: String) =
|
2020-05-05 22:11:15 -06:00
|
|
|
ScopedBody(_.Board.Play) { implicit req => me =>
|
|
|
|
impl.command(me, cmd)(WithPovAsBoard)
|
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
|
|
|
|
// common code for bot & board APIs
|
|
|
|
private object impl {
|
|
|
|
|
|
|
|
def gameStream(me: UserModel, pov: Pov)(implicit lang: Lang) =
|
|
|
|
env.game.gameRepo.withInitialFen(pov.game) map { wf =>
|
2020-05-25 11:09:52 -06:00
|
|
|
apiC.sourceToNdJsonOption(env.bot.gameStateStream(wf, pov.color, me))
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
def move(me: UserModel, pov: Pov, uci: String, offeringDraw: Option[Boolean]) =
|
2020-03-11 15:27:56 -06:00
|
|
|
env.bot.player(pov, me, uci, offeringDraw) pipe toResult
|
2020-02-24 15:24:33 -07:00
|
|
|
|
|
|
|
def command(me: UserModel, cmd: String)(
|
|
|
|
as: (String, UserModel) => (Pov => Fu[Result]) => Fu[Result]
|
|
|
|
)(implicit req: Request[_]): Fu[Result] =
|
|
|
|
cmd.split('/') match {
|
|
|
|
case Array("game", id, "chat") =>
|
|
|
|
as(id, me) { pov =>
|
2020-07-22 04:52:52 -06:00
|
|
|
env.bot.form.chat
|
|
|
|
.bindFromRequest()
|
|
|
|
.fold(
|
|
|
|
jsonFormErrorDefaultLang,
|
|
|
|
res => env.bot.player.chat(pov.gameId, me, res) inject jsonOkResult
|
|
|
|
) pipe catchClientError
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
|
|
|
case Array("game", id, "abort") =>
|
|
|
|
as(id, me) { pov =>
|
2020-03-11 15:27:56 -06:00
|
|
|
env.bot.player.abort(pov) pipe toResult
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
|
|
|
case Array("game", id, "resign") =>
|
|
|
|
as(id, me) { pov =>
|
2020-03-11 15:27:56 -06:00
|
|
|
env.bot.player.resign(pov) pipe toResult
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
2020-03-11 15:50:34 -06:00
|
|
|
case Array("game", id, "draw", bool) =>
|
|
|
|
as(id, me) { pov =>
|
|
|
|
fuccess(env.bot.player.setDraw(pov, lila.common.Form.trueish(bool))) pipe toResult
|
2021-08-12 07:16:38 -06:00
|
|
|
}
|
2021-08-24 09:49:48 -06:00
|
|
|
case Array("game", id, "takeback", bool) =>
|
|
|
|
as(id, me) { pov =>
|
|
|
|
fuccess(env.bot.player.setTakeback(pov, lila.common.Form.trueish(bool))) pipe toResult
|
|
|
|
}
|
2021-08-12 07:16:38 -06:00
|
|
|
case Array("game", id, "claim-victory") =>
|
|
|
|
as(id, me) { pov =>
|
|
|
|
env.bot.player.claimVictory(pov) pipe toResult
|
2020-03-11 15:50:34 -06:00
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
case _ => notFoundJson("No such command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 06:00:52 -06:00
|
|
|
def boardCommandGet(cmd: String) =
|
|
|
|
ScopedBody(_.Board.Play) { implicit req => me =>
|
|
|
|
cmd.split('/') match {
|
|
|
|
case Array("game", id, "chat") =>
|
|
|
|
WithPovAsBoard(id, me) { pov =>
|
|
|
|
env.chat.api.userChat.find(lila.chat.Chat.Id(pov.game.id)) map
|
|
|
|
lila.chat.JsonView.boardApi map JsonOk
|
|
|
|
}
|
2021-08-29 09:18:51 -06:00
|
|
|
case _ => notFoundJson("No such command")
|
2021-08-27 06:00:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 15:24:33 -07:00
|
|
|
// utils
|
|
|
|
|
2020-03-11 15:27:56 -06:00
|
|
|
private def toResult(f: Funit): Fu[Result] = catchClientError(f inject jsonOkResult)
|
2020-05-05 22:11:15 -06:00
|
|
|
private def catchClientError(f: Fu[Result]): Fu[Result] =
|
2020-09-21 01:28:28 -06:00
|
|
|
f recover { case e: lila.round.BenignError =>
|
|
|
|
BadRequest(jsonError(e.getMessage))
|
2020-05-05 22:11:15 -06:00
|
|
|
}
|
2020-03-11 15:27:56 -06:00
|
|
|
|
2020-02-24 15:24:33 -07:00
|
|
|
private def WithPovAsBot(anyId: String, me: lila.user.User)(f: Pov => Fu[Result]) =
|
|
|
|
WithPov(anyId, me) { pov =>
|
2020-02-24 17:45:04 -07:00
|
|
|
if (me.noBot)
|
2020-02-24 15:24:33 -07:00
|
|
|
BadRequest(
|
|
|
|
jsonError(
|
|
|
|
"This endpoint can only be used with a Bot account. See https://lichess.org/api#operation/botAccountUpgrade"
|
|
|
|
)
|
|
|
|
).fuccess
|
2020-02-28 12:01:10 -07:00
|
|
|
else if (!lila.game.Game.isBotCompatible(pov.game))
|
2020-02-24 17:45:04 -07:00
|
|
|
BadRequest(jsonError("This game cannot be played with the Bot API.")).fuccess
|
2020-02-24 15:24:33 -07:00
|
|
|
else f(pov)
|
|
|
|
}
|
|
|
|
|
|
|
|
private def WithPovAsBoard(anyId: String, me: lila.user.User)(f: Pov => Fu[Result]) =
|
|
|
|
WithPov(anyId, me) { pov =>
|
2020-02-28 12:01:10 -07:00
|
|
|
if (me.isBot) notForBotAccounts.fuccess
|
|
|
|
else if (!lila.game.Game.isBoardCompatible(pov.game))
|
2020-02-24 17:45:04 -07:00
|
|
|
BadRequest(jsonError("This game cannot be played with the Board API.")).fuccess
|
|
|
|
else f(pov)
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private def WithPov(anyId: String, me: lila.user.User)(f: Pov => Fu[Result]) =
|
|
|
|
env.round.proxyRepo.game(lila.game.Game takeGameId anyId) flatMap {
|
|
|
|
case None => NotFound(jsonError("No such game")).fuccess
|
|
|
|
case Some(game) =>
|
|
|
|
Pov(game, me) match {
|
|
|
|
case None => NotFound(jsonError("Not your game")).fuccess
|
|
|
|
case Some(pov) => f(pov)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def botOnline =
|
|
|
|
Open { implicit ctx =>
|
|
|
|
env.user.repo.botsByIds(env.bot.onlineApiUsers.get) map { users =>
|
|
|
|
Ok(views.html.user.bots(users))
|
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|
2021-07-25 01:52:40 -06:00
|
|
|
|
|
|
|
def botOnlineApi =
|
|
|
|
Action { implicit req =>
|
|
|
|
apiC.jsonStream {
|
|
|
|
env.user.repo
|
|
|
|
.botsByIdsCursor(env.bot.onlineApiUsers.get)
|
2021-07-25 02:03:19 -06:00
|
|
|
.documentSource(getInt("nb", req) | Int.MaxValue)
|
2021-07-25 01:52:40 -06:00
|
|
|
.throttle(50, 1 second)
|
2021-10-03 10:14:09 -06:00
|
|
|
.map { env.user.jsonView.full(_, withOnline = false) }
|
2021-07-25 01:52:40 -06:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 15:24:33 -07:00
|
|
|
}
|