TvTManager.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.entity;
  20. import java.util.Calendar;
  21. import java.util.concurrent.ScheduledFuture;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.ThreadPoolManager;
  25. import com.l2jserver.gameserver.util.Broadcast;
  26. /**
  27. * @author HorridoJoho
  28. */
  29. public class TvTManager
  30. {
  31. protected static final Logger _log = Logger.getLogger(TvTManager.class.getName());
  32. /** Task for event cycles<br> */
  33. private TvTStartTask _task;
  34. /**
  35. * New instance only by getInstance()<br>
  36. */
  37. protected TvTManager()
  38. {
  39. if (Config.TVT_EVENT_ENABLED)
  40. {
  41. TvTEvent.init();
  42. scheduleEventStart();
  43. _log.info("TvTEventEngine[TvTManager.TvTManager()]: Started.");
  44. }
  45. else
  46. {
  47. _log.info("TvTEventEngine[TvTManager.TvTManager()]: Engine is disabled.");
  48. }
  49. }
  50. /**
  51. * Initialize new/Returns the one and only instance<br>
  52. * <br>
  53. * @return TvTManager<br>
  54. */
  55. public static TvTManager getInstance()
  56. {
  57. return SingletonHolder._instance;
  58. }
  59. /**
  60. * Starts TvTStartTask
  61. */
  62. public void scheduleEventStart()
  63. {
  64. try
  65. {
  66. Calendar currentTime = Calendar.getInstance();
  67. Calendar nextStartTime = null;
  68. Calendar testStartTime = null;
  69. for (String timeOfDay : Config.TVT_EVENT_INTERVAL)
  70. {
  71. // Creating a Calendar object from the specified interval value
  72. testStartTime = Calendar.getInstance();
  73. testStartTime.setLenient(true);
  74. String[] splitTimeOfDay = timeOfDay.split(":");
  75. testStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTimeOfDay[0]));
  76. testStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTimeOfDay[1]));
  77. // If the date is in the past, make it the next day (Example: Checking for "1:00", when the time is 23:57.)
  78. if (testStartTime.getTimeInMillis() < currentTime.getTimeInMillis())
  79. {
  80. testStartTime.add(Calendar.DAY_OF_MONTH, 1);
  81. }
  82. // Check for the test date to be the minimum (smallest in the specified list)
  83. if ((nextStartTime == null) || (testStartTime.getTimeInMillis() < nextStartTime.getTimeInMillis()))
  84. {
  85. nextStartTime = testStartTime;
  86. }
  87. }
  88. if (nextStartTime != null)
  89. {
  90. _task = new TvTStartTask(nextStartTime.getTimeInMillis());
  91. ThreadPoolManager.getInstance().executeGeneral(_task);
  92. }
  93. }
  94. catch (Exception e)
  95. {
  96. _log.warning("TvTEventEngine[TvTManager.scheduleEventStart()]: Error figuring out a start time. Check TvTEventInterval in config file.");
  97. }
  98. }
  99. /**
  100. * Method to start participation
  101. */
  102. public void startReg()
  103. {
  104. if (!TvTEvent.startParticipation())
  105. {
  106. Broadcast.toAllOnlinePlayers("TvT Event: Event was cancelled.");
  107. _log.warning("TvTEventEngine[TvTManager.run()]: Error spawning event npc for participation.");
  108. scheduleEventStart();
  109. }
  110. else
  111. {
  112. Broadcast.toAllOnlinePlayers("TvT Event: Registration opened for " + Config.TVT_EVENT_PARTICIPATION_TIME + " minute(s).");
  113. // schedule registration end
  114. _task.setStartTime(System.currentTimeMillis() + (60000L * Config.TVT_EVENT_PARTICIPATION_TIME));
  115. ThreadPoolManager.getInstance().executeGeneral(_task);
  116. }
  117. }
  118. /**
  119. * Method to start the fight
  120. */
  121. public void startEvent()
  122. {
  123. if (!TvTEvent.startFight())
  124. {
  125. Broadcast.toAllOnlinePlayers("TvT Event: Event cancelled due to lack of Participation.");
  126. _log.info("TvTEventEngine[TvTManager.run()]: Lack of registration, abort event.");
  127. scheduleEventStart();
  128. }
  129. else
  130. {
  131. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting participants to an arena in " + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  132. _task.setStartTime(System.currentTimeMillis() + (60000L * Config.TVT_EVENT_RUNNING_TIME));
  133. ThreadPoolManager.getInstance().executeGeneral(_task);
  134. }
  135. }
  136. /**
  137. * Method to end the event and reward
  138. */
  139. public void endEvent()
  140. {
  141. Broadcast.toAllOnlinePlayers(TvTEvent.calculateRewards());
  142. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting back to the registration npc in " + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  143. TvTEvent.stopFight();
  144. scheduleEventStart();
  145. }
  146. public void skipDelay()
  147. {
  148. if (_task.nextRun.cancel(false))
  149. {
  150. _task.setStartTime(System.currentTimeMillis());
  151. ThreadPoolManager.getInstance().executeGeneral(_task);
  152. }
  153. }
  154. /**
  155. * Class for TvT cycles
  156. */
  157. class TvTStartTask implements Runnable
  158. {
  159. private long _startTime;
  160. public ScheduledFuture<?> nextRun;
  161. public TvTStartTask(long startTime)
  162. {
  163. _startTime = startTime;
  164. }
  165. public void setStartTime(long startTime)
  166. {
  167. _startTime = startTime;
  168. }
  169. @Override
  170. public void run()
  171. {
  172. int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
  173. if (delay > 0)
  174. {
  175. announce(delay);
  176. }
  177. int nextMsg = 0;
  178. if (delay > 3600)
  179. {
  180. nextMsg = delay - 3600;
  181. }
  182. else if (delay > 1800)
  183. {
  184. nextMsg = delay - 1800;
  185. }
  186. else if (delay > 900)
  187. {
  188. nextMsg = delay - 900;
  189. }
  190. else if (delay > 600)
  191. {
  192. nextMsg = delay - 600;
  193. }
  194. else if (delay > 300)
  195. {
  196. nextMsg = delay - 300;
  197. }
  198. else if (delay > 60)
  199. {
  200. nextMsg = delay - 60;
  201. }
  202. else if (delay > 5)
  203. {
  204. nextMsg = delay - 5;
  205. }
  206. else if (delay > 0)
  207. {
  208. nextMsg = delay;
  209. }
  210. else
  211. {
  212. // start
  213. if (TvTEvent.isInactive())
  214. {
  215. startReg();
  216. }
  217. else if (TvTEvent.isParticipating())
  218. {
  219. startEvent();
  220. }
  221. else
  222. {
  223. endEvent();
  224. }
  225. }
  226. if (delay > 0)
  227. {
  228. nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
  229. }
  230. }
  231. private void announce(long time)
  232. {
  233. if ((time >= 3600) && ((time % 3600) == 0))
  234. {
  235. if (TvTEvent.isParticipating())
  236. {
  237. Broadcast.toAllOnlinePlayers("TvT Event: " + (time / 60 / 60) + " hour(s) until registration is closed!");
  238. }
  239. else if (TvTEvent.isStarted())
  240. {
  241. TvTEvent.sysMsgToAllParticipants("TvT Event: " + (time / 60 / 60) + " hour(s) until event is finished!");
  242. }
  243. }
  244. else if (time >= 60)
  245. {
  246. if (TvTEvent.isParticipating())
  247. {
  248. Broadcast.toAllOnlinePlayers("TvT Event: " + (time / 60) + " minute(s) until registration is closed!");
  249. }
  250. else if (TvTEvent.isStarted())
  251. {
  252. TvTEvent.sysMsgToAllParticipants("TvT Event: " + (time / 60) + " minute(s) until the event is finished!");
  253. }
  254. }
  255. else
  256. {
  257. if (TvTEvent.isParticipating())
  258. {
  259. Broadcast.toAllOnlinePlayers("TvT Event: " + time + " second(s) until registration is closed!");
  260. }
  261. else if (TvTEvent.isStarted())
  262. {
  263. TvTEvent.sysMsgToAllParticipants("TvT Event: " + time + " second(s) until the event is finished!");
  264. }
  265. }
  266. }
  267. }
  268. private static class SingletonHolder
  269. {
  270. protected static final TvTManager _instance = new TvTManager();
  271. }
  272. }