Reputation: 23
I'm making a game on Node.js with REDIS and Socket.io.
I have "rooms" in this game. There are from 1 to 9 players in room. They can join and leave whenever they want.
Also there is a loop (setInterval) in room that takes one player from Redis and makes this player a "leader".
Players stored in Redis as set (rooms:ROOMID:online). When user joins room I add him in this set. When user leaves, I remove him from set.
Maybe it's a dumb question, but...
How to get next user on every iteration WITHOUT DUPLICATE until I manually stop the loop, considering that the set could change on every iteration of the loop (can become smaller or bigger, users can join and leave).
Example of what I want: there are 3 users in set (0, 1, 2)
setInterval(function, 1000);
>0
>1
>2
> User 3 joined the room (added to set)
>0
>1
>2
>3
>0
>1
>2
> User 2 left the room (removed from set)
>3
>0
>1
>3
>...etc
Note: I can't SPOP from my main set (rooms:ROOMID:online).
Sorry for my English and thanks for any help.
Upvotes: 0
Views: 277
Reputation: 50082
I suggest you keep another set, e.g. rooms:ROOMID:elected
. Keep it in sync with the rooms:ROOMID:online
set on every join/sync event. SPOP from it and once it is empty, copy contents rooms:ROOMID:online to it.
Upvotes: 0