TvTManager.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.entity;
  16. import java.util.Calendar;
  17. import java.util.concurrent.ScheduledFuture;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.Announcements;
  21. import com.l2jserver.gameserver.ThreadPoolManager;
  22. /**
  23. * @author FBIagent
  24. */
  25. public class TvTManager
  26. {
  27. protected static final Logger _log = Logger.getLogger(TvTManager.class.getName());
  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 SingletonHolder._instance;
  54. }
  55. /**
  56. * Starts TvTStartTask
  57. */
  58. public void scheduleEventStart()
  59. {
  60. try
  61. {
  62. Calendar currentTime = Calendar.getInstance();
  63. Calendar nextStartTime = null;
  64. Calendar testStartTime = null;
  65. for (String timeOfDay : Config.TVT_EVENT_INTERVAL)
  66. {
  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
  106. + " minute(s).");
  107. // schedule registration end
  108. _task.setStartTime(System.currentTimeMillis() + 60000L * Config.TVT_EVENT_PARTICIPATION_TIME);
  109. ThreadPoolManager.getInstance().executeTask(_task);
  110. }
  111. }
  112. /**
  113. * Method to start the fight
  114. */
  115. public void startEvent()
  116. {
  117. if (!TvTEvent.startFight())
  118. {
  119. Announcements.getInstance().announceToAll("TvT Event: Event cancelled due to lack of Participation.");
  120. _log.info("TvTEventEngine[TvTManager.run()]: Lack of registration, abort event.");
  121. this.scheduleEventStart();
  122. }
  123. else
  124. {
  125. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting participants to an arena in "
  126. + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  127. _task.setStartTime(System.currentTimeMillis() + 60000L * Config.TVT_EVENT_RUNNING_TIME);
  128. ThreadPoolManager.getInstance().executeTask(_task);
  129. }
  130. }
  131. /**
  132. * Method to end the event and reward
  133. */
  134. public void endEvent()
  135. {
  136. Announcements.getInstance().announceToAll(TvTEvent.calculateRewards());
  137. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting back to the registration npc in "
  138. + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  139. TvTEvent.stopFight();
  140. this.scheduleEventStart();
  141. }
  142. public void skipDelay()
  143. {
  144. if (_task.nextRun.cancel(false))
  145. {
  146. _task.setStartTime(System.currentTimeMillis());
  147. ThreadPoolManager.getInstance().executeTask(_task);
  148. }
  149. }
  150. /**
  151. * Class for TvT cycles
  152. */
  153. class TvTStartTask implements Runnable
  154. {
  155. private long _startTime;
  156. public ScheduledFuture<?> nextRun;
  157. public TvTStartTask(long startTime)
  158. {
  159. _startTime = startTime;
  160. }
  161. public void setStartTime(long startTime)
  162. {
  163. _startTime = startTime;
  164. }
  165. /**
  166. * @see java.lang.Runnable#run()
  167. */
  168. @Override
  169. public void run()
  170. {
  171. int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
  172. if (delay > 0)
  173. {
  174. this.announce(delay);
  175. }
  176. int nextMsg = 0;
  177. if (delay > 3600)
  178. {
  179. nextMsg = delay - 3600;
  180. }
  181. else if (delay > 1800)
  182. {
  183. nextMsg = delay - 1800;
  184. }
  185. else if (delay > 900)
  186. {
  187. nextMsg = delay - 900;
  188. }
  189. else if (delay > 600)
  190. {
  191. nextMsg = delay - 600;
  192. }
  193. else if (delay > 300)
  194. {
  195. nextMsg = delay - 300;
  196. }
  197. else if (delay > 60)
  198. {
  199. nextMsg = delay - 60;
  200. }
  201. else if (delay > 5)
  202. {
  203. nextMsg = delay - 5;
  204. }
  205. else if (delay > 0)
  206. {
  207. nextMsg = delay;
  208. }
  209. else
  210. {
  211. // start
  212. if (TvTEvent.isInactive())
  213. {
  214. TvTManager.this.startReg();
  215. }
  216. else if (TvTEvent.isParticipating())
  217. {
  218. TvTManager.this.startEvent();
  219. }
  220. else
  221. {
  222. TvTManager.this.endEvent();
  223. }
  224. }
  225. if (delay > 0)
  226. {
  227. nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
  228. }
  229. }
  230. private void announce(long time)
  231. {
  232. if (time >= 3600 && time % 3600 == 0)
  233. {
  234. if (TvTEvent.isParticipating())
  235. {
  236. Announcements.getInstance().announceToAll("TvT Event: " + (time / 60 / 60) + " hour(s) until registration is closed!");
  237. }
  238. else if (TvTEvent.isStarted())
  239. {
  240. TvTEvent.sysMsgToAllParticipants("TvT Event: " + (time / 60 / 60) + " hour(s) until event is finished!");
  241. }
  242. }
  243. else if (time >= 60)
  244. {
  245. if (TvTEvent.isParticipating())
  246. {
  247. Announcements.getInstance().announceToAll("TvT Event: " + (time / 60) + " minute(s) until registration is closed!");
  248. }
  249. else if (TvTEvent.isStarted())
  250. {
  251. TvTEvent.sysMsgToAllParticipants("TvT Event: " + (time / 60) + " minute(s) until the event is finished!");
  252. }
  253. }
  254. else
  255. {
  256. if (TvTEvent.isParticipating())
  257. {
  258. Announcements.getInstance().announceToAll("TvT Event: " + time + " second(s) until registration is closed!");
  259. }
  260. else if (TvTEvent.isStarted())
  261. {
  262. TvTEvent.sysMsgToAllParticipants("TvT Event: " + time + " second(s) until the event is finished!");
  263. }
  264. }
  265. }
  266. }
  267. @SuppressWarnings("synthetic-access")
  268. private static class SingletonHolder
  269. {
  270. protected static final TvTManager _instance = new TvTManager();
  271. }
  272. }