roomList_lb - room variables and display

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Post Reply
starvingeyes
Posts: 17
Joined: 25 Oct 2007, 05:50

roomList_lb - room variables and display

Post by starvingeyes »

I'm trying to make the game room display window in the lobby only display games that are not in the middle of being played. right now, any person can join a game in progress and i do not want this. i was thinking that perhaps i could make a room variable when i create the room called isInPlay and set it to false. When the game started, this would be set to true. When this was set to true, the game would no longer be displayed in the list of rooms. I think all of this has to do only with these 3 bits of code taken from the tutorials, but I can't seem to get the syntax right (at all). how would I write this?

create a room variable called isInPlay and set it to false

Code: Select all

function createRoom(name:String, pwd:String, max:Number) {
	hideWindow("newGameWindow");
	var gameRoom:Object = new Object();
	gameRoom.name = name;
	gameRoom.password = pwd;
	gameRoom.maxUsers = 8;
	gameRoom.isGame = true;
	gameRoom.isTemp = true;
	//
	//create a room variable here that shows the game is not being played
	//
	smartfox.createRoom(gameRoom);
}
only display rooms where isInPlay = false

how do i write an if statement to access the isInPlay room variable?

Code: Select all

smartfox.onRoomListUpdate = function(roomList:Object) {
	roomList_lb.removeAll();
	for (var i in roomList) {
		var room:Room = roomList[i];
		if (room.isGame()) {
			roomList_lb.addItem(room.getName()+" ("+room.getUserCount()+")", room.getId());
		}
	}
	roomList_lb.sortItemsBy("label", "ASC");
	// Join the default room
	this.joinRoom("The Entrance", "");
};

Code: Select all

function changeRoom() {
	
	if (!_global.isBusy) {
		var item:Object = roomList_lb.getSelectedItem();
		// new Room id
		var newRoom:Number = item.data;
		//
		//
		if (newRoom != smartfox.activeRoomId) {
			// Check if new room is password protected	
			var priv:Boolean = smartfox.getRoom(newRoom).isPrivate();
			if (priv) {
				// Save newroom as _global for later use
				_global.newRoom = newRoom;
				showWindow("passwordWindow");
			} else {
				// Pass the room id
				smartfox.joinRoom(item.data);
			}
		}
	}
}
alcho
Posts: 84
Joined: 02 Dec 2006, 19:37
Location: UK
Contact:

Post by alcho »

Hmm, i am looking to have the same feature, i was going to do it like this:

When a game is created it will create a room variable where name=isStarted and val=false

Code: Select all

vObj = new Array()
vObj.push({name:"isStarted", val:false})

smartfox.setRoomVariables(vObj)
When the game starts the room variable is set to true

This variable can be checked in the main lobby in the onRoomListUpdate function. Where it will only add a room to the list where:

room.getVariable("isStarted") == false

This method should work but I havnt tested it out yet.

Hope this helps
Its all about the SmartFox!
starvingeyes
Posts: 17
Joined: 25 Oct 2007, 05:50

Post by starvingeyes »

you mean like this? on my trace action below, i'm still getting undefined. I think it has to do with the way I'm trying to create the "isInPlay" variable in the create room code. I just don't know how to get the syntax down.

Code: Select all

smartfox.onRoomListUpdate = function(roomList:Object) {
	roomList_lb.removeAll();
	for (var i in roomList) {
		var room:Room = roomList[i];
		trace("ROOMTEST "+room.getVariable("isInPlay"));
		if (room.isGame()) {
			roomList_lb.addItem(room.getName()+" ("+room.getUserCount()+")", room.getId());
		}
	}
	roomList_lb.sortItemsBy("label", "ASC");
	// Join the default room
	this.joinRoom("The Entrance", "");
};

Code: Select all

smartfox.onRoomVariablesUpdate = function(roomObj:Room) {
	trace("---------------------");
	trace("-------RECIEVE-------");
	trace("---------------------");
	var rVars:Object = roomObj.getVariables();
	trace("TEST ROOM VAR "+rVars["isInPlay"]);
};
//
function createRoom(name:String, pwd:String, max:Number) {
	hideWindow("newGameWindow");
	var gameRoom:Object = new Object();
	gameRoom.name = name;
	gameRoom.password = pwd;
	gameRoom.maxUsers = 8;
	gameRoom.isGame = true;
	gameRoom.isTemp = true;
	
	//
	//create a room variable here that shows the game is not being played
	//
	vObj7 = new Array();
	vObj7.push({name:"isInPlay", val:false});
	smartfox.setRoomVariables(vObj7);
	//
	smartfox.createRoom(gameRoom);
}
it's also not working when I try to set isInPlay this way:

Code: Select all

function createRoom(name:String, pwd:String, max:Number) {
	hideWindow("newGameWindow");
	var gameRoom:Object = new Object();
	gameRoom.name = name;
	gameRoom.password = pwd;
	gameRoom.maxUsers = 8;
	gameRoom.isGame = true;
	gameRoom.isTemp = true;
	
	//
	var vars:Array = new Array()
	vars.push( {name:"isInPlay", val:false} )
	gameRoom.vars = vars
	//
	smartfox.createRoom(gameRoom);
}
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I'm trying to make the game room display window in the lobby only display games that are not in the middle of being played. right now, any person can join a game in progress and i do not want this. i was thinking that perhaps i could make a room variable when i create the room called isInPlay and set it to false.
In general this type of lobby logic is best obtained with a server side extension. Extensions allows you to easily validate any of these parameters in a very simple and secure way.

Implementing the same logic on the client side is possible, but provides really weak security, it's prone to hacking attempts and it's probably trickier to develop as it's not in a "central place".

On the server side each User and Room object has carries a properties object where you can store any runtime values, status, flag etc... So you could use it for keeping the "inPlay" status of each room.

All you have to do is send a request from the client to your extension saying "join me in game room 10". The server side part will do all the necessary validation and either join the player or send back an error.

About real time updates of the status of each room ( game running, not running etc... ) you can send an additional update to your client that informs them about those status changes.

SFS also helps you keeping the bandwidth usage low by providing the JSON and Raw protocols. They become very useful when sending frequent updates to many clients.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
alcho
Posts: 84
Joined: 02 Dec 2006, 19:37
Location: UK
Contact:

Post by alcho »

Hi Lapo,

I like your suggestion. But just to confirm my knoweldge of room variables, why does the above code (trace("ROOMTEST "+room.getVariable("isInPlay")); ) return undefined?

Can you use the room.getVariable function outside of the actuall room?

Thanks
Its all about the SmartFox!
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Room variables are essentially done for sharing data between users in the same room.
Outside of that room you don't get updates, as this would be an overkill... imagine that each game room uses 10 variables and you have 1000 game rooms... you would need Gb of bandwidth to handle all that :)
Lapo
--
gotoAndPlay()
...addicted to flash games
alcho
Posts: 84
Joined: 02 Dec 2006, 19:37
Location: UK
Contact:

Post by alcho »

Ah yes that makes sense. Thanks very much for clearing that up
Its all about the SmartFox!
starvingeyes
Posts: 17
Joined: 25 Oct 2007, 05:50

Post by starvingeyes »

hmmmm...so i would have to get PRO to do this then right? in my admin tool it says I can't access this feature with basic. If I do this is it easy? Is there a work around?

could i rename the room "(game in progress) game room"? then use a substring to not let the player enter into the room if the substring is "(game in progress)"?
Post Reply