RaidBossSpawnManager.java 14 KB

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