Creating Game Rooms at runtime for only 2 users

Post here your questions about the Flash / Flex / Air API for SFS2X

Moderators: Lapo, Bax

iceherosubzero
Posts: 15
Joined: 06 Mar 2009, 07:52
Location: India
Contact:

Creating Game Rooms at runtime for only 2 users

Postby iceherosubzero » 04 Dec 2013, 22:44

Hello,

I need to create a game room dynamically for only 2 users. How can I create a different room dynamically if the room created earlier has 2 users in it already.
What I need exactly is, if there are 0 users currently connected and then (user1) connects, he gets to create a game room. The next (user2) connecting gets to join the room already created by the (user1), instead of creating a game room of his own. The next user connecting (user3) then creates a game room of his own as no room is available for him to join. Later (user4) connects and he gets to join the room created by (user3) instead of creating a room of his own. This goes on and on for every ODD user being my first player and the EVEN being my second player.

Need help with this.

Thanks,
Iceheros :)
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Creating Game Rooms at runtime for only 2 users

Postby Lapo » 05 Dec 2013, 09:01

Hi,
I think I've posted this example somewhere else but I can't find it right now so I'll post it here again.

It's a bit of server side code that handles a request called "joinme" from the client. The server then searches for a Room with a free spot and if it is found it will join the player. Otherwise it will create a new Room with makeNewRoom(...) method and join the player there.

Code: Select all

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import com.smartfoxserver.v2.api.CreateRoomSettings;
import com.smartfoxserver.v2.entities.*;
import com.smartfoxserver.v2.exceptions.*;

public class AutoJoinerExtension extends SFSExtension
{
   private final String version = "1.0.0";
   private final Random rnd = new Random();
   private final AtomicInteger roomId = new AtomicInteger();
   
   @Override
   public void init()
   {
      trace("Java AutoJoiner: " + version);
   }
   
   @Override
   public void handleClientRequest(String cmd, User user, ISFSObject params)
   {
      try
      {
         if (cmd.equals("joinMe"))
            joinUser(user);
      }
      catch(Exception err)
      {
         trace(ExtensionLogLevel.ERROR, err.toString());
      }
   }
   
   private void joinUser(User user) throws SFSException
    {
       List<Room> rList = getParentZone().getRoomList();
       Room theRoom = null;
      
       for (Room room : rList)
       {
          if (room.isFull())
             continue;
          else
          {
             theRoom = room;
             break;
          }
       }
      
       if (theRoom == null)
          theRoom = makeNewRoom(user);
      
       try
       {
          getApi().joinRoom(user, theRoom);
       }
       catch (SFSJoinRoomException e)
       {
          trace(ExtensionLogLevel.ERROR, e.toString());
      }
    }
   
   private Room makeNewRoom(User owner) throws SFSCreateRoomException
    {
      Room room = null;
      
      CreateRoomSettings rs = new CreateRoomSettings();
      rs.setGame(false);
      rs.setDynamic(true);
      rs.setName("ChatRoom_" + roomId.getAndIncrement());
      rs.setMaxUsers(3);
      
      room = getApi().createRoom(getParentZone(), rs, owner);
      return room;
    }
}


Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
iceherosubzero
Posts: 15
Joined: 06 Mar 2009, 07:52
Location: India
Contact:

Re: Creating Game Rooms at runtime for only 2 users

Postby iceherosubzero » 05 Dec 2013, 10:38

Hi Lapo,

Thanks a lot for your help.

Iceheros
iceherosubzero
Posts: 15
Joined: 06 Mar 2009, 07:52
Location: India
Contact:

Re: Creating Game Rooms at runtime for only 2 users

Postby iceherosubzero » 13 Dec 2013, 11:44

What do I need to do to display my user names / player names in two different text fields one to the left and 1 to the right for a game room for only two players?
Do I need to program it on the server side or client side and do I need to use user variables.
I just need to display the player names.
ss3.JPG
(26.25 KiB) Not downloaded yet


How to do this?

Please help

Regards,
Iceheros.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Creating Game Rooms at runtime for only 2 users

Postby Lapo » 13 Dec 2013, 12:18

Once both players are connected you can obtain their names from the Room's user list.
When you are waiting for the 2nd player to join the Room you can use a temporary text such as "Waiting player..."

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
iceherosubzero
Posts: 15
Joined: 06 Mar 2009, 07:52
Location: India
Contact:

Re: Creating Game Rooms at runtime for only 2 users

Postby iceherosubzero » 15 Dec 2013, 05:50

How can I make the AutoJoinerExtension into a separate ClientRequestHandler Class of its own.
the below code

Code: Select all

    import java.util.*;
    import java.util.concurrent.atomic.AtomicInteger;
    import com.smartfoxserver.v2.api.CreateRoomSettings;
    import com.smartfoxserver.v2.entities.*;
    import com.smartfoxserver.v2.exceptions.*;

    public class AutoJoinerExtension extends SFSExtension
    {
       private final String version = "1.0.0";
       private final Random rnd = new Random();
       private final AtomicInteger roomId = new AtomicInteger();
       
       @Override
       public void init()
       {
          trace("Java AutoJoiner: " + version);
       }
       
       @Override
       public void handleClientRequest(String cmd, User user, ISFSObject params)
       {
          try
          {
             if (cmd.equals("joinMe"))
                joinUser(user);
          }
          catch(Exception err)
          {
             trace(ExtensionLogLevel.ERROR, err.toString());
          }
       }
       
       private void joinUser(User user) throws SFSException
        {
           List<Room> rList = getParentZone().getRoomList();
           Room theRoom = null;
         
           for (Room room : rList)
           {
              if (room.isFull())
                 continue;
              else
              {
                 theRoom = room;
                 break;
              }
           }
         
           if (theRoom == null)
              theRoom = makeNewRoom(user);
         
           try
           {
              getApi().joinRoom(user, theRoom);
           }
           catch (SFSJoinRoomException e)
           {
              trace(ExtensionLogLevel.ERROR, e.toString());
          }
        }
       
       private Room makeNewRoom(User owner) throws SFSCreateRoomException
        {
          Room room = null;
         
          CreateRoomSettings rs = new CreateRoomSettings();
          rs.setGame(false);
          rs.setDynamic(true);
          rs.setName("ChatRoom_" + roomId.getAndIncrement());
          rs.setMaxUsers(3);
         
          room = getApi().createRoom(getParentZone(), rs, owner);
          return room;
        }
    }

Regards,
Iceheros
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Creating Game Rooms at runtime for only 2 users

Postby Lapo » 15 Dec 2013, 11:58

Simply move the code to a new class that extends BaseClientRequestHandler and register it in the main Extension's init method:

Code: Select all

@Override
public void init()
{
    // Add a new Request Handler
    addRequestHandler("AutoJoin", AutoJoiner.class)
}


More details and examples here:
http://docs2x.smartfoxserver.com/Advanc ... extensions
Lapo

--

gotoAndPlay()

...addicted to flash games
iceherosubzero
Posts: 15
Joined: 06 Mar 2009, 07:52
Location: India
Contact:

Re: Creating Game Rooms at runtime for only 2 users

Postby iceherosubzero » 15 Dec 2013, 14:44

Hi Lapo,

I tried that, But then I want to know do I need this condition in my handleClientRequest ?

Code: Select all

if (cmd.equals("joinMe"))
                joinUser(user);


Or something else comes there ?

as

Code: Select all

handleClientRequest(String cmd, User user, ISFSObject params) //needs 3 parameters when it extends SFSExtension and only 2 when it extends
BaseClientRequestHandler(User user, ISFSObject params)


Regards,
Iceheros
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Creating Game Rooms at runtime for only 2 users

Postby Lapo » 16 Dec 2013, 09:39

When you use the addRequestHandler(...) method you are already saying which command name should trigger which Request handler, therefore you don't get the command name in your request handler anymore and you don't need any if statement.

More on this here:
http://docs2x.smartfoxserver.com/Advanc ... extensions
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X ActionScript 3 API”

Who is online

Users browsing this forum: No registered users and 18 guests