lila/modules/tv/src/main/TvTrouper.scala

108 lines
3.4 KiB
Scala
Raw Normal View History

2018-12-06 08:04:20 -07:00
package lila.tv
import akka.actor._
import akka.pattern.{ ask => actorAsk }
import play.api.libs.json.Json
import scala.concurrent.duration._
import scala.concurrent.Promise
import lila.common.LightUser
2019-08-20 05:17:51 -06:00
import lila.game.Game
2018-12-06 08:04:20 -07:00
import lila.hub.Trouper
private[tv] final class TvTrouper(
system: ActorSystem,
rendererActor: ActorSelection,
lightUser: LightUser.GetterSync,
2019-08-20 05:17:51 -06:00
onSelect: Game => Unit,
2019-08-24 03:53:09 -06:00
proxyGame: Game.ID => Fu[Option[Game]],
rematchOf: Game.ID => Option[Game.ID]
2018-12-06 08:04:20 -07:00
) extends Trouper {
import TvTrouper._
2019-08-20 05:17:51 -06:00
system.lilaBus.subscribe(this, 'startGame)
private val channelTroupers: Map[Tv.Channel, ChannelTrouper] = Tv.Channel.all.map { c =>
2019-08-24 03:53:09 -06:00
c -> new ChannelTrouper(c, lightUser, onSelect = this.!, proxyGame, rematchOf)
2018-12-06 08:04:20 -07:00
}.toMap
private var channelChampions = Map[Tv.Channel, Tv.Champion]()
private def forward[A](channel: Tv.Channel, msg: Any) =
channelTroupers get channel foreach { _ ! msg }
2018-12-07 19:25:35 -07:00
protected val process: Trouper.Receive = {
2018-12-06 08:04:20 -07:00
case GetGameId(channel, promise) =>
forward(channel, ChannelTrouper.GetGameId(promise))
case GetGameIdAndHistory(channel, promise) =>
forward(channel, ChannelTrouper.GetGameIdAndHistory(promise))
case GetGameIds(channel, max, promise) =>
forward(channel, ChannelTrouper.GetGameIds(max, promise))
case GetChampions(promise) => promise success Tv.Champions(channelChampions)
2019-08-20 23:47:45 -06:00
case lila.game.actorApi.StartGame(g) => if (g.hasClock) {
2019-08-20 05:17:51 -06:00
val candidate = Tv.toCandidate(lightUser)(g)
channelTroupers collect {
case (chan, trouper) if chan filter candidate => trouper
} foreach (_ addCandidate g)
2019-08-20 23:47:45 -06:00
}
2019-08-20 05:17:51 -06:00
case s @ TvTrouper.Select => channelTroupers.foreach(_._2 ! s)
2018-12-06 08:04:20 -07:00
case Selected(channel, game) =>
import lila.socket.Socket.makeMessage
val player = game.firstPlayer
val user = player.userId flatMap lightUser
(user |@| player.rating) apply {
case (u, r) => channelChampions += (channel -> Tv.Champion(u, r, game.id))
}
onSelect(game)
2019-11-05 01:24:13 -07:00
val data = Json.obj(
2018-12-06 08:04:20 -07:00
"channel" -> channel.key,
"id" -> game.id,
"color" -> game.firstColor.name,
"player" -> user.map { u =>
Json.obj(
"name" -> u.name,
"title" -> u.title,
"rating" -> player.rating
)
}
2019-11-05 01:24:13 -07:00
)
2019-11-25 14:36:39 -07:00
system.lilaBus.publish(lila.hub.actorApi.tv.TvSelect(game.id, game.speed, data), 'tvSelect)
2018-12-06 08:04:20 -07:00
if (channel == Tv.Channel.Best) {
implicit def timeout = makeTimeout(100 millis)
actorAsk(rendererActor, actorApi.RenderFeaturedJs(game)) onSuccess {
case html: String =>
val event = lila.hub.actorApi.game.ChangeFeatured(
game.id,
makeMessage("featured", Json.obj(
"html" -> html,
"color" -> game.firstColor.name,
"id" -> game.id
))
)
system.lilaBus.publish(event, 'changeFeaturedGame)
}
}
}
}
private[tv] object TvTrouper {
case class GetGameId(channel: Tv.Channel, promise: Promise[Option[Game.ID]])
case class GetGameIds(channel: Tv.Channel, max: Int, promise: Promise[List[Game.ID]])
case class GetGameIdAndHistory(channel: Tv.Channel, promise: Promise[ChannelTrouper.GameIdAndHistory])
case object Select
case class Selected(channel: Tv.Channel, game: Game)
case class GetChampions(promise: Promise[Tv.Champions])
}