RaidBossSpawnManager.java 14 KB

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