OlympiadManager.java 13 KB

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