TrackManiaControl/core/Callbacks/TimerManager.php
axelalex2 fa7ea9f4f3
Multiple Features to improve ManiaControl usability (#234)
* ServerUIPropertiesMenu for Configurator to edit builtin UIProperties of MP

* fixed unregister-functions of CallbackManager

* Reducing menuItemHeight in Configurator to avoid overlapping of the menu items

* Fully rebuild the admins menu after a player rights changed

* Added function to FileUtil to improve realpath, so symbolic links can be resolved

* Fixed indentation

* Update FileUtil.php

Fixed error in case of an absolute path on Unix-like systems.

* New CallQueue which runs once inbetween the MC-loops

* Added important queued call to be executed earlier

* ErrorMethod made optional, as in some cases, there might be nothing to fail

* Clean up repository from unnecessary files

* Added easy installation script for DB on Unix-like systems

* Replaced deprecated is_real by is_float

* Add milliseconds with dot instead of double colon

* Resolved deprecated curly braces error

* gitignore all hidden files (except git and gitignore)

* Update MC-update-check-interval, so that a restart is not required

* Update gitignore to not ignore changes in MCTeam-Plugins

* Update gitignore again

* And another try

* fixed MasterAdmin //delrec, added personal /delrec-command with permission-setting

* Increase version number of local records plugin

* Add Permission to delete any record

* Reworked notifications of locals, removed private only, added private at rank

* Fix formatting

* Allow AuthenticationManager to store Plugin Permissions

* Method to check, if a named function call is already queued

* Added command disable feature

* Reset timer if DeltaTime updated, so it does not try to catch up missed timings

* Added private notification setting

* To reduce load of multiple records (especially in rounds), queue call chat notifications

* Added internal function to plugin manager to return plugin menu

* restore .idea codestyle files

* Update MC-Version number to 0.250
2020-02-24 17:20:51 +01:00

156 lines
3.9 KiB
PHP

<?php
namespace ManiaControl\Callbacks;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\ManiaControl;
/**
* Class for managing Timed Callbacks
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class TimerManager implements UsageInformationAble {
use UsageInformationTrait;
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
/** @var TimerListening[] $timerListenings */
private $timerListenings = array();
/**
* Construct a new Timer Manager
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Registers a One Time Listening
*
* @param TimerListener $listener
* @param string $method
* @param float $milliSeconds
*/
public function registerOneTimeListening(TimerListener $listener, $method, $milliSeconds) {
$this->registerTimerListening($listener, $method, $milliSeconds, true);
}
/**
* Register a Timer Listening, note < 10ms it can get inaccurate
*
* @param TimerListener $listener
* @param string $method
* @param float $milliSeconds
* @param bool $oneTime
* @return bool
*/
public function registerTimerListening(TimerListener $listener, $method, $milliSeconds, $oneTime = false) {
if ((!is_string($method) || !method_exists($listener, $method)) && !is_callable($method)) {
trigger_error("Given Listener (" . get_class($listener) . ") can't handle Timer Callback (No Method '{$method}')!");
return false;
}
// Build Timer Listening
$listening = new TimerListening($listener, $method, $milliSeconds, $oneTime);
$this->addTimerListening($listening);
return true;
}
/**
* Add a Listening to the current List of managed Timers
*
* @param TimerListening $timerListening
*/
public function addTimerListening(TimerListening $timerListening) {
array_push($this->timerListenings, $timerListening);
}
/**
* Update the deltaTime of a Timer Listening
*
* @param TimerListener $listener
* @param string|callable $method
* @param float $milliSeconds
*/
public function updateTimerListening(TimerListener $listener, $method, $milliSeconds) {
$updated = false;
foreach ($this->timerListenings as $key => &$listening) {
if ($listening->listener === $listener && $listening->method === $method) {
$listening->setDeltaTime($milliSeconds);
$updated = true;
}
}
return $updated;
}
/**
* Unregister a Timer Listening
*
* @param TimerListener $listener
* @param string $method
* @return bool
*/
public function unregisterTimerListening(TimerListener $listener, $method) {
$removed = false;
foreach ($this->timerListenings as $key => &$listening) {
if ($listening->listener === $listener && $listening->method === $method) {
unset($this->timerListenings[$key]);
$removed = true;
}
}
return $removed;
}
/**
* Unregister a Timer Listener
*
* @param TimerListener $listener
* @return bool
*/
public function unregisterTimerListenings(TimerListener $listener) {
$removed = false;
foreach ($this->timerListenings as $key => &$listening) {
if ($listening->listener === $listener) {
unset($this->timerListenings[$key]);
$removed = true;
}
}
return $removed;
}
/**
* Manage the Timings on every ms
*/
public function manageTimings() {
$time = microtime(true);
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]);
}
$listening->tick();
// Call the User Function
$listening->triggerCallback($time);
}
}
}