RaidBossSpawnManager.java 16 KB

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