Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. package cron;
  2.  
  3. import java.util.Calendar;
  4. import java.util.concurrent.ScheduledFuture;
  5. import java.util.logging.Logger;
  6.  
  7. import com.l2jserver.gameserver.Announcements;
  8. import com.l2jserver.gameserver.ThreadPoolManager;
  9. import com.l2jserver.gameserver.util.Util;
  10.  
  11. import events.Tournament.Tournament;
  12. import events.Tournament.TournamentManager;
  13.  
  14. /**
  15.  * @author denser
  16.  */
  17. public class AutoEventTournament
  18. {
  19.     public static final Logger _log = Logger.getLogger(AutoEventTournament.class.getName());
  20.    
  21.     public startTournament _task;
  22.     public String[] EVENT_INTERVAL = { "23:59" };
  23.     public int[] EVENT_INTERVAL_DAYS = { Calendar.WEDNESDAY };
  24.    
  25.     public AutoEventTournament()
  26.     {
  27.         if (Util.contains(EVENT_INTERVAL_DAYS, Calendar.DAY_OF_WEEK))
  28.         {
  29.             this.calculateEventStart();
  30.             _log.info(getClass().getSimpleName() + ": Started.");
  31.         }
  32.     }
  33.    
  34.     private class startTournament implements Runnable
  35.     {
  36.         private long _startTime;
  37.         public ScheduledFuture<?> nextRun;
  38.        
  39.         public startTournament(long startTime)
  40.         {
  41.             _startTime = startTime;
  42.         }
  43.        
  44.         public void setStartTime(long startTime)
  45.         {
  46.             _startTime = startTime;
  47.         }
  48.        
  49.         private long getTime()
  50.         {
  51.             long delay = Math.round((_startTime - System.currentTimeMillis()) / 1000L);
  52.             return delay;
  53.         }
  54.        
  55.         private void announce(long time)
  56.         {
  57.             Announcements announce = Announcements.getInstance();
  58.            
  59.             if (time >= 3600 && time % 3600 == 0)
  60.             {
  61.                 if (!TournamentManager.getInstance().isRunning())
  62.                     announce.announceToAll(Tournament.EVENT_NAME + ": " + (time / 60 / 60) + " часов до начала! Спешите на регистрацию!");
  63.             }
  64.             else if (time >= 60)
  65.             {
  66.                 if (!TournamentManager.getInstance().isRunning())
  67.                     announce.announceToAll(Tournament.EVENT_NAME + ": " + (time / 60) + " минут до начала! Спешите на регистрацию!");
  68.             }
  69.             else
  70.             {
  71.                 if (!TournamentManager.getInstance().isRunning())
  72.                     announce.announceToAll(Tournament.EVENT_NAME + ": " + time + " секунд до начала! Спешите на регистрацию!");
  73.             }
  74.         }
  75.        
  76.         public void run()
  77.         {
  78.             int delay = Math.round((_startTime - System.currentTimeMillis()) / 1000F);
  79.            
  80.             if (delay > 0)
  81.                 this.announce(delay);
  82.            
  83.             int nextMsg = 0;
  84.             if (delay > 3600)
  85.                 nextMsg = delay - 3600;
  86.             else if (delay > 1800)
  87.                 nextMsg = delay - 1800;
  88.             else if (delay > 900)
  89.                 nextMsg = delay - 900;
  90.             else if (delay > 600)
  91.                 nextMsg = delay - 600;
  92.             else if (delay > 300)
  93.                 nextMsg = delay - 300;
  94.             else if (delay > 60)
  95.                 nextMsg = delay - 60;
  96.             else if (delay > 5)
  97.                 nextMsg = delay - 5;
  98.             else if (delay > 0)
  99.                 nextMsg = delay;
  100.             else
  101.             {
  102.                 //start
  103.                 TournamentManager.getInstance().start();
  104.             }
  105.            
  106.             if (delay > 0)
  107.                 nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
  108.         }
  109.     }
  110.    
  111.     public void calculateEventStart()
  112.     {
  113.         try
  114.         {
  115.             Calendar currentTime = Calendar.getInstance();
  116.             Calendar nextStartTime = null;
  117.             Calendar testStartTime = null;
  118.             for (String timeOfDay : EVENT_INTERVAL)
  119.             {
  120.                 // Creating a Calendar object from the specified interval value
  121.                 testStartTime = Calendar.getInstance();
  122.                 testStartTime.setLenient(true);
  123.                 String[] splitTimeOfDay = timeOfDay.split(":");
  124.                 testStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTimeOfDay[0]));
  125.                 testStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTimeOfDay[1]));
  126.                 // If the date is in the past, make it the next day (Example: Checking for "1:00", when the time is 23:57.)
  127.                 if (testStartTime.getTimeInMillis() < currentTime.getTimeInMillis())
  128.                     testStartTime.add(Calendar.DAY_OF_MONTH, 1);
  129.                 // Check for the test date to be the minimum (smallest in the specified list)
  130.                 if (nextStartTime == null || testStartTime.getTimeInMillis() < nextStartTime.getTimeInMillis())
  131.                     nextStartTime = testStartTime;
  132.             }
  133.             _task = new startTournament(nextStartTime.getTimeInMillis());
  134.             ThreadPoolManager.getInstance().executeTask(_task);
  135.         }
  136.         catch (Exception e)
  137.         {
  138.             _log.warning("Tornament: Error with interval check.");
  139.         }
  140.     }
  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.     public long getTimeToNextStep()
  152.     {
  153.         return _task.getTime();
  154.     }
  155.    
  156.     public static void main(String[] args)
  157.     {
  158.         new AutoEventTournament();
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement