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
|
|
|
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
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>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-02 17:50:30 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-04-12 12:14:37 +02:00
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class TimerManager implements UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Private properties
|
2014-04-12 12:14:37 +02:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2014-01-30 22:35:32 +01:00
|
|
|
private $maniaControl = null;
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var TimerListening[] $timerListenings */
|
2014-01-30 22:35:32 +01:00
|
|
|
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:07:56 +02:00
|
|
|
* @param float $milliSeconds
|
2014-01-30 23:34:36 +01:00
|
|
|
*/
|
2014-05-12 20:07:56 +02:00
|
|
|
public function registerOneTimeListening(TimerListener $listener, $method, $milliSeconds) {
|
|
|
|
$this->registerTimerListening($listener, $method, $milliSeconds, 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:07:56 +02:00
|
|
|
* @param float $milliSeconds
|
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:07:56 +02:00
|
|
|
public function registerTimerListening(TimerListener $listener, $method, $milliSeconds, $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:07:56 +02:00
|
|
|
$listening = new TimerListening($listener, $method, $milliSeconds, $oneTime);
|
2014-05-19 15:22:53 +02:00
|
|
|
$this->addTimerListening($listening);
|
2014-01-30 23:34:36 +01:00
|
|
|
|
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-19 15:22:53 +02:00
|
|
|
/**
|
|
|
|
* Add a Listening to the current List of managed Timers
|
|
|
|
*
|
|
|
|
* @param TimerListening $timerListening
|
|
|
|
*/
|
|
|
|
public function addTimerListening(TimerListening $timerListening) {
|
|
|
|
array_push($this->timerListenings, $timerListening);
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-06-14 14:32:29 +02:00
|
|
|
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
|
|
|
}
|