/*******************************************************************************************
 * 
 * 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 com.smartfoxserver.v2.SmartFoxServer;
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.entities.variables.RoomVariable;
import com.smartfoxserver.v2.entities.variables.SFSRoomVariable;
import com.smartfoxserver.v2.entities.variables.VariableType;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;


import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

import sfs2x.extensions.games.spacerace.SpaceRaceZoneExtension;

/**
 * SFS2X Space Race demo - OnUserExitGame Event Handler
 * Called when a user closes the application
 *  
 * @author A51 Integrated
 *
 */
public class OnUserExitGame extends BaseServerEventHandler
{	
	SpaceRaceZoneExtension zoneExt;
	Room curRoom;
	Zone curZone;
	
	boolean start = true;
	ISFSObject obj = new SFSObject();
	
	
	@Override
	public void handleServerEvent(ISFSEvent event)
	{ 		
    	// Obtain the user who fired the event and the room they are in
		User player = (User) event.getParameter(SFSEventParam.USER);
    	curRoom = (Room) event.getParameter(SFSEventParam.ROOM);

		// We set UserVariable before joining a room, if this is the case, ignore this event
		if(curRoom == null) return;
		
		// If the user closes the application while in a race we update the RoomVariables to make sure the ranking list is correct
		if(curRoom.isGame())
		{			
			List<RoomVariable> varList = curRoom.getVariables();
			
			Iterator<RoomVariable> iterator = varList.iterator();
			while (iterator.hasNext())
			{
				RoomVariable variable = iterator.next();
				if (variable.getType() == VariableType.OBJECT)
				{
					obj = (SFSObject) variable.getSFSObjectValue();
					
					if (obj.containsKey(SpaceRaceZoneExtension.PLAYER_NAME))
					{
						// Update the variable this user is associated with 
						if (obj.getUtfString(SpaceRaceZoneExtension.PLAYER_NAME).equalsIgnoreCase(player.getName()))
						{
							SFSObject new_obj = new SFSObject();
							new_obj.putUtfString(SpaceRaceZoneExtension.PLAYER_NAME, player.getName());
							new_obj.putInt("t", -1);
							
							RoomVariable var = new SFSRoomVariable(variable.getName(), new_obj);
							List<RoomVariable> listOfVars = new ArrayList<RoomVariable>();
							listOfVars.add(var);
							
							// Update the RoomVariables
							SmartFoxServer.getInstance().getAPIManager().getSFSApi().setRoomVariables(player, curRoom, listOfVars);
						}
					}
				}				
			}
		}
	}
}
