GrandBossManager.java 13 KB

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