/* * 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.model;import java.io.File;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.Map;import java.util.TreeMap;import java.util.logging.Logger;import net.sf.l2j.Config;import net.sf.l2j.L2DatabaseFactory;import net.sf.l2j.gameserver.serverpackets.PledgeShowMemberListAll;import net.sf.l2j.gameserver.serverpackets.PledgeStatusChanged;import net.sf.l2j.gameserver.serverpackets.ServerBasePacket;import net.sf.l2j.gameserver.serverpackets.SystemMessage;public class L2Alliance{ private static final Logger _log = Logger.getLogger(L2Clan.class.getName()); private String _name; private int _allyId; private L2Clan _leader; private boolean _hasCrest; private Map<String, L2Clan> _members = new TreeMap<String, L2Clan>(); /** * called if a alliance is referenced only by id. * in this case all other data needs to be fetched from db * @param allyId */ public L2Alliance (int allyId) { _allyId = allyId; restore(); File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Alliance_"+_allyId+".bmp"); _hasCrest = crestFile.exists(); } /** * this is only called if a new alliance is created * @param allyId * @param allianceName * @param leader */ public L2Alliance(int allyId, String allianceName, L2Clan leader) { _allyId = allyId; _name = allianceName; setLeader(leader); } /** * @return Returns the allianceId. */ public int getAllianceId() { return _allyId; } /** * @param allianceId The clanId to set. */ public void setAllianceId(int allianceId) { _allyId = allianceId; } /** * @return Returns the leaderId. */ public int getLeaderId() { return _leader.getClanId(); } /** * @param leaderId The leaderId to set. */ public void setLeader(L2Clan leader) { _leader = leader; _members.put(leader.getName(), leader); } /** * @return Returns the name. */ public String getName() { return _name; } /** * @param name The name to set. */ public void setName(String name) { _name = name; } public void addAllyMember(L2Clan newClan) { _members.put(newClan.getName(), newClan); // store in db storeNewMembersInDatabase(newClan); } private void storeNewMembersInDatabase(L2Clan member) { java.sql.Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET allyid=? WHERE obj_Id=?"); statement.setInt(1, getAllianceId()); statement.setInt(2, member.getClanId()); statement.execute(); statement.close(); if (Config.DEBUG) _log.fine("New alliance member saved in db: "+member.getClanId()); } catch (Exception e) { _log.warning("error while saving new clan member to db "+e); } finally { try { con.close(); } catch (Exception e) {} } } } public void removeAlliance() { broadcastToOnlineMembers(new SystemMessage(SystemMessage.CLAN_MEMBERSHIP_TERMINATED)); for (L2ClanMember member : getMembers()) { removeClanMember(member.getName()); } for (L2ItemInstance item : _warehouse.listItems()) { _warehouse.destroyItem(item.getItemId(),item.getCount()); } java.sql.Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("DELETE FROM clan_data WHERE clan_id=?"); statement.setInt(1, getClanId()); statement.execute(); statement.close(); if (Config.DEBUG) _log.fine("clan removed in db: "+getClanId()); } catch (Exception e) { _log.warning("error while removing clan in db "+e); } finally { try { con.close(); } catch (Exception e) {} } } public void store() { java.sql.Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("INSERT INTO alliance_data (alliance_id,alliance_name,leader_id) values (?,?,?)"); statement.setInt(1, getAllianceId()); statement.setString(2, getName()); statement.setInt(3, getLeaderId()); statement.execute(); statement.close(); statement = con.prepareStatement("UPDATE clan_table SET ally_id=? WHERE clan_id=?"); statement.setInt(1, getAllianceId()); statement.setInt(2, getLeaderId()); statement.execute(); statement.close(); if (Config.DEBUG) _log.fine("New alliance saved in db: "+getAllianceId()); } catch (Exception e) { _log.warning("error while saving new alliance to db "+e); } finally { try { con.close(); } catch (Exception e) {} } } private void restore() { java.sql.Connection con = null; try { L2ClanMember member; con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("SELECT alliance_name,leader_id FROM alliance_data where alliance_id=?"); statement.setInt(1, getAllianceId()); ResultSet allianceData = statement.executeQuery(); if (clanData.next()) { setName(allianceData.getString("alliance_name")); setLeader(allianceData.getInt("leader_id")); setHasCastle(clanData.getInt("hasCastle")); setHasHideout(clanData.getInt("hasHideout")); setAllyId(clanData.getInt("ally_id")); setAllyName(clanData.getString("ally_name")); int leaderId = (clanData.getInt("leader_id")); PreparedStatement statement2 = con.prepareStatement("SELECT char_name,level,classid,obj_Id FROM characters WHERE clanid=?"); statement2.setInt(1, getClanId()); ResultSet clanMembers = statement2.executeQuery(); while (clanMembers.next()) { member = new L2ClanMember(clanMembers.getString("char_name"), clanMembers.getInt("level"), clanMembers.getInt("classid"), clanMembers.getInt("obj_id")); if (member.getObjectId() == leaderId) { setLeader(member); } else { addClanMember(member); } } clanMembers.close(); statement2.close(); } clanData.close(); statement.close(); if (getName() != null) _log.config("Restored clan data for \"" + getName() + "\" from database."); } catch (Exception e) { _log.warning("error while restoring clan "+e); } finally { try { con.close(); } catch (Exception e) {} } } public void broadcastToOnlineMembers(ServerBasePacket packet) { }}