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
Creating Game Rooms at runtime for only 2 users
-
- Posts: 15
- Joined: 06 Mar 2009, 07:52
- Location: India
- Contact:
Re: Creating Game Rooms at runtime for only 2 users
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.
Cheers
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
-
- Posts: 15
- Joined: 06 Mar 2009, 07:52
- Location: India
- Contact:
Re: Creating Game Rooms at runtime for only 2 users
Hi Lapo,
Thanks a lot for your help.
Iceheros
Thanks a lot for your help.
Iceheros
-
- Posts: 15
- Joined: 06 Mar 2009, 07:52
- Location: India
- Contact:
Re: Creating Game Rooms at runtime for only 2 users
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.
How to do this?
Please help
Regards,
Iceheros.
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.
How to do this?
Please help
Regards,
Iceheros.
Re: Creating Game Rooms at runtime for only 2 users
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
When you are waiting for the 2nd player to join the Room you can use a temporary text such as "Waiting player..."
cheers
-
- Posts: 15
- Joined: 06 Mar 2009, 07:52
- Location: India
- Contact:
Re: Creating Game Rooms at runtime for only 2 users
How can I make the AutoJoinerExtension into a separate ClientRequestHandler Class of its own.
the below code
Regards,
Iceheros
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
Simply move the code to a new class that extends BaseClientRequestHandler and register it in the main Extension's init method:
More details and examples here:
http://docs2x.smartfoxserver.com/Advanc ... extensions
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
-
- Posts: 15
- Joined: 06 Mar 2009, 07:52
- Location: India
- Contact:
Re: Creating Game Rooms at runtime for only 2 users
Hi Lapo,
I tried that, But then I want to know do I need this condition in my handleClientRequest ?
Or something else comes there ?
as
Regards,
Iceheros
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
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
http://docs2x.smartfoxserver.com/Advanc ... extensions
Return to “SFS2X ActionScript 3 API”
Who is online
Users browsing this forum: No registered users and 20 guests