<commit_before>package controllers

import org.joda.time.DateTime
import play.api.libs.iteratee._
import play.api.libs.json._
import play.api.mvc._
import scala.concurrent.duration._

import lila.app._

object Bot extends LilaController {

  def gameStream(id: String) = Scoped(_.Bot.Play) { req => me =>
    WithMyBotGame(id, me) { pov =>
      RequireHttp11(req) {
        lila.game.GameRepo.withInitialFen(pov.game) map { wf =>
          Ok.chunked(Env.bot.gameStateStream(me, wf, pov.color))
        }
      }
    }
  }

  def move(id: String, uci: String) = Scoped(_.Bot.Play) { _ => me =>
    WithMyBotGame(id, me) { pov =>
      Env.bot.player(pov, me, uci) inject jsonOkResult recover {
        case e: Exception => BadRequest(jsonError(e.getMessage))
      }
    }
  }

  def command(cmd: String) = ScopedBody(_.Bot.Play) { implicit req => me =>
    cmd.split('/') match {
      case Array("account", "upgrade") =>
        lila.user.UserRepo.setBot(me) >>- Env.user.lightUserApi.invalidate(me.id) inject jsonOkResult recover {
          case e: Exception => BadRequest(jsonError(e.getMessage))
        }
      case Array("game", id, "chat") => WithBot(me) {
        Env.bot.form.chat.bindFromRequest.fold(
          err => BadRequest(errorsAsJson(err)).fuccess,
          res => WithMyBotGame(id, me) { pov =>
            Env.bot.player.chat(pov.gameId, me, res) inject jsonOkResult
          }
        )
      }
      case Array("game", id, "abort") => WithBot(me) {
        WithMyBotGame(id, me) { pov =>
          Env.bot.player.abort(pov) inject jsonOkResult
        }
      }
      case _ => notFoundJson("No such command")
    }
  }

  private def WithMyBotGame(anyId: String, me: lila.user.User)(f: lila.game.Pov => Fu[Result]) =
    WithBot(me) {
      Env.round.roundProxyGame(lila.game.Game takeGameId anyId) flatMap {
        case None => NotFound(jsonError("No such game")).fuccess
        case Some(game) => lila.game.Pov(game, me) match {
          case None => NotFound(jsonError("Not your game")).fuccess
          case Some(pov) => f(pov)
        }
      }
    }

  private def WithBot(me: lila.user.User)(f: => Fu[Result]) =
    if (!me.isBot) BadRequest(jsonError("This endpoint only works for bot accounts. See https://lichess.org/api#operation/botAccountUpgrade")).fuccess
    else f
}<commit_msg>bot abort 400<commit_after>package controllers

import org.joda.time.DateTime
import play.api.libs.iteratee._
import play.api.libs.json._
import play.api.mvc._
import scala.concurrent.duration._

import lila.app._

object Bot extends LilaController {

  def gameStream(id: String) = Scoped(_.Bot.Play) { req => me =>
    WithMyBotGame(id, me) { pov =>
      RequireHttp11(req) {
        lila.game.GameRepo.withInitialFen(pov.game) map { wf =>
          Ok.chunked(Env.bot.gameStateStream(me, wf, pov.color))
        }
      }
    }
  }

  def move(id: String, uci: String) = Scoped(_.Bot.Play) { _ => me =>
    WithMyBotGame(id, me) { pov =>
      Env.bot.player(pov, me, uci) inject jsonOkResult recover {
        case e: Exception => BadRequest(jsonError(e.getMessage))
      }
    }
  }

  def command(cmd: String) = ScopedBody(_.Bot.Play) { implicit req => me =>
    cmd.split('/') match {
      case Array("account", "upgrade") =>
        lila.user.UserRepo.setBot(me) >>- Env.user.lightUserApi.invalidate(me.id) inject jsonOkResult recover {
          case e: Exception => BadRequest(jsonError(e.getMessage))
        }
      case Array("game", id, "chat") => WithBot(me) {
        Env.bot.form.chat.bindFromRequest.fold(
          err => BadRequest(errorsAsJson(err)).fuccess,
          res => WithMyBotGame(id, me) { pov =>
            Env.bot.player.chat(pov.gameId, me, res) inject jsonOkResult
          }
        )
      }
      case Array("game", id, "abort") => WithBot(me) {
        WithMyBotGame(id, me) { pov =>
          Env.bot.player.abort(pov) inject jsonOkResult recover {
            case e: lila.base.LilaException => BadRequest(e.getMessage)
          }
        }
      }
      case _ => notFoundJson("No such command")
    }
  }

  private def WithMyBotGame(anyId: String, me: lila.user.User)(f: lila.game.Pov => Fu[Result]) =
    WithBot(me) {
      Env.round.roundProxyGame(lila.game.Game takeGameId anyId) flatMap {
        case None => NotFound(jsonError("No such game")).fuccess
        case Some(game) => lila.game.Pov(game, me) match {
          case None => NotFound(jsonError("Not your game")).fuccess
          case Some(pov) => f(pov)
        }
      }
    }

  private def WithBot(me: lila.user.User)(f: => Fu[Result]) =
    if (!me.isBot) BadRequest(jsonError("This endpoint only works for bot accounts. See https://lichess.org/api#operation/botAccountUpgrade")).fuccess
    else f
}
{'fraction_non_alphanumeric': 0.14406065711878685, 'fraction_numerical': 0.0014743049705139006, 'mean_word_length': 2.6587057010785826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 58, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194712', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1606, 'n_words': 319}