This document provides quick snippets of code organized by theme that will get you started with most of the server side coding tasks. Feel free to suggest more "recipes" by sending us an email or posting in our support forums
» CreateRoom Recipes
Rooms can be dynamically created at any time and with many different settings. The following recipes will guide you through some of the most common uses cases.
In this recipe we show how to create a server-owned room at the initialization time of your Extension. Each room has an owner or creator that refers to the User that created it. Rooms created by users will be auto-removed according to a simple set of rules that are discussed in various sections of the documentation ( see this article ) There is however a special case in which the owner of the Room is the Server itself. In this case the room will never be auto-removed, unless done manually via code or Admin Tool.
ExtensionHelper helper;
Zone currentZone;
public void init()
{
helper = ExtensionHelper.instance();
currentZone = helper.getZone(this.getOwnerZone);
createLobbyRoom();
}
private void createLobbyRoom()
{
Room newRoom = null;
// Here we pass the room parameters
HashMap map = new HashMap();
map.put("name", "The Lobby");
map.put("pwd", "");
map.put("maxU", "99999");
map.put("maxS", "0");
map.put("isGame", "false");
map.put("isLimbo", "true");
try
{
newRoom = helper.createRoom(currentZone, map, null, true, false);
}
catch(ExtensionHelperException ehe)
{
this.trace("Could not create room: " + name + ", Reason: " + ehe.getMessage());
}
return newRoom;
}
According to the javadoc we pass a Map containing the necessary Room settings.
The createRoom() method takes the following parameters:
In this recipe we create a dynamic room upon user request and we also initialize the Room with a few room variables and attach a server side extension to it.
The user request should contain the following parameters:
Also note that the this time we call an overloaded version of the createRoom() method that takes 7 parameters
ExtensionHelper helper;
Zone currentZone;
final int DEFAULT_ROOM_SIZE = 10;
public void init()
{
helper = ExtensionHelper.instance();
currentZone = helper.getZone(this.getOwnerZone);
}
public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
if (cmd.equals("makeGameRoom"))
{
String gameName = asObj.getString("gameName");
String type = asObj.getString("type");
int numberOfPlayers = (int) asObj.getNumber("nplayers");
// Check supported games
if (!type.equals("NavyBattle") || !type.equals("MineField"))
throw new IllegalArgumentException("This game type is not suppoerted: " + type);
// Check if the number of players is correct
if (numberOfPlayers < 2 || numberOfPlayers > 4)
throw new IllegalArgumentException("The requested number of players is not supported");
Room gameRoom = createGameRoom(user, gameName, type, numberOfPlayers);
}
}
private void createLobbyRoom(User user, String gameName, String type, int numberOfPlayers)
{
Room newRoom = null;
// Here we pass the room parameters
HashMap map = new HashMap();
map.put("name", gameName);
map.put("pwd", "");
map.put("maxU", String.valueOf(numberOfPlayers));
map.put("maxS", "0");
map.put("isGame", "true");
// Prepare Room Variables
Map<String, RoomVariable> roomVariables = new HashMap<String, RoomVariable>();
roomVariables.put("gameStarted", new RoomVariable("false", RoomVariable.TYPE_BOOLEAN, null, true, false));
roomVariables.put("gameState", new RoomVariable("WAITING", RoomVariable.TYPE_STRING, null, true, false));
// Prepare Extension
if (gameName.equals("NavyBattle"))
{
map.put("xtName", "gameExt");
map.put("xtClass", "com.smartfoxserver.games.NavyBattle");
}
else
{
map.put("xtName", "gameExt");
map.put("xtClass", "com.smartfoxserver.games.MineField");
}
try
{
newRoom = helper.createRoom(currentZone,
map,
null,
roomVariables,
null,
true,
true,
false);
}
catch(ExtensionHelperException ehe)
{
this.trace("Could not create room: " + name + ", Reason: " + ehe.getMessage());
}
return newRoom;
}
| doc index |