How to prevent user from leaving the current room and join the newly created room?

StackOverflow https://stackoverflow.com/questions/15019474

  •  10-03-2022
  •  | 
  •  

Question

I'm facing a problem while working with SmartFoxServer 2X that I want to share seeking any help/advice from the community. It might be the default behaviour of SFS, but I want to do it another way for my project. Here's the scenario:

Server Version: 2.0.0-RC1 and I'm using AS3 for client side coding. I'm not doing anything on server side and using basic/default methods of SFS in AS3.

The user logs in a particular Zone and gets a list of rooms available. All the rooms must have a maximum of 2 users. If there is no room, the user creates the room (with settings.maxUsers = 2;) and joins that room. If there is any room, the user checks for a room with room.userCount<2 and joins that room.

If all the rooms are full, the user creates a new room and joins that room, so that another user can log in and join this room. Now, when the first user logs in, a room is created and the user waits for the next user to log in and join that room.

The second user logs in and joins the room created by the first user. Now, when the third user logs in, the second user joins the newly created room while staying in the room created by the first user as well. (NOTE: Only the second user behaves like this; the first user stays in the same room. Similarly when there are 4, 5 and 6 users in the game, the 5th shares the rooms with the 4th and 6th users, in the same fashion as the second user does).

Now there are two rooms. The first room is shared by the first and second users, and the second room is shared by the second and third users. That might be the default way of SFS to handle rooms and users joining them. But I want to keep the first and second players in the first room, even on creation of a new room in the zone, and then create a new room for the third user so that the 4th user can join the room with the third user and so on.

Thanks for having a look into it. Please help.


private function onLogin(event:SFSEvent):void {
  SFSVar.removeEventListener(SFSEvent.LOGIN, onLogin);
  var count:int = 0;
  var roomList:Array = event.currentTarget.roomList;
  if(roomList.length==0) {
    var my_date:Date = new Date();
    var settings:RoomSettings = new RoomSettings("PoolGame"+my_date.fullYear+my_date.month+my_date.date+my_date.hours+my_date.minutes+my_date.seconds);
    settings.maxUsers = 2;
    settings.groupId = "default";
    settings.isGame = true;
    SFSVar.send(new CreateRoomRequest(settings, false));
  } else if(roomList.length>0) {
    for each(var room:Room in roomList) {
      count++;
      if(room.userCount==2) {
        if(count==roomList.length) {
          var my_date:Date = new Date();
          var settings:RoomSettings = new RoomSettings("PoolGame"+my_date.fullYear+my_date.month+my_date.date+my_date.hours+my_date.minutes+my_date.seconds);
          settings.maxUsers = 2;
          settings.groupId = "default";
          settings.isGame = true;
          SFSVar.send(new CreateRoomRequest(settings,false));
          break;
        }
      } else if(room.userCount==1) {
        var roomRequest:JoinRoomRequest = new JoinRoomRequest(room.id,null,-1);
        SFSVar.send(roomRequest);
        break;
      }
    }
  }
}

private function onRoomCreation(event:SFSEvent):void {
  SFSVar.removeEventListener(SFSEvent.ROOM_ADD, onRoomCreation);
  var room:Room = event.params.room;
  var roomRequest:JoinRoomRequest = new JoinRoomRequest(room.id,null,-1);
  SFSVar.send(roomRequest);
}

private function onRoomJoin(event:SFSEvent):void {
  SFSVar.removeEventListener(SFSEvent.ROOM_JOIN, onRoomJoin);
  trace("room joined!!!");
}
Was it helpful?

Solution

I'm sure the original poster figured this out already but for other people reading this, the problem is that the 2nd user still has its room_add event listener. When the 3rd user joins, a new room is created, and because both the 2nd and 3rd users still have their room_add event listeners attached, they both join the new room.

To fix it, either add a line to remove the room_add event listener to the room_join event handler, or add some checking to the room_add event handler to send a room join request only if the player isn't already in a room.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top