2014-05-07 23:00:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Callbacks;
|
|
|
|
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
|
|
|
|
2014-05-07 23:00:07 +02:00
|
|
|
/**
|
2014-05-13 01:18:47 +02:00
|
|
|
* Model Class for a Timer Listening
|
2014-05-07 23:00:07 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2017-02-04 11:49:23 +01:00
|
|
|
* @copyright 2014-2017 ManiaControl Team
|
2014-05-07 23:00:07 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class TimerListening extends Listening implements UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
|
|
|
|
2014-05-07 23:00:07 +02:00
|
|
|
/*
|
|
|
|
* Public Properties
|
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
public $deltaTime = null;
|
|
|
|
public $oneTime = null;
|
2014-05-07 23:00:07 +02:00
|
|
|
public $lastTrigger = null;
|
2014-05-09 13:19:57 +02:00
|
|
|
public $instantCall = null;
|
2014-05-07 23:00:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new Timer Listening
|
|
|
|
*
|
|
|
|
* @param TimerListener $listener
|
2014-05-13 01:18:47 +02:00
|
|
|
* @param mixed $method
|
2014-05-12 20:07:56 +02:00
|
|
|
* @param float $milliSeconds
|
2014-05-09 13:22:21 +02:00
|
|
|
* @param bool $oneTime
|
2014-05-09 13:19:57 +02:00
|
|
|
* @param bool $instantCall
|
2014-05-07 23:00:07 +02:00
|
|
|
*/
|
2014-05-12 20:07:56 +02:00
|
|
|
public function __construct(TimerListener $listener, $method, $milliSeconds, $oneTime = false, $instantCall = true) {
|
2014-05-13 01:18:47 +02:00
|
|
|
parent::__construct($listener, $method);
|
|
|
|
|
2014-05-19 15:22:53 +02:00
|
|
|
$this->deltaTime = $milliSeconds / 1000.;
|
2017-03-26 19:44:55 +02:00
|
|
|
$this->oneTime = (bool) $oneTime;
|
2014-05-19 15:22:53 +02:00
|
|
|
if ($this->oneTime) {
|
2017-03-26 19:44:55 +02:00
|
|
|
$this->lastTrigger = microtime(true); //TODO verify before here was time()
|
2014-05-19 15:22:53 +02:00
|
|
|
}
|
2017-03-26 19:44:55 +02:00
|
|
|
$this->instantCall = (bool) $instantCall;
|
2014-05-09 13:19:57 +02:00
|
|
|
if (!$this->instantCall) {
|
|
|
|
$this->lastTrigger = microtime(true);
|
|
|
|
}
|
2014-05-07 23:00:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase last Trigger Time
|
|
|
|
*/
|
|
|
|
public function tick() {
|
2014-05-09 16:54:00 +02:00
|
|
|
if ($this->lastTrigger === null) {
|
|
|
|
$this->lastTrigger = microtime(true);
|
|
|
|
} else {
|
|
|
|
$this->lastTrigger += $this->deltaTime;
|
|
|
|
}
|
2014-05-07 23:00:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the desired Time is reached
|
|
|
|
*
|
|
|
|
* @param float $time
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isTimeReached($time = null) {
|
2014-05-09 13:19:57 +02:00
|
|
|
if ($this->lastTrigger === null) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-07 23:00:07 +02:00
|
|
|
if (!$time) {
|
|
|
|
$time = microtime(true);
|
|
|
|
}
|
2014-05-08 20:37:36 +02:00
|
|
|
return ($this->lastTrigger + $this->deltaTime <= $time);
|
2014-05-07 23:00:07 +02:00
|
|
|
}
|
|
|
|
}
|