Isspectator doesnt seem to work 100%

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

Moderators: Lapo, Bax

Post Reply
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Isspectator doesnt seem to work 100%

Post by knegarpetter »

Hi i got a problem with the Spectator variable.

A user joins a room as a Spectator.
In that room he runs a

Code: Select all

sfs.switchSpectator();

but if the same user now tries to run a

Code: Select all

var thisRoom:Room = sfs.getActiveRoom();
			trace("Active room: " + thisRoom.getName());
			playerDP.removeAll();
			myParent.PlayersDictionary = new Dictionary();
			var users:Array = thisRoom.getUserList();
			for (var u:String in users){
				if(!users[u].isSpectator()){ 
...

it wont work, the current (own) user wont turn into a non spectator user....



but if another user joins the same room, and tries to run... getUSerlist etc... it will look to him as if the first user is a non spectator user...


the same goes for player ID, if a user looks at his own player id it wont change even though he has done a spectator swith, but for other users it will look as he got an ID...


regards
Petter
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Is this a game room?
If you check the SFSTris example game you will see that spectators and switchSpectator commands work correctly. Maybe you can use that code to double check you're doing things right.

Let us know...
Lapo
--
gotoAndPlay()
...addicted to flash games
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Post by knegarpetter »

well, it is a gameroom... and to all other clients it looks like the first client turns into a player... put not to the first player himself.... strange.

regards
Petter
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Post by knegarpetter »

I guess you mean advance sftris...
it doesnt seem to work 100% that example, if a player joins as player, but the second user first joins as spectator, and later turns into a player... the first player will end up trying to play against himself... and keeps waiting for the "other" player to take his turn...
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Post by knegarpetter »

just tried it again, it must be a bug.

Code: Select all

fs.switchSpectator(sfs.activeRoomId);
i get a success from onSpectatorSwitched...

but

Code: Select all

var thisRoom:Room = sfs.getActiveRoom();
var users:Array = thisRoom.getUserList();
			for (var u:String in users){
				if(!users[u].isSpectator()){......

Will still treat the "own" client as a spectator...

If another clients joins the room and runs the second code, it will look to him as if the first player is a "player" not a spectator...

i can send you an fla with code to show you...
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

thank you, I don't think it's necessary.
I'll post a workaround as soon as possible.
Lapo
--
gotoAndPlay()
...addicted to flash games
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Post by knegarpetter »

Run this code with the template_AS3... (just swap the actionscritp on frame 1 with this...

Code: Select all

import it.gotoandplay.smartfoxserver.SmartFoxClient;
import it.gotoandplay.smartfoxserver.SFSEvent;
import it.gotoandplay.smartfoxserver.data.Room;
import flash.events.Event;

const NEWLINE:String = "\n";
var sfs:SmartFoxClient;
var firstRound:Boolean = true;

main()

stop()
function main():void
{
	sfs = new SmartFoxClient(true);
	
	// Register for SFS events
	sfs.addEventListener(SFSEvent.onConnection, onConnection);
	sfs.addEventListener(SFSEvent.onConnectionLost, onConnectionLost);
	sfs.addEventListener(SFSEvent.onLogin, onLogin);
	sfs.addEventListener(SFSEvent.onRoomListUpdate, onRoomListUpdate);
	sfs.addEventListener(SFSEvent.onJoinRoom, onJoinRoom);
	sfs.addEventListener(SFSEvent.onJoinRoomError, onJoinRoomError);
	sfs.addEventListener(SFSEvent.onAdminMessage, onAdminMessageHandler)
	sfs.addEventListener(SFSEvent.onSpectatorSwitched, onSpectatorSwitchedHandler);
	
	// Register for generic errors
	sfs.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)
	sfs.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
	
	bt_connect.addEventListener(MouseEvent.CLICK, bt_connect_click)
	
	debugTrace("Click the CONNECT button to start");
}

/**
 * Handles the button click
 * Loads the external config.xml file and starts a connection
 */
function bt_connect_click(evt:Event):void
{
	if (!sfs.isConnected)
		sfs.loadConfig()
	else
		debugTrace("You are already connected!");
}

/**
 * Handle connection
 */
function onConnection(evt:SFSEvent):void
{
	var success:Boolean = evt.params.success;
	
	if (success)
	{
		debugTrace("Connection successfull!");
		trace("Zone "+sfs.defaultZone);
		
		// sfs.defaultZone refers to the Zone set in the external XML file
		// We use an empty name and passoword to login as guest user
		sfs.login(sfs.defaultZone, "", "");
	}
	else
	{
		debugTrace("Connection failed!");	
	}
}

/**
 * Handle connection lost
 */
function onConnectionLost(evt:SFSEvent):void
{
	debugTrace("Connection lost!");
}

/**
 * Handle login response
 */
function onLogin(evt:SFSEvent):void
{
	if (evt.params.success)
	{
		debugTrace("Successfully logged in");
	}
	else
	{
		debugTrace("Login failed. Reason: " + evt.params.error);
	}
}

/**
 * Handle room list
 */
function onRoomListUpdate(evt:SFSEvent):void
{
	debugTrace("Room list received");
	  for (var r:String in evt.params.roomList)
        trace(evt.params.roomList[r].getName())
		
	// Tell the server to auto-join us in the default room for this Zone
	if(firstRound){
		sfs.autoJoin();
		firstRound = false;
	}
}

/**
 * Handle successfull join
 */
function onJoinRoom(evt:SFSEvent):void
{
	debugTrace("Successfully joined room: " + evt.params.room.getName());
	if( evt.params.room.getName()=="The Entrance"){
		sfs.joinRoom("The Garden","",true);
	}else{
		whatAmI();
		sfs.switchSpectator();
	}
}

/**
 * Handle problems with join
 */
function onJoinRoomError(evt:SFSEvent):void
{
	debugTrace("Problems joining default room. Reason: " + evt.params.error);	
}

/**
 * Handle a Security Error
 */
function onSecurityError(evt:SecurityErrorEvent):void
{
	debugTrace("Security error: " + evt.text);
}

/**
 * Handle an I/O Error
 */
function onIOError(evt:IOErrorEvent):void
{
	debugTrace("I/O Error: " + evt.text);
}

/**
 * Trace messages to the debug text area
 */
function debugTrace(msg:String):void
{
	ta_debug.text += "--> " + msg + NEWLINE;
}

function onAdminMessageHandler(evt:SFSEvent):void
{
    trace("Administrator said: " + evt.params.message)
}

function whatAmI(){
	debugTrace("what am i?");
	var thisRoom:Room = sfs.getActiveRoom();
	debugTrace("Where am I: " + thisRoom.getName());
	var users:Array = thisRoom.getUserList();
	for (var u:String in users){
		debugTrace("User: "+users[u].getName());
		if(!users[u].isSpectator()){
			debugTrace("Is NOT a spectator!");
		}else{
			debugTrace("IS a spectator");
		}
	}
}

function onSpectatorSwitchedHandler(evt:SFSEvent):void {
	debugTrace("onSpectatorSwitchedHandler");
	whatAmI();
}
The user will never look like a spectator to him self.. but if you run another client with the swf, it will look to him as if the first client IS NOT a spectaror...

regards
Petter


Sorry for wasting your time like this but we are building a huge POC and it is kind of vital that this works....
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Sorry for wasting your time like this but we are building a huge POC and it is kind of vital that this works....
No problem, it's not a waste of time! :shock: Conversely we always need feedback to make our product the best it can be.

Thanks for the code example, we're looking into this in the next hours. Stay tuned.
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Update on your test code.
We have tested it and it seems to be working fine.
I had a slightly different setup:
"The Hall" is the autojoin room
"The Garden" is joined after the first join is okay.
We specified the following properties to "The Garden" room:

Code: Select all

isGame="true"
maxSpectator="5"
This is the resulting output:
Image

Maybe you didn't set the room as a game room?
Spectators are not available for "regular" rooms.
Lapo
--
gotoAndPlay()
...addicted to flash games
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Post by knegarpetter »

well by the look of the consol... it doenst work...

the guest is supposed to _not_ be a spectator after the switch... and it looks to me that he still is... ???

But try to run another swf while the first one is connedcted and it will look to the second one like the first one isnt a spectator...



regards
Petter
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I've checked the problem you have reported:

1. Examples don't work correctly:
You are right, out of the box the SFSTris examples don't seem to work correctly with the spectators.

The problem is however very simple. The examples are not compiled with the bundled API. Instead the are compiled with an earlier version. We apologize for the inconvenience.
However both the examples and API work correctly, you simply have to open the example(s) in Flash and recompile them with the provided API.

2. Spectator issue with the provided code:
As you can see in the SFSTris examples we treat the spectator status of the current users differently from the status of the other users in the room.
If you check the code you can better see how it is handled.

For every other users in the room it is correct to use the User.isSpectator() method.
For the current user you should check if playerId > 0 ( http://www.smartfoxserver.com/docs/docP ... l#playerId )
This follows the same principle of properties like the amImoderator, myUserId etc... which describe the status of the current user.

We'll try to better document this part of the API to avoid misunderstandings.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Post by knegarpetter »

well... that sounds... inconsistent...
If i get a userlist, i asume that all the users should/could be handled the same way...
and checking the playerId... that sounds like a not so nice workaround... if so we sholud have a amISpectator or something... but htat should work like isSpectator... as i see it it doenst follow the principle of properties like amImoderator... etc.

is sounds... a bit odd all of this.

but ok if i get a userList... how do i check if the user is "me"??
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I agree, it should be redesigned to be more consistent: (however it is not a show stopper) it is in our to-do list, so you can be assured that it will get nicer in a later release.
but ok if i get a userList... how do i check if the user is "me"??

Code: Select all

if (user.getId() == sfs.myUserId)
{
// code here
}
Lapo
--
gotoAndPlay()
...addicted to flash games
knegarpetter
Posts: 13
Joined: 11 Apr 2008, 10:25

Post by knegarpetter »

Ah, thanx!

have a nice W-E

P1r
Post Reply