CHSiegeManager.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.Map;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastMap;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.model.L2Clan;
  25. import com.l2jserver.gameserver.model.StatsSet;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.entity.clanhall.ClanHallSiegeEngine;
  29. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  30. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  31. import com.l2jserver.gameserver.network.SystemMessageId;
  32. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  33. /**
  34. * @author BiggBoss
  35. */
  36. public final class CHSiegeManager
  37. {
  38. private static final Logger _log = Logger.getLogger(CHSiegeManager.class.getName());
  39. private static final String SQL_LOAD_HALLS = "SELECT * FROM siegable_clanhall";
  40. private final FastMap<Integer, SiegableHall> _siegableHalls = new FastMap<>();
  41. protected CHSiegeManager()
  42. {
  43. loadClanHalls();
  44. }
  45. private final void loadClanHalls()
  46. {
  47. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  48. {
  49. PreparedStatement statement = con.prepareStatement(SQL_LOAD_HALLS);
  50. ResultSet rs = statement.executeQuery();
  51. _siegableHalls.clear();
  52. while (rs.next())
  53. {
  54. final int id = rs.getInt("clanHallId");
  55. StatsSet set = new StatsSet();
  56. set.set("id", id);
  57. set.set("name", rs.getString("name"));
  58. set.set("ownerId", rs.getInt("ownerId"));
  59. set.set("desc", rs.getString("desc"));
  60. set.set("location", rs.getString("location"));
  61. set.set("nextSiege", rs.getLong("nextSiege"));
  62. set.set("siegeLenght", rs.getLong("siegeLenght"));
  63. set.set("scheduleConfig", rs.getString("schedule_config"));
  64. SiegableHall hall = new SiegableHall(set);
  65. _siegableHalls.put(id, hall);
  66. ClanHallManager.addClanHall(hall);
  67. }
  68. _log.info(getClass().getSimpleName() + ": Loaded " + _siegableHalls.size() + " conquerable clan halls.");
  69. rs.close();
  70. statement.close();
  71. }
  72. catch (Exception e)
  73. {
  74. _log.warning("CHSiegeManager: Could not load siegable clan halls!:");
  75. }
  76. }
  77. public FastMap<Integer, SiegableHall> getConquerableHalls()
  78. {
  79. return _siegableHalls;
  80. }
  81. public SiegableHall getSiegableHall(int clanHall)
  82. {
  83. return getConquerableHalls().get(clanHall);
  84. }
  85. public final SiegableHall getNearbyClanHall(L2Character activeChar)
  86. {
  87. return getNearbyClanHall(activeChar.getX(), activeChar.getY(), 10000);
  88. }
  89. public final SiegableHall getNearbyClanHall(int x, int y, int maxDist)
  90. {
  91. L2ClanHallZone zone = null;
  92. for (Map.Entry<Integer, SiegableHall> ch : _siegableHalls.entrySet())
  93. {
  94. zone = ch.getValue().getZone();
  95. if ((zone != null) && (zone.getDistanceToZone(x, y) < maxDist))
  96. {
  97. return ch.getValue();
  98. }
  99. }
  100. return null;
  101. }
  102. public final ClanHallSiegeEngine getSiege(L2Character character)
  103. {
  104. SiegableHall hall = getNearbyClanHall(character);
  105. if (hall == null)
  106. {
  107. return null;
  108. }
  109. return hall.getSiege();
  110. }
  111. public final void registerClan(L2Clan clan, SiegableHall hall, L2PcInstance player)
  112. {
  113. if (clan.getLevel() < Config.CHS_CLAN_MINLEVEL)
  114. {
  115. player.sendMessage("Only clans of level " + Config.CHS_CLAN_MINLEVEL + " or higher may register for a castle siege");
  116. }
  117. else if (hall.isWaitingBattle())
  118. {
  119. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.DEADLINE_FOR_SIEGE_S1_PASSED);
  120. sm.addString(hall.getName());
  121. player.sendPacket(sm);
  122. }
  123. else if (hall.isInSiege())
  124. {
  125. player.sendPacket(SystemMessageId.NOT_SIEGE_REGISTRATION_TIME2);
  126. }
  127. else if (hall.getOwnerId() == clan.getClanId())
  128. {
  129. player.sendPacket(SystemMessageId.CLAN_THAT_OWNS_CASTLE_IS_AUTOMATICALLY_REGISTERED_DEFENDING);
  130. }
  131. else if ((clan.getCastleId() != 0) || (clan.getHideoutId() != 0))
  132. {
  133. player.sendPacket(SystemMessageId.CLAN_THAT_OWNS_CASTLE_CANNOT_PARTICIPATE_OTHER_SIEGE);
  134. }
  135. else if (hall.getSiege().checkIsAttacker(clan))
  136. {
  137. player.sendPacket(SystemMessageId.ALREADY_REQUESTED_SIEGE_BATTLE);
  138. }
  139. else if (isClanParticipating(clan))
  140. {
  141. player.sendPacket(SystemMessageId.APPLICATION_DENIED_BECAUSE_ALREADY_SUBMITTED_A_REQUEST_FOR_ANOTHER_SIEGE_BATTLE);
  142. }
  143. else if (hall.getSiege().getAttackers().size() >= Config.CHS_MAX_ATTACKERS)
  144. {
  145. player.sendPacket(SystemMessageId.ATTACKER_SIDE_FULL);
  146. }
  147. else
  148. {
  149. hall.addAttacker(clan);
  150. }
  151. }
  152. public final void unRegisterClan(L2Clan clan, SiegableHall hall)
  153. {
  154. if (!hall.isRegistering())
  155. {
  156. return;
  157. }
  158. hall.removeAttacker(clan);
  159. }
  160. public final boolean isClanParticipating(L2Clan clan)
  161. {
  162. for (SiegableHall hall : getConquerableHalls().values())
  163. {
  164. if ((hall.getSiege() != null) && hall.getSiege().checkIsAttacker(clan))
  165. {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. public final void onServerShutDown()
  172. {
  173. for (SiegableHall hall : getConquerableHalls().values())
  174. {
  175. // Rainbow springs has his own attackers table
  176. if ((hall.getId() == 62) || (hall.getSiege() == null))
  177. {
  178. continue;
  179. }
  180. hall.getSiege().saveAttackers();
  181. }
  182. }
  183. public static CHSiegeManager getInstance()
  184. {
  185. return SingletonHolder._instance;
  186. }
  187. private static final class SingletonHolder
  188. {
  189. protected static final CHSiegeManager _instance = new CHSiegeManager();
  190. }
  191. }