begin timin

This commit is contained in:
kremsy 2014-01-30 22:35:32 +01:00 committed by Steffen Schröder
parent b751629dc5
commit 140c77441e
5 changed files with 102 additions and 20 deletions

View File

@ -69,6 +69,7 @@ class CallbackManager {
$this->last1Second = time(); $this->last1Second = time();
$this->last5Second = time(); $this->last5Second = time();
$this->last1Minute = time(); $this->last1Minute = time();
} }
/** /**
@ -80,11 +81,11 @@ class CallbackManager {
* @return bool * @return bool
*/ */
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
if(!method_exists($listener, $method)) { if (!method_exists($listener, $method)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!"); trigger_error("Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
return false; return false;
} }
if(!array_key_exists($callbackName, $this->callbackListeners)) { if (!array_key_exists($callbackName, $this->callbackListeners)) {
$this->callbackListeners[$callbackName] = array(); $this->callbackListeners[$callbackName] = array();
} }
array_push($this->callbackListeners[$callbackName], array($listener, $method)); array_push($this->callbackListeners[$callbackName], array($listener, $method));
@ -100,11 +101,11 @@ class CallbackManager {
* @return bool * @return bool
*/ */
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
if(!method_exists($listener, $method)) { if (!method_exists($listener, $method)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!"); trigger_error("Given listener (" . get_class($listener) . ") can't handle script callback '{$callbackName}' (no method '{$method}')!");
return false; return false;
} }
if(!array_key_exists($callbackName, $this->scriptCallbackListener)) { if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
$this->scriptCallbackListener[$callbackName] = array(); $this->scriptCallbackListener[$callbackName] = array();
} }
array_push($this->scriptCallbackListener[$callbackName], array($listener, $method)); array_push($this->scriptCallbackListener[$callbackName], array($listener, $method));
@ -121,7 +122,7 @@ class CallbackManager {
$removed = false; $removed = false;
foreach($this->callbackListeners as &$listeners) { foreach($this->callbackListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) { foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] != $listener) { if ($listenerCallback[0] != $listener) {
continue; continue;
} }
unset($listeners[$key]); unset($listeners[$key]);
@ -141,7 +142,7 @@ class CallbackManager {
$removed = false; $removed = false;
foreach($this->scriptCallbackListener as &$listeners) { foreach($this->scriptCallbackListener as &$listeners) {
foreach($listeners as $key => &$listenerCallback) { foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] != $listener) { if ($listenerCallback[0] != $listener) {
continue; continue;
} }
unset($listeners[$key]); unset($listeners[$key]);
@ -158,7 +159,7 @@ class CallbackManager {
* @param array $callback * @param array $callback
*/ */
public function triggerCallback($callbackName, array $callback) { public function triggerCallback($callbackName, array $callback) {
if(!array_key_exists($callbackName, $this->callbackListeners)) { if (!array_key_exists($callbackName, $this->callbackListeners)) {
return; return;
} }
foreach($this->callbackListeners[$callbackName] as $listener) { foreach($this->callbackListeners[$callbackName] as $listener) {
@ -173,7 +174,7 @@ class CallbackManager {
* @param array $callback * @param array $callback
*/ */
public function triggerScriptCallback($callbackName, array $callback) { public function triggerScriptCallback($callbackName, array $callback) {
if(!array_key_exists($callbackName, $this->scriptCallbackListener)) { if (!array_key_exists($callbackName, $this->scriptCallbackListener)) {
return; return;
} }
foreach($this->scriptCallbackListener[$callbackName] as $listener) { foreach($this->scriptCallbackListener[$callbackName] as $listener) {
@ -186,17 +187,19 @@ class CallbackManager {
*/ */
public function manageCallbacks() { public function manageCallbacks() {
// Timed callbacks // Timed callbacks
$this->maniaControl->timerManager->manageTimings();
$this->manageTimedCallbacks(); $this->manageTimedCallbacks();
// Server Callbacks // Server Callbacks
if(!$this->maniaControl->client) { if (!$this->maniaControl->client) {
return; return;
} }
try{ try {
$callbacks = $this->maniaControl->client->executeCallbacks(); $callbacks = $this->maniaControl->client->executeCallbacks();
}catch(Exception $e){ } catch(Exception $e) {
trigger_error("Error reading server callbacks. " . $e->getMessage()); trigger_error("Error reading server callbacks. " . $e->getMessage());
return;
} }
// Handle callbacks // Handle callbacks
@ -204,14 +207,14 @@ class CallbackManager {
$callbackName = $callback[0]; $callbackName = $callback[0];
switch($callbackName) { switch($callbackName) {
case 'ManiaPlanet.BeginMap': case 'ManiaPlanet.BeginMap':
if(!$this->mapBegan) { if (!$this->mapBegan) {
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback); $this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
$this->mapBegan = true; $this->mapBegan = true;
$this->mapEnded = false; $this->mapEnded = false;
} }
break; break;
case 'ManiaPlanet.EndMap': case 'ManiaPlanet.EndMap':
if(!$this->mapEnded) { if (!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback); $this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true; $this->mapEnded = true;
$this->mapBegan = false; $this->mapBegan = false;
@ -244,7 +247,7 @@ class CallbackManager {
case 'BeginMap': case 'BeginMap':
case 'LibXmlRpc_BeginMap': case 'LibXmlRpc_BeginMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
if(!$this->mapBegan) { if (!$this->mapBegan) {
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback); $this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
$this->mapBegan = true; $this->mapBegan = true;
$this->mapEnded = false; $this->mapEnded = false;
@ -253,7 +256,7 @@ class CallbackManager {
case 'EndMap': case 'EndMap':
case 'LibXmlRpc_EndMap': case 'LibXmlRpc_EndMap':
$this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData);
if(!$this->mapEnded) { if (!$this->mapEnded) {
$this->triggerCallback(self::CB_MC_ENDMAP, $callback); $this->triggerCallback(self::CB_MC_ENDMAP, $callback);
$this->mapEnded = true; $this->mapEnded = true;
$this->mapBegan = false; $this->mapBegan = false;
@ -270,21 +273,21 @@ class CallbackManager {
*/ */
private function manageTimedCallbacks() { private function manageTimedCallbacks() {
// 1 second // 1 second
if($this->last1Second > time() - 1) { if ($this->last1Second > time() - 1) {
return; return;
} }
$this->last1Second = time(); $this->last1Second = time();
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND)); $this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
// 5 second // 5 second
if($this->last5Second > time() - 5) { if ($this->last5Second > time() - 5) {
return; return;
} }
$this->last5Second = time(); $this->last5Second = time();
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND)); $this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
// 1 minute // 1 minute
if($this->last1Minute > time() - 60) { if ($this->last1Minute > time() - 60) {
return; return;
} }
$this->last1Minute = time(); $this->last1Minute = time();

View File

@ -0,0 +1,15 @@
<?php
/**
* Interface for TimerListener
*
* @author steeffeen & kremsy
*/
namespace ManiaControl\Callbacks;
interface TimerListener {
/**
* Constants
*/
const TIMERLISTENER_INTERFACE = __CLASS__;
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Created by PhpStorm.
* User: Lukas
* Date: 30.01.14
* Time: 21:11
*/
namespace ManiaControl\Callbacks;
use ManiaControl\ManiaControl;
class TimerManager {
private $maniaControl = null;
private $timerListenings = array();
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Registers a Timing Listening, note < 10ms it can get inaccurate
*
* @param TimerListener $listener
* @param $method
* @param $time
* @return bool
*/
public function registerTimerListening(TimerListener $listener, $method, $time) {
if (!method_exists($listener, $method)) {
trigger_error("Given listener (" . get_class($listener) . ") can't handle timer (no method '{$method}')!");
return false;
}
array_push($this->timerListenings, array("Listener" => $listener, "Method" => $method, "DeltaTime" => ($time / 1000), "LastTrigger" => microtime(true)));
return true;
}
/**
* Manage the Timings on every ms
*/
public function manageTimings() {
$time = microtime(true);
foreach($this->timerListenings as $key => $listening) {
if ($listening["LastTrigger"] + $listening["DeltaTime"] < $time) {
call_user_func(array($listening["Listener"], $listening["Method"]), $time);
$this->timerListenings[$key]["LastTrigger"] += ($listening["DeltaTime"]);
}
}
}
}

View File

@ -6,6 +6,7 @@ use ErrorHandler;
use ManiaControl\Admin\ActionsMenu; use ManiaControl\Admin\ActionsMenu;
use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerManager;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Commands\CommandManager; use ManiaControl\Commands\CommandManager;
use ManiaControl\Configurators\Configurator; use ManiaControl\Configurators\Configurator;
@ -62,6 +63,7 @@ class ManiaControl implements CommandListener {
public $statisticManager = null; public $statisticManager = null;
public $updateManager = null; public $updateManager = null;
public $errorHandler = null; public $errorHandler = null;
public $timerManager = null;
/** /**
* Private properties * Private properties
@ -83,6 +85,7 @@ class ManiaControl implements CommandListener {
// Load ManiaControl Modules // Load ManiaControl Modules
$this->database = new Database($this); $this->database = new Database($this);
$this->callbackManager = new CallbackManager($this); $this->callbackManager = new CallbackManager($this);
$this->timerManager = new TimerManager($this);
$this->settingManager = new SettingManager($this); $this->settingManager = new SettingManager($this);
$this->statisticManager = new StatisticManager($this); $this->statisticManager = new StatisticManager($this);
$this->manialinkManager = new ManialinkManager($this); $this->manialinkManager = new ManialinkManager($this);
@ -277,7 +280,8 @@ class ManiaControl implements CommandListener {
// Yield for next tick // Yield for next tick
$loopEnd = microtime(true); $loopEnd = microtime(true);
$sleepTime = 1000 - $loopEnd + $loopStart;
$sleepTime = (int) (1000 - $loopEnd + $loopStart);
if ($sleepTime > 0) { if ($sleepTime > 0) {
usleep($sleepTime); usleep($sleepTime);
} }

View File

@ -18,6 +18,7 @@ use FML\Script\Script;
use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Formatter; use ManiaControl\Formatter;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkManager;
@ -28,7 +29,7 @@ use ManiaControl\Manialinks\ManialinkPageAnswerListener;
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
class PlayerList implements ManialinkPageAnswerListener, CallbackListener { class PlayerList implements ManialinkPageAnswerListener, CallbackListener, TimerListener {
/** /**
* Constants * Constants
*/ */
@ -75,6 +76,12 @@ class PlayerList implements ManialinkPageAnswerListener, CallbackListener {
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'updateWidget'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'updateWidget'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'updateWidget');
$this->maniaControl->callbackManager->registerCallbackListener(AuthenticationManager::CB_AUTH_LEVEL_CHANGED, $this, 'updateWidget'); $this->maniaControl->callbackManager->registerCallbackListener(AuthenticationManager::CB_AUTH_LEVEL_CHANGED, $this, 'updateWidget');
$this->maniaControl->timerManager->registerTimerListening($this, 'test', 1); //TODo just a test
}
public function test($triggerTime){
var_dump($triggerTime);
} }
public function addPlayerToShownList(Player $player, $showStatus = self::SHOWN_MAIN_WINDOW) { public function addPlayerToShownList(Player $player, $showStatus = self::SHOWN_MAIN_WINDOW) {