OlympiadManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 net.sf.l2j.gameserver.model.olympiad;
  16. import java.util.Map;
  17. import java.util.logging.Logger;
  18. import javolution.util.FastMap;
  19. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  20. import net.sf.l2j.gameserver.model.olympiad.Olympiad.COMP_TYPE;
  21. import net.sf.l2j.gameserver.model.olympiad.OlympiadGame;
  22. import net.sf.l2j.gameserver.model.olympiad.OlympiadGameTask;
  23. import net.sf.l2j.util.L2FastList;
  24. import net.sf.l2j.util.Rnd;
  25. /**
  26. *
  27. * @author GodKratos
  28. */
  29. class OlympiadManager implements Runnable
  30. {
  31. protected static final Logger _log = Logger.getLogger(OlympiadManager.class.getName());
  32. private Map<Integer, OlympiadGame> _olympiadInstances;
  33. protected static OlympiadManager _instance;
  34. protected static final OlympiadStadium[] STADIUMS = {
  35. new OlympiadStadium(-88000, -252637, -3331, 17100001, 17100002),
  36. new OlympiadStadium(-83760, -252637, -3331, 17100003, 17100004),
  37. new OlympiadStadium(-79600, -252637, -3331, 17100005, 17100006),
  38. new OlympiadStadium(-75648, -252637, -3331, 17100007, 17100008),
  39. new OlympiadStadium(-88000, -249762, -3331, 17100009, 17100010),
  40. new OlympiadStadium(-83760, -249762, -3331, 17100011, 17100012),
  41. new OlympiadStadium(-79600, -249762, -3331, 17100013, 17100014),
  42. new OlympiadStadium(-75648, -249762, -3331, 17100015, 17100016),
  43. new OlympiadStadium(-88000, -246951, -3331, 17100017, 17100018),
  44. new OlympiadStadium(-83760, -246951, -3331, 17100019, 17100020),
  45. new OlympiadStadium(-79600, -246951, -3331, 17100021, 17100022),
  46. new OlympiadStadium(-75648, -246951, -3331, 17100023, 17100024),
  47. new OlympiadStadium(-88000, -244290, -3331, 17100025, 17100026),
  48. new OlympiadStadium(-83760, -244290, -3331, 17100027, 17100028),
  49. new OlympiadStadium(-79600, -244290, -3331, 17100029, 17100030),
  50. new OlympiadStadium(-75648, -244290, -3331, 17100031, 17100032),
  51. new OlympiadStadium(-88000, -241490, -3331, 17100033, 17100034),
  52. new OlympiadStadium(-83760, -241490, -3331, 17100035, 17100036),
  53. new OlympiadStadium(-79600, -241490, -3331, 17100037, 17100038),
  54. new OlympiadStadium(-75648, -241490, -3331, 17100039, 17100040),
  55. new OlympiadStadium(-88000, -238825, -3331, 17100041, 17100042),
  56. new OlympiadStadium(-83760, -238825, -3331, 17100043, 17100044) };
  57. public OlympiadManager()
  58. {
  59. _olympiadInstances = new FastMap<Integer, OlympiadGame>();
  60. _instance = this;
  61. }
  62. public static OlympiadManager getInstance()
  63. {
  64. if (_instance == null)
  65. {
  66. _instance = new OlympiadManager();
  67. }
  68. return _instance;
  69. }
  70. public synchronized void run()
  71. {
  72. if (Olympiad.getInstance().isOlympiadEnd())
  73. return;
  74. Map<Integer, OlympiadGameTask> _gamesQueue = new FastMap<Integer, OlympiadGameTask>();
  75. while (Olympiad.getInstance().inCompPeriod())
  76. {
  77. if (Olympiad.getNobleCount() == 0)
  78. {
  79. try
  80. {
  81. wait(60000);
  82. }
  83. catch (InterruptedException ex)
  84. {
  85. }
  86. continue;
  87. }
  88. int _gamesQueueSize = 0;
  89. // _compStarted = true;
  90. L2FastList<Integer> readyClasses = Olympiad.hasEnoughRegisteredClassed();
  91. boolean readyNonClassed = Olympiad.hasEnoughRegisteredNonClassed();
  92. if (readyClasses != null || readyNonClassed)
  93. {
  94. // first cycle do nothing
  95. _gamesQueueSize = _gamesQueue.size();
  96. for (int i = 0; i < _gamesQueueSize; i++)
  97. {
  98. if (_gamesQueue.get(i) == null
  99. || _gamesQueue.get(i).isTerminated()
  100. || _gamesQueue.get(i)._game == null)
  101. {
  102. if (_gamesQueue.containsKey(i))
  103. {
  104. // removes terminated games from the queue
  105. try
  106. {
  107. _olympiadInstances.remove(i);
  108. _gamesQueue.remove(i);
  109. STADIUMS[i].setStadiaFree();
  110. }
  111. catch (Exception e)
  112. {
  113. e.printStackTrace();
  114. }
  115. }
  116. else
  117. {
  118. _gamesQueueSize = _gamesQueueSize + 1;
  119. }
  120. }
  121. }
  122. // set up the games queue
  123. for (int i = 0; i < STADIUMS.length; i++)
  124. {
  125. if (!existNextOpponents(Olympiad.getRegisteredNonClassBased())
  126. && !existNextOpponents(getRandomClassList(Olympiad.getRegisteredClassBased(), readyClasses)))
  127. {
  128. break;
  129. }
  130. if (STADIUMS[i].isFreeToUse())
  131. {
  132. if (i < STADIUMS.length / 2)
  133. {
  134. if (readyNonClassed && existNextOpponents(Olympiad.getRegisteredNonClassBased()))
  135. {
  136. try
  137. {
  138. _olympiadInstances.put(i, new OlympiadGame(i, COMP_TYPE.NON_CLASSED, nextOpponents(Olympiad.getRegisteredNonClassBased())));
  139. _gamesQueue.put(i, new OlympiadGameTask(_olympiadInstances.get(i)));
  140. STADIUMS[i].setStadiaBusy();
  141. }
  142. catch (Exception ex)
  143. {
  144. if (_olympiadInstances.get(i) != null)
  145. {
  146. for (L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  147. {
  148. player.sendMessage("Your olympiad registration was canceled due to an error");
  149. player.setIsInOlympiadMode(false);
  150. player.setIsOlympiadStart(false);
  151. player.setOlympiadSide(-1);
  152. player.setOlympiadGameId(-1);
  153. }
  154. _olympiadInstances.remove(i);
  155. }
  156. if (_gamesQueue.get(i) != null)
  157. _gamesQueue.remove(i);
  158. STADIUMS[i].setStadiaFree();
  159. // try to reuse this stadia next time
  160. i--;
  161. }
  162. }
  163. else if (readyClasses != null && existNextOpponents(getRandomClassList(Olympiad.getRegisteredClassBased(), readyClasses)))
  164. {
  165. try
  166. {
  167. _olympiadInstances.put(i, new OlympiadGame(i, COMP_TYPE.CLASSED, nextOpponents(getRandomClassList(Olympiad.getRegisteredClassBased(), readyClasses))));
  168. _gamesQueue.put(i, new OlympiadGameTask(_olympiadInstances.get(i)));
  169. STADIUMS[i].setStadiaBusy();
  170. }
  171. catch (Exception ex)
  172. {
  173. if (_olympiadInstances.get(i) != null)
  174. {
  175. for (L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  176. {
  177. player.sendMessage("Your olympiad registration was canceled due to an error");
  178. player.setIsInOlympiadMode(false);
  179. player.setIsOlympiadStart(false);
  180. player.setOlympiadSide(-1);
  181. player.setOlympiadGameId(-1);
  182. }
  183. _olympiadInstances.remove(i);
  184. }
  185. if (_gamesQueue.get(i) != null)
  186. _gamesQueue.remove(i);
  187. STADIUMS[i].setStadiaFree();
  188. // try to reuse this stadia next time
  189. i--;
  190. }
  191. }
  192. }
  193. else
  194. {
  195. if (readyClasses != null && existNextOpponents(getRandomClassList(Olympiad.getRegisteredClassBased(), readyClasses)))
  196. {
  197. try
  198. {
  199. _olympiadInstances.put(i, new OlympiadGame(i, COMP_TYPE.CLASSED, nextOpponents(getRandomClassList(Olympiad.getRegisteredClassBased(), readyClasses))));
  200. _gamesQueue.put(i, new OlympiadGameTask(_olympiadInstances.get(i)));
  201. STADIUMS[i].setStadiaBusy();
  202. }
  203. catch (Exception ex)
  204. {
  205. if (_olympiadInstances.get(i) != null)
  206. {
  207. for (L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  208. {
  209. player.sendMessage("Your olympiad registration was canceled due to an error");
  210. player.setIsInOlympiadMode(false);
  211. player.setIsOlympiadStart(false);
  212. player.setOlympiadSide(-1);
  213. player.setOlympiadGameId(-1);
  214. }
  215. _olympiadInstances.remove(i);
  216. }
  217. if (_gamesQueue.get(i) != null)
  218. _gamesQueue.remove(i);
  219. STADIUMS[i].setStadiaFree();
  220. // try to reuse this stadia next time
  221. i--;
  222. }
  223. }
  224. else if (readyNonClassed && existNextOpponents(Olympiad.getRegisteredNonClassBased()))
  225. {
  226. try
  227. {
  228. _olympiadInstances.put(i, new OlympiadGame(i, COMP_TYPE.NON_CLASSED, nextOpponents(Olympiad.getRegisteredNonClassBased())));
  229. _gamesQueue.put(i, new OlympiadGameTask(_olympiadInstances.get(i)));
  230. STADIUMS[i].setStadiaBusy();
  231. }
  232. catch (Exception ex)
  233. {
  234. if (_olympiadInstances.get(i) != null)
  235. {
  236. for (L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  237. {
  238. player.sendMessage("Your olympiad registration was canceled due to an error");
  239. player.setIsInOlympiadMode(false);
  240. player.setIsOlympiadStart(false);
  241. player.setOlympiadSide(-1);
  242. player.setOlympiadGameId(-1);
  243. }
  244. _olympiadInstances.remove(i);
  245. }
  246. if (_gamesQueue.get(i) != null)
  247. _gamesQueue.remove(i);
  248. STADIUMS[i].setStadiaFree();
  249. // try to reuse this stadia next time
  250. i--;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. /*try
  257. {
  258. wait(30000);
  259. }
  260. catch (InterruptedException e)
  261. {
  262. }*/
  263. // Start games
  264. _gamesQueueSize = _gamesQueue.size();
  265. for (int i = 0; i < _gamesQueueSize; i++)
  266. {
  267. if (_gamesQueue.get(i) != null
  268. && !_gamesQueue.get(i).isTerminated()
  269. && !_gamesQueue.get(i).isStarted())
  270. {
  271. // start new games
  272. Thread T = new Thread(_gamesQueue.get(i));
  273. T.start();
  274. }
  275. // Pause one second between games starting to reduce OlympiadManager shout spam.
  276. try
  277. {
  278. wait(1000);
  279. }
  280. catch (InterruptedException e)
  281. {
  282. }
  283. }
  284. }
  285. // wait 30 sec for !stress the server
  286. try
  287. {
  288. wait(30000);
  289. }
  290. catch (InterruptedException e)
  291. {
  292. }
  293. }
  294. // when comp time finish wait for all games terminated before execute
  295. // the cleanup code
  296. boolean allGamesTerminated = false;
  297. // wait for all games terminated
  298. while (!allGamesTerminated)
  299. {
  300. try
  301. {
  302. wait(30000);
  303. }
  304. catch (InterruptedException e)
  305. {
  306. }
  307. if (_gamesQueue.size() == 0)
  308. {
  309. allGamesTerminated = true;
  310. }
  311. else
  312. {
  313. for (OlympiadGameTask game : _gamesQueue.values())
  314. {
  315. allGamesTerminated = allGamesTerminated
  316. || game.isTerminated();
  317. }
  318. }
  319. }
  320. // when all games terminated clear all
  321. _gamesQueue.clear();
  322. _olympiadInstances.clear();
  323. Olympiad.clearRegistered();
  324. OlympiadGame._battleStarted = false;
  325. }
  326. protected OlympiadGame getOlympiadGame(int index)
  327. {
  328. if (_olympiadInstances != null && _olympiadInstances.size() > 0)
  329. {
  330. return _olympiadInstances.get(index);
  331. }
  332. return null;
  333. }
  334. protected void removeGame(OlympiadGame game)
  335. {
  336. if (_olympiadInstances != null && _olympiadInstances.size() > 0)
  337. {
  338. for (int i = 0; i < _olympiadInstances.size(); i++)
  339. {
  340. if (_olympiadInstances.get(i) == game)
  341. {
  342. _olympiadInstances.remove(i);
  343. }
  344. }
  345. }
  346. }
  347. protected Map<Integer, OlympiadGame> getOlympiadGames()
  348. {
  349. return (_olympiadInstances == null) ? null : _olympiadInstances;
  350. }
  351. protected L2FastList<L2PcInstance> getRandomClassList(Map<Integer, L2FastList<L2PcInstance>> list, L2FastList<Integer> classList)
  352. {
  353. if (list == null || classList == null || list.size() == 0 || classList.size() == 0)
  354. return null;
  355. return list.get(classList.get(Rnd.nextInt(classList.size())));
  356. }
  357. protected L2FastList<L2PcInstance> nextOpponents(
  358. L2FastList<L2PcInstance> list)
  359. {
  360. L2FastList<L2PcInstance> opponents = new L2FastList<L2PcInstance>();
  361. if (list.size() == 0)
  362. return opponents;
  363. int loopCount = (list.size() / 2);
  364. int first;
  365. int second;
  366. if (loopCount < 1)
  367. return opponents;
  368. first = Rnd.nextInt(list.size());
  369. opponents.add(list.get(first));
  370. list.remove(first);
  371. second = Rnd.nextInt(list.size());
  372. opponents.add(list.get(second));
  373. list.remove(second);
  374. return opponents;
  375. }
  376. protected boolean existNextOpponents(L2FastList<L2PcInstance> list)
  377. {
  378. if (list == null)
  379. return false;
  380. if (list.size() == 0)
  381. return false;
  382. int loopCount = list.size() >> 1;
  383. if (loopCount < 1)
  384. return false;
  385. else
  386. return true;
  387. }
  388. protected FastMap<Integer, String> getAllTitles()
  389. {
  390. FastMap<Integer, String> titles = new FastMap<Integer, String>();
  391. for (OlympiadGame instance : _olympiadInstances.values())
  392. {
  393. if (instance._gamestarted != true)
  394. continue;
  395. titles.put(instance._stadiumID, instance.getTitle());
  396. }
  397. return titles;
  398. }
  399. }