model class for timer listenings
This commit is contained in:
parent
a79b6a3156
commit
c58298a0bd
77
application/core/Callbacks/TimerListening.php
Normal file
77
application/core/Callbacks/TimerListening.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ManiaControl\Callbacks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model Class for a TimerListening
|
||||||
|
*
|
||||||
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||||
|
* @copyright 2014 ManiaControl Team
|
||||||
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||||
|
*/
|
||||||
|
class TimerListening {
|
||||||
|
/*
|
||||||
|
* Public Properties
|
||||||
|
*/
|
||||||
|
public $listener = null;
|
||||||
|
public $method = null;
|
||||||
|
public $deltaTime = null;
|
||||||
|
public $oneTime = null;
|
||||||
|
public $lastTrigger = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new Timer Listening
|
||||||
|
*
|
||||||
|
* @param TimerListener $listener
|
||||||
|
* @param string $method
|
||||||
|
* @param float $deltaTime
|
||||||
|
*/
|
||||||
|
public function __construct(TimerListener $listener, $method, $deltaTime, $oneTime = false) {
|
||||||
|
$this->listener = $listener;
|
||||||
|
$this->method = $method;
|
||||||
|
$this->deltaTime = $deltaTime / 1000.;
|
||||||
|
$this->lastTrigger = microtime(true);
|
||||||
|
$this->oneTime = (bool)$oneTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increase last Trigger Time
|
||||||
|
*/
|
||||||
|
public function tick() {
|
||||||
|
$this->lastTrigger += $this->deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger the Listener's Method
|
||||||
|
*
|
||||||
|
* @param float $time
|
||||||
|
*/
|
||||||
|
public function triggerCallback($time) {
|
||||||
|
call_user_func($this->getUserFunction(), $time);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Callable User Function
|
||||||
|
*
|
||||||
|
* @return callable
|
||||||
|
*/
|
||||||
|
public function getUserFunction() {
|
||||||
|
if (is_callable($this->method)) {
|
||||||
|
return $this->method;
|
||||||
|
}
|
||||||
|
return array($this->listener, $this->method);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the desired Time is reached
|
||||||
|
*
|
||||||
|
* @param float $time
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isTimeReached($time = null) {
|
||||||
|
if (!$time) {
|
||||||
|
$time = microtime(true);
|
||||||
|
}
|
||||||
|
return ($this->lastTrigger + $this->deltaTime > $time);
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@ namespace ManiaControl\Callbacks;
|
|||||||
use ManiaControl\ManiaControl;
|
use ManiaControl\ManiaControl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for managing Timers
|
* Class for managing Timed Callbacks
|
||||||
*
|
*
|
||||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||||
* @copyright 2014 ManiaControl Team
|
* @copyright 2014 ManiaControl Team
|
||||||
@ -49,22 +49,12 @@ class TimerManager {
|
|||||||
*/
|
*/
|
||||||
public function registerTimerListening(TimerListener $listener, $method, $time, $oneTime = false) {
|
public function registerTimerListening(TimerListener $listener, $method, $time, $oneTime = false) {
|
||||||
if ((!is_string($method) || !method_exists($listener, $method)) && !is_callable($method)) {
|
if ((!is_string($method) || !method_exists($listener, $method)) && !is_callable($method)) {
|
||||||
trigger_error("Given listener (" . get_class($listener) . ") can't handle timer (no method '{$method}')!");
|
trigger_error("Given Listener (" . get_class($listener) . ") can't handle Timer Callback (No Method '{$method}')!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Init the Timer Listening
|
// Build Timer Listening
|
||||||
// TODO: extra model class
|
$listening = new TimerListening($listener, $method, $time, $oneTime);
|
||||||
$listening = new \stdClass();
|
|
||||||
$listening->listener = $listener;
|
|
||||||
$listening->method = $method;
|
|
||||||
$listening->deltaTime = $time / 1000;
|
|
||||||
$listening->oneTime = $oneTime;
|
|
||||||
if ($oneTime) {
|
|
||||||
$listening->lastTrigger = microtime(true);
|
|
||||||
} else {
|
|
||||||
$listening->lastTrigger = -1;
|
|
||||||
}
|
|
||||||
array_push($this->timerListenings, $listening);
|
array_push($this->timerListenings, $listening);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -78,17 +68,18 @@ class TimerManager {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function unregisterTimerListening(TimerListener $listener, $method) {
|
public function unregisterTimerListening(TimerListener $listener, $method) {
|
||||||
foreach ($this->timerListenings as $key => $listening) {
|
$removed = false;
|
||||||
if ($listening->listener == $listener && $listening->method == $method) {
|
foreach ($this->timerListenings as $key => &$listening) {
|
||||||
|
if ($listening->listener === $listener && $listening->method == $method) {
|
||||||
unset($this->timerListenings[$key]);
|
unset($this->timerListenings[$key]);
|
||||||
return true;
|
$removed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return $removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a Timer Listener
|
* Unregister a Timer Listener
|
||||||
*
|
*
|
||||||
* @param TimerListener $listener
|
* @param TimerListener $listener
|
||||||
* @return bool
|
* @return bool
|
||||||
@ -96,11 +87,10 @@ class TimerManager {
|
|||||||
public function unregisterTimerListenings(TimerListener $listener) {
|
public function unregisterTimerListenings(TimerListener $listener) {
|
||||||
$removed = false;
|
$removed = false;
|
||||||
foreach ($this->timerListenings as $key => &$listening) {
|
foreach ($this->timerListenings as $key => &$listening) {
|
||||||
if ($listening->listener != $listener) {
|
if ($listening->listener === $listener) {
|
||||||
continue;
|
unset($this->timerListenings[$key]);
|
||||||
|
$removed = true;
|
||||||
}
|
}
|
||||||
unset($this->timerListenings[$key]);
|
|
||||||
$removed = true;
|
|
||||||
}
|
}
|
||||||
return $removed;
|
return $removed;
|
||||||
}
|
}
|
||||||
@ -110,29 +100,23 @@ class TimerManager {
|
|||||||
*/
|
*/
|
||||||
public function manageTimings() {
|
public function manageTimings() {
|
||||||
$time = microtime(true);
|
$time = microtime(true);
|
||||||
foreach ($this->timerListenings as $key => &$listening) {
|
|
||||||
|
|
||||||
if (($listening->lastTrigger + $listening->deltaTime) <= $time) {
|
foreach ($this->timerListenings as $key => $listening) {
|
||||||
//Increase the lastTrigger time manually (to improve accuracy)
|
/** @var TimerListening $listening */
|
||||||
if ($listening->lastTrigger != -1) {
|
|
||||||
$listening->lastTrigger += $listening->deltaTime;
|
|
||||||
} else {
|
|
||||||
//Initialize Timer
|
|
||||||
$listening->lastTrigger = $time;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Unregister one time Listening
|
if (!$listening->isTimeReached($time)) {
|
||||||
if ($listening->oneTime == true) {
|
continue;
|
||||||
unset($this->timerListenings[$key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Call the User func (at the end to avoid endless loops)
|
|
||||||
if (is_callable($listening->method)) {
|
|
||||||
call_user_func($listening->method, $time);
|
|
||||||
} else {
|
|
||||||
call_user_func(array($listening->listener, $listening->method), $time);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($listening->oneTime) {
|
||||||
|
// Unregister one time Listening
|
||||||
|
unset($this->timerListenings[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$listening->tick();
|
||||||
|
|
||||||
|
// Call the User Function
|
||||||
|
$listening->triggerCallback($time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user