lila/app/controllers/Tv.scala

128 lines
4.2 KiB
Scala
Raw Permalink Normal View History

2013-07-31 09:21:42 -06:00
package controllers
2019-12-04 23:52:53 -07:00
import play.api.http.ContentTypes
import scala.util.chaining._
2020-12-20 13:04:51 -07:00
import views._
2013-07-31 09:21:42 -06:00
2014-05-10 07:50:54 -06:00
import lila.api.Context
2013-07-31 09:21:42 -06:00
import lila.app._
2019-08-20 07:35:41 -06:00
import lila.game.Pov
2013-07-31 09:21:42 -06:00
2019-12-04 23:52:53 -07:00
final class Tv(
env: Env,
apiC: => Api,
gameC: => Game
2019-12-04 23:52:53 -07:00
) extends LilaController(env) {
2013-07-31 09:21:42 -06:00
2015-06-16 10:46:30 -06:00
def index = onChannel(lila.tv.Tv.Channel.Best.key)
2014-06-22 15:40:44 -06:00
2020-05-05 22:11:15 -06:00
def onChannel(chanKey: String) =
Open { implicit ctx =>
(lila.tv.Tv.Channel.byKey get chanKey).fold(notFound)(lichessTv)
}
2014-05-10 07:50:54 -06:00
2020-05-05 22:11:15 -06:00
def sides(gameId: String, color: String) =
Open { implicit ctx =>
2020-09-21 03:31:16 -06:00
OptionFuResult(chess.Color.fromName(color) ?? { env.round.proxyRepo.pov(gameId, _) }) { pov =>
2020-05-05 22:11:15 -06:00
env.game.crosstableApi.withMatchup(pov.game) map { ct =>
Ok(html.tv.side.sides(pov, ct))
}
2019-04-06 19:43:03 -06:00
}
2014-10-19 05:12:55 -06:00
}
2020-05-05 22:11:15 -06:00
def channels =
apiC.ApiRequest { _ =>
import play.api.libs.json._
implicit val championWrites = Json.writes[lila.tv.Tv.Champion]
env.tv.tv.getChampions map {
_.channels map { case (chan, champ) => chan.name -> champ }
} map { Json.toJson(_) } map Api.Data.apply
}
2018-01-07 18:55:11 -07:00
2015-06-16 10:46:30 -06:00
private def lichessTv(channel: lila.tv.Tv.Channel)(implicit ctx: Context) =
2020-09-21 01:28:28 -06:00
OptionFuResult(env.tv.tv getGameAndHistory channel) { case (game, history) =>
val flip = getBool("flip")
val natural = Pov naturalOrientation game
val pov = if (flip) !natural else natural
val onTv = lila.round.OnLichessTv(channel.key, flip)
negotiate(
html = env.tournament.api.gameView.watcher(pov.game) flatMap { tour =>
env.api.roundApi.watcher(pov, tour, lila.api.Mobile.Api.currentVersion, tv = onTv.some) zip
env.game.crosstableApi.withMatchup(game) zip
env.tv.tv.getChampions map { case ((data, cross), champions) =>
2020-09-21 01:28:28 -06:00
NoCache {
Ok(html.tv.index(channel, champions, pov, data, cross, history))
}
2019-12-13 07:30:20 -07:00
}
2020-09-21 01:28:28 -06:00
},
api = apiVersion => env.api.roundApi.watcher(pov, none, apiVersion, tv = onTv.some) map { Ok(_) }
)
2015-06-16 10:46:30 -06:00
}
2014-05-10 07:50:54 -06:00
2015-09-20 03:21:32 -06:00
def games = gamesChannel(lila.tv.Tv.Channel.Best.key)
2020-05-05 22:11:15 -06:00
def gamesChannel(chanKey: String) =
Open { implicit ctx =>
lila.tv.Tv.Channel.byKey.get(chanKey) ?? { channel =>
2020-09-21 01:28:28 -06:00
env.tv.tv.getChampions zip env.tv.tv.getGames(channel, 15) map { case (champs, games) =>
NoCache {
Ok(html.tv.games(channel, games map Pov.naturalOrientation, champs))
}
2020-05-05 22:11:15 -06:00
}
2015-09-20 03:21:32 -06:00
}
2018-03-31 08:03:15 -06:00
}
2015-09-20 03:21:32 -06:00
2021-08-24 11:12:34 -06:00
def gameChannelReplacement(chanKey: String, gameId: String, exclude: List[String]) =
Open { implicit ctx =>
val gameFu = lila.tv.Tv.Channel.byKey.get(chanKey) ?? { channel =>
2021-08-24 11:12:34 -06:00
env.tv.tv.getReplacementGame(channel, gameId, exclude)
}
OptionResult(gameFu) { game =>
JsonOk {
play.api.libs.json.Json.obj(
"id" -> game.id,
"html" -> views.html.game.mini(Pov naturalOrientation game).toString
)
}
}
}
def apiGamesChannel(chanKey: String) =
Action.async { req =>
lila.tv.Tv.Channel.byKey.get(chanKey) ?? { channel =>
2021-06-16 06:29:06 -06:00
env.tv.tv.getGameIds(channel, getInt("nb", req).fold(10)(_ atMost 30 atLeast 1)) map { gameIds =>
val config =
lila.api.GameApiV2.ByIdsConfig(
ids = gameIds,
format = lila.api.GameApiV2.Format byRequest req,
flags = gameC.requestPgnFlags(req, extended = false).copy(delayMoves = false),
perSecond = lila.common.config.MaxPerSecond(30)
)
noProxyBuffer(Ok.chunked(env.api.gameApiV2.exportByIds(config))).as(gameC.gameContentType(config))
}
}
}
2020-05-05 22:11:15 -06:00
def feed =
2020-12-20 10:26:48 -07:00
Action.async { req =>
2020-05-05 22:11:15 -06:00
import makeTimeout.short
import akka.pattern.ask
import lila.round.TvBroadcast
import play.api.libs.EventSource
2020-12-20 13:04:51 -07:00
val bc = getBool("bc", req)
env.round.tvBroadcast ? TvBroadcast.Connect(bc) mapTo
2020-05-05 22:11:15 -06:00
manifest[TvBroadcast.SourceType] map { source =>
2020-12-20 13:04:51 -07:00
if (bc) Ok.chunked(source via EventSource.flow).as(ContentTypes.EVENT_STREAM) pipe noProxyBuffer
else apiC.sourceToNdJson(source)
2020-09-21 01:28:28 -06:00
}
2019-12-13 07:30:20 -07:00
}
2014-05-10 07:50:54 -06:00
2020-05-05 22:11:15 -06:00
def frame =
Action.async { implicit req =>
env.tv.tv.getBestGame map {
case None => NotFound
2020-08-16 02:10:50 -06:00
case Some(game) => Ok(views.html.tv.embed(Pov naturalOrientation game))
2020-05-05 22:11:15 -06:00
}
}
2013-07-31 09:21:42 -06:00
}