RaidBossSpawnManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 net.sf.l2j.gameserver.instancemanager;
  16. /**
  17. @author godson
  18. **/
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import java.util.Calendar;
  24. import java.util.Map;
  25. import java.util.concurrent.ScheduledFuture;
  26. import java.util.logging.Logger;
  27. import javolution.util.FastMap;
  28. import net.sf.l2j.Config;
  29. import net.sf.l2j.L2DatabaseFactory;
  30. import net.sf.l2j.gameserver.GmListTable;
  31. import net.sf.l2j.gameserver.ThreadPoolManager;
  32. import net.sf.l2j.gameserver.datatables.NpcTable;
  33. import net.sf.l2j.gameserver.datatables.SpawnTable;
  34. import net.sf.l2j.gameserver.model.L2Spawn;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
  36. import net.sf.l2j.gameserver.templates.StatsSet;
  37. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  38. import net.sf.l2j.util.Rnd;
  39. public class RaidBossSpawnManager
  40. {
  41. private static Logger _log = Logger.getLogger(RaidBossSpawnManager.class.getName());
  42. protected static Map<Integer, L2RaidBossInstance> _bosses;
  43. protected static Map<Integer, L2Spawn> _spawns;
  44. protected static Map<Integer, StatsSet> _storedInfo;
  45. protected static Map<Integer, ScheduledFuture<?>> _schedules;
  46. public static enum StatusEnum
  47. {
  48. ALIVE,
  49. DEAD,
  50. UNDEFINED
  51. }
  52. private RaidBossSpawnManager()
  53. {
  54. init();
  55. }
  56. public static RaidBossSpawnManager getInstance()
  57. {
  58. return SingletonHolder._instance;
  59. }
  60. private void init()
  61. {
  62. _bosses = new FastMap<Integer, L2RaidBossInstance>();
  63. _schedules = new FastMap<Integer, ScheduledFuture<?>>();
  64. _storedInfo = new FastMap<Integer, StatsSet>();
  65. _spawns = new FastMap<Integer, L2Spawn>();
  66. Connection con = null;
  67. try
  68. {
  69. con = L2DatabaseFactory.getInstance().getConnection();
  70. PreparedStatement statement = con.prepareStatement("SELECT * from raidboss_spawnlist ORDER BY boss_id");
  71. ResultSet rset = statement.executeQuery();
  72. L2Spawn spawnDat;
  73. L2NpcTemplate template;
  74. long respawnTime;
  75. while (rset.next())
  76. {
  77. template = getValidTemplate(rset.getInt("boss_id"));
  78. if (template != null)
  79. {
  80. spawnDat = new L2Spawn(template);
  81. spawnDat.setLocx(rset.getInt("loc_x"));
  82. spawnDat.setLocy(rset.getInt("loc_y"));
  83. spawnDat.setLocz(rset.getInt("loc_z"));
  84. spawnDat.setAmount(rset.getInt("amount"));
  85. spawnDat.setHeading(rset.getInt("heading"));
  86. spawnDat.setRespawnMinDelay(rset.getInt("respawn_min_delay"));
  87. spawnDat.setRespawnMaxDelay(rset.getInt("respawn_max_delay"));
  88. respawnTime = rset.getLong("respawn_time");
  89. addNewSpawn(spawnDat, respawnTime, rset.getDouble("currentHP"), rset.getDouble("currentMP"), false);
  90. }
  91. else
  92. {
  93. _log.warning("RaidBossSpawnManager: Could not load raidboss #" + rset.getInt("boss_id") + " from DB");
  94. }
  95. }
  96. _log.info("RaidBossSpawnManager: Loaded " + _bosses.size() + " Instances");
  97. _log.info("RaidBossSpawnManager: Scheduled " + _schedules.size() + " Instances");
  98. rset.close();
  99. statement.close();
  100. }
  101. catch (SQLException e)
  102. {
  103. _log.warning("RaidBossSpawnManager: Couldnt load raidboss_spawnlist table");
  104. }
  105. catch (Exception e)
  106. {
  107. e.printStackTrace();
  108. }
  109. finally
  110. {
  111. try
  112. {
  113. con.close();
  114. }
  115. catch (Exception e)
  116. {
  117. e.printStackTrace();
  118. }
  119. }
  120. }
  121. private class spawnSchedule implements Runnable
  122. {
  123. private int bossId;
  124. public spawnSchedule(int npcId)
  125. {
  126. bossId = npcId;
  127. }
  128. public void run()
  129. {
  130. L2RaidBossInstance raidboss = null;
  131. if (bossId == 25328)
  132. raidboss = DayNightSpawnManager.getInstance().handleBoss(_spawns.get(bossId));
  133. else
  134. raidboss = (L2RaidBossInstance) _spawns.get(bossId).doSpawn();
  135. if (raidboss != null)
  136. {
  137. raidboss.setRaidStatus(StatusEnum.ALIVE);
  138. StatsSet info = new StatsSet();
  139. info.set("currentHP", raidboss.getCurrentHp());
  140. info.set("currentMP", raidboss.getCurrentMp());
  141. info.set("respawnTime", 0L);
  142. _storedInfo.put(bossId, info);
  143. GmListTable.broadcastMessageToGMs("Spawning Raid Boss " + raidboss.getName());
  144. _bosses.put(bossId, raidboss);
  145. }
  146. _schedules.remove(bossId);
  147. }
  148. }
  149. public void updateStatus(L2RaidBossInstance boss, boolean isBossDead)
  150. {
  151. if (!_storedInfo.containsKey(boss.getNpcId()))
  152. return;
  153. StatsSet info = _storedInfo.get(boss.getNpcId());
  154. if (isBossDead)
  155. {
  156. boss.setRaidStatus(StatusEnum.DEAD);
  157. long respawnTime;
  158. int RespawnMinDelay = boss.getSpawn().getRespawnMinDelay();
  159. int RespawnMaxDelay = boss.getSpawn().getRespawnMaxDelay();
  160. long respawn_delay = Rnd.get((int) (RespawnMinDelay * 1000 * Config.RAID_MIN_RESPAWN_MULTIPLIER), (int) (RespawnMaxDelay * 1000 * Config.RAID_MAX_RESPAWN_MULTIPLIER));
  161. respawnTime = Calendar.getInstance().getTimeInMillis() + respawn_delay;
  162. info.set("currentHP", boss.getMaxHp());
  163. info.set("currentMP", boss.getMaxMp());
  164. info.set("respawnTime", respawnTime);
  165. _log.info("RaidBossSpawnManager: Updated " + boss.getName() + " respawn time to " + respawnTime);
  166. if (!_schedules.containsKey(boss.getNpcId()))
  167. {
  168. ScheduledFuture<?> futureSpawn;
  169. futureSpawn = ThreadPoolManager.getInstance().scheduleGeneral(new spawnSchedule(boss.getNpcId()), respawn_delay);
  170. _schedules.put(boss.getNpcId(), futureSpawn);
  171. //To update immediately Database uncomment on the following line, to post the hour of respawn raid boss on your site for example or to envisage a crash landing of the waiter.
  172. updateDb();
  173. }
  174. }
  175. else
  176. {
  177. boss.setRaidStatus(StatusEnum.ALIVE);
  178. info.set("currentHP", boss.getCurrentHp());
  179. info.set("currentMP", boss.getCurrentMp());
  180. info.set("respawnTime", 0L);
  181. }
  182. _storedInfo.remove(boss.getNpcId());
  183. _storedInfo.put(boss.getNpcId(), info);
  184. }
  185. public void addNewSpawn(L2Spawn spawnDat, long respawnTime, double currentHP, double currentMP, boolean storeInDb)
  186. {
  187. if (spawnDat == null)
  188. return;
  189. if (_spawns.containsKey(spawnDat.getNpcid()))
  190. return;
  191. int bossId = spawnDat.getNpcid();
  192. long time = Calendar.getInstance().getTimeInMillis();
  193. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  194. if (respawnTime == 0L || (time > respawnTime))
  195. {
  196. L2RaidBossInstance raidboss = null;
  197. if (bossId == 25328)
  198. raidboss = DayNightSpawnManager.getInstance().handleBoss(spawnDat);
  199. else
  200. raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  201. if (raidboss != null)
  202. {
  203. raidboss.setCurrentHp(currentHP);
  204. raidboss.setCurrentMp(currentMP);
  205. raidboss.setRaidStatus(StatusEnum.ALIVE);
  206. _bosses.put(bossId, raidboss);
  207. StatsSet info = new StatsSet();
  208. info.set("currentHP", currentHP);
  209. info.set("currentMP", currentMP);
  210. info.set("respawnTime", 0L);
  211. _storedInfo.put(bossId, info);
  212. }
  213. }
  214. else
  215. {
  216. ScheduledFuture<?> futureSpawn;
  217. long spawnTime = respawnTime - Calendar.getInstance().getTimeInMillis();
  218. futureSpawn = ThreadPoolManager.getInstance().scheduleGeneral(new spawnSchedule(bossId), spawnTime);
  219. _schedules.put(bossId, futureSpawn);
  220. }
  221. _spawns.put(bossId, spawnDat);
  222. if (storeInDb)
  223. {
  224. Connection con = null;
  225. try
  226. {
  227. con = L2DatabaseFactory.getInstance().getConnection();
  228. PreparedStatement statement = con.prepareStatement("INSERT INTO raidboss_spawnlist (boss_id,amount,loc_x,loc_y,loc_z,heading,respawn_time,currentHp,currentMp) values(?,?,?,?,?,?,?,?,?)");
  229. statement.setInt(1, spawnDat.getNpcid());
  230. statement.setInt(2, spawnDat.getAmount());
  231. statement.setInt(3, spawnDat.getLocx());
  232. statement.setInt(4, spawnDat.getLocy());
  233. statement.setInt(5, spawnDat.getLocz());
  234. statement.setInt(6, spawnDat.getHeading());
  235. statement.setLong(7, respawnTime);
  236. statement.setDouble(8, currentHP);
  237. statement.setDouble(9, currentMP);
  238. statement.execute();
  239. statement.close();
  240. }
  241. catch (Exception e)
  242. {
  243. // problem with storing spawn
  244. _log.warning("RaidBossSpawnManager: Could not store raidboss #" + bossId + " in the DB:" + e);
  245. }
  246. finally
  247. {
  248. try
  249. {
  250. con.close();
  251. }
  252. catch (Exception e)
  253. {
  254. }
  255. }
  256. }
  257. }
  258. public void deleteSpawn(L2Spawn spawnDat, boolean updateDb)
  259. {
  260. if (spawnDat == null)
  261. return;
  262. if (!_spawns.containsKey(spawnDat.getNpcid()))
  263. return;
  264. int bossId = spawnDat.getNpcid();
  265. SpawnTable.getInstance().deleteSpawn(spawnDat, false);
  266. _spawns.remove(bossId);
  267. if (_bosses.containsKey(bossId))
  268. _bosses.remove(bossId);
  269. if (_schedules.containsKey(bossId))
  270. {
  271. ScheduledFuture<?> f = _schedules.get(bossId);
  272. f.cancel(true);
  273. _schedules.remove(bossId);
  274. }
  275. if (_storedInfo.containsKey(bossId))
  276. _storedInfo.remove(bossId);
  277. if (updateDb)
  278. {
  279. Connection con = null;
  280. try
  281. {
  282. con = L2DatabaseFactory.getInstance().getConnection();
  283. PreparedStatement statement = con.prepareStatement("DELETE FROM raidboss_spawnlist WHERE boss_id=?");
  284. statement.setInt(1, bossId);
  285. statement.execute();
  286. statement.close();
  287. }
  288. catch (Exception e)
  289. {
  290. // problem with deleting spawn
  291. _log.warning("RaidBossSpawnManager: Could not remove raidboss #" + bossId + " from DB: " + e);
  292. }
  293. finally
  294. {
  295. try
  296. {
  297. con.close();
  298. }
  299. catch (Exception e)
  300. {
  301. }
  302. }
  303. }
  304. }
  305. private void updateDb()
  306. {
  307. for (Integer bossId : _storedInfo.keySet())
  308. {
  309. Connection con = null;
  310. try
  311. {
  312. con = L2DatabaseFactory.getInstance().getConnection();
  313. L2RaidBossInstance boss = _bosses.get(bossId);
  314. if (boss == null)
  315. continue;
  316. if (boss.getRaidStatus().equals(StatusEnum.ALIVE))
  317. updateStatus(boss, false);
  318. StatsSet info = _storedInfo.get(bossId);
  319. if (info == null)
  320. continue;
  321. PreparedStatement statement = con.prepareStatement("UPDATE raidboss_spawnlist set respawn_time = ?, currentHP = ?, currentMP = ? where boss_id = ?");
  322. statement.setLong(1, info.getLong("respawnTime"));
  323. statement.setDouble(2, info.getDouble("currentHP"));
  324. statement.setDouble(3, info.getDouble("currentMP"));
  325. statement.setInt(4, bossId);
  326. statement.execute();
  327. statement.close();
  328. }
  329. catch (SQLException e)
  330. {
  331. _log.warning("RaidBossSpawnManager: Couldnt update raidboss_spawnlist table");
  332. }
  333. finally
  334. {
  335. try
  336. {
  337. con.close();
  338. }
  339. catch (Exception e)
  340. {
  341. e.printStackTrace();
  342. }
  343. }
  344. }
  345. }
  346. public String[] getAllRaidBossStatus()
  347. {
  348. String[] msg = new String[_bosses == null ? 0 : _bosses.size()];
  349. if (_bosses == null)
  350. {
  351. msg[0] = "None";
  352. return msg;
  353. }
  354. int index = 0;
  355. for (int i : _bosses.keySet())
  356. {
  357. L2RaidBossInstance boss = _bosses.get(i);
  358. msg[index++] = boss.getName() + ": " + boss.getRaidStatus().name();
  359. }
  360. return msg;
  361. }
  362. public String getRaidBossStatus(int bossId)
  363. {
  364. String msg = "RaidBoss Status....\n";
  365. if (_bosses == null)
  366. {
  367. msg += "None";
  368. return msg;
  369. }
  370. if (_bosses.containsKey(bossId))
  371. {
  372. L2RaidBossInstance boss = _bosses.get(bossId);
  373. msg += boss.getName() + ": " + boss.getRaidStatus().name();
  374. }
  375. return msg;
  376. }
  377. public StatusEnum getRaidBossStatusId(int bossId)
  378. {
  379. if (_bosses.containsKey(bossId))
  380. return _bosses.get(bossId).getRaidStatus();
  381. else if (_schedules.containsKey(bossId))
  382. return StatusEnum.DEAD;
  383. else
  384. return StatusEnum.UNDEFINED;
  385. }
  386. public L2NpcTemplate getValidTemplate(int bossId)
  387. {
  388. L2NpcTemplate template = NpcTable.getInstance().getTemplate(bossId);
  389. if (template == null)
  390. return null;
  391. if (!template.type.equalsIgnoreCase("L2RaidBoss"))
  392. return null;
  393. return template;
  394. }
  395. public void notifySpawnNightBoss(L2RaidBossInstance raidboss)
  396. {
  397. StatsSet info = new StatsSet();
  398. info.set("currentHP", raidboss.getCurrentHp());
  399. info.set("currentMP", raidboss.getCurrentMp());
  400. info.set("respawnTime", 0L);
  401. raidboss.setRaidStatus(StatusEnum.ALIVE);
  402. _storedInfo.put(raidboss.getNpcId(), info);
  403. GmListTable.broadcastMessageToGMs("Spawning Raid Boss " + raidboss.getName());
  404. _bosses.put(raidboss.getNpcId(), raidboss);
  405. }
  406. public boolean isDefined(int bossId)
  407. {
  408. return _spawns.containsKey(bossId);
  409. }
  410. public Map<Integer, L2RaidBossInstance> getBosses()
  411. {
  412. return _bosses;
  413. }
  414. public Map<Integer, L2Spawn> getSpawns()
  415. {
  416. return _spawns;
  417. }
  418. public void reloadBosses()
  419. {
  420. init();
  421. }
  422. /**
  423. * Saves all raidboss status and then clears all info from memory,
  424. * including all schedules.
  425. */
  426. public void cleanUp()
  427. {
  428. updateDb();
  429. _bosses.clear();
  430. if (_schedules != null)
  431. {
  432. for (Integer bossId : _schedules.keySet())
  433. {
  434. ScheduledFuture<?> f = _schedules.get(bossId);
  435. f.cancel(true);
  436. }
  437. }
  438. _schedules.clear();
  439. _storedInfo.clear();
  440. _spawns.clear();
  441. }
  442. @SuppressWarnings("synthetic-access")
  443. private static class SingletonHolder
  444. {
  445. protected static final RaidBossSpawnManager _instance = new RaidBossSpawnManager();
  446. }
  447. }