RaidBossSpawnManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Calendar;
  26. import java.util.Map;
  27. import java.util.concurrent.ConcurrentHashMap;
  28. import java.util.concurrent.ScheduledFuture;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  33. import com.l2jserver.gameserver.ThreadPoolManager;
  34. import com.l2jserver.gameserver.datatables.SpawnTable;
  35. import com.l2jserver.gameserver.model.L2Spawn;
  36. import com.l2jserver.gameserver.model.StatsSet;
  37. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  38. import com.l2jserver.util.Rnd;
  39. /**
  40. * Raid Boss spawn manager.
  41. * @author godson
  42. */
  43. public class RaidBossSpawnManager
  44. {
  45. private static final Logger _log = Logger.getLogger(RaidBossSpawnManager.class.getName());
  46. protected static final Map<Integer, L2RaidBossInstance> _bosses = new ConcurrentHashMap<>();
  47. protected static final Map<Integer, L2Spawn> _spawns = new ConcurrentHashMap<>();
  48. protected static final Map<Integer, StatsSet> _storedInfo = new ConcurrentHashMap<>();
  49. protected static final Map<Integer, ScheduledFuture<?>> _schedules = new ConcurrentHashMap<>();
  50. public static enum StatusEnum
  51. {
  52. ALIVE,
  53. DEAD,
  54. UNDEFINED
  55. }
  56. /**
  57. * Instantiates a new raid boss spawn manager.
  58. */
  59. protected RaidBossSpawnManager()
  60. {
  61. load();
  62. }
  63. /**
  64. * Load.
  65. */
  66. public void load()
  67. {
  68. _bosses.clear();
  69. _spawns.clear();
  70. _storedInfo.clear();
  71. _schedules.clear();
  72. try (Connection con = ConnectionFactory.getInstance().getConnection();
  73. Statement s = con.createStatement();
  74. ResultSet rs = s.executeQuery("SELECT * FROM raidboss_spawnlist ORDER BY boss_id"))
  75. {
  76. while (rs.next())
  77. {
  78. final L2Spawn spawnDat = new L2Spawn(rs.getInt("boss_id"));
  79. spawnDat.setX(rs.getInt("loc_x"));
  80. spawnDat.setY(rs.getInt("loc_y"));
  81. spawnDat.setZ(rs.getInt("loc_z"));
  82. spawnDat.setAmount(rs.getInt("amount"));
  83. spawnDat.setHeading(rs.getInt("heading"));
  84. spawnDat.setRespawnDelay(rs.getInt("respawn_delay"), rs.getInt("respawn_random"));
  85. addNewSpawn(spawnDat, rs.getLong("respawn_time"), rs.getDouble("currentHP"), rs.getDouble("currentMP"), false);
  86. }
  87. _log.info(getClass().getSimpleName() + ": Loaded " + _bosses.size() + " Instances");
  88. _log.info(getClass().getSimpleName() + ": Scheduled " + _schedules.size() + " Instances");
  89. }
  90. catch (SQLException e)
  91. {
  92. _log.warning(getClass().getSimpleName() + ": Couldnt load raidboss_spawnlist table");
  93. }
  94. catch (Exception e)
  95. {
  96. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while initializing RaidBossSpawnManager: " + e.getMessage(), e);
  97. }
  98. }
  99. private static class SpawnSchedule implements Runnable
  100. {
  101. private static final Logger _log = Logger.getLogger(SpawnSchedule.class.getName());
  102. private final int bossId;
  103. /**
  104. * Instantiates a new spawn schedule.
  105. * @param npcId the npc id
  106. */
  107. public SpawnSchedule(int npcId)
  108. {
  109. bossId = npcId;
  110. }
  111. @Override
  112. public void run()
  113. {
  114. L2RaidBossInstance raidboss = null;
  115. if (bossId == 25328)
  116. {
  117. raidboss = DayNightSpawnManager.getInstance().handleBoss(_spawns.get(bossId));
  118. }
  119. else
  120. {
  121. raidboss = (L2RaidBossInstance) _spawns.get(bossId).doSpawn();
  122. }
  123. if (raidboss != null)
  124. {
  125. raidboss.setRaidStatus(StatusEnum.ALIVE);
  126. final StatsSet info = new StatsSet();
  127. info.set("currentHP", raidboss.getCurrentHp());
  128. info.set("currentMP", raidboss.getCurrentMp());
  129. info.set("respawnTime", 0L);
  130. _storedInfo.put(bossId, info);
  131. _log.info(getClass().getSimpleName() + ": Spawning Raid Boss " + raidboss.getName());
  132. _bosses.put(bossId, raidboss);
  133. }
  134. _schedules.remove(bossId);
  135. }
  136. }
  137. /**
  138. * Update status.
  139. * @param boss the boss
  140. * @param isBossDead the is boss dead
  141. */
  142. public void updateStatus(L2RaidBossInstance boss, boolean isBossDead)
  143. {
  144. final StatsSet info = _storedInfo.get(boss.getId());
  145. if (info == null)
  146. {
  147. return;
  148. }
  149. if (isBossDead)
  150. {
  151. boss.setRaidStatus(StatusEnum.DEAD);
  152. final int respawnMinDelay = (int) (boss.getSpawn().getRespawnMinDelay() * Config.RAID_MIN_RESPAWN_MULTIPLIER);
  153. final int respawnMaxDelay = (int) (boss.getSpawn().getRespawnMaxDelay() * Config.RAID_MAX_RESPAWN_MULTIPLIER);
  154. final int respawnDelay = Rnd.get(respawnMinDelay, respawnMaxDelay);
  155. final long respawnTime = Calendar.getInstance().getTimeInMillis() + respawnDelay;
  156. info.set("currentHP", boss.getMaxHp());
  157. info.set("currentMP", boss.getMaxMp());
  158. info.set("respawnTime", respawnTime);
  159. if (!_schedules.containsKey(boss.getId()) && ((respawnMinDelay > 0) || (respawnMaxDelay > 0)))
  160. {
  161. final Calendar time = Calendar.getInstance();
  162. time.setTimeInMillis(respawnTime);
  163. _log.info(getClass().getSimpleName() + ": Updated " + boss.getName() + " respawn time to " + time.getTime());
  164. _schedules.put(boss.getId(), ThreadPoolManager.getInstance().scheduleGeneral(new SpawnSchedule(boss.getId()), respawnDelay));
  165. updateDb();
  166. }
  167. }
  168. else
  169. {
  170. boss.setRaidStatus(StatusEnum.ALIVE);
  171. info.set("currentHP", boss.getCurrentHp());
  172. info.set("currentMP", boss.getCurrentMp());
  173. info.set("respawnTime", 0L);
  174. }
  175. _storedInfo.put(boss.getId(), info);
  176. }
  177. /**
  178. * Adds the new spawn.
  179. * @param spawnDat the spawn dat
  180. * @param respawnTime the respawn time
  181. * @param currentHP the current hp
  182. * @param currentMP the current mp
  183. * @param storeInDb the store in db
  184. */
  185. public void addNewSpawn(L2Spawn spawnDat, long respawnTime, double currentHP, double currentMP, boolean storeInDb)
  186. {
  187. if (spawnDat == null)
  188. {
  189. return;
  190. }
  191. if (_spawns.containsKey(spawnDat.getId()))
  192. {
  193. return;
  194. }
  195. final int bossId = spawnDat.getId();
  196. final long time = Calendar.getInstance().getTimeInMillis();
  197. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  198. if ((respawnTime == 0L) || (time > respawnTime))
  199. {
  200. L2RaidBossInstance raidboss = null;
  201. if (bossId == 25328)
  202. {
  203. raidboss = DayNightSpawnManager.getInstance().handleBoss(spawnDat);
  204. }
  205. else
  206. {
  207. raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  208. }
  209. if (raidboss != null)
  210. {
  211. raidboss.setCurrentHp(currentHP);
  212. raidboss.setCurrentMp(currentMP);
  213. raidboss.setRaidStatus(StatusEnum.ALIVE);
  214. _bosses.put(bossId, raidboss);
  215. final StatsSet info = new StatsSet();
  216. info.set("currentHP", currentHP);
  217. info.set("currentMP", currentMP);
  218. info.set("respawnTime", 0L);
  219. _storedInfo.put(bossId, info);
  220. }
  221. }
  222. else
  223. {
  224. final long spawnTime = respawnTime - Calendar.getInstance().getTimeInMillis();
  225. _schedules.put(bossId, ThreadPoolManager.getInstance().scheduleGeneral(new SpawnSchedule(bossId), spawnTime));
  226. }
  227. _spawns.put(bossId, spawnDat);
  228. if (storeInDb)
  229. {
  230. try (Connection con = ConnectionFactory.getInstance().getConnection();
  231. PreparedStatement ps = con.prepareStatement("INSERT INTO raidboss_spawnlist (boss_id,amount,loc_x,loc_y,loc_z,heading,respawn_time,currentHp,currentMp) VALUES(?,?,?,?,?,?,?,?,?)"))
  232. {
  233. ps.setInt(1, spawnDat.getId());
  234. ps.setInt(2, spawnDat.getAmount());
  235. ps.setInt(3, spawnDat.getX());
  236. ps.setInt(4, spawnDat.getY());
  237. ps.setInt(5, spawnDat.getZ());
  238. ps.setInt(6, spawnDat.getHeading());
  239. ps.setLong(7, respawnTime);
  240. ps.setDouble(8, currentHP);
  241. ps.setDouble(9, currentMP);
  242. ps.execute();
  243. }
  244. catch (Exception e)
  245. {
  246. // problem with storing spawn
  247. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not store raidboss #" + bossId + " in the DB:" + e.getMessage(), e);
  248. }
  249. }
  250. }
  251. /**
  252. * Delete spawn.
  253. * @param spawnDat the spawn dat
  254. * @param updateDb the update db
  255. */
  256. public void deleteSpawn(L2Spawn spawnDat, boolean updateDb)
  257. {
  258. if (spawnDat == null)
  259. {
  260. return;
  261. }
  262. final int bossId = spawnDat.getId();
  263. if (!_spawns.containsKey(bossId))
  264. {
  265. return;
  266. }
  267. SpawnTable.getInstance().deleteSpawn(spawnDat, false);
  268. _spawns.remove(bossId);
  269. if (_bosses.containsKey(bossId))
  270. {
  271. _bosses.remove(bossId);
  272. }
  273. if (_schedules.containsKey(bossId))
  274. {
  275. final ScheduledFuture<?> f = _schedules.remove(bossId);
  276. f.cancel(true);
  277. }
  278. if (_storedInfo.containsKey(bossId))
  279. {
  280. _storedInfo.remove(bossId);
  281. }
  282. if (updateDb)
  283. {
  284. try (Connection con = ConnectionFactory.getInstance().getConnection();
  285. PreparedStatement ps = con.prepareStatement("DELETE FROM raidboss_spawnlist WHERE boss_id=?"))
  286. {
  287. ps.setInt(1, bossId);
  288. ps.execute();
  289. }
  290. catch (Exception e)
  291. {
  292. // problem with deleting spawn
  293. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not remove raidboss #" + bossId + " from DB: " + e.getMessage(), e);
  294. }
  295. }
  296. }
  297. /**
  298. * Update database.
  299. */
  300. private void updateDb()
  301. {
  302. try (Connection con = ConnectionFactory.getInstance().getConnection();
  303. PreparedStatement ps = con.prepareStatement("UPDATE raidboss_spawnlist SET respawn_time = ?, currentHP = ?, currentMP = ? WHERE boss_id = ?"))
  304. {
  305. for (Integer bossId : _storedInfo.keySet())
  306. {
  307. if (bossId == null)
  308. {
  309. continue;
  310. }
  311. L2RaidBossInstance boss = _bosses.get(bossId);
  312. if (boss == null)
  313. {
  314. continue;
  315. }
  316. if (boss.getRaidStatus().equals(StatusEnum.ALIVE))
  317. {
  318. updateStatus(boss, false);
  319. }
  320. StatsSet info = _storedInfo.get(bossId);
  321. if (info == null)
  322. {
  323. continue;
  324. }
  325. try
  326. {
  327. // TODO(Zoey76): Change this to use batch.
  328. ps.setLong(1, info.getLong("respawnTime"));
  329. ps.setDouble(2, info.getDouble("currentHP"));
  330. ps.setDouble(3, info.getDouble("currentMP"));
  331. ps.setInt(4, bossId);
  332. ps.executeUpdate();
  333. ps.clearParameters();
  334. }
  335. catch (SQLException e)
  336. {
  337. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldnt update raidboss_spawnlist table " + e.getMessage(), e);
  338. }
  339. }
  340. }
  341. catch (SQLException e)
  342. {
  343. _log.log(Level.WARNING, getClass().getSimpleName() + ": SQL error while updating RaidBoss spawn to database: " + e.getMessage(), e);
  344. }
  345. }
  346. /**
  347. * Gets the all raid boss status.
  348. * @return the all raid boss status
  349. */
  350. public String[] getAllRaidBossStatus()
  351. {
  352. final String[] msg = new String[(_bosses == null) ? 0 : _bosses.size()];
  353. if (_bosses == null)
  354. {
  355. msg[0] = "None";
  356. return msg;
  357. }
  358. int index = 0;
  359. for (int i : _bosses.keySet())
  360. {
  361. L2RaidBossInstance boss = _bosses.get(i);
  362. msg[index++] = boss.getName() + ": " + boss.getRaidStatus().name();
  363. }
  364. return msg;
  365. }
  366. /**
  367. * Gets the raid boss status.
  368. * @param bossId the boss id
  369. * @return the raid boss status
  370. */
  371. public String getRaidBossStatus(int bossId)
  372. {
  373. String msg = "RaidBoss Status..." + Config.EOL;
  374. if (_bosses == null)
  375. {
  376. msg += "None";
  377. return msg;
  378. }
  379. if (_bosses.containsKey(bossId))
  380. {
  381. final L2RaidBossInstance boss = _bosses.get(bossId);
  382. msg += boss.getName() + ": " + boss.getRaidStatus().name();
  383. }
  384. return msg;
  385. }
  386. /**
  387. * Gets the raid boss status id.
  388. * @param bossId the boss id
  389. * @return the raid boss status id
  390. */
  391. public StatusEnum getRaidBossStatusId(int bossId)
  392. {
  393. if (_bosses.containsKey(bossId))
  394. {
  395. return _bosses.get(bossId).getRaidStatus();
  396. }
  397. else if (_schedules.containsKey(bossId))
  398. {
  399. return StatusEnum.DEAD;
  400. }
  401. else
  402. {
  403. return StatusEnum.UNDEFINED;
  404. }
  405. }
  406. /**
  407. * Notify spawn night boss.
  408. * @param raidboss the raidboss
  409. */
  410. public void notifySpawnNightBoss(L2RaidBossInstance raidboss)
  411. {
  412. final StatsSet info = new StatsSet();
  413. info.set("currentHP", raidboss.getCurrentHp());
  414. info.set("currentMP", raidboss.getCurrentMp());
  415. info.set("respawnTime", 0L);
  416. raidboss.setRaidStatus(StatusEnum.ALIVE);
  417. _storedInfo.put(raidboss.getId(), info);
  418. _log.info(getClass().getSimpleName() + ": Spawning Night Raid Boss " + raidboss.getName());
  419. _bosses.put(raidboss.getId(), raidboss);
  420. }
  421. /**
  422. * Checks if the boss is defined.
  423. * @param bossId the boss id
  424. * @return {@code true} if is defined
  425. */
  426. public boolean isDefined(int bossId)
  427. {
  428. return _spawns.containsKey(bossId);
  429. }
  430. /**
  431. * Gets the bosses.
  432. * @return the bosses
  433. */
  434. public Map<Integer, L2RaidBossInstance> getBosses()
  435. {
  436. return _bosses;
  437. }
  438. /**
  439. * Gets the spawns.
  440. * @return the spawns
  441. */
  442. public Map<Integer, L2Spawn> getSpawns()
  443. {
  444. return _spawns;
  445. }
  446. /**
  447. * Gets the stored info.
  448. * @return the stored info
  449. */
  450. public Map<Integer, StatsSet> getStoredInfo()
  451. {
  452. return _storedInfo;
  453. }
  454. /**
  455. * Saves and clears the raid bosses status, including all schedules.
  456. */
  457. public void cleanUp()
  458. {
  459. updateDb();
  460. _bosses.clear();
  461. if (_schedules != null)
  462. {
  463. for (Integer bossId : _schedules.keySet())
  464. {
  465. ScheduledFuture<?> f = _schedules.get(bossId);
  466. f.cancel(true);
  467. }
  468. _schedules.clear();
  469. }
  470. _storedInfo.clear();
  471. _spawns.clear();
  472. }
  473. /**
  474. * Gets the single instance of RaidBossSpawnManager.
  475. * @return single instance of RaidBossSpawnManager
  476. */
  477. public static RaidBossSpawnManager getInstance()
  478. {
  479. return SingletonHolder._instance;
  480. }
  481. private static class SingletonHolder
  482. {
  483. protected static final RaidBossSpawnManager _instance = new RaidBossSpawnManager();
  484. }
  485. }