OlympiadGameTask.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Copyright (C) 2004-2014 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.model.olympiad;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  25. import com.l2jserver.gameserver.network.SystemMessageId;
  26. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  27. /**
  28. * @author DS
  29. */
  30. public final class OlympiadGameTask implements Runnable
  31. {
  32. protected static final Logger _log = Logger.getLogger(OlympiadGameTask.class.getName());
  33. protected static final long BATTLE_PERIOD = Config.ALT_OLY_BATTLE; // 6 mins
  34. public static final int[] TELEPORT_TO_ARENA =
  35. {
  36. 120,
  37. 60,
  38. 30,
  39. 15,
  40. 10,
  41. 5,
  42. 4,
  43. 3,
  44. 2,
  45. 1,
  46. 0
  47. };
  48. public static final int[] BATTLE_START_TIME_FIRST =
  49. {
  50. 60,
  51. 50,
  52. 40,
  53. 30,
  54. 20,
  55. 10,
  56. 0
  57. };
  58. public static final int[] BATTLE_START_TIME_SECOND =
  59. {
  60. 10,
  61. 5,
  62. 4,
  63. 3,
  64. 2,
  65. 1,
  66. 0
  67. };
  68. public static final int[] TELEPORT_TO_TOWN =
  69. {
  70. 40,
  71. 30,
  72. 20,
  73. 10,
  74. 5,
  75. 4,
  76. 3,
  77. 2,
  78. 1,
  79. 0
  80. };
  81. private final L2OlympiadStadiumZone _zone;
  82. private AbstractOlympiadGame _game;
  83. private GameState _state = GameState.IDLE;
  84. private boolean _needAnnounce = false;
  85. private int _countDown = 0;
  86. private static enum GameState
  87. {
  88. BEGIN,
  89. TELEPORT_TO_ARENA,
  90. GAME_STARTED,
  91. BATTLE_COUNTDOWN_FIRST,
  92. BATTLE_COUNTDOWN_SECOND,
  93. BATTLE_STARTED,
  94. BATTLE_IN_PROGRESS,
  95. GAME_STOPPED,
  96. TELEPORT_TO_TOWN,
  97. CLEANUP,
  98. IDLE
  99. }
  100. public OlympiadGameTask(L2OlympiadStadiumZone zone)
  101. {
  102. _zone = zone;
  103. zone.registerTask(this);
  104. }
  105. public final boolean isRunning()
  106. {
  107. return _state != GameState.IDLE;
  108. }
  109. public final boolean isGameStarted()
  110. {
  111. return (_state.ordinal() >= GameState.GAME_STARTED.ordinal()) && (_state.ordinal() <= GameState.CLEANUP.ordinal());
  112. }
  113. public final boolean isBattleStarted()
  114. {
  115. return _state == GameState.BATTLE_IN_PROGRESS;
  116. }
  117. public final boolean isBattleFinished()
  118. {
  119. return _state == GameState.TELEPORT_TO_TOWN;
  120. }
  121. public final boolean needAnnounce()
  122. {
  123. if (_needAnnounce)
  124. {
  125. _needAnnounce = false;
  126. return true;
  127. }
  128. return false;
  129. }
  130. public final L2OlympiadStadiumZone getZone()
  131. {
  132. return _zone;
  133. }
  134. public final AbstractOlympiadGame getGame()
  135. {
  136. return _game;
  137. }
  138. public final void attachGame(AbstractOlympiadGame game)
  139. {
  140. if ((game != null) && (_state != GameState.IDLE))
  141. {
  142. _log.log(Level.WARNING, "Attempt to overwrite non-finished game in state " + _state);
  143. return;
  144. }
  145. _game = game;
  146. _state = GameState.BEGIN;
  147. _needAnnounce = false;
  148. ThreadPoolManager.getInstance().executeGeneral(this);
  149. }
  150. @Override
  151. public final void run()
  152. {
  153. try
  154. {
  155. int delay = 1; // schedule next call after 1s
  156. switch (_state)
  157. {
  158. // Game created
  159. case BEGIN:
  160. {
  161. _state = GameState.TELEPORT_TO_ARENA;
  162. _countDown = Config.ALT_OLY_WAIT_TIME;
  163. break;
  164. }
  165. // Teleport to arena countdown
  166. case TELEPORT_TO_ARENA:
  167. {
  168. if (_countDown > 0)
  169. {
  170. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_WILL_ENTER_THE_OLYMPIAD_STADIUM_IN_S1_SECOND_S);
  171. sm.addInt(_countDown);
  172. _game.broadcastPacket(sm);
  173. }
  174. delay = getDelay(TELEPORT_TO_ARENA);
  175. if (_countDown <= 0)
  176. {
  177. _state = GameState.GAME_STARTED;
  178. }
  179. break;
  180. }
  181. // Game start, port players to arena
  182. case GAME_STARTED:
  183. {
  184. if (!startGame())
  185. {
  186. _state = GameState.GAME_STOPPED;
  187. break;
  188. }
  189. _state = GameState.BATTLE_COUNTDOWN_FIRST;
  190. _countDown = BATTLE_START_TIME_FIRST[0];
  191. delay = 5;
  192. break;
  193. }
  194. // Battle start countdown, first part (60-10)
  195. case BATTLE_COUNTDOWN_FIRST:
  196. {
  197. if (_countDown > 0)
  198. {
  199. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_WILL_START_IN_S1_SECOND_S);
  200. sm.addInt(_countDown);
  201. _zone.broadcastPacket(sm);
  202. }
  203. delay = getDelay(BATTLE_START_TIME_FIRST);
  204. if (_countDown <= 0)
  205. {
  206. openDoors();
  207. _state = GameState.BATTLE_COUNTDOWN_SECOND;
  208. _countDown = BATTLE_START_TIME_SECOND[0];
  209. delay = getDelay(BATTLE_START_TIME_SECOND);
  210. }
  211. break;
  212. }
  213. // Battle start countdown, second part (10-0)
  214. case BATTLE_COUNTDOWN_SECOND:
  215. {
  216. if (_countDown > 0)
  217. {
  218. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_WILL_START_IN_S1_SECOND_S);
  219. sm.addInt(_countDown);
  220. _zone.broadcastPacket(sm);
  221. }
  222. delay = getDelay(BATTLE_START_TIME_SECOND);
  223. if (_countDown <= 0)
  224. {
  225. _state = GameState.BATTLE_STARTED;
  226. }
  227. break;
  228. }
  229. // Beginning of the battle
  230. case BATTLE_STARTED:
  231. {
  232. _countDown = 0;
  233. _state = GameState.BATTLE_IN_PROGRESS; // set state first, used in zone update
  234. if (!startBattle())
  235. {
  236. _state = GameState.GAME_STOPPED;
  237. }
  238. break;
  239. }
  240. // Checks during battle
  241. case BATTLE_IN_PROGRESS:
  242. {
  243. _countDown += 1000;
  244. if (checkBattle() || (_countDown > Config.ALT_OLY_BATTLE))
  245. {
  246. _state = GameState.GAME_STOPPED;
  247. }
  248. break;
  249. }
  250. // End of the battle
  251. case GAME_STOPPED:
  252. {
  253. _state = GameState.TELEPORT_TO_TOWN;
  254. _countDown = TELEPORT_TO_TOWN[0];
  255. stopGame();
  256. delay = getDelay(TELEPORT_TO_TOWN);
  257. break;
  258. }
  259. // Teleport to town countdown
  260. case TELEPORT_TO_TOWN:
  261. {
  262. if (_countDown > 0)
  263. {
  264. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  265. sm.addInt(_countDown);
  266. _game.broadcastPacket(sm);
  267. }
  268. delay = getDelay(TELEPORT_TO_TOWN);
  269. if (_countDown <= 0)
  270. {
  271. _state = GameState.CLEANUP;
  272. }
  273. break;
  274. }
  275. // Removals
  276. case CLEANUP:
  277. {
  278. cleanupGame();
  279. _state = GameState.IDLE;
  280. _game = null;
  281. return;
  282. }
  283. }
  284. ThreadPoolManager.getInstance().scheduleGeneral(this, delay * 1000);
  285. }
  286. catch (Exception e)
  287. {
  288. switch (_state)
  289. {
  290. case GAME_STOPPED:
  291. case TELEPORT_TO_TOWN:
  292. case CLEANUP:
  293. case IDLE:
  294. {
  295. _log.log(Level.WARNING, "Unable to return players back in town, exception: " + e.getMessage());
  296. _state = GameState.IDLE;
  297. _game = null;
  298. return;
  299. }
  300. }
  301. _log.log(Level.WARNING, "Exception in " + _state + ", trying to port players back: " + e.getMessage(), e);
  302. _state = GameState.GAME_STOPPED;
  303. ThreadPoolManager.getInstance().scheduleGeneral(this, 1000);
  304. }
  305. }
  306. private final int getDelay(int[] times)
  307. {
  308. int time;
  309. for (int i = 0; i < (times.length - 1); i++)
  310. {
  311. time = times[i];
  312. if (time >= _countDown)
  313. {
  314. continue;
  315. }
  316. final int delay = _countDown - time;
  317. _countDown = time;
  318. return delay;
  319. }
  320. // should not happens
  321. _countDown = -1;
  322. return 1;
  323. }
  324. /**
  325. * Second stage: check for defaulted, port players to arena, announce game.
  326. * @return true if no participants defaulted.
  327. */
  328. private final boolean startGame()
  329. {
  330. try
  331. {
  332. // Checking for opponents and teleporting to arena
  333. if (_game.checkDefaulted())
  334. {
  335. return false;
  336. }
  337. _zone.closeDoors();
  338. if (_game.needBuffers())
  339. {
  340. _zone.spawnBuffers();
  341. }
  342. if (!_game.portPlayersToArena(_zone.getSpawns()))
  343. {
  344. return false;
  345. }
  346. _game.removals();
  347. _needAnnounce = true;
  348. OlympiadGameManager.getInstance().startBattle(); // inform manager
  349. return true;
  350. }
  351. catch (Exception e)
  352. {
  353. _log.log(Level.WARNING, e.getMessage(), e);
  354. }
  355. return false;
  356. }
  357. /**
  358. * Third stage: open doors.
  359. */
  360. private final void openDoors()
  361. {
  362. try
  363. {
  364. _game.resetDamage();
  365. _zone.openDoors();
  366. }
  367. catch (Exception e)
  368. {
  369. _log.log(Level.WARNING, e.getMessage(), e);
  370. }
  371. }
  372. /**
  373. * Fourth stage: last checks, remove buffers, start competition itself.
  374. * @return true if all participants online and ready on the stadium.
  375. */
  376. private final boolean startBattle()
  377. {
  378. try
  379. {
  380. if (_game.needBuffers())
  381. {
  382. _zone.deleteBuffers();
  383. }
  384. if (_game.checkBattleStatus() && _game.makeCompetitionStart())
  385. {
  386. // game successfully started
  387. _game.broadcastOlympiadInfo(_zone);
  388. _zone.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.STARTS_THE_GAME));
  389. _zone.updateZoneStatusForCharactersInside();
  390. return true;
  391. }
  392. }
  393. catch (Exception e)
  394. {
  395. _log.log(Level.WARNING, e.getMessage(), e);
  396. }
  397. return false;
  398. }
  399. /**
  400. * Fifth stage: battle is running, returns true if winner found.
  401. * @return
  402. */
  403. private final boolean checkBattle()
  404. {
  405. try
  406. {
  407. return _game.haveWinner();
  408. }
  409. catch (Exception e)
  410. {
  411. _log.log(Level.WARNING, e.getMessage(), e);
  412. }
  413. return true;
  414. }
  415. /**
  416. * Sixth stage: winner's validations
  417. */
  418. private final void stopGame()
  419. {
  420. try
  421. {
  422. _game.validateWinner(_zone);
  423. }
  424. catch (Exception e)
  425. {
  426. _log.log(Level.WARNING, e.getMessage(), e);
  427. }
  428. try
  429. {
  430. _zone.updateZoneStatusForCharactersInside();
  431. }
  432. catch (Exception e)
  433. {
  434. _log.log(Level.WARNING, e.getMessage(), e);
  435. }
  436. try
  437. {
  438. _game.cleanEffects();
  439. }
  440. catch (Exception e)
  441. {
  442. _log.log(Level.WARNING, e.getMessage(), e);
  443. }
  444. }
  445. /**
  446. * Seventh stage: game cleanup (port players back, closing doors, etc)
  447. */
  448. private final void cleanupGame()
  449. {
  450. try
  451. {
  452. _game.playersStatusBack();
  453. }
  454. catch (Exception e)
  455. {
  456. _log.log(Level.WARNING, e.getMessage(), e);
  457. }
  458. try
  459. {
  460. _game.portPlayersBack();
  461. }
  462. catch (Exception e)
  463. {
  464. _log.log(Level.WARNING, e.getMessage(), e);
  465. }
  466. try
  467. {
  468. _game.clearPlayers();
  469. }
  470. catch (Exception e)
  471. {
  472. _log.log(Level.WARNING, e.getMessage(), e);
  473. }
  474. try
  475. {
  476. _zone.closeDoors();
  477. }
  478. catch (Exception e)
  479. {
  480. _log.log(Level.WARNING, e.getMessage(), e);
  481. }
  482. }
  483. }