TvTManager.java 7.7 KB

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