Location: PHPKode > projects > PhpBlock > A9.8/script/tick/tick.class.php
<?php
abstract class Tick{
	
	private $timeBase;
	protected $timeElapsedInMs;
	protected $timeBaseBetweenRuns;
	protected $resetTimeAfterRun;
	protected $serverTickNumber;
	
    public function __construct($parServerTickNumber, $parTimeBaseBetweenRuns, $parResetTimeAfterRun=false) {
        $this->timeBaseBetweenRuns = $parTimeBaseBetweenRuns;
        $this->timeBase = 1;
        $this->timeElapsedInMs = Server::getInstance()->getValue(Server::$SERVER_INFO_TYPE1_TICK_TIME_ELAPSED, $parServerTickNumber);
        $this->resetTimeAfterRun = $parResetTimeAfterRun;
        $this->serverTickNumber = $parServerTickNumber;
    }

    protected abstract function run();
    public abstract function getName();

    public function getTimeElapsedInMs() {
    	return $this->timeElapsedInMs;
    }
    
    public function setTimeBase($parTimeBase) {
        $this->timeBase = $parTimeBase;
    }

    public function getTimeBetweenRunsInMs() {
        return $this->timeBase * $this->timeBaseBetweenRuns;
    }

    public function getTimeSinceLastRun() {
        return $this->getTimeBetweenRunsInMs();
    }

    public function timeElapsed($parElapsedTimeInMs) {
        $locTimeBetweenRunsInMs = $this->getTimeBetweenRunsInMs();

        $this->timeElapsedInMs += $parElapsedTimeInMs;
        if($this->timeElapsedInMs > $locTimeBetweenRunsInMs) {
            $locBeginMsTick=microtime(true);
            Log::info("begin tick : ".$this->getName()." (elapsed time : ".$this->timeElapsedInMs."ms, for a needed of : ".$locTimeBetweenRunsInMs."ms)");
            $this->run();
            Log::info("end tick : ".$this->getName()." (duration : ".((microtime(true)-$locBeginMsTick)*1000)."ms)");
            if($this->resetTimeAfterRun) {
                $this->timeElapsedInMs = 0;
            } else {
                $this->timeElapsedInMs = min($this->timeElapsedInMs-$locTimeBetweenRunsInMs, 5*$locTimeBetweenRunsInMs);
            }
        }
        Server::getInstance()->upsertValue(Server::$SERVER_INFO_TYPE1_TICK_TIME_ELAPSED, $this->serverTickNumber, $this->timeElapsedInMs);
    }
}
?>
Return current item: PhpBlock