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 }
|
2017-03-29 08:27:46 -06:00
|
|
|
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(
|
2020-11-08 15:51:26 -07:00
|
|
|
userId = me.id,
|
2021-09-07 11:43:17 -06:00
|
|
|
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-03-12 05:56:44 -07:00
|
|
|
}
|
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) =
|
2019-12-11 08:53:27 -07:00
|
|
|
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
|
2020-04-22 16:53:54 -06:00
|
|
|
(!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)
|
2021-04-09 00:59:24 -06:00
|
|
|
) 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
|
|
|
)
|
2021-04-09 00:59:24 -06:00
|
|
|
}
|
2020-09-21 01:28:28 -06:00
|
|
|
}
|
2019-12-13 07:30:20 -07:00
|
|
|
}
|
2016-02-15 04:04:17 -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))
|
2020-07-07 12:31:53 -06:00
|
|
|
} 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) =
|
2020-10-18 12:21:34 -06:00
|
|
|
get("fen").map(FEN.clean).fold(or) { atFen =>
|
2016-02-15 04:04:17 -07:00
|
|
|
val url = routes.Round.watcher(pov.gameId, pov.color.name)
|
|
|
|
fuccess {
|
2019-12-13 07:30:20 -07:00
|
|
|
chess.Replay
|
2020-10-18 12:21:34 -06:00
|
|
|
.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")
|
|
|
|
)
|
2016-02-15 04:04:17 -07:00
|
|
|
}
|
2014-11-10 08:13:52 -07:00
|
|
|
}
|
2015-05-13 05:00:18 -06:00
|
|
|
|
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
|
|
|
}
|