OlympiadGameManager.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. @Override
  56. public final void run()
  57. {
  58. if (Olympiad.getInstance().isOlympiadEnd())
  59. return;
  60. if (Olympiad.getInstance().inCompPeriod())
  61. {
  62. OlympiadGameTask task;
  63. AbstractOlympiadGame newGame;
  64. List<List<Integer>> readyClassed = OlympiadManager.getInstance().hasEnoughRegisteredClassed();
  65. boolean readyNonClassed = OlympiadManager.getInstance().hasEnoughRegisteredNonClassed();
  66. boolean readyTeams = OlympiadManager.getInstance().hasEnoughRegisteredTeams();
  67. if (readyClassed != null || readyNonClassed || readyTeams)
  68. {
  69. // set up the games queue
  70. for (int i = 0; i < _tasks.length; i++)
  71. {
  72. task = _tasks[i];
  73. synchronized (task)
  74. {
  75. if (!task.isRunning())
  76. {
  77. // Fair arena distribution
  78. // 0,2,4,6,8.. arenas checked for classed or teams first
  79. if ((readyClassed != null || readyTeams) && (i % 2) == 0)
  80. {
  81. // 0,4,8.. arenas checked for teams first
  82. if (readyTeams && (i % 4) == 0)
  83. {
  84. newGame = OlympiadGameTeams.createGame(i, OlympiadManager.getInstance().getRegisteredTeamsBased());
  85. if (newGame != null)
  86. {
  87. task.attachGame(newGame);
  88. continue;
  89. }
  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. readyClassed = null;
  102. }
  103. }
  104. // 1,3,5,7,9.. arenas used for non-classed
  105. // also other arenas will be used for non-classed if no classed or teams available
  106. if (readyNonClassed)
  107. {
  108. newGame = OlympiadGameNonClassed.createGame(i, OlympiadManager.getInstance().getRegisteredNonClassBased());
  109. if (newGame != null)
  110. {
  111. task.attachGame(newGame);
  112. continue;
  113. }
  114. readyNonClassed = false;
  115. }
  116. }
  117. }
  118. // stop generating games if no more participants
  119. if (readyClassed == null && !readyNonClassed && !readyTeams)
  120. break;
  121. }
  122. }
  123. }
  124. else
  125. {
  126. // not in competition period
  127. if (isAllTasksFinished())
  128. {
  129. OlympiadManager.getInstance().clearRegistered();
  130. _battleStarted = false;
  131. _log.log(Level.INFO, "Olympiad System: All current games finished.");
  132. }
  133. }
  134. }
  135. public final boolean isAllTasksFinished()
  136. {
  137. for (OlympiadGameTask task : _tasks)
  138. {
  139. if (task.isRunning())
  140. return false;
  141. }
  142. return true;
  143. }
  144. public final OlympiadGameTask getOlympiadTask(int id)
  145. {
  146. if (id < 0 || id >= _tasks.length)
  147. return null;
  148. return _tasks[id];
  149. }
  150. public final int getNumberOfStadiums()
  151. {
  152. return _tasks.length;
  153. }
  154. public final void notifyCompetitorDamage(L2PcInstance player, int damage)
  155. {
  156. if (player == null)
  157. return;
  158. final int id = player.getOlympiadGameId();
  159. if (id < 0 || id >= _tasks.length)
  160. return;
  161. final AbstractOlympiadGame game = _tasks[id].getGame();
  162. if (game != null)
  163. game.addDamage(player, damage);
  164. }
  165. @SuppressWarnings("synthetic-access")
  166. private static class SingletonHolder
  167. {
  168. protected static final OlympiadGameManager _instance = new OlympiadGameManager();
  169. }
  170. }