All pastes #27543 Raw Edit

Unnamed

public text v1 · immutable
#27543 ·published 2005-11-03 22:21 UTC
rendered paste body
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package net.sf.l2j.gameserver;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.logging.Logger;

import net.sf.l2j.Config;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.idfactory.IdFactory;
import net.sf.l2j.gameserver.model.L2Alliance;
import net.sf.l2j.gameserver.model.L2PcInstance;

/**
 * This class ...
 * 
 * @version $Revision: 1.11.2.5.2.5 $ $Date: 2005/03/27 15:29:18 $
 */
public class AllianceTable
{
    private static Logger _log = Logger.getLogger(AllianceTable.class.getName());
    
    private static AllianceTable _instance;
    
    private HashMap<Integer, L2Alliance> _alliances;
    
    public static AllianceTable getInstance()
    {
        if (_instance == null)
        {
            _instance = new AllianceTable();
        }
        return _instance;
    }
    
    private AllianceTable()
    {
        _alliances = new HashMap<Integer, L2Alliance>();
    }
    
    /**
     * @param allyId
     * @return
     */
    public L2Alliance getAlliance(int allyId)
    {
        L2Alliance alliance = _alliances.get(new Integer(allyId));
        if (alliance == null)
        {
            alliance = new L2Alliance(allyId);
            if (alliance != null)
            {
                _alliances.put(alliance.getAllianceId(), alliance);
            }
        }
        
        return alliance; 
    }
    
    /**
     * @param player
     * @return NULL if alliance with same name already exists
     */
    public L2Alliance createAlliance(L2PcInstance player, String allianceName)
    {
        java.sql.Connection con = null;
        boolean allianceExists = true;
        L2Alliance alliance = null;
        
        try//store the new alliance in db
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement = con.prepareStatement("SELECT ally_id FROM alliance_data WHERE ally_name=?");
            statement.setString(1, allianceName);
            ResultSet result = statement.executeQuery();
            allianceExists = result.next(); 
            result.close();
            statement.close();
        }
        catch (Exception e)
        {
            _log.warning("Error while saving new alliance to db "+e);
        }
        finally
        {
            try { con.close(); } catch (Exception e) {}
        }

        if (!allianceExists)
        {
            // no alliance with same name exists
            alliance = new L2Alliance(IdFactory.getInstance().getNextId(), allianceName, player.getClan());
            alliance.store();
            player.setAlliance(alliance);

            if (Config.DEBUG) _log.fine("New alliances created: "+alliance.getAllianceId() + " " +alliance.getName());
            
            _alliances.put(new Integer(alliance.getAllianceId()), alliance);
        }
        
        return alliance;
    }
}