RaidBossSpawnManager.java 14 KB

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