package sfs3.extensions.examples.spacewar2.handlers.event;

import com.smartfoxserver.core.ISFSEvent;
import com.smartfoxserver.core.SFSEventParam;
import com.smartfoxserver.entities.Room;
import com.smartfoxserver.entities.User;
import com.smartfoxserver.exceptions.SFSException;
import com.smartfoxserver.extensions.BaseServerEventHandler;

import sfs3.extensions.examples.spacewar2.SW2RoomExtension;
import sfs3.extensions.examples.spacewar2.util.Constants;

public class UserLeaveRoomEventHandler extends BaseServerEventHandler
{ 
	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException
	{
		SW2RoomExtension roomExt = (SW2RoomExtension) this.getParentExtension();
		Room room = roomExt.getParentRoom();
		
		User user = (User) event.getParameter(SFSEventParam.USER);
		boolean gameRunning = room.getVariable(Constants.ROOMVAR_GAME_STATE).getIntValue() == Constants.GAMESTATE_RUNNING;
		
		// If the game is running, remove user starship from game, ignore otherwise
		if (gameRunning)
			roomExt.getGame().removeStarshipOnPlayerGone(user.getId());
	}
}