GrandBossManager.java 13 KB

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