OlympiadGameManager.java 5.4 KB

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