SFSEventType.USER_JOIN_ROOM playerlist/userlist size issues

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

Moderators: Lapo, Bax

arun8483
Posts: 33
Joined: 15 Dec 2010, 02:35
Contact:

SFSEventType.USER_JOIN_ROOM playerlist/userlist size issues

Postby arun8483 » 01 Sep 2017, 11:56

I am expecting JoinRoomHandler handleServerEvent to be called on both occassions, ie, when Player 1 and Payer 2 join the room. Its working as expected but the PlayerList size is 2 on both occassion. How can I rely on this count to start a game?. If this is the case, game starts twice. Is there any other parameter in Zone, Room, User which I can make use to start game once.


Code: Select all

public void onSearchSuccess(User player1, User player2) {
    SmartFoxServer.getInstance().getAPIManager().getSFSApi().joinRoom(player1, gameRoom, null, false, null, false, true);
    SmartFoxServer.getInstance().getAPIManager().getSFSApi().joinRoom(player2, gameRoom, null, false, null, false, true);
}   



Code: Select all

public class JoinRoomHandler extends BaseServerEventHandler {

    public void handleServerEvent(ISFSEvent event) throws SFSException {
        if (event.getType() == SFSEventType.USER_JOIN_ROOM) {
            trace("Players List Size: " + room.getPlayersList().size());
           
        }
    }

}



However.....
When I try with synchronization things started working as expected... Following snippet works. But using synchronization as below recommended ?

Code: Select all

public void handleServerEvent(ISFSEvent event) throws SFSException {
if (event.getType() == SFSEventType.USER_JOIN_ROOM) {
    Room room = (Room) event.getParameter(SFSEventParam.ROOM);
    RoomVariable roomVariable = room.getVariable("game_state");
    trace("game_state: " + roomVariable.getStringValue());
    trace("Players List Size: " + room.getPlayersList().size());
    if (room.isGame()
       && room.getPlayersList().size() == 2
       && GameState.NOT_STARTED.getName().equals(roomVariable.getStringValue())) {
   GameExtension gameExtension = (GameExtension) room.getExtension();
   gameExtension.sendSearchResponse();
    }
}
}



Code: Select all

public synchronized void sendSearchResponse() {
trace("*** sendSearchResponse *** : " + getGameState());
if (getGameState() == GameState.NOT_STARTED) {
    //update room variable state to SEARCH_SUCCESS
    List<User> players = getParentRoom().getPlayersList();
    ISFSObject param = new SFSObject();
    param.putBool("count_down", true);
    ResponseHandler.sendResponse(Command.SEARCH_SUCCESS, param, players,
       getParentRoom(), true);
    startTimer(players);
    setGameState(GameState.SEARCH_SUCCESS);
}
}
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSEventType.USER_JOIN_ROOM playerlist/userlist size issues

Postby Lapo » 02 Sep 2017, 07:40

Something is not clear.
If the join is done from server side, why do you need to listen for the JOIN_ROOM event also on the server side?

You don't need to be notified of a User joining a Room when your code is actually doing the Join. Makes sense?
Events are useful only when the action (join in this case) by someone else, typically a remote client connected to the server.

Sorry if this doesn't answer your question entirely, but this matter seems to need to be clarified first.

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
arun8483
Posts: 33
Joined: 15 Dec 2010, 02:35
Contact:

Re: SFSEventType.USER_JOIN_ROOM playerlist/userlist size issues

Postby arun8483 » 02 Sep 2017, 11:47

aahhh.... that make sense.. understood...
But by looking at the way how events are fired, It seems we can't rely on joinRoom event handler to start a game...
If two remote clients A and B joins at the same time, same min, same sec, same nanosec... exactly at the same time...
User Join Event fires twice with playerList size = 2 on both occasions right ????

Because this is what happened when I listen for two clients joining on server side rather than from client side.

so question is, does the triggering event mechanism differs for remote clients ?

The expected behavior is,
For the first client, triggering onJoinRoom event, playerList size should be 1
For the second client onJoinRoom, playerList size should be 2

Instead of the above results,
why its 2 for both players ?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSEventType.USER_JOIN_ROOM playerlist/userlist size issues

Postby Lapo » 04 Sep 2017, 08:03

arun8483 wrote:aahhh.... that make sense.. understood...
But by looking at the way how events are fired, It seems we can't rely on joinRoom event handler to start a game...
If two remote clients A and B joins at the same time, same min, same sec, same nanosec... exactly at the same time...
User Join Event fires twice with playerList size = 2 on both occasions right ????


If two users join so close in time that when the first event is fired they're both in the Room, you already have the condition you're expecting to start the game. In other words that room.size() == 2

You are right in saying that you will get two events both saying that the Room has two players in it, but this should not break your game logic. You can simply verify if the game is already started: if it is, you don't need to start it again.

Because this is what happened when I listen for two clients joining on server side rather than from client side.

It is the same problem as above.
Everything happens concurrently, so by the time the first "join event" gets to your Extension code another client might have joined. Since the event passes the Room object where the join has occurred you will see the state of that Room as of now, not as of the time the event occurred.

That's the crucial point. In other words the event tells you that User X has joined Room Y.
You're expecting to find the Room in the state it was when User X joined but that's not what is happening. The Room object is not a snapshot of the past. It's a live object.

Once this is clear I think you can adjust your code to work exactly as you desire.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
arun8483
Posts: 33
Joined: 15 Dec 2010, 02:35
Contact:

Re: SFSEventType.USER_JOIN_ROOM playerlist/userlist size issues

Postby arun8483 » 04 Sep 2017, 09:52

So my ultimate question is, when 2 players joins concurrently, and if my game starting logic rely on user join event handler, does it requires synchronized method to make sure the game starts only once.

Code: Select all

if (room.size() == 2 && !gameStarted ) {
    gameStarted = true
}


Yes, you need synchronization to avoid edge cases where the gameStarted flag is not yet set in both threads. This will avoid the double execution of the game logic.

Cheers
arun8483
Posts: 33
Joined: 15 Dec 2010, 02:35
Contact:

Re: SFSEventType.USER_JOIN_ROOM playerlist/userlist size issues

Postby arun8483 » 04 Sep 2017, 10:51

thank you so much...that answered my question... :D

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 55 guests