TvTManager.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 net.sf.l2j.gameserver.model.entity;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.Announcements;
  19. import net.sf.l2j.gameserver.ThreadPoolManager;
  20. /**
  21. * @author FBIagent
  22. */
  23. public class TvTManager
  24. {
  25. protected static final Logger _log = Logger.getLogger(TvTManager.class.getName());
  26. /** The one and only instance of this class<br> */
  27. private static TvTManager _instance = null;
  28. /** Task for event cycles<br> */
  29. private TvTStartTask _task;
  30. /**
  31. * New instance only by getInstance()<br>
  32. */
  33. private TvTManager()
  34. {
  35. if (Config.TVT_EVENT_ENABLED)
  36. {
  37. TvTEvent.init();
  38. this.scheduleEventStart();
  39. _log.info("TvTEventEngine[TvTManager.TvTManager()]: Started.");
  40. }
  41. else
  42. {
  43. _log.info("TvTEventEngine[TvTManager.TvTManager()]: Engine is disabled.");
  44. }
  45. }
  46. /**
  47. * Initialize new/Returns the one and only instance<br><br>
  48. *
  49. * @return TvTManager<br>
  50. */
  51. public static TvTManager getInstance()
  52. {
  53. return _instance == null ? ( _instance = new TvTManager() ) : _instance;
  54. }
  55. /**
  56. * Starts TvTStartTask
  57. */
  58. public void scheduleEventStart()
  59. {
  60. _task = new TvTStartTask(System.currentTimeMillis() + 60000L*Config.TVT_EVENT_INTERVAL);
  61. ThreadPoolManager.getInstance().executeTask(_task);
  62. }
  63. /**
  64. * Method to start participation
  65. */
  66. public void startReg()
  67. {
  68. if (!TvTEvent.startParticipation())
  69. {
  70. Announcements.getInstance().announceToAll("TvT Event: Event was cancelled.");
  71. _log.warning("TvTEventEngine[TvTManager.run()]: Error spawning event npc for participation.");
  72. this.scheduleEventStart();
  73. }
  74. else
  75. {
  76. Announcements.getInstance().announceToAll("TvT Event: Registration opened for " + Config.TVT_EVENT_PARTICIPATION_TIME + " minute(s).");
  77. // schedule registration end
  78. _task.setStartTime(System.currentTimeMillis() + 60000L*Config.TVT_EVENT_PARTICIPATION_TIME);
  79. ThreadPoolManager.getInstance().executeTask(_task);
  80. }
  81. }
  82. /**
  83. * Method to start the fight
  84. */
  85. public void startEvent()
  86. {
  87. if (!TvTEvent.startFight())
  88. {
  89. Announcements.getInstance().announceToAll("TvT Event: Event cancelled due to lack of Participation.");
  90. _log.info("TvTEventEngine[TvTManager.run()]: Lack of registration, abort event.");
  91. this.scheduleEventStart();
  92. }
  93. else
  94. {
  95. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting participants to an arena in " + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  96. _task.setStartTime(System.currentTimeMillis() + 60000L*Config.TVT_EVENT_RUNNING_TIME);
  97. ThreadPoolManager.getInstance().executeTask(_task);
  98. }
  99. }
  100. /**
  101. * Method to end the event and reward
  102. */
  103. public void endEvent()
  104. {
  105. Announcements.getInstance().announceToAll(TvTEvent.calculateRewards());
  106. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting back to the registration npc in " + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  107. TvTEvent.stopFight();
  108. this.scheduleEventStart();
  109. }
  110. /**
  111. * Class for TvT cycles
  112. */
  113. class TvTStartTask implements Runnable
  114. {
  115. private long _startTime;
  116. public TvTStartTask(long startTime)
  117. {
  118. _startTime = startTime;
  119. }
  120. public void setStartTime(long startTime)
  121. {
  122. _startTime = startTime;
  123. }
  124. /**
  125. * @see java.lang.Runnable#run()
  126. */
  127. public void run()
  128. {
  129. int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
  130. if (delay > 0)
  131. {
  132. this.announce(delay);
  133. }
  134. int nextMsg = 0;
  135. if (delay > 3600)
  136. {
  137. nextMsg = delay - 3600;
  138. }
  139. else if (delay > 1800)
  140. {
  141. nextMsg = delay - 1800;
  142. }
  143. else if (delay > 900)
  144. {
  145. nextMsg = delay - 900;
  146. }
  147. else if (delay > 600)
  148. {
  149. nextMsg = delay - 600;
  150. }
  151. else if (delay > 300)
  152. {
  153. nextMsg = delay - 300;
  154. }
  155. else if (delay > 60)
  156. {
  157. nextMsg = delay - 60;
  158. }
  159. else if (delay > 5)
  160. {
  161. nextMsg = delay - 5;
  162. }
  163. else if (delay > 0)
  164. {
  165. nextMsg = delay;
  166. }
  167. else
  168. {
  169. // start
  170. if (TvTEvent.isInactive())
  171. {
  172. TvTManager.this.startReg();
  173. }
  174. else if (TvTEvent.isParticipating())
  175. {
  176. TvTManager.this.startEvent();
  177. }
  178. else
  179. {
  180. TvTManager.this.endEvent();
  181. }
  182. }
  183. if (delay > 0)
  184. {
  185. ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg*1000);
  186. }
  187. }
  188. private void announce(long time)
  189. {
  190. if (time >= 3600 && time%3600 == 0)
  191. {
  192. if (TvTEvent.isParticipating())
  193. {
  194. Announcements.getInstance().announceToAll("TvT Event: "+(time/60/60)+" hour(s) until registration is closed!");
  195. }
  196. else if (TvTEvent.isStarted())
  197. {
  198. TvTEvent.sysMsgToAllParticipants("TvT Event: "+(time/60/60)+" hour(s) until event is finished!");
  199. }
  200. }
  201. else if (time >= 60)
  202. {
  203. if (TvTEvent.isParticipating())
  204. {
  205. Announcements.getInstance().announceToAll("TvT Event: "+(time/60)+" minute(s) until registration is closed!");
  206. }
  207. else if (TvTEvent.isStarted())
  208. {
  209. TvTEvent.sysMsgToAllParticipants("TvT Event: "+(time/60)+" minute(s) until the event is finished!");
  210. }
  211. }
  212. else
  213. {
  214. if (TvTEvent.isParticipating())
  215. {
  216. Announcements.getInstance().announceToAll("TvT Event: "+time+" second(s) until registration is closed!");
  217. }
  218. else if (TvTEvent.isStarted())
  219. {
  220. TvTEvent.sysMsgToAllParticipants("TvT Event: "+time+" second(s) until the event is finished!");
  221. }
  222. }
  223. }
  224. }
  225. }