TrackManiaControl/application/core/Callbacks/TimerManager.php

139 lines
3.5 KiB
PHP
Raw Normal View History

2014-01-30 22:35:32 +01:00
<?php
namespace ManiaControl\Callbacks;
2014-01-30 22:35:32 +01:00
use ManiaControl\ManiaControl;
/**
* Class for managing Timers
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2014-01-30 22:35:32 +01:00
class TimerManager {
/*
* Private Properties
*/
2014-01-30 22:35:32 +01:00
private $maniaControl = null;
private $timerListenings = array();
2014-01-30 23:34:36 +01:00
/**
* Construct a new Timer Manager
*
2014-05-07 22:10:35 +02:00
* @param ManiaControl $maniaControl
2014-01-30 23:34:36 +01:00
*/
2014-01-30 22:35:32 +01:00
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
2014-01-30 23:34:36 +01:00
/**
* Registers a One Time Listening
*
* @param TimerListener $listener
2014-05-07 22:10:35 +02:00
* @param string $method
* @param float $time
2014-01-30 23:34:36 +01:00
*/
public function registerOneTimeListening(TimerListener $listener, $method, $time) {
$this->registerTimerListening($listener, $method, $time, true);
}
2014-01-30 22:35:32 +01:00
/**
* Register a Timer Listening, note < 10ms it can get inaccurate
2014-01-30 22:35:32 +01:00
*
2014-05-07 22:10:35 +02:00
* @param TimerListener $listener
* @param string $method
* @param float $time
* @param bool $oneTime
2014-01-30 22:35:32 +01:00
* @return bool
*/
2014-01-30 23:34:36 +01:00
public function registerTimerListening(TimerListener $listener, $method, $time, $oneTime = false) {
2014-03-02 11:25:47 +01:00
if ((!is_string($method) || !method_exists($listener, $method)) && !is_callable($method)) {
2014-01-30 22:35:32 +01:00
trigger_error("Given listener (" . get_class($listener) . ") can't handle timer (no method '{$method}')!");
return false;
}
2014-01-30 23:34:36 +01:00
//Init the Timer Listening
// TODO: extra model class
2014-05-02 17:50:30 +02:00
$listening = new \stdClass();
$listening->listener = $listener;
$listening->method = $method;
$listening->deltaTime = $time / 1000;
$listening->oneTime = $oneTime;
if ($oneTime) {
2014-03-02 11:25:47 +01:00
$listening->lastTrigger = microtime(true);
2014-05-02 17:50:30 +02:00
} else {
2014-03-02 11:25:47 +01:00
$listening->lastTrigger = -1;
}
2014-01-30 23:34:36 +01:00
array_push($this->timerListenings, $listening);
2014-01-30 22:35:32 +01:00
return true;
2014-01-30 23:34:36 +01:00
}
2014-01-30 22:35:32 +01:00
2014-05-02 17:50:30 +02:00
/**
* Unregister a Timer Listening
*
* @param TimerListener $listener
2014-05-07 22:10:35 +02:00
* @param string $method
2014-05-02 17:50:30 +02:00
* @return bool
*/
public function unregisterTimerListening(TimerListener $listener, $method) {
foreach ($this->timerListenings as $key => $listening) {
if ($listening->listener == $listener && $listening->method == $method) {
unset($this->timerListenings[$key]);
return true;
}
}
return false;
}
2014-01-30 23:34:36 +01:00
/**
2014-04-28 20:52:04 +02:00
* Remove a Timer Listener
2014-01-30 23:34:36 +01:00
*
2014-04-28 17:53:40 +02:00
* @param TimerListener $listener
2014-01-30 23:34:36 +01:00
* @return bool
*/
2014-04-28 17:53:40 +02:00
public function unregisterTimerListenings(TimerListener $listener) {
2014-01-30 23:34:36 +01:00
$removed = false;
2014-05-02 17:50:30 +02:00
foreach ($this->timerListenings as $key => &$listening) {
2014-01-30 23:34:36 +01:00
if ($listening->listener != $listener) {
continue;
}
unset($this->timerListenings[$key]);
$removed = true;
}
return $removed;
2014-01-30 22:35:32 +01:00
}
/**
* Manage the Timings on every ms
*/
public function manageTimings() {
$time = microtime(true);
2014-05-02 17:50:30 +02:00
foreach ($this->timerListenings as $key => &$listening) {
2014-01-31 00:41:10 +01:00
2014-01-30 23:34:36 +01:00
if (($listening->lastTrigger + $listening->deltaTime) <= $time) {
2014-01-31 00:41:10 +01:00
//Increase the lastTrigger time manually (to improve accuracy)
if ($listening->lastTrigger != -1) {
$listening->lastTrigger += $listening->deltaTime;
} else {
//Initialize Timer
$listening->lastTrigger = $time;
}
2014-01-30 23:34:36 +01:00
//Unregister one time Listening
if ($listening->oneTime == true) {
unset($this->timerListenings[$key]);
}
2014-01-31 00:41:10 +01:00
//Call the User func (at the end to avoid endless loops)
2014-02-07 19:49:00 +01:00
if (is_callable($listening->method)) {
call_user_func($listening->method, $time);
} else {
call_user_func(array($listening->listener, $listening->method), $time);
}
2014-01-30 22:35:32 +01:00
}
}
}
}