/*******************************************************************************************
 * 
 * TITLE: 		SFS2X Space Race
 * VERSION:		1.0
 * RELEASE:		2011-03-15
 * COPYRIGHT:	2011 gotoAndPlay() - http://www.smartfoxserver.com
 * DEVELOPER:	A51 Integrated - http://a51integrated.com
 * 
 * This file is part of SFS2X Space Race.
 * 
 * Contributers: Wayne Helman, Fabricio Medeiros, David Irons,
 * 				 Steve Schoger, Ryan Albrecht, Andy Rohan
 * 
 * SFS2X Space Race is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * included license for more details.
 *
 * You are not allowed to rent, lend, lease, license or distribute SFS2X Space Race or a
 * modified version of SFS2X Space Race to any other person or organization in any way.
 * 
 * For commercial licensing information, please contact gotoAndPlay().
 * 
 *******************************************************************************************/

package sfs2x.extensions.games.spacerace.handlers;

import sfs2x.extensions.games.spacerace.SpaceRaceZoneExtension;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.Zone;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;


/**
 * SFS2X Space Race demo - OnUserDIsconnect Event Handler
 * Called when a user disconnects from the server
 *  
 * @author A51 Integrated
 *
 */
public class OnUserDisconnect extends BaseServerEventHandler
{	
	SpaceRaceZoneExtension zoneExt;
	Room curRoom;
	Zone curZone;
	
	ISFSObject obj = new SFSObject();
	
	
	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException
	{		
    	// Obtain the user who fired the event and the room they are in
		User player = (User) event.getParameter(SFSEventParam.USER);
		
		// Obtain a reference to the parent extension and the current Zone
		zoneExt = (SpaceRaceZoneExtension) getParentExtension();
		curZone = zoneExt.getZone();
		
		Room lobby = curZone.getRoomByName(SpaceRaceZoneExtension.LOBBY_ROOM);
		
		// Notify all users in the lobby of the user who has disconnected
		obj.putUtfString(SpaceRaceZoneExtension.PLAYER_NAME, player.getName());
		zoneExt.send(SpaceRaceZoneExtension.USER_DISCONNECT, obj, lobby.getUserList(), false);		
	}
}
