Join room synchronously

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

votagusvotag
Posts: 38
Joined: 07 Nov 2016, 12:26

Join room synchronously

Postby votagusvotag » 09 Jun 2017, 21:28

Hi, in the game there is a matchmaking room that search for game rooms with an odd number of players every few seconds and if there is one, the user will be joined to that game room to balance the battle. I'm doing the search with the MatchExpression class, the thing is, if this is done in a number of matchmaking rooms, there is a chance that more than one will find the same game room and will try to join the user, which would maintain the unbalance. How could I do the check for odd players and join event synchronous?

Code: Select all

                // Would this work?
                synchronized (Matchmaking.class) {
                    if (gameRoom.getPlayersList().size() % 2 == 0) { // Odd number.
                        getApi().joinRoom(user, gameRoom);
                    }
                }


Thanks!
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Join room synchronously

Postby Lapo » 10 Jun 2017, 16:32

Hi,
I think the code should work a little differently. Instead of having multiple Rooms running a search every second it would be better to have a separate entity (let's call it the RoomMonitor) that runs the search for every one and the match-making rooms can simply consult the results stored in by the RoomMonitor itself.

To avoid multiple users join the same Room (and thus maintain the unbalance) you should probably put the "joinable users" in a queue and then synchronize the joining process on that queue.

In other words, every second this happens:
1- Room Monitor keeps track of the list of rooms with odd number of players.
2- synchronize on the user's queue:
3- iterate over every uneven room, extract 1 user from the queue and join him/her in that room
4- exit synchronized block

hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
votagusvotag
Posts: 38
Joined: 07 Nov 2016, 12:26

Re: Join room synchronously

Postby votagusvotag » 13 Jun 2017, 14:55

Thanks!, I'll try that way. :)

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Stevenor and 66 guests