GrandBossManager.java 12 KB

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