TrackManiaControl/application/core/Callbacks/TimerManager.php

134 lines
3.3 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;
/**
2014-05-07 23:00:07 +02:00
* Class for managing Timed Callbacks
*
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
*/
/** @var ManiaControl $maniaControl */
2014-01-30 22:35:32 +01:00
private $maniaControl = null;
/** @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
* @param float $milliSeconds
2014-01-30 23:34:36 +01: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
/**
* 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 $milliSeconds
2014-05-07 22:10:35 +02:00
* @param bool $oneTime
2014-01-30 22:35:32 +01:00
* @return bool
*/
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
$listening = new TimerListening($listener, $method, $milliSeconds, $oneTime);
$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
/**
* 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) {
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
}
}
}