InstanceManager.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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.util.HashMap;
  20. import java.util.Map;
  21. import javolution.util.FastList;
  22. import javolution.util.FastMap;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.entity.Instance;
  29. /**
  30. * @author evill33t, GodKratos
  31. */
  32. public class InstanceManager extends DocumentParser
  33. {
  34. private static final FastMap<Integer, Instance> _instanceList = new FastMap<>();
  35. private final FastMap<Integer, InstanceWorld> _instanceWorlds = new FastMap<>();
  36. private int _dynamic = 300000;
  37. // InstanceId Names
  38. private final static Map<Integer, String> _instanceIdNames = new HashMap<>();
  39. private final Map<Integer, Map<Integer, Long>> _playerInstanceTimes = new FastMap<>();
  40. private static final String ADD_INSTANCE_TIME = "INSERT INTO character_instance_time (charId,instanceId,time) values (?,?,?) ON DUPLICATE KEY UPDATE time=?";
  41. private static final String RESTORE_INSTANCE_TIMES = "SELECT instanceId,time FROM character_instance_time WHERE charId=?";
  42. private static final String DELETE_INSTANCE_TIME = "DELETE FROM character_instance_time WHERE charId=? AND instanceId=?";
  43. protected InstanceManager()
  44. {
  45. // Creates the multiverse.
  46. final Instance multiverse = new Instance(-1);
  47. multiverse.setName("multiverse");
  48. _instanceList.put(-1, multiverse);
  49. _log.info(getClass().getSimpleName() + ": Multiverse Instance created.");
  50. // Creates the universe.
  51. final Instance universe = new Instance(0);
  52. universe.setName("universe");
  53. _instanceList.put(0, universe);
  54. _log.info(getClass().getSimpleName() + ": Universe Instance created.");
  55. load();
  56. }
  57. @Override
  58. public void load()
  59. {
  60. _instanceIdNames.clear();
  61. parseDatapackFile("data/instancenames.xml");
  62. _log.info(getClass().getSimpleName() + ": Loaded " + _instanceIdNames.size() + " instance names.");
  63. }
  64. /**
  65. * @param playerObjId
  66. * @param id
  67. * @return
  68. */
  69. public long getInstanceTime(int playerObjId, int id)
  70. {
  71. if (!_playerInstanceTimes.containsKey(playerObjId))
  72. {
  73. restoreInstanceTimes(playerObjId);
  74. }
  75. if (_playerInstanceTimes.get(playerObjId).containsKey(id))
  76. {
  77. return _playerInstanceTimes.get(playerObjId).get(id);
  78. }
  79. return -1;
  80. }
  81. /**
  82. * @param playerObjId
  83. * @return
  84. */
  85. public Map<Integer, Long> getAllInstanceTimes(int playerObjId)
  86. {
  87. if (!_playerInstanceTimes.containsKey(playerObjId))
  88. {
  89. restoreInstanceTimes(playerObjId);
  90. }
  91. return _playerInstanceTimes.get(playerObjId);
  92. }
  93. /**
  94. * @param playerObjId
  95. * @param id
  96. * @param time
  97. */
  98. public void setInstanceTime(int playerObjId, int id, long time)
  99. {
  100. if (!_playerInstanceTimes.containsKey(playerObjId))
  101. {
  102. restoreInstanceTimes(playerObjId);
  103. }
  104. Connection con = null;
  105. try
  106. {
  107. con = L2DatabaseFactory.getInstance().getConnection();
  108. PreparedStatement statement = con.prepareStatement(ADD_INSTANCE_TIME);
  109. statement.setInt(1, playerObjId);
  110. statement.setInt(2, id);
  111. statement.setLong(3, time);
  112. statement.setLong(4, time);
  113. statement.execute();
  114. statement.close();
  115. _playerInstanceTimes.get(playerObjId).put(id, time);
  116. }
  117. catch (Exception e)
  118. {
  119. _log.warning(getClass().getSimpleName() + ": Could not insert character instance time data: " + e.getMessage());
  120. }
  121. finally
  122. {
  123. L2DatabaseFactory.close(con);
  124. }
  125. }
  126. /**
  127. * @param playerObjId
  128. * @param id
  129. */
  130. public void deleteInstanceTime(int playerObjId, int id)
  131. {
  132. Connection con = null;
  133. try
  134. {
  135. con = L2DatabaseFactory.getInstance().getConnection();
  136. PreparedStatement statement = con.prepareStatement(DELETE_INSTANCE_TIME);
  137. statement.setInt(1, playerObjId);
  138. statement.setInt(2, id);
  139. statement.execute();
  140. statement.close();
  141. _playerInstanceTimes.get(playerObjId).remove(id);
  142. }
  143. catch (Exception e)
  144. {
  145. _log.warning(getClass().getSimpleName() + ": Could not delete character instance time data: " + e.getMessage());
  146. }
  147. finally
  148. {
  149. L2DatabaseFactory.close(con);
  150. }
  151. }
  152. /**
  153. * @param playerObjId
  154. */
  155. public void restoreInstanceTimes(int playerObjId)
  156. {
  157. if (_playerInstanceTimes.containsKey(playerObjId))
  158. {
  159. return; // already restored
  160. }
  161. _playerInstanceTimes.put(playerObjId, new FastMap<Integer, Long>());
  162. Connection con = null;
  163. try
  164. {
  165. con = L2DatabaseFactory.getInstance().getConnection();
  166. PreparedStatement statement = con.prepareStatement(RESTORE_INSTANCE_TIMES);
  167. statement.setInt(1, playerObjId);
  168. ResultSet rset = statement.executeQuery();
  169. while (rset.next())
  170. {
  171. int id = rset.getInt("instanceId");
  172. long time = rset.getLong("time");
  173. if (time < System.currentTimeMillis())
  174. {
  175. deleteInstanceTime(playerObjId, id);
  176. }
  177. else
  178. {
  179. _playerInstanceTimes.get(playerObjId).put(id, time);
  180. }
  181. }
  182. rset.close();
  183. statement.close();
  184. }
  185. catch (Exception e)
  186. {
  187. _log.warning(getClass().getSimpleName() + ": Could not delete character instance time data: " + e.getMessage());
  188. }
  189. finally
  190. {
  191. L2DatabaseFactory.close(con);
  192. }
  193. }
  194. /**
  195. * @param id
  196. * @return
  197. */
  198. public String getInstanceIdName(int id)
  199. {
  200. if (_instanceIdNames.containsKey(id))
  201. {
  202. return _instanceIdNames.get(id);
  203. }
  204. return ("UnknownInstance");
  205. }
  206. @Override
  207. protected void parseDocument()
  208. {
  209. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  210. {
  211. if ("list".equals(n.getNodeName()))
  212. {
  213. NamedNodeMap attrs;
  214. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  215. {
  216. if ("instance".equals(d.getNodeName()))
  217. {
  218. attrs = d.getAttributes();
  219. _instanceIdNames.put(parseInteger(attrs, "id"), attrs.getNamedItem("name").getNodeValue());
  220. }
  221. }
  222. }
  223. }
  224. }
  225. public static class InstanceWorld
  226. {
  227. public int instanceId;
  228. public int templateId = -1;
  229. public FastList<Integer> allowed = new FastList<Integer>();
  230. public volatile int status;
  231. }
  232. /**
  233. * @param world
  234. */
  235. public void addWorld(InstanceWorld world)
  236. {
  237. _instanceWorlds.put(world.instanceId, world);
  238. }
  239. /**
  240. * @param instanceId
  241. * @return
  242. */
  243. public InstanceWorld getWorld(int instanceId)
  244. {
  245. return _instanceWorlds.get(instanceId);
  246. }
  247. /**
  248. * @param player
  249. * @return
  250. */
  251. public InstanceWorld getPlayerWorld(L2PcInstance player)
  252. {
  253. for (InstanceWorld temp : _instanceWorlds.values())
  254. {
  255. if (temp == null)
  256. {
  257. continue;
  258. }
  259. // check if the player have a World Instance where he/she is allowed to enter
  260. if (temp.allowed.contains(player.getObjectId()))
  261. {
  262. return temp;
  263. }
  264. }
  265. return null;
  266. }
  267. /**
  268. * @param instanceid
  269. */
  270. public void destroyInstance(int instanceid)
  271. {
  272. if (instanceid <= 0)
  273. {
  274. return;
  275. }
  276. Instance temp = _instanceList.get(instanceid);
  277. if (temp != null)
  278. {
  279. temp.removeNpcs();
  280. temp.removePlayers();
  281. temp.removeDoors();
  282. temp.cancelTimer();
  283. _instanceList.remove(instanceid);
  284. if (_instanceWorlds.containsKey(instanceid))
  285. {
  286. _instanceWorlds.remove(instanceid);
  287. }
  288. }
  289. }
  290. /**
  291. * @param instanceid
  292. * @return
  293. */
  294. public Instance getInstance(int instanceid)
  295. {
  296. return _instanceList.get(instanceid);
  297. }
  298. /**
  299. * @return
  300. */
  301. public FastMap<Integer, Instance> getInstances()
  302. {
  303. return _instanceList;
  304. }
  305. /**
  306. * @param objectId
  307. * @return
  308. */
  309. public int getPlayerInstance(int objectId)
  310. {
  311. for (Instance temp : _instanceList.values())
  312. {
  313. if (temp == null)
  314. {
  315. continue;
  316. }
  317. // check if the player is in any active instance
  318. if (temp.containsPlayer(objectId))
  319. {
  320. return temp.getId();
  321. }
  322. }
  323. // 0 is default instance aka the world
  324. return 0;
  325. }
  326. /**
  327. * @param id
  328. * @return
  329. */
  330. public boolean createInstance(int id)
  331. {
  332. if (getInstance(id) != null)
  333. {
  334. return false;
  335. }
  336. Instance instance = new Instance(id);
  337. _instanceList.put(id, instance);
  338. return true;
  339. }
  340. /**
  341. * @param id
  342. * @param template
  343. * @return
  344. */
  345. public boolean createInstanceFromTemplate(int id, String template)
  346. {
  347. if (getInstance(id) != null)
  348. {
  349. return false;
  350. }
  351. Instance instance = new Instance(id);
  352. _instanceList.put(id, instance);
  353. instance.loadInstanceTemplate(template);
  354. return true;
  355. }
  356. /**
  357. * Create a new instance with a dynamic instance id based on a template (or null)
  358. * @param template xml file
  359. * @return
  360. */
  361. public int createDynamicInstance(String template)
  362. {
  363. while (getInstance(_dynamic) != null)
  364. {
  365. _dynamic++;
  366. if (_dynamic == Integer.MAX_VALUE)
  367. {
  368. _log.warning(getClass().getSimpleName() + ": More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  369. _dynamic = 300000;
  370. }
  371. }
  372. Instance instance = new Instance(_dynamic);
  373. _instanceList.put(_dynamic, instance);
  374. if (template != null)
  375. {
  376. instance.loadInstanceTemplate(template);
  377. }
  378. return _dynamic;
  379. }
  380. public static final InstanceManager getInstance()
  381. {
  382. return SingletonHolder._instance;
  383. }
  384. private static class SingletonHolder
  385. {
  386. protected static final InstanceManager _instance = new InstanceManager();
  387. }
  388. }