package sfs2x.extensions.games.spacewar2.game.handlers.event;

import sfs2x.extensions.games.spacewar2.game.SW2RoomExtension;
import sfs2x.extensions.games.spacewar2.shared.util.Constants;

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

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());
	}
}