GrandBossManager.java 12 KB

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