lila/app/controllers/Analyse.scala

143 lines
4.7 KiB
Scala
Raw Normal View History

2013-05-06 19:57:42 -06:00
package controllers
2021-02-20 04:37:15 -07:00
import chess.format.FEN
import chess.White
2013-05-30 04:35:21 -06:00
import play.api.mvc._
2021-02-20 04:37:15 -07:00
import views._
2013-05-30 04:35:21 -06:00
2014-02-01 11:45:02 -07:00
import lila.api.Context
2013-05-06 19:57:42 -06:00
import lila.app._
2014-11-16 16:58:59 -07:00
import lila.common.HTTPRequest
2019-12-13 07:30:20 -07:00
import lila.game.{ PgnDump, Pov }
import lila.round.JsonView.WithFlags
2013-05-06 19:57:42 -06:00
2019-12-04 16:39:16 -07:00
final class Analyse(
env: Env,
2019-12-05 14:51:18 -07:00
gameC: => Game,
roundC: => Round
2019-12-13 22:14:46 -07:00
) extends LilaController(env) {
2013-05-06 19:57:42 -06:00
2020-05-05 22:11:15 -06:00
def requestAnalysis(id: String) =
Auth { implicit ctx => me =>
OptionFuResult(env.game.gameRepo game id) { game =>
env.fishnet.analyser(
game,
lila.fishnet.Work.Sender(
userId = me.id,
ip = ctx.ip.some,
2020-05-05 22:11:15 -06:00
mod = isGranted(_.Hunter) || isGranted(_.Relay),
system = false
)
) map {
case true => NoContent
case false => Unauthorized
}
}
2016-09-30 06:16:46 -06:00
}
2015-06-27 11:01:49 -06:00
2014-12-26 14:29:53 -07:00
def replay(pov: Pov, userTv: Option[lila.user.User])(implicit ctx: Context) =
if (HTTPRequest isCrawler ctx.req) replayBot(pov)
2019-12-13 07:30:20 -07:00
else
env.game.gameRepo initialFen pov.gameId flatMap { initialFen =>
gameC.preloadUsers(pov.game) >> RedirectAtFen(pov, initialFen) {
(env.analyse.analyser get pov.game) zip
(!pov.game.metadata.analysed ?? env.fishnet.api.userAnalysisExists(pov.gameId)) zip
2019-12-13 07:30:20 -07:00
(pov.game.simulId ?? env.simul.repo.find) zip
roundC.getWatcherChat(pov.game) zip
(ctx.noBlind ?? env.game.crosstableApi.withMatchup(pov.game)) zip
env.bookmark.api.exists(pov.game, ctx.me) zip
2020-05-05 22:11:15 -06:00
env.api.pgnDump(
pov.game,
initialFen,
analysis = none,
PgnDump.WithFlags(clocks = false)
) flatMap {
case ((((((analysis, analysisInProgress), simul), chat), crosstable), bookmarked), pgn) =>
env.api.roundApi.review(
pov,
lila.api.Mobile.Api.currentVersion,
tv = userTv.map { u =>
lila.round.OnUserTv(u.id)
},
analysis,
initialFenO = initialFen.some,
withFlags = WithFlags(
movetimes = true,
clocks = true,
division = true,
opening = true
)
) map { data =>
EnableSharedArrayBuffer(
Ok(
html.analyse.replay(
pov,
data,
initialFen,
env.analyse.annotator(pgn, pov.game, analysis).toString,
analysis,
analysisInProgress,
simul,
crosstable,
userTv,
chat,
bookmarked = bookmarked
)
2019-12-23 04:16:55 -07:00
)
2019-12-13 07:30:20 -07:00
)
}
2020-09-21 01:28:28 -06:00
}
2019-12-13 07:30:20 -07:00
}
}
2020-05-05 22:11:15 -06:00
def embed(gameId: String, color: String) =
Action.async { implicit req =>
env.game.gameRepo.gameWithInitialFen(gameId) flatMap {
case Some((game, initialFen)) =>
2020-09-21 03:31:16 -06:00
val pov = Pov(game, chess.Color.fromName(color) | White)
2020-05-05 22:11:15 -06:00
env.api.roundApi.embed(
pov,
lila.api.Mobile.Api.currentVersion,
initialFenO = initialFen.some,
withFlags = WithFlags(opening = true)
) map { data =>
Ok(html.analyse.embed(pov, data))
}
case _ => fuccess(NotFound(html.analyse.embed.notFound))
} dmap EnableSharedArrayBuffer
2016-10-22 08:08:11 -06:00
}
2018-05-06 16:40:17 -06:00
private def RedirectAtFen(pov: Pov, initialFen: Option[FEN])(or: => Fu[Result])(implicit ctx: Context) =
get("fen").map(FEN.clean).fold(or) { atFen =>
val url = routes.Round.watcher(pov.gameId, pov.color.name)
fuccess {
2019-12-13 07:30:20 -07:00
chess.Replay
.plyAtFen(pov.game.pgnMoves, initialFen, pov.game.variant, atFen)
2019-12-13 07:30:20 -07:00
.fold(
err => {
lila.log("analyse").info(s"RedirectAtFen: ${pov.gameId} $atFen $err")
Redirect(url)
},
ply => Redirect(s"$url#$ply")
)
}
}
2019-12-13 07:30:20 -07:00
private def replayBot(pov: Pov)(implicit ctx: Context) =
for {
initialFen <- env.game.gameRepo initialFen pov.gameId
analysis <- env.analyse.analyser get pov.game
simul <- pov.game.simulId ?? env.simul.repo.find
crosstable <- env.game.crosstableApi.withMatchup(pov.game)
pgn <- env.api.pgnDump(pov.game, initialFen, analysis, PgnDump.WithFlags(clocks = false))
} yield Ok(
html.analyse.replayBot(
pov,
initialFen,
2021-03-11 10:08:21 -07:00
env.analyse.annotator(pgn, pov.game, analysis).toString,
2019-12-13 07:30:20 -07:00
simul,
crosstable
)
)
2013-05-06 19:57:42 -06:00
}