better crowd notification

This commit is contained in:
Thibault Duplessis 2013-05-15 00:07:55 -03:00
parent 12728ee120
commit 0579abc6e7
5 changed files with 26 additions and 24 deletions

View file

@ -40,6 +40,7 @@ final class Env(
val socketHub = system.actorOf(Props(new SocketHub(
makeHistory = history,
getUsername = getUsername,
uidTimeout = UidTimeout,
socketTimeout = SocketTimeout,
playerTimeout = PlayerTimeout,

View file

@ -16,6 +16,7 @@ import play.api.libs.iteratee._
private[round] final class Socket(
gameId: String,
history: ActorRef,
getUsername: String Fu[Option[String]],
uidTimeout: Duration,
socketTimeout: Duration,
playerTimeout: Duration) extends SocketActor[Member](uidTimeout) {
@ -64,7 +65,7 @@ private[round] final class Socket(
val (enumerator, channel) = Concurrent.broadcast[JsValue]
val member = Member(channel, user, color, owner)
addMember(uid, member)
notify(crowdEvent :: Nil)
notifyCrowd
if (playerIsGone(color)) notifyGone(color, false)
playerTime(color, nowMillis)
sender ! Connected(enumerator, member)
@ -79,7 +80,7 @@ private[round] final class Socket(
case Quit(uid) {
quit(uid)
notify(crowdEvent :: Nil)
notifyCrowd
}
case Close {
@ -88,22 +89,18 @@ private[round] final class Socket(
}
}
// TODO get usernames like in tournament socket
def crowdEvent = Event.Crowd(
white = ownerOf(White).isDefined,
black = ownerOf(Black).isDefined,
watchers = members.values
.filter(_.watcher)
.map(_.userId)
.toList.partition(_.isDefined) match {
case (users, anons) users.flatten.distinct |> { userList
anons.size match {
case 0 userList
case 1 userList :+ "Anonymous"
case x userList :+ ("Anonymous (%d)" format x)
}
}
})
def notifyCrowd {
members.values.filter(_.watcher).map(_.userId).toList.partition(_.isDefined) match {
case (users, anons)
(users.flatten.distinct map getUsername).sequence map { userList
notify(Event.Crowd(
white = ownerOf(White).isDefined,
black = ownerOf(Black).isDefined,
watchers = showSpectators(userList.flatten, anons.size)
) :: Nil)
} logFailure ("[round] notify crowd")
}
}
def notify(events: Events) {
history ? AddEvents(events) mapTo manifest[List[VersionedEvent]] foreach { vevents

View file

@ -14,6 +14,7 @@ import makeTimeout.short
private[round] final class SocketHub(
makeHistory: () History,
getUsername: String Fu[Option[String]],
uidTimeout: Duration,
socketTimeout: Duration,
playerTimeout: Duration,
@ -36,6 +37,7 @@ private[round] final class SocketHub(
Props(makeHistory()),
name = gameSocketName(id) + "-history"
),
getUsername = getUsername,
uidTimeout = uidTimeout,
socketTimeout = socketTimeout,
playerTimeout = playerTimeout

View file

@ -136,6 +136,12 @@ abstract class SocketActor[M <: SocketMember](uidTtl: Duration) extends Actor {
members.values filter (_ liveGames gameId) foreach (_.channel push msg)
}
def showSpectators(users: List[String], nbAnons: Int) = nbAnons match {
case 0 users
case 1 users :+ "Anonymous"
case x users :+ ("Anonymous (%d)" format x)
}
def registerLiveGames(uid: String, ids: List[String]) {
withMember(uid)(_ addLiveGames ids)
}

View file

@ -91,12 +91,8 @@ private[tournament] final class Socket(
def notifyCrowd {
members.values.map(_.userId).toList.partition(_.isDefined) match {
case (users, anons)
(users.flatten.distinct map getUsername).sequence map (_.flatten) foreach { userList
notifyVersion("crowd", anons.size match {
case 0 userList
case 1 userList :+ "Anonymous"
case x userList :+ ("Anonymous (%d)" format x)
})
(users.flatten.distinct map getUsername).sequence foreach { userList
notifyVersion("crowd", showSpectators(userList.flatten, anons.size))
}
}
}