From 018e3e6e26d4b4da1a0e93cb2a483868bfb3a02f Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Sun, 6 Mar 2016 09:42:22 +0700 Subject: [PATCH] mobile BC: lobby round count - fixes #1686 - for veloce/lichobile#297 --- app/controllers/Lobby.scala | 6 +++++- modules/lobby/src/main/Socket.scala | 17 +++++++++++++---- modules/lobby/src/main/SocketHandler.scala | 4 ++-- modules/lobby/src/main/actorApi.scala | 10 ++++++---- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/controllers/Lobby.scala b/app/controllers/Lobby.scala index e674f69718..d7d13132cf 100644 --- a/app/controllers/Lobby.scala +++ b/app/controllers/Lobby.scala @@ -43,7 +43,11 @@ object Lobby extends LilaController { def socket(apiVersion: Int) = SocketOption[JsValue] { implicit ctx => get("sri") ?? { uid => - Env.lobby.socketHandler(uid = uid, ip = ctx.req.remoteAddress, user = ctx.me) map some + Env.lobby.socketHandler( + uid = uid, + ip = ctx.req.remoteAddress, + user = ctx.me, + mobile = getBool("mobile")) map some } } diff --git a/modules/lobby/src/main/Socket.scala b/modules/lobby/src/main/Socket.scala index c45b620d5c..170ce0be04 100644 --- a/modules/lobby/src/main/Socket.scala +++ b/modules/lobby/src/main/Socket.scala @@ -1,6 +1,7 @@ package lila.lobby import scala.concurrent.duration._ +import scala.concurrent.Future import akka.actor._ import akka.pattern.ask @@ -39,9 +40,9 @@ private[lobby] final class Socket( history.since(v).fold(resync(m))(_ foreach sendMessage(m)) } - case Join(uid, ip, user, blocks) => + case Join(uid, ip, user, blocks, mobile) => val (enumerator, channel) = Concurrent.broadcast[JsValue] - val member = Member(channel, user, blocks, uid, ip) + val member = Member(channel, user, blocks, uid, ip, mobile) addMember(uid, member) sender ! Connected(enumerator, member) @@ -76,9 +77,11 @@ private[lobby] final class Socket( case lila.hub.actorApi.StreamsOnAir(html) => notifyAllAsync(makeMessage("streams", html)) case NbMembers(nb) => pong = pong + ("d" -> JsNumber(nb)) - case lila.hub.actorApi.round.NbRounds(nb) => pong = pong + ("r" -> JsNumber(nb)) + case lila.hub.actorApi.round.NbRounds(nb) => + pong = pong + ("r" -> JsNumber(nb)) + notifyMobileUsers(makeMessage("nbr", nb)) // BC, remove me - case ChangeFeatured(_, msg) => notifyAllAsync(msg) + case ChangeFeatured(_, msg) => notifyAllAsync(msg) } private def notifyPlayerStart(game: lila.game.Game, color: chess.Color) = @@ -98,4 +101,10 @@ private[lobby] final class Socket( private def notifySeeks() { notifyAll(makeMessage("reload_seeks")) } + + def notifyMobileUsers(msg: JsObject) = Future { + members.values.foreach { m => + if (m.mobile) m push msg + } + } } diff --git a/modules/lobby/src/main/SocketHandler.scala b/modules/lobby/src/main/SocketHandler.scala index 11981f7768..295d8e0cc3 100644 --- a/modules/lobby/src/main/SocketHandler.scala +++ b/modules/lobby/src/main/SocketHandler.scala @@ -50,9 +50,9 @@ private[lobby] final class SocketHandler( } } - def apply(uid: String, ip: String, user: Option[User]): Fu[JsSocketHandler] = + def apply(uid: String, ip: String, user: Option[User], mobile: Boolean): Fu[JsSocketHandler] = (user ?? (u => blocking(u.id))) flatMap { blockedUserIds => - val join = Join(uid = uid, ip = ip, user = user, blocking = blockedUserIds) + val join = Join(uid = uid, ip = ip, user = user, blocking = blockedUserIds, mobile = mobile) Handler(hub, socket, uid, join, user map (_.id)) { case Connected(enum, member) => (controller(socket, uid, member), enum, member) diff --git a/modules/lobby/src/main/actorApi.scala b/modules/lobby/src/main/actorApi.scala index 93bf8d8137..9aeb3341bb 100644 --- a/modules/lobby/src/main/actorApi.scala +++ b/modules/lobby/src/main/actorApi.scala @@ -32,7 +32,8 @@ private[lobby] case class Member( channel: JsChannel, user: Option[LobbyUser], uid: String, - ip: String) extends SocketMember { + ip: String, + mobile: Boolean) extends SocketMember { val userId = user map (_.id) val troll = user ?? (_.troll) @@ -40,11 +41,12 @@ private[lobby] case class Member( private[lobby] object Member { - def apply(channel: JsChannel, user: Option[User], blocking: Set[String], uid: String, ip: String): Member = Member( + def apply(channel: JsChannel, user: Option[User], blocking: Set[String], uid: String, ip: String, mobile: Boolean): Member = Member( channel = channel, user = user map { LobbyUser.make(_, blocking) }, uid = uid, - ip = ip) + ip = ip, + mobile = mobile) } private[lobby] case class HookMeta(hookId: Option[String] = None) @@ -64,7 +66,7 @@ private[lobby] case class BiteHook(hookId: String, uid: String, user: Option[Lob private[lobby] case class BiteSeek(seekId: String, user: LobbyUser) private[lobby] case class JoinHook(uid: String, hook: Hook, game: Game, creatorColor: chess.Color) private[lobby] case class JoinSeek(userId: String, seek: Seek, game: Game, creatorColor: chess.Color) -private[lobby] case class Join(uid: String, ip: String, user: Option[User], blocking: Set[String]) +private[lobby] case class Join(uid: String, ip: String, user: Option[User], blocking: Set[String], mobile: Boolean) private[lobby] case object Resync private[lobby] case class HookIds(ids: List[String])