TrackManiaControl/core/Callbacks/TimerManager.php

156 lines
3.9 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\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
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>
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
*/
class TimerManager implements UsageInformationAble {
use UsageInformationTrait;
/*
* 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);
}
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
/**
* 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;
}
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
}
}
}