DeBot Framework


core/Timers.php



/**
 * DeBot Core - Timers
 * Created by dab ??? ?? 2009
 * Last Edited: Aug 15 2010
 *
 * We maintain a time based 
 * 
 * @author David (dab) 
 * @version v1.0
*/
class Timers extends Singleton // implements ArrayAccess
{
	
    /**
     * The timers we are storing
     * @var array an array of timers
     */
	private $m_aTimers;
	
    /**
     * The last time we updated the timers
     * @var integer The last time we ran the timers (seconds eopch time)
     */
    private $m_iTime;
	
	public function load( ) 
	{
		$this -> m_iTime = time( );
	}
	
    /**
     * Add a timer to be used.
     * @param string $sNick the owner of the timer (bot)
     * @param integer $iSeconds the number of seconds to elapse.
     * @param integer $iRepeat the number of times to repeat > 1
     * @param object $oFunction The object function array(class, 'function' )
     * @param array $aParams The params to send to teh function/method call
     *
     * @return string The ID of the Timer
     */
	public function addTimer( $sNick, $iSeconds, $iRepeat, $oFunction, $aParams = null )
	{
		$iUID = substr( md5( uniqid( ) ), 3, 6 );

		$this -> m_aTimers [ $iUID ] [ 'owner' ] = $sNick;
		$this -> m_aTimers [ $iUID ] [ 'startsecs' ] = $iSeconds;
		$this -> m_aTimers [ $iUID ] [ 'seconds' ] = $iSeconds;
		$this -> m_aTimers [ $iUID ] [ 'repeat' ] = $iRepeat;
		$this -> m_aTimers [ $iUID ] [ 'function' ] = $oFunction;
		if ( $aParams != null )
			$this -> m_aTimers [ $iUID ] [ 'fparams' ] = $aParams;

		return $iUID;
	}
	
    /**
     * Delete a timer (Duh?)
     *
     * @param string $iUID The ID of the timer to remove
     */
	public function delTimer( $iUID )
	{

		if ( isset( $this -> m_aTimers ) )
			unset( $this -> m_aTimers [ $iUID ] );

	}
	
    /**
     * TICK TOCK TICK TOCK. We check for timers to update.
     * We update our $this->m_iTime and loop through our timers,
     * ensure we are to tick. Then ExECUTE OUR FUNCTIONS!
     * Sorry... 3:28 am... really loopy and tired.
     */
	public function tick( )
	{

		if ( time() - $this -> m_iTime < 1 )
			return ;

		if ( count( $this -> m_aTimers ) > 0 )
		{
			foreach( $this -> m_aTimers as $iUID => $aTInfo )
			{
				
				$this -> m_aTimers[ $iUID ][ 'seconds']--;

				if ($this -> m_aTimers[ $iUID ][ 'seconds'] <= 0 )
				{
					if ( $aTInfo[ 'repeat' ] >= 1 )
						$this -> m_aTimers[ $iUID ][ 'repeat' ]--;

					if ( isset( $aTInfo[ 'fparams' ] ) )
						call_user_func( $aTInfo[ 'function' ], $aTInfo[ 'fparams' ] );
					else
						call_user_func( $aTInfo[ 'function' ] );

					if ( $aTInfo[ 'repeat' ] > 1 )
						$this -> m_aTimers[ $iUID ][ 'seconds'] = $aTInfo[ 'startsecs'];
					else
						$this -> delTimer( $iUID );
				}
			}
		}

		$this -> m_iTime = time();
	}

/*	
	public function offsetExists( $sKey )
	{
		if ( isset( $this -> m_aServerChannels[ $sName ] ) )
			return true;
	}
	
	public function offsetGet( $sKey )
	{

		

	} // End offsetGet Function

	public function offsetSet( $sKey, $mValue )
	{
		return ;
	}	
	public function offsetUnset( $sKey )
	{
		return ;
	}
*/

}
?>

DeBot Framework