Skip to content

Commit

Permalink
try to fix ClientIn.Crowd equality in prod
Browse files Browse the repository at this point in the history
atm we can get duplicated events like
```
{"t":"crowd","d":{"nb":44}}
{"t":"crowd","d":{"nb":44}}
```
but only in prod and I don't get it
  • Loading branch information
ornicar committed Sep 21, 2024
1 parent 03be160 commit 62013bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/scala/actor/RoomActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object RoomActor:
case class State(
room: RoomId,
isTroll: IsTroll,
lastCrowd: ClientIn.Crowd = ClientIn.emptyCrowd
lastCrowd: ClientIn.Crowd = ClientIn.Crowd.empty
)

def onStart(
Expand Down Expand Up @@ -52,11 +52,12 @@ object RoomActor:

case crowd: ClientIn.Crowd =>
val shouldSend =
if crowd == state.lastCrowd then false
if crowd.sameAs(state.lastCrowd) then false
else if crowd.users != state.lastCrowd.users then true
else if crowd.members > 1000 && crowd.members % 100 != 0 then false
else if crowd.members > 100 && crowd.members % 10 != 0 then false
else true

if shouldSend then
deps.clientIn(crowd)
Some(state.copy(lastCrowd = crowd)) -> None
Expand Down
9 changes: 7 additions & 2 deletions src/main/scala/ipc/ClientIn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ object ClientIn:
def payload(js: JsValue) = Payload(JsonString(Json.stringify(js)))
def payload(tpe: String, js: JsonString) = Payload(JsonString(cliMsg(tpe, js)))

case class Crowd(doc: JsObject, members: Int, users: Set[User.Id]) extends ClientIn:
case class Crowd(doc: JsObject, members: Int, users: String) extends ClientIn:
lazy val write = cliMsg("crowd", doc)
val emptyCrowd = Crowd(Json.obj(), 0, Set.empty)
inline def sameAs(that: Crowd) = members == that.members && users == that.users

object Crowd:
def make(doc: JsObject, members: Int, userIds: Iterable[User.Id]) =
Crowd(doc, members, User.Id.raw(userIds.toList).sorted.mkString(","))
val empty = make(Json.obj(), 0, Nil)

case class LobbyPairing(fullId: Game.FullId) extends ClientIn:
def write =
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/ipc/CrowdJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class CrowdJson(inquirers: Inquirers, mongo: Mongo, lightUserApi: LightUse
keepOnlyStudyMembers(crowd).map: users =>
crowd.copy(users = users, anons = 0)
else Future.successful(crowd)
}.flatMap(spectatorsOf(_, crowd.users)).map(ClientIn.Crowd(_, crowd.members, crowd.users.toSet))
}.flatMap(spectatorsOf(_, crowd.users)).map(ClientIn.Crowd.make(_, crowd.members, crowd.users))

def round(crowd: RoundCrowd.Output): Future[ClientIn.Crowd] =
spectatorsOf(
Expand All @@ -20,15 +20,15 @@ final class CrowdJson(inquirers: Inquirers, mongo: Mongo, lightUserApi: LightUse
),
crowd.room.users
).map { spectators =>
ClientIn.Crowd(
ClientIn.Crowd.make(
Json
.obj(
"white" -> (crowd.players.white > 0),
"black" -> (crowd.players.black > 0),
"watchers" -> spectators
),
crowd.room.members,
Set.empty
Nil
)
}

Expand Down

0 comments on commit 62013bc

Please sign in to comment.