ClanHallManager.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  29. import com.l2jserver.gameserver.data.sql.impl.ClanTable;
  30. import com.l2jserver.gameserver.model.L2Clan;
  31. import com.l2jserver.gameserver.model.L2Object;
  32. import com.l2jserver.gameserver.model.StatsSet;
  33. import com.l2jserver.gameserver.model.entity.Auction;
  34. import com.l2jserver.gameserver.model.entity.ClanHall;
  35. import com.l2jserver.gameserver.model.entity.clanhall.AuctionableHall;
  36. import com.l2jserver.gameserver.model.entity.clanhall.SiegableHall;
  37. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  38. /**
  39. * @author Steuf
  40. */
  41. public final class ClanHallManager
  42. {
  43. protected static final Logger _log = Logger.getLogger(ClanHallManager.class.getName());
  44. private final Map<Integer, AuctionableHall> _clanHall = new ConcurrentHashMap<>();
  45. private final Map<Integer, AuctionableHall> _freeClanHall = new ConcurrentHashMap<>();
  46. private final Map<Integer, AuctionableHall> _allAuctionableClanHalls = new HashMap<>();
  47. private static Map<Integer, ClanHall> _allClanHalls = new HashMap<>();
  48. private boolean _loaded = false;
  49. public boolean loaded()
  50. {
  51. return _loaded;
  52. }
  53. protected ClanHallManager()
  54. {
  55. load();
  56. }
  57. /** Load All Clan Hall */
  58. private final void load()
  59. {
  60. try (Connection con = ConnectionFactory.getInstance().getConnection();
  61. Statement s = con.createStatement();
  62. ResultSet rs = s.executeQuery("SELECT * FROM clanhall ORDER BY id"))
  63. {
  64. int id, ownerId, lease;
  65. while (rs.next())
  66. {
  67. StatsSet set = new StatsSet();
  68. id = rs.getInt("id");
  69. ownerId = rs.getInt("ownerId");
  70. lease = rs.getInt("lease");
  71. set.set("id", id);
  72. set.set("name", rs.getString("name"));
  73. set.set("ownerId", ownerId);
  74. set.set("lease", lease);
  75. set.set("desc", rs.getString("desc"));
  76. set.set("location", rs.getString("location"));
  77. set.set("paidUntil", rs.getLong("paidUntil"));
  78. set.set("grade", rs.getInt("Grade"));
  79. set.set("paid", rs.getBoolean("paid"));
  80. AuctionableHall ch = new AuctionableHall(set);
  81. _allAuctionableClanHalls.put(id, ch);
  82. addClanHall(ch);
  83. if (ch.getOwnerId() > 0)
  84. {
  85. _clanHall.put(id, ch);
  86. continue;
  87. }
  88. _freeClanHall.put(id, ch);
  89. Auction auc = AuctionManager.getInstance().getAuction(id);
  90. if ((auc == null) && (lease > 0))
  91. {
  92. AuctionManager.getInstance().initNPC(id);
  93. }
  94. }
  95. _log.info(getClass().getSimpleName() + ": Loaded: " + getClanHalls().size() + " clan halls");
  96. _log.info(getClass().getSimpleName() + ": Loaded: " + getFreeClanHalls().size() + " free clan halls");
  97. _loaded = true;
  98. }
  99. catch (Exception e)
  100. {
  101. _log.log(Level.WARNING, "Exception: ClanHallManager.load(): " + e.getMessage(), e);
  102. }
  103. }
  104. public static final Map<Integer, ClanHall> getAllClanHalls()
  105. {
  106. return _allClanHalls;
  107. }
  108. /**
  109. * @return all FreeClanHalls
  110. */
  111. public final Map<Integer, AuctionableHall> getFreeClanHalls()
  112. {
  113. return _freeClanHall;
  114. }
  115. /**
  116. * @return all ClanHalls that have owner
  117. */
  118. public final Map<Integer, AuctionableHall> getClanHalls()
  119. {
  120. return _clanHall;
  121. }
  122. /**
  123. * @return all ClanHalls
  124. */
  125. public final Map<Integer, AuctionableHall> getAllAuctionableClanHalls()
  126. {
  127. return _allAuctionableClanHalls;
  128. }
  129. public static final void addClanHall(ClanHall hall)
  130. {
  131. _allClanHalls.put(hall.getId(), hall);
  132. }
  133. /**
  134. * @param chId
  135. * @return true is free ClanHall
  136. */
  137. public final boolean isFree(int chId)
  138. {
  139. if (_freeClanHall.containsKey(chId))
  140. {
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * Free a ClanHall
  147. * @param chId
  148. */
  149. public final synchronized void setFree(int chId)
  150. {
  151. _freeClanHall.put(chId, _clanHall.get(chId));
  152. ClanTable.getInstance().getClan(_freeClanHall.get(chId).getOwnerId()).setHideoutId(0);
  153. _freeClanHall.get(chId).free();
  154. _clanHall.remove(chId);
  155. }
  156. /**
  157. * Set ClanHallOwner
  158. * @param chId
  159. * @param clan
  160. */
  161. public final synchronized void setOwner(int chId, L2Clan clan)
  162. {
  163. if (!_clanHall.containsKey(chId))
  164. {
  165. _clanHall.put(chId, _freeClanHall.get(chId));
  166. _freeClanHall.remove(chId);
  167. }
  168. else
  169. {
  170. _clanHall.get(chId).free();
  171. }
  172. ClanTable.getInstance().getClan(clan.getId()).setHideoutId(chId);
  173. _clanHall.get(chId).setOwner(clan);
  174. }
  175. /**
  176. * @param clanHallId
  177. * @return Clan Hall by Id
  178. */
  179. public final ClanHall getClanHallById(int clanHallId)
  180. {
  181. return _allClanHalls.get(clanHallId);
  182. }
  183. public final AuctionableHall getAuctionableHallById(int clanHallId)
  184. {
  185. return _allAuctionableClanHalls.get(clanHallId);
  186. }
  187. /**
  188. * @param x
  189. * @param y
  190. * @param z
  191. * @return Clan Hall by x,y,z
  192. */
  193. public final ClanHall getClanHall(int x, int y, int z)
  194. {
  195. for (ClanHall temp : _allClanHalls.values())
  196. {
  197. if (temp.checkIfInZone(x, y, z))
  198. {
  199. return temp;
  200. }
  201. }
  202. return null;
  203. }
  204. public final ClanHall getClanHall(L2Object activeObject)
  205. {
  206. return getClanHall(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  207. }
  208. public final AuctionableHall getNearbyClanHall(int x, int y, int maxDist)
  209. {
  210. L2ClanHallZone zone = null;
  211. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  212. {
  213. zone = ch.getValue().getZone();
  214. if ((zone != null) && (zone.getDistanceToZone(x, y) < maxDist))
  215. {
  216. return ch.getValue();
  217. }
  218. }
  219. for (Map.Entry<Integer, AuctionableHall> ch : _freeClanHall.entrySet())
  220. {
  221. zone = ch.getValue().getZone();
  222. if ((zone != null) && (zone.getDistanceToZone(x, y) < maxDist))
  223. {
  224. return ch.getValue();
  225. }
  226. }
  227. return null;
  228. }
  229. public final ClanHall getNearbyAbstractHall(int x, int y, int maxDist)
  230. {
  231. L2ClanHallZone zone = null;
  232. for (Map.Entry<Integer, ClanHall> ch : _allClanHalls.entrySet())
  233. {
  234. zone = ch.getValue().getZone();
  235. if ((zone != null) && (zone.getDistanceToZone(x, y) < maxDist))
  236. {
  237. return ch.getValue();
  238. }
  239. }
  240. return null;
  241. }
  242. /**
  243. * @param clan
  244. * @return Clan Hall by Owner
  245. */
  246. public final AuctionableHall getClanHallByOwner(L2Clan clan)
  247. {
  248. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  249. {
  250. if (clan.getId() == ch.getValue().getOwnerId())
  251. {
  252. return ch.getValue();
  253. }
  254. }
  255. return null;
  256. }
  257. public final ClanHall getAbstractHallByOwner(L2Clan clan)
  258. {
  259. // Separate loops to avoid iterating over free clan halls
  260. for (Map.Entry<Integer, AuctionableHall> ch : _clanHall.entrySet())
  261. {
  262. if (clan.getId() == ch.getValue().getOwnerId())
  263. {
  264. return ch.getValue();
  265. }
  266. }
  267. for (Map.Entry<Integer, SiegableHall> ch : CHSiegeManager.getInstance().getConquerableHalls().entrySet())
  268. {
  269. if (clan.getId() == ch.getValue().getOwnerId())
  270. {
  271. return ch.getValue();
  272. }
  273. }
  274. return null;
  275. }
  276. public static ClanHallManager getInstance()
  277. {
  278. return SingletonHolder._instance;
  279. }
  280. private static class SingletonHolder
  281. {
  282. protected static final ClanHallManager _instance = new ClanHallManager();
  283. }
  284. }