OlympiadGameManager.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. readyTeams = false;
  90. }
  91. // if no ready teams found check for classed
  92. if (readyClassed != null)
  93. {
  94. newGame = OlympiadGameClassed.createGame(i, readyClassed);
  95. if (newGame != null)
  96. {
  97. task.attachGame(newGame);
  98. continue;
  99. }
  100. readyClassed = null;
  101. }
  102. }
  103. // 1,3,5,7,9.. arenas used for non-classed
  104. // also other arenas will be used for non-classed if no classed or teams available
  105. if (readyNonClassed)
  106. {
  107. newGame = OlympiadGameNonClassed.createGame(i, OlympiadManager.getInstance().getRegisteredNonClassBased());
  108. if (newGame != null)
  109. {
  110. task.attachGame(newGame);
  111. continue;
  112. }
  113. readyNonClassed = false;
  114. }
  115. }
  116. }
  117. // stop generating games if no more participants
  118. if (readyClassed == null && !readyNonClassed && !readyTeams)
  119. break;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. // not in competition period
  126. if (isAllTasksFinished())
  127. {
  128. OlympiadManager.getInstance().clearRegistered();
  129. _battleStarted = false;
  130. _log.log(Level.INFO, "Olympiad System: All current games finished.");
  131. }
  132. }
  133. }
  134. public final boolean isAllTasksFinished()
  135. {
  136. for (OlympiadGameTask task : _tasks)
  137. {
  138. if (task.isRunning())
  139. return false;
  140. }
  141. return true;
  142. }
  143. public final OlympiadGameTask getOlympiadTask(int id)
  144. {
  145. if (id < 0 || id >= _tasks.length)
  146. return null;
  147. return _tasks[id];
  148. }
  149. public final int getNumberOfStadiums()
  150. {
  151. return _tasks.length;
  152. }
  153. public final void notifyCompetitorDamage(L2PcInstance player, int damage)
  154. {
  155. if (player == null)
  156. return;
  157. final int id = player.getOlympiadGameId();
  158. if (id < 0 || id >= _tasks.length)
  159. return;
  160. final AbstractOlympiadGame game = _tasks[id].getGame();
  161. if (game != null)
  162. game.addDamage(player, damage);
  163. }
  164. @SuppressWarnings("synthetic-access")
  165. private static class SingletonHolder
  166. {
  167. protected static final OlympiadGameManager _instance = new OlympiadGameManager();
  168. }
  169. }