OlympiadGameManager.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.Collection;
  17. import java.util.List;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  23. /**
  24. * @author GodKratos, DS
  25. */
  26. public class OlympiadGameManager implements Runnable
  27. {
  28. private static final Logger _log = Logger.getLogger(OlympiadGameManager.class.getName());
  29. private volatile boolean _battleStarted = false;
  30. private final OlympiadGameTask[] _tasks;
  31. protected OlympiadGameManager()
  32. {
  33. final Collection<L2OlympiadStadiumZone> zones = ZoneManager.getInstance().getAllZones(L2OlympiadStadiumZone.class);
  34. if ((zones == null) || zones.isEmpty())
  35. {
  36. throw new Error("No olympiad stadium zones defined !");
  37. }
  38. _tasks = new OlympiadGameTask[zones.size()];
  39. int i = 0;
  40. for (L2OlympiadStadiumZone zone : zones)
  41. {
  42. _tasks[i++] = new OlympiadGameTask(zone);
  43. }
  44. _log.log(Level.INFO, "Olympiad System: Loaded " + _tasks.length + " stadiums.");
  45. }
  46. public static final OlympiadGameManager getInstance()
  47. {
  48. return SingletonHolder._instance;
  49. }
  50. protected final boolean isBattleStarted()
  51. {
  52. return _battleStarted;
  53. }
  54. protected final void startBattle()
  55. {
  56. _battleStarted = true;
  57. }
  58. @Override
  59. public final void run()
  60. {
  61. if (Olympiad.getInstance().isOlympiadEnd())
  62. {
  63. return;
  64. }
  65. if (Olympiad.getInstance().inCompPeriod())
  66. {
  67. OlympiadGameTask task;
  68. AbstractOlympiadGame newGame;
  69. List<List<Integer>> readyClassed = OlympiadManager.getInstance().hasEnoughRegisteredClassed();
  70. boolean readyNonClassed = OlympiadManager.getInstance().hasEnoughRegisteredNonClassed();
  71. boolean readyTeams = OlympiadManager.getInstance().hasEnoughRegisteredTeams();
  72. if ((readyClassed != null) || readyNonClassed || readyTeams)
  73. {
  74. // set up the games queue
  75. for (int i = 0; i < _tasks.length; i++)
  76. {
  77. task = _tasks[i];
  78. synchronized (task)
  79. {
  80. if (!task.isRunning())
  81. {
  82. // Fair arena distribution
  83. // 0,2,4,6,8.. arenas checked for classed or teams first
  84. if (((readyClassed != null) || readyTeams) && ((i % 2) == 0))
  85. {
  86. // 0,4,8.. arenas checked for teams first
  87. if (readyTeams && ((i % 4) == 0))
  88. {
  89. newGame = OlympiadGameTeams.createGame(i, OlympiadManager.getInstance().getRegisteredTeamsBased());
  90. if (newGame != null)
  91. {
  92. task.attachGame(newGame);
  93. continue;
  94. }
  95. readyTeams = false;
  96. }
  97. // if no ready teams found check for classed
  98. if (readyClassed != null)
  99. {
  100. newGame = OlympiadGameClassed.createGame(i, readyClassed);
  101. if (newGame != null)
  102. {
  103. task.attachGame(newGame);
  104. continue;
  105. }
  106. readyClassed = null;
  107. }
  108. }
  109. // 1,3,5,7,9.. arenas used for non-classed
  110. // also other arenas will be used for non-classed if no classed or teams available
  111. if (readyNonClassed)
  112. {
  113. newGame = OlympiadGameNonClassed.createGame(i, OlympiadManager.getInstance().getRegisteredNonClassBased());
  114. if (newGame != null)
  115. {
  116. task.attachGame(newGame);
  117. continue;
  118. }
  119. readyNonClassed = false;
  120. }
  121. }
  122. }
  123. // stop generating games if no more participants
  124. if ((readyClassed == null) && !readyNonClassed && !readyTeams)
  125. {
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. else
  132. {
  133. // not in competition period
  134. if (isAllTasksFinished())
  135. {
  136. OlympiadManager.getInstance().clearRegistered();
  137. _battleStarted = false;
  138. _log.log(Level.INFO, "Olympiad System: All current games finished.");
  139. }
  140. }
  141. }
  142. public final boolean isAllTasksFinished()
  143. {
  144. for (OlympiadGameTask task : _tasks)
  145. {
  146. if (task.isRunning())
  147. {
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. public final OlympiadGameTask getOlympiadTask(int id)
  154. {
  155. if ((id < 0) || (id >= _tasks.length))
  156. {
  157. return null;
  158. }
  159. return _tasks[id];
  160. }
  161. public final int getNumberOfStadiums()
  162. {
  163. return _tasks.length;
  164. }
  165. public final void notifyCompetitorDamage(L2PcInstance player, int damage)
  166. {
  167. if (player == null)
  168. {
  169. return;
  170. }
  171. final int id = player.getOlympiadGameId();
  172. if ((id < 0) || (id >= _tasks.length))
  173. {
  174. return;
  175. }
  176. final AbstractOlympiadGame game = _tasks[id].getGame();
  177. if (game != null)
  178. {
  179. game.addDamage(player, damage);
  180. }
  181. }
  182. private static class SingletonHolder
  183. {
  184. protected static final OlympiadGameManager _instance = new OlympiadGameManager();
  185. }
  186. }