GrandBossManager.java 13 KB

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