GrandBossManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright (C) 2004-2013 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.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Date;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastMap;
  30. import com.l2jserver.L2DatabaseFactory;
  31. import com.l2jserver.gameserver.datatables.NpcTable;
  32. import com.l2jserver.gameserver.model.L2Object;
  33. import com.l2jserver.gameserver.model.Location;
  34. import com.l2jserver.gameserver.model.StatsSet;
  35. import com.l2jserver.gameserver.model.actor.L2Character;
  36. import com.l2jserver.gameserver.model.actor.instance.L2GrandBossInstance;
  37. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  38. import com.l2jserver.gameserver.model.zone.type.L2BossZone;
  39. import com.l2jserver.util.L2FastList;
  40. import gnu.trove.map.hash.TIntIntHashMap;
  41. import gnu.trove.map.hash.TIntObjectHashMap;
  42. /**
  43. * @author DaRkRaGe Revised by Emperorc
  44. */
  45. public final class GrandBossManager
  46. {
  47. // SQL queries
  48. private static final String DELETE_GRAND_BOSS_LIST = "DELETE FROM grandboss_list";
  49. private static final String INSERT_GRAND_BOSS_LIST = "INSERT INTO grandboss_list (player_id,zone) VALUES (?,?)";
  50. private static final String UPDATE_GRAND_BOSS_DATA = "UPDATE grandboss_data set loc_x = ?, loc_y = ?, loc_z = ?, heading = ?, respawn_time = ?, currentHP = ?, currentMP = ?, status = ? where boss_id = ?";
  51. private static final String UPDATE_GRAND_BOSS_DATA2 = "UPDATE grandboss_data set status = ? where boss_id = ?";
  52. protected static Logger _log = Logger.getLogger(GrandBossManager.class.getName());
  53. protected static Map<Integer, L2GrandBossInstance> _bosses;
  54. protected static TIntObjectHashMap<StatsSet> _storedInfo;
  55. private TIntIntHashMap _bossStatus;
  56. private L2FastList<L2BossZone> _zones;
  57. protected GrandBossManager()
  58. {
  59. init();
  60. }
  61. private void init()
  62. {
  63. _zones = new L2FastList<>();
  64. _bosses = new FastMap<>();
  65. _storedInfo = new TIntObjectHashMap<>();
  66. _bossStatus = new TIntIntHashMap();
  67. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  68. Statement s = con.createStatement();
  69. ResultSet rs = s.executeQuery("SELECT * from grandboss_data ORDER BY boss_id"))
  70. {
  71. while (rs.next())
  72. {
  73. // Read all info from DB, and store it for AI to read and decide what to do
  74. // faster than accessing DB in real time
  75. StatsSet info = new StatsSet();
  76. int bossId = rs.getInt("boss_id");
  77. info.set("loc_x", rs.getInt("loc_x"));
  78. info.set("loc_y", rs.getInt("loc_y"));
  79. info.set("loc_z", rs.getInt("loc_z"));
  80. info.set("heading", rs.getInt("heading"));
  81. info.set("respawn_time", rs.getLong("respawn_time"));
  82. double HP = rs.getDouble("currentHP"); // jython doesn't recognize doubles
  83. int true_HP = (int) HP; // so use java's ability to type cast
  84. info.set("currentHP", true_HP); // to convert double to int
  85. double MP = rs.getDouble("currentMP");
  86. int true_MP = (int) MP;
  87. info.set("currentMP", true_MP);
  88. int status = rs.getInt("status");
  89. _bossStatus.put(bossId, status);
  90. _storedInfo.put(bossId, info);
  91. _log.info(getClass().getSimpleName() + ": " + NpcTable.getInstance().getTemplate(bossId).getName() + "(" + bossId + ") status is " + status + ".");
  92. if (status > 0)
  93. {
  94. _log.info(getClass().getSimpleName() + ": Next spawn date of " + NpcTable.getInstance().getTemplate(bossId).getName() + " is " + new Date(info.getLong("respawn_time")) + ".");
  95. }
  96. }
  97. _log.info(getClass().getSimpleName() + ": Loaded " + _storedInfo.size() + " Instances");
  98. }
  99. catch (SQLException e)
  100. {
  101. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not load grandboss_data table: " + e.getMessage(), e);
  102. }
  103. catch (Exception e)
  104. {
  105. _log.log(Level.WARNING, "Error while initializing GrandBossManager: " + e.getMessage(), e);
  106. }
  107. }
  108. /**
  109. * Zone Functions
  110. */
  111. public void initZones()
  112. {
  113. FastMap<Integer, L2FastList<Integer>> zones = new FastMap<>();
  114. if (_zones == null)
  115. {
  116. _log.warning(getClass().getSimpleName() + ": Could not read Grand Boss zone data");
  117. return;
  118. }
  119. for (L2BossZone zone : _zones)
  120. {
  121. if (zone == null)
  122. {
  123. continue;
  124. }
  125. zones.put(zone.getId(), new L2FastList<Integer>());
  126. }
  127. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  128. Statement s = con.createStatement();
  129. ResultSet rs = s.executeQuery("SELECT * from grandboss_list ORDER BY player_id"))
  130. {
  131. while (rs.next())
  132. {
  133. int id = rs.getInt("player_id");
  134. int zone_id = rs.getInt("zone");
  135. zones.get(zone_id).add(id);
  136. }
  137. _log.info(getClass().getSimpleName() + ": Initialized " + _zones.size() + " Grand Boss Zones");
  138. }
  139. catch (SQLException e)
  140. {
  141. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not load grandboss_list table: " + e.getMessage(), e);
  142. }
  143. catch (Exception e)
  144. {
  145. _log.log(Level.WARNING, "Error while initializing GrandBoss zones: " + e.getMessage(), e);
  146. }
  147. for (L2BossZone zone : _zones)
  148. {
  149. if (zone == null)
  150. {
  151. continue;
  152. }
  153. zone.setAllowedPlayers(zones.get(zone.getId()));
  154. }
  155. zones.clear();
  156. }
  157. public void addZone(L2BossZone zone)
  158. {
  159. if (_zones != null)
  160. {
  161. _zones.add(zone);
  162. }
  163. }
  164. public final L2BossZone getZone(int zoneId)
  165. {
  166. if (_zones != null)
  167. {
  168. for (L2BossZone temp : _zones)
  169. {
  170. if (temp.getId() == zoneId)
  171. {
  172. return temp;
  173. }
  174. }
  175. }
  176. return null;
  177. }
  178. public final L2BossZone getZone(L2Character character)
  179. {
  180. if (_zones != null)
  181. {
  182. for (L2BossZone temp : _zones)
  183. {
  184. if (temp.isCharacterInZone(character))
  185. {
  186. return temp;
  187. }
  188. }
  189. }
  190. return null;
  191. }
  192. public final L2BossZone getZone(Location loc)
  193. {
  194. return getZone(loc.getX(), loc.getY(), loc.getZ());
  195. }
  196. public final L2BossZone getZone(int x, int y, int z)
  197. {
  198. if (_zones != null)
  199. {
  200. for (L2BossZone temp : _zones)
  201. {
  202. if (temp.isInsideZone(x, y, z))
  203. {
  204. return temp;
  205. }
  206. }
  207. }
  208. return null;
  209. }
  210. public boolean checkIfInZone(String zoneType, L2Object obj)
  211. {
  212. L2BossZone temp = getZone(obj.getX(), obj.getY(), obj.getZ());
  213. if (temp == null)
  214. {
  215. return false;
  216. }
  217. return temp.getName().equalsIgnoreCase(zoneType);
  218. }
  219. public boolean checkIfInZone(L2PcInstance player)
  220. {
  221. if (player == null)
  222. {
  223. return false;
  224. }
  225. L2BossZone temp = getZone(player.getX(), player.getY(), player.getZ());
  226. if (temp == null)
  227. {
  228. return false;
  229. }
  230. return true;
  231. }
  232. public int getBossStatus(int bossId)
  233. {
  234. return _bossStatus.get(bossId);
  235. }
  236. public void setBossStatus(int bossId, int status)
  237. {
  238. _bossStatus.put(bossId, status);
  239. _log.info(getClass().getSimpleName() + ": Updated " + NpcTable.getInstance().getTemplate(bossId).getName() + "(" + bossId + ") status to " + status);
  240. updateDb(bossId, true);
  241. }
  242. /**
  243. * Adds a L2GrandBossInstance to the list of bosses.
  244. * @param boss
  245. */
  246. public void addBoss(L2GrandBossInstance boss)
  247. {
  248. if (boss != null)
  249. {
  250. _bosses.put(boss.getNpcId(), boss);
  251. }
  252. }
  253. public L2GrandBossInstance getBoss(int bossId)
  254. {
  255. return _bosses.get(bossId);
  256. }
  257. public StatsSet getStatsSet(int bossId)
  258. {
  259. return _storedInfo.get(bossId);
  260. }
  261. public void setStatsSet(int bossId, StatsSet info)
  262. {
  263. _storedInfo.put(bossId, info);
  264. updateDb(bossId, false);
  265. }
  266. private void storeToDb()
  267. {
  268. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  269. PreparedStatement delete = con.prepareStatement(DELETE_GRAND_BOSS_LIST))
  270. {
  271. delete.executeUpdate();
  272. try (PreparedStatement insert = con.prepareStatement(INSERT_GRAND_BOSS_LIST))
  273. {
  274. for (L2BossZone zone : _zones)
  275. {
  276. if (zone == null)
  277. {
  278. continue;
  279. }
  280. Integer id = zone.getId();
  281. L2FastList<Integer> list = zone.getAllowedPlayers();
  282. if ((list == null) || list.isEmpty())
  283. {
  284. continue;
  285. }
  286. for (Integer player : list)
  287. {
  288. insert.setInt(1, player);
  289. insert.setInt(2, id);
  290. insert.executeUpdate();
  291. insert.clearParameters();
  292. }
  293. }
  294. }
  295. for (Integer bossId : _storedInfo.keys())
  296. {
  297. final L2GrandBossInstance boss = _bosses.get(bossId);
  298. StatsSet info = _storedInfo.get(bossId);
  299. if ((boss == null) || (info == null))
  300. {
  301. try (PreparedStatement update = con.prepareStatement(UPDATE_GRAND_BOSS_DATA2))
  302. {
  303. update.setInt(1, _bossStatus.get(bossId));
  304. update.setInt(2, bossId);
  305. update.executeUpdate();
  306. update.clearParameters();
  307. }
  308. }
  309. else
  310. {
  311. try (PreparedStatement update = con.prepareStatement(UPDATE_GRAND_BOSS_DATA))
  312. {
  313. update.setInt(1, boss.getX());
  314. update.setInt(2, boss.getY());
  315. update.setInt(3, boss.getZ());
  316. update.setInt(4, boss.getHeading());
  317. update.setLong(5, info.getLong("respawn_time"));
  318. double hp = boss.getCurrentHp();
  319. double mp = boss.getCurrentMp();
  320. if (boss.isDead())
  321. {
  322. hp = boss.getMaxHp();
  323. mp = boss.getMaxMp();
  324. }
  325. update.setDouble(6, hp);
  326. update.setDouble(7, mp);
  327. update.setInt(8, _bossStatus.get(bossId));
  328. update.setInt(9, bossId);
  329. update.executeUpdate();
  330. update.clearParameters();
  331. }
  332. }
  333. }
  334. }
  335. catch (SQLException e)
  336. {
  337. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store grandbosses to database:" + e.getMessage(), e);
  338. }
  339. }
  340. private void updateDb(int bossId, boolean statusOnly)
  341. {
  342. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  343. {
  344. L2GrandBossInstance boss = _bosses.get(bossId);
  345. StatsSet info = _storedInfo.get(bossId);
  346. if (statusOnly || (boss == null) || (info == null))
  347. {
  348. try (PreparedStatement ps = con.prepareStatement(UPDATE_GRAND_BOSS_DATA2))
  349. {
  350. ps.setInt(1, _bossStatus.get(bossId));
  351. ps.setInt(2, bossId);
  352. ps.executeUpdate();
  353. }
  354. }
  355. else
  356. {
  357. try (PreparedStatement ps = con.prepareStatement(UPDATE_GRAND_BOSS_DATA))
  358. {
  359. ps.setInt(1, boss.getX());
  360. ps.setInt(2, boss.getY());
  361. ps.setInt(3, boss.getZ());
  362. ps.setInt(4, boss.getHeading());
  363. ps.setLong(5, info.getLong("respawn_time"));
  364. double hp = boss.getCurrentHp();
  365. double mp = boss.getCurrentMp();
  366. if (boss.isDead())
  367. {
  368. hp = boss.getMaxHp();
  369. mp = boss.getMaxMp();
  370. }
  371. ps.setDouble(6, hp);
  372. ps.setDouble(7, mp);
  373. ps.setInt(8, _bossStatus.get(bossId));
  374. ps.setInt(9, bossId);
  375. ps.executeUpdate();
  376. }
  377. }
  378. }
  379. catch (SQLException e)
  380. {
  381. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update grandbosses to database:" + e.getMessage(), e);
  382. }
  383. }
  384. /**
  385. * Saves all Grand Boss info and then clears all info from memory, including all schedules.
  386. */
  387. public void cleanUp()
  388. {
  389. storeToDb();
  390. _bosses.clear();
  391. _storedInfo.clear();
  392. _bossStatus.clear();
  393. _zones.clear();
  394. }
  395. public L2FastList<L2BossZone> getZones()
  396. {
  397. return _zones;
  398. }
  399. /**
  400. * Gets the single instance of {@code GrandBossManager}.
  401. * @return single instance of {@code GrandBossManager}
  402. */
  403. public static GrandBossManager getInstance()
  404. {
  405. return SingletonHolder._instance;
  406. }
  407. private static class SingletonHolder
  408. {
  409. protected static final GrandBossManager _instance = new GrandBossManager();
  410. }
  411. }