TvTManager.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (nextStartTime != null)
  85. {
  86. _task = new TvTStartTask(nextStartTime.getTimeInMillis());
  87. ThreadPoolManager.getInstance().executeTask(_task);
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. _log.warning("TvTEventEngine[TvTManager.scheduleEventStart()]: Error figuring out a start time. Check TvTEventInterval in config file.");
  93. }
  94. }
  95. /**
  96. * Method to start participation
  97. */
  98. public void startReg()
  99. {
  100. if (!TvTEvent.startParticipation())
  101. {
  102. Announcements.getInstance().announceToAll("TvT Event: Event was cancelled.");
  103. _log.warning("TvTEventEngine[TvTManager.run()]: Error spawning event npc for participation.");
  104. this.scheduleEventStart();
  105. }
  106. else
  107. {
  108. Announcements.getInstance().announceToAll("TvT Event: Registration opened for " + Config.TVT_EVENT_PARTICIPATION_TIME
  109. + " minute(s).");
  110. // schedule registration end
  111. _task.setStartTime(System.currentTimeMillis() + 60000L * Config.TVT_EVENT_PARTICIPATION_TIME);
  112. ThreadPoolManager.getInstance().executeTask(_task);
  113. }
  114. }
  115. /**
  116. * Method to start the fight
  117. */
  118. public void startEvent()
  119. {
  120. if (!TvTEvent.startFight())
  121. {
  122. Announcements.getInstance().announceToAll("TvT Event: Event cancelled due to lack of Participation.");
  123. _log.info("TvTEventEngine[TvTManager.run()]: Lack of registration, abort event.");
  124. this.scheduleEventStart();
  125. }
  126. else
  127. {
  128. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting participants to an arena in "
  129. + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  130. _task.setStartTime(System.currentTimeMillis() + 60000L * Config.TVT_EVENT_RUNNING_TIME);
  131. ThreadPoolManager.getInstance().executeTask(_task);
  132. }
  133. }
  134. /**
  135. * Method to end the event and reward
  136. */
  137. public void endEvent()
  138. {
  139. Announcements.getInstance().announceToAll(TvTEvent.calculateRewards());
  140. TvTEvent.sysMsgToAllParticipants("TvT Event: Teleporting back to the registration npc in "
  141. + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  142. TvTEvent.stopFight();
  143. this.scheduleEventStart();
  144. }
  145. public void skipDelay()
  146. {
  147. if (_task.nextRun.cancel(false))
  148. {
  149. _task.setStartTime(System.currentTimeMillis());
  150. ThreadPoolManager.getInstance().executeTask(_task);
  151. }
  152. }
  153. /**
  154. * Class for TvT cycles
  155. */
  156. class TvTStartTask implements Runnable
  157. {
  158. private long _startTime;
  159. public ScheduledFuture<?> nextRun;
  160. public TvTStartTask(long startTime)
  161. {
  162. _startTime = startTime;
  163. }
  164. public void setStartTime(long startTime)
  165. {
  166. _startTime = startTime;
  167. }
  168. /**
  169. * @see java.lang.Runnable#run()
  170. */
  171. @Override
  172. public void run()
  173. {
  174. int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
  175. if (delay > 0)
  176. {
  177. this.announce(delay);
  178. }
  179. int nextMsg = 0;
  180. if (delay > 3600)
  181. {
  182. nextMsg = delay - 3600;
  183. }
  184. else if (delay > 1800)
  185. {
  186. nextMsg = delay - 1800;
  187. }
  188. else if (delay > 900)
  189. {
  190. nextMsg = delay - 900;
  191. }
  192. else if (delay > 600)
  193. {
  194. nextMsg = delay - 600;
  195. }
  196. else if (delay > 300)
  197. {
  198. nextMsg = delay - 300;
  199. }
  200. else if (delay > 60)
  201. {
  202. nextMsg = delay - 60;
  203. }
  204. else if (delay > 5)
  205. {
  206. nextMsg = delay - 5;
  207. }
  208. else if (delay > 0)
  209. {
  210. nextMsg = delay;
  211. }
  212. else
  213. {
  214. // start
  215. if (TvTEvent.isInactive())
  216. {
  217. TvTManager.this.startReg();
  218. }
  219. else if (TvTEvent.isParticipating())
  220. {
  221. TvTManager.this.startEvent();
  222. }
  223. else
  224. {
  225. TvTManager.this.endEvent();
  226. }
  227. }
  228. if (delay > 0)
  229. {
  230. nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
  231. }
  232. }
  233. private void announce(long time)
  234. {
  235. if (time >= 3600 && time % 3600 == 0)
  236. {
  237. if (TvTEvent.isParticipating())
  238. {
  239. Announcements.getInstance().announceToAll("TvT Event: " + (time / 60 / 60) + " hour(s) until registration is closed!");
  240. }
  241. else if (TvTEvent.isStarted())
  242. {
  243. TvTEvent.sysMsgToAllParticipants("TvT Event: " + (time / 60 / 60) + " hour(s) until event is finished!");
  244. }
  245. }
  246. else if (time >= 60)
  247. {
  248. if (TvTEvent.isParticipating())
  249. {
  250. Announcements.getInstance().announceToAll("TvT Event: " + (time / 60) + " minute(s) until registration is closed!");
  251. }
  252. else if (TvTEvent.isStarted())
  253. {
  254. TvTEvent.sysMsgToAllParticipants("TvT Event: " + (time / 60) + " minute(s) until the event is finished!");
  255. }
  256. }
  257. else
  258. {
  259. if (TvTEvent.isParticipating())
  260. {
  261. Announcements.getInstance().announceToAll("TvT Event: " + time + " second(s) until registration is closed!");
  262. }
  263. else if (TvTEvent.isStarted())
  264. {
  265. TvTEvent.sysMsgToAllParticipants("TvT Event: " + time + " second(s) until the event is finished!");
  266. }
  267. }
  268. }
  269. }
  270. @SuppressWarnings("synthetic-access")
  271. private static class SingletonHolder
  272. {
  273. protected static final TvTManager _instance = new TvTManager();
  274. }
  275. }