lila/app/controllers/Tv.scala

94 lines
3.0 KiB
Scala
Raw 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._
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-12-29 02:51:40 -07:00
import views._
2013-07-31 09:21:42 -06:00
2019-12-04 23:52:53 -07:00
final class Tv(
env: Env,
2019-12-05 14:51:18 -07:00
apiC: => Api
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 =>
OptionFuResult(chess.Color(color) ?? { env.round.proxyRepo.pov(gameId, _) }) { pov =>
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 =>
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
2020-05-05 22:11:15 -06:00
def feed =
Action.async {
import makeTimeout.short
import akka.pattern.ask
import lila.round.TvBroadcast
import play.api.libs.EventSource
env.round.tvBroadcast ? TvBroadcast.Connect mapTo
manifest[TvBroadcast.SourceType] map { source =>
2020-09-21 01:28:28 -06:00
Ok.chunked(source via EventSource.flow).as(ContentTypes.EVENT_STREAM) pipe noProxyBuffer
}
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
}