Page 1 of 1

Creating Game Rooms at runtime for only 2 users

Posted: 04 Dec 2013, 22:44
by iceherosubzero
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 :)

Re: Creating Game Rooms at runtime for only 2 users

Posted: 05 Dec 2013, 09:01
by Lapo
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

Re: Creating Game Rooms at runtime for only 2 users

Posted: 05 Dec 2013, 10:38
by iceherosubzero
Hi Lapo,

Thanks a lot for your help.

Iceheros

Re: Creating Game Rooms at runtime for only 2 users

Posted: 13 Dec 2013, 11:44
by iceherosubzero
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.

Re: Creating Game Rooms at runtime for only 2 users

Posted: 13 Dec 2013, 12:18
by Lapo
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

Re: Creating Game Rooms at runtime for only 2 users

Posted: 15 Dec 2013, 05:50
by iceherosubzero
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

Re: Creating Game Rooms at runtime for only 2 users

Posted: 15 Dec 2013, 11:58
by Lapo
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

Re: Creating Game Rooms at runtime for only 2 users

Posted: 15 Dec 2013, 14:44
by iceherosubzero
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

Re: Creating Game Rooms at runtime for only 2 users

Posted: 16 Dec 2013, 09:39
by Lapo
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