2014-01-30 22:35:32 +01:00
|
|
|
<?php
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
namespace ManiaControl\Callbacks;
|
2014-01-30 22:35:32 +01:00
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/**
|
2014-05-07 23:00:07 +02:00
|
|
|
* Class for managing Timed Callbacks
|
2014-04-12 12:14:37 +02:00
|
|
|
*
|
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-04-12 12:14:37 +02:00
|
|
|
*/
|
2014-01-30 22:35:32 +01:00
|
|
|
class TimerManager {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2014-05-12 20:00:58 +02:00
|
|
|
* @param float $seconds
|
2014-01-30 23:34:36 +01:00
|
|
|
*/
|
2014-05-12 20:00:58 +02:00
|
|
|
public function registerOneTimeListening(TimerListener $listener, $method, $seconds) {
|
|
|
|
$this->registerTimerListening($listener, $method, $seconds, true);
|
2014-01-30 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 22:35:32 +01:00
|
|
|
/**
|
2014-05-02 16:13:45 +02: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
|
2014-05-12 20:00:58 +02:00
|
|
|
* @param float $seconds
|
2014-05-07 22:10:35 +02:00
|
|
|
* @param bool $oneTime
|
2014-01-30 22:35:32 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-12 20:00:58 +02:00
|
|
|
public function registerTimerListening(TimerListener $listener, $method, $seconds, $oneTime = false) {
|
2014-03-02 11:25:47 +01:00
|
|
|
if ((!is_string($method) || !method_exists($listener, $method)) && !is_callable($method)) {
|
2014-05-07 23:00:07 +02:00
|
|
|
trigger_error("Given Listener (" . get_class($listener) . ") can't handle Timer Callback (No Method '{$method}')!");
|
2014-01-30 22:35:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-30 23:34:36 +01:00
|
|
|
|
2014-05-07 23:00:07 +02:00
|
|
|
// Build Timer Listening
|
2014-05-12 20:00:58 +02:00
|
|
|
$listening = new TimerListening($listener, $method, $seconds, $oneTime);
|
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) {
|
2014-05-07 23:00:07 +02:00
|
|
|
$removed = false;
|
|
|
|
foreach ($this->timerListenings as $key => &$listening) {
|
|
|
|
if ($listening->listener === $listener && $listening->method == $method) {
|
2014-05-02 17:50:30 +02:00
|
|
|
unset($this->timerListenings[$key]);
|
2014-05-07 23:00:07 +02:00
|
|
|
$removed = true;
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-07 23:00:07 +02:00
|
|
|
return $removed;
|
2014-05-02 17:50:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-30 23:34:36 +01:00
|
|
|
/**
|
2014-05-07 23:00:07 +02:00
|
|
|
* Unregister 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-05-07 23:00:07 +02:00
|
|
|
if ($listening->listener === $listener) {
|
|
|
|
unset($this->timerListenings[$key]);
|
|
|
|
$removed = true;
|
2014-01-30 23:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $removed;
|
2014-01-30 22:35:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage the Timings on every ms
|
|
|
|
*/
|
|
|
|
public function manageTimings() {
|
|
|
|
$time = microtime(true);
|
2014-01-31 00:41:10 +01:00
|
|
|
|
2014-05-07 23:00:07 +02:00
|
|
|
foreach ($this->timerListenings as $key => $listening) {
|
|
|
|
/** @var TimerListening $listening */
|
|
|
|
|
|
|
|
if (!$listening->isTimeReached($time)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($listening->oneTime) {
|
|
|
|
// Unregister one time Listening
|
|
|
|
unset($this->timerListenings[$key]);
|
2014-01-30 22:35:32 +01:00
|
|
|
}
|
2014-05-07 23:00:07 +02:00
|
|
|
|
|
|
|
$listening->tick();
|
|
|
|
|
|
|
|
// Call the User Function
|
|
|
|
$listening->triggerCallback($time);
|
2014-01-30 22:35:32 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-12 12:14:37 +02:00
|
|
|
}
|