OlympiadGameManager.java 5.5 KB

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