ClanHallManager.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.Level;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastMap;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.datatables.ClanTable;
  25. import com.l2jserver.gameserver.model.L2Clan;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.StatsSet;
  28. import com.l2jserver.gameserver.model.entity.Auction;
  29. import com.l2jserver.gameserver.model.entity.ClanHall;
  30. import com.l2jserver.gameserver.model.entity.clanhall.AuctionableHall;
  31. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  32. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  33. /**
  34. * @author Steuf
  35. */
  36. public final class ClanHallManager
  37. {
  38. protected static final Logger _log = Logger.getLogger(ClanHallManager.class.getName());
  39. private Map<Integer, AuctionableHall> _clanHall;
  40. private Map<Integer, AuctionableHall> _freeClanHall;
  41. private Map<Integer, AuctionableHall> _allAuctionableClanHalls;
  42. private static Map<Integer, ClanHall> _allClanHalls = new FastMap<Integer, ClanHall>();
  43. private boolean _loaded = false;
  44. public static ClanHallManager getInstance()
  45. {
  46. return SingletonHolder._instance;
  47. }
  48. public boolean loaded()
  49. {
  50. return _loaded;
  51. }
  52. protected ClanHallManager()
  53. {
  54. _clanHall = new FastMap<Integer, AuctionableHall>();
  55. _freeClanHall = new FastMap<Integer, AuctionableHall>();
  56. _allAuctionableClanHalls = new FastMap<Integer, AuctionableHall>();
  57. load();
  58. }
  59. /** Reload All Clan Hall */
  60. /* public final void reload() Cant reload atm - would loose zone info
  61. {
  62. _clanHall.clear();
  63. _freeClanHall.clear();
  64. load();
  65. }
  66. */
  67. /** Load All Clan Hall */
  68. private final void load()
  69. {
  70. Connection con = null;
  71. try
  72. {
  73. int id, ownerId, lease;
  74. con = L2DatabaseFactory.getInstance().getConnection();
  75. PreparedStatement statement = con.prepareStatement("SELECT * FROM clanhall ORDER BY id");
  76. ResultSet rs = statement.executeQuery();
  77. while (rs.next())
  78. {
  79. StatsSet set = new StatsSet();
  80. id = rs.getInt("id");
  81. ownerId = rs.getInt("ownerId");
  82. lease = rs.getInt("lease");
  83. set.set("id", id);
  84. set.set("name", rs.getString("name"));
  85. set.set("ownerId", ownerId);
  86. set.set("lease", lease);
  87. set.set("desc", rs.getString("desc"));
  88. set.set("location", rs.getString("location"));
  89. set.set("paidUntil", rs.getLong("paidUntil"));
  90. set.set("grade", rs.getInt("Grade"));
  91. set.set("paid", rs.getBoolean("paid"));
  92. AuctionableHall ch = new AuctionableHall(set);
  93. _allAuctionableClanHalls.put(id, ch);
  94. addClanHall(ch);
  95. if (ch.getOwnerId() > 0)
  96. {
  97. _clanHall.put(id, ch);
  98. continue;
  99. }
  100. _freeClanHall.put(id, ch);
  101. Auction auc = AuctionManager.getInstance().getAuction(id);
  102. if (auc == null && lease > 0)
  103. AuctionManager.getInstance().initNPC(id);
  104. }
  105. rs.close();
  106. statement.close();
  107. _log.info("Loaded: " + getClanHalls().size() + " clan halls");
  108. _log.info("Loaded: " + getFreeClanHalls().size() + " free clan halls");
  109. _loaded = true;
  110. }
  111. catch (Exception e)
  112. {
  113. _log.log(Level.WARNING, "Exception: ClanHallManager.load(): " + e.getMessage(), e);
  114. }
  115. finally
  116. {
  117. L2DatabaseFactory.close(con);
  118. }
  119. }
  120. public static final Map<Integer, ClanHall> getAllClanHalls()
  121. {
  122. return _allClanHalls;
  123. }
  124. /**
  125. * @return all FreeClanHalls
  126. */
  127. public final Map<Integer, AuctionableHall> getFreeClanHalls()
  128. {
  129. return _freeClanHall;
  130. }
  131. /**
  132. * @return all ClanHalls that have owner
  133. */
  134. public final Map<Integer, AuctionableHall> getClanHalls()
  135. {
  136. return _clanHall;
  137. }
  138. /**
  139. * @return all ClanHalls
  140. */
  141. public final Map<Integer, AuctionableHall> getAllAuctionableClanHalls()
  142. {
  143. return _allAuctionableClanHalls;
  144. }
  145. public static final void addClanHall(ClanHall hall)
  146. {
  147. _allClanHalls.put(hall.getId(), hall);
  148. }
  149. /**
  150. * @param chId
  151. * @return true is free ClanHall
  152. */
  153. public final boolean isFree(int chId)
  154. {
  155. if (_freeClanHall.containsKey(chId))
  156. return true;
  157. return false;
  158. }
  159. /**
  160. * Free a ClanHall
  161. * @param chId
  162. */
  163. public final synchronized void setFree(int chId)
  164. {
  165. _freeClanHall.put(chId, _clanHall.get(chId));
  166. ClanTable.getInstance().getClan(_freeClanHall.get(chId).getOwnerId()).setHideoutId(0);
  167. _freeClanHall.get(chId).free();
  168. _clanHall.remove(chId);
  169. }
  170. /**
  171. * Set ClanHallOwner
  172. * @param chId
  173. * @param clan
  174. */
  175. public final synchronized void setOwner(int chId, L2Clan clan)
  176. {
  177. if (!_clanHall.containsKey(chId))
  178. {
  179. _clanHall.put(chId, _freeClanHall.get(chId));
  180. _freeClanHall.remove(chId);
  181. }
  182. else
  183. _clanHall.get(chId).free();
  184. ClanTable.getInstance().getClan(clan.getClanId()).setHideoutId(chId);
  185. _clanHall.get(chId).setOwner(clan);
  186. }
  187. /**
  188. * @param clanHallId
  189. * @return Clan Hall by Id
  190. */
  191. public final ClanHall getClanHallById(int clanHallId)
  192. {
  193. return _allClanHalls.get(clanHallId);
  194. }
  195. public final AuctionableHall getAuctionableHallById(int clanHallId)
  196. {
  197. return _allAuctionableClanHalls.get(clanHallId);
  198. }
  199. /**
  200. * @param x
  201. * @param y
  202. * @param z
  203. * @return Clan Hall by x,y,z
  204. */
  205. public final ClanHall getClanHall(int x, int y, int z)
  206. {
  207. for (ClanHall temp : getAllClanHalls().values())
  208. {
  209. if (temp.checkIfInZone(x, y, z))
  210. return temp;
  211. }
  212. return null;
  213. }
  214. public final ClanHall getClanHall(L2Object activeObject)
  215. {
  216. return getClanHall(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  217. }
  218. public final AuctionableHall getNearbyClanHall(int x, int y, int maxDist)
  219. {
  220. L2ClanHallZone zone = null;
  221. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  222. {
  223. zone = ch.getValue().getZone();
  224. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  225. return ch.getValue();
  226. }
  227. for (Map.Entry<Integer, AuctionableHall> ch : _freeClanHall.entrySet())
  228. {
  229. zone = ch.getValue().getZone();
  230. if (zone != null && zone.getDistanceToZone(x, y) < maxDist)
  231. return ch.getValue();
  232. }
  233. return null;
  234. }
  235. public final ClanHall getNearbyAbstractHall(int x, int y, int maxDist)
  236. {
  237. L2ClanHallZone zone = null;
  238. for(Map.Entry<Integer, ClanHall> ch : _allClanHalls.entrySet())
  239. {
  240. zone = ch.getValue().getZone();
  241. if(zone != null && zone.getDistanceToZone(x, y) < maxDist)
  242. return ch.getValue();
  243. }
  244. return null;
  245. }
  246. /**
  247. * @param clan
  248. * @return Clan Hall by Owner
  249. */
  250. public final AuctionableHall getClanHallByOwner(L2Clan clan)
  251. {
  252. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  253. {
  254. if (clan.getClanId() == ch.getValue().getOwnerId())
  255. return ch.getValue();
  256. }
  257. return null;
  258. }
  259. public final ClanHall getAbstractHallByOwner(L2Clan clan)
  260. {
  261. // Separate loops to avoid iterating over free clan halls
  262. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  263. {
  264. if (clan.getClanId() == ch.getValue().getOwnerId())
  265. return ch.getValue();
  266. }
  267. for(Map.Entry<Integer, SiegableHall> ch : CHSiegeManager.getInstance().getConquerableHalls().entrySet())
  268. {
  269. if(clan.getClanId() == ch.getValue().getOwnerId())
  270. return ch.getValue();
  271. }
  272. return null;
  273. }
  274. private static class SingletonHolder
  275. {
  276. protected static final ClanHallManager _instance = new ClanHallManager();
  277. }
  278. }