2
0

RaidBossSpawnManager.java 14 KB

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