Jump to content
  • 0

advertisement in events


l2jkain

Question

Hello would like help to leave the registration time every minute.

 

package net.sf.l2j.gameserver.model.entity.capturetheflag;

import java.util.Calendar;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;

import net.sf.l2j.Config;
import net.sf.l2j.commons.concurrent.ThreadPool;
import net.sf.l2j.gameserver.util.Broadcast;

/**
 * @author FBIagent
 */
public class CTFManager
{
	protected static final Logger _log = Logger.getLogger(CTFManager.class.getName());
	
	/** Task for event cycles<br> */
	private CTFStartTask _task;
	
	/**
	 * New instance only by getInstance()<br>
	 */
	public CTFManager()
	{
		if (Config.CTF_EVENT_ENABLED)
		{
			CTFEvent.init();
			
			this.scheduleEventStart();
			_log.info("CTFEventEngine: Started.");
		}
		else
		{
			_log.info("CTFEventEngine: Disabled.");
		}
	}
	
	/**
	 * Initialize new/Returns the one and only instance<br><br>
	 *
	 * @return CTFManager<br>
	 */
	public static CTFManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	/**
	 * Starts CTFStartTask
	 */
	public void scheduleEventStart()
	{
		try
		{
			Calendar currentTime = Calendar.getInstance();
			Calendar nextStartTime = null;
			Calendar testStartTime = null;
			for (String timeOfDay : Config.CTF_EVENT_INTERVAL)
			{
				// Creating a Calendar object from the specified interval value
				testStartTime = Calendar.getInstance();
				testStartTime.setLenient(true);
				String[] splitTimeOfDay = timeOfDay.split(":");
				testStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTimeOfDay[0]));
				testStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTimeOfDay[1]));
				// If the date is in the past, make it the next day (Example: Checking for "1:00", when the time is 23:57.)
				if (testStartTime.getTimeInMillis() < currentTime.getTimeInMillis())
				{
					testStartTime.add(Calendar.DAY_OF_MONTH, 1);
				}
				// Check for the test date to be the minimum (smallest in the specified list)
				if (nextStartTime == null || testStartTime.getTimeInMillis() < nextStartTime.getTimeInMillis())
				{
					nextStartTime = testStartTime;
				}
			}
			if (nextStartTime != null)
			{
				_task = new CTFStartTask(nextStartTime.getTimeInMillis());
				ThreadPool.execute(_task);
			}
		}
		catch (Exception e)
		{
			_log.warning("CTFEventEngine[CTFManager.scheduleEventStart()]: Error figuring out a start time. Check CTFEventInterval in config file.");
		}
	}
	
	/**
	 * Method to start participation
	 */
	public void startReg()
	{
		if (!CTFEvent.startParticipation())
		{
			Broadcast.announceToOnlinePlayers("CTF Event: Event was cancelled.");
			_log.warning("CTFEventEngine[CTFManager.run()]: Error spawning event npc for participation.");
			
			this.scheduleEventStart();
		}
		else
		{
			Broadcast.announceToOnlinePlayers("CTF Event: Registration opened for " + Config.CTF_EVENT_PARTICIPATION_TIME + " minute(s).", true);
			Broadcast.announceToOnlinePlayers("CTF Event: Joinable in " + Config.CTF_JOIN_LOCATION + ".");
			
			if (Config.CTF_VOICED_COMMAND)
				Broadcast.announceToOnlinePlayers("CTF Event : Commands .ctfjoin .ctfleave .ctfinfo!");

			// schedule registration end
			_task.setStartTime(System.currentTimeMillis() + 60000L * Config.CTF_EVENT_PARTICIPATION_TIME);
			ThreadPool.execute(_task);
		}
	}
	
	/**
	 * Method to start the fight
	 */
	public void startEvent()
	{
		if (!CTFEvent.startFight())
		{
			Broadcast.announceToOnlinePlayers("CTF Event: Event cancelled due to lack of Participation.");
			_log.info("CTFEventEngine[CTFManager.run()]: Lack of registration, abort event.");
			
			this.scheduleEventStart();
		}
		else
		{
			CTFEvent.sysMsgToAllParticipants("CTF Event: Teleporting participants to an arena in "
					+ Config.CTF_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
			_task.setStartTime(System.currentTimeMillis() + 60000L * Config.CTF_EVENT_RUNNING_TIME);
			ThreadPool.execute(_task);
		}
	}
	
	/**
	 * Method to end the event and reward
	 */
	public void endEvent()
	{
		Broadcast.announceToOnlinePlayers(CTFEvent.calculateRewards());
		CTFEvent.sysMsgToAllParticipants("CTF Event: Teleporting back to the registration npc in " + Config.CTF_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
		CTFEvent.stopFight();
		
		this.scheduleEventStart();
	}
	
	public void skipDelay()
	{
		if (_task.nextRun.cancel(false))
		{
			_task.setStartTime(System.currentTimeMillis());
			ThreadPool.execute(_task);
		}
	}
	
	/**
	 * Class for CTF cycles
	 */
	class CTFStartTask implements Runnable
	{
		private long _startTime;
		public ScheduledFuture<?> nextRun;
		
		public CTFStartTask(long startTime)
		{
			_startTime = startTime;
		}
		
		public void setStartTime(long startTime)
		{
			_startTime = startTime;
		}
		
		/**
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run()
		{
			int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
			
			if (delay > 0)
			{
				this.announce(delay);
			}
			
			int nextMsg = 0;
			if (delay > 3600)
			{
				nextMsg = delay - 3600;
			}
			else if (delay > 1800)
			{
				nextMsg = delay - 1800;
			}
			else if (delay > 900)
			{
				nextMsg = delay - 900;
			}
			else if (delay > 600)
			{
				nextMsg = delay - 600;
			}
			else if (delay > 300)
			{
				nextMsg = delay - 300;
			}
			else if (delay > 60)
			{
				nextMsg = delay - 60;
			}
			else if (delay > 5)
			{
				nextMsg = delay - 5;
			}
			else if (delay > 0)
			{
				nextMsg = delay;
			}
			else
			{
				// start
				if (CTFEvent.isInactive())
				{
					CTFManager.this.startReg();
				}
				else if (CTFEvent.isParticipating())
				{
					CTFManager.this.startEvent();
				}
				else
				{
					CTFManager.this.endEvent();
				}
			}
			
			if (delay > 0)
			{
				nextRun = ThreadPool.schedule(this, nextMsg * 1000);
			}
		}
		
		private void announce(long time)
		{
			if (time >= 3600 && time % 3600 == 0)
			{
				if (CTFEvent.isParticipating())
				{
					Broadcast.announceToOnlinePlayers("CTF Event: " + (time / 60 / 60) + " hour(s) until registration is closed!");
				}
				else if (CTFEvent.isStarted())
				{
					CTFEvent.sysMsgToAllParticipants("CTF Event: " + (time / 60 / 60) + " hour(s) until event is finished!");
				}
			}
			else if (time >= 60)
			{
				if (CTFEvent.isParticipating())
				{
					Broadcast.announceToOnlinePlayers("CTF Event: " + (time / 60) + " minute(s) until registration is closed!");
				}
				else if (CTFEvent.isStarted())
				{
					CTFEvent.sysMsgToAllParticipants("CTF Event: " + (time / 60) + " minute(s) until the event is finished!");
				}
			}
			else
			{
				if (CTFEvent.isParticipating())
				{
					Broadcast.announceToOnlinePlayers("CTF Event: " + time + " second(s) until registration is closed!");
				}
				else if (CTFEvent.isStarted())
				{
					CTFEvent.sysMsgToAllParticipants("CTF Event: " + time + " second(s) until the event is finished!");
				}
			}
		}
	}

	private static class SingletonHolder
	{
		protected static final CTFManager _instance = new CTFManager();
	}
}

 

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...