6.x Java Extensions CookBook

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

» Room Variables Recipes

Room Variables can be accessed from server side to easily propagate Room-related values across the users in the same room. This is typically used to change state in games. The following recipes show a number of common use cases and examples of usage. Throughout the documentation you can find many documents and examples using Room Variables extensively:

Recipe #1:

In this recipe we show how to create a few simple Room Variables upon user request. The first two variables are assigned to the requesting user, while the third is owned by the Server (owner == null). By also keeping it private we ensure that no other user will be able to overwrite that variable.

The setRoomVariables() method takes the following parameters:

ExtensionHelper helper;
Zone currentZone;

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("setVars"))
	{
		Room targetRoom = zone.getRoom(fromRoom);
		
		if (targetRoom != null)
		{
			// Prepare Room Variables
			Map<String, RoomVariable> roomVariables = new HashMap<String, RoomVariable>();
			roomVariables.put("minRank", new RoomVariable("200", RoomVariable.TYPE_NUMBER, user, true, false));
			roomVariables.put("gameStarted", new RoomVariable("false", RoomVariable.TYPE_BOOLEAN, user, true, false));
			roomVariables.put("roomDesc", new RoomVariable("This is a poker game room", RoomVariable.TYPE_STRING, null, true, true));
			
			helper.setRoomVariables(targetRoom, user, roomVariables, true, true)
		}
	}
}	

Recipe #2:

Continuing from the previous example in this recipe we show how to update one of the previously created Room Variable upon client request. We also how to access variables from the Room object.

The setRoomVariable() method takes the following parameters:

ExtensionHelper helper;
Zone currentZone;

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("updVar"))
	{
		Room targetRoom = zone.getRoom(fromRoom);
		RoomVariable gameStartedVar = targetRoom.get("gameStarted");
		
		// We check the 'gameStarted' RoomVariable to see if it is set to true
		if (gameStartedVar.getValue().equals("false"))
			throw new IllegalStateException("You can't modify the request value until the game is started");
		
		String newValue = asObj.getString("newVal");
		
		// Set the new RoomVariable value
		helper.setRoomVariable(targetRoom, user, "minRank", RoomVariable.TYPE_NUMBER, newValue, false, true, true, true);
		
	}
}

 


doc index