From 140c77441e17035caab9d312192e1e404c40f815 Mon Sep 17 00:00:00 2001 From: kremsy Date: Thu, 30 Jan 2014 22:35:32 +0100 Subject: [PATCH] begin timin --- .../core/Callbacks/CallbackManager.php | 39 +++++++------- application/core/Callbacks/TimerListener.php | 15 ++++++ application/core/Callbacks/TimerManager.php | 53 +++++++++++++++++++ application/core/ManiaControl.php | 6 ++- application/core/Players/PlayerList.php | 9 +++- 5 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 application/core/Callbacks/TimerListener.php create mode 100644 application/core/Callbacks/TimerManager.php diff --git a/application/core/Callbacks/CallbackManager.php b/application/core/Callbacks/CallbackManager.php index 206195c3..39b8ff1e 100644 --- a/application/core/Callbacks/CallbackManager.php +++ b/application/core/Callbacks/CallbackManager.php @@ -69,6 +69,7 @@ class CallbackManager { $this->last1Second = time(); $this->last5Second = time(); $this->last1Minute = time(); + } /** @@ -80,11 +81,11 @@ class CallbackManager { * @return bool */ 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}')!"); return false; } - if(!array_key_exists($callbackName, $this->callbackListeners)) { + if (!array_key_exists($callbackName, $this->callbackListeners)) { $this->callbackListeners[$callbackName] = array(); } array_push($this->callbackListeners[$callbackName], array($listener, $method)); @@ -100,11 +101,11 @@ class CallbackManager { * @return bool */ 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}')!"); return false; } - if(!array_key_exists($callbackName, $this->scriptCallbackListener)) { + if (!array_key_exists($callbackName, $this->scriptCallbackListener)) { $this->scriptCallbackListener[$callbackName] = array(); } array_push($this->scriptCallbackListener[$callbackName], array($listener, $method)); @@ -121,7 +122,7 @@ class CallbackManager { $removed = false; foreach($this->callbackListeners as &$listeners) { foreach($listeners as $key => &$listenerCallback) { - if($listenerCallback[0] != $listener) { + if ($listenerCallback[0] != $listener) { continue; } unset($listeners[$key]); @@ -141,7 +142,7 @@ class CallbackManager { $removed = false; foreach($this->scriptCallbackListener as &$listeners) { foreach($listeners as $key => &$listenerCallback) { - if($listenerCallback[0] != $listener) { + if ($listenerCallback[0] != $listener) { continue; } unset($listeners[$key]); @@ -158,7 +159,7 @@ class CallbackManager { * @param array $callback */ public function triggerCallback($callbackName, array $callback) { - if(!array_key_exists($callbackName, $this->callbackListeners)) { + if (!array_key_exists($callbackName, $this->callbackListeners)) { return; } foreach($this->callbackListeners[$callbackName] as $listener) { @@ -173,7 +174,7 @@ class CallbackManager { * @param array $callback */ public function triggerScriptCallback($callbackName, array $callback) { - if(!array_key_exists($callbackName, $this->scriptCallbackListener)) { + if (!array_key_exists($callbackName, $this->scriptCallbackListener)) { return; } foreach($this->scriptCallbackListener[$callbackName] as $listener) { @@ -186,17 +187,19 @@ class CallbackManager { */ public function manageCallbacks() { // Timed callbacks + $this->maniaControl->timerManager->manageTimings(); $this->manageTimedCallbacks(); // Server Callbacks - if(!$this->maniaControl->client) { + if (!$this->maniaControl->client) { return; } - try{ + try { $callbacks = $this->maniaControl->client->executeCallbacks(); - }catch(Exception $e){ + } catch(Exception $e) { trigger_error("Error reading server callbacks. " . $e->getMessage()); + return; } // Handle callbacks @@ -204,14 +207,14 @@ class CallbackManager { $callbackName = $callback[0]; switch($callbackName) { case 'ManiaPlanet.BeginMap': - if(!$this->mapBegan) { + if (!$this->mapBegan) { $this->triggerCallback(self::CB_MC_BEGINMAP, $callback); $this->mapBegan = true; $this->mapEnded = false; } break; case 'ManiaPlanet.EndMap': - if(!$this->mapEnded) { + if (!$this->mapEnded) { $this->triggerCallback(self::CB_MC_ENDMAP, $callback); $this->mapEnded = true; $this->mapBegan = false; @@ -244,7 +247,7 @@ class CallbackManager { case 'BeginMap': case 'LibXmlRpc_BeginMap': $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); - if(!$this->mapBegan) { + if (!$this->mapBegan) { $this->triggerCallback(self::CB_MC_BEGINMAP, $callback); $this->mapBegan = true; $this->mapEnded = false; @@ -253,7 +256,7 @@ class CallbackManager { case 'EndMap': case 'LibXmlRpc_EndMap': $this->triggerScriptCallback($scriptCallbackName, $scriptCallbackData); - if(!$this->mapEnded) { + if (!$this->mapEnded) { $this->triggerCallback(self::CB_MC_ENDMAP, $callback); $this->mapEnded = true; $this->mapBegan = false; @@ -270,21 +273,21 @@ class CallbackManager { */ private function manageTimedCallbacks() { // 1 second - if($this->last1Second > time() - 1) { + if ($this->last1Second > time() - 1) { return; } $this->last1Second = time(); $this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND)); // 5 second - if($this->last5Second > time() - 5) { + if ($this->last5Second > time() - 5) { return; } $this->last5Second = time(); $this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND)); // 1 minute - if($this->last1Minute > time() - 60) { + if ($this->last1Minute > time() - 60) { return; } $this->last1Minute = time(); diff --git a/application/core/Callbacks/TimerListener.php b/application/core/Callbacks/TimerListener.php new file mode 100644 index 00000000..65ab67f4 --- /dev/null +++ b/application/core/Callbacks/TimerListener.php @@ -0,0 +1,15 @@ +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"]); + } + } + } + +} \ No newline at end of file diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 1dbb0535..41c1ea55 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -6,6 +6,7 @@ use ErrorHandler; use ManiaControl\Admin\ActionsMenu; use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackManager; +use ManiaControl\Callbacks\TimerManager; use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandManager; use ManiaControl\Configurators\Configurator; @@ -62,6 +63,7 @@ class ManiaControl implements CommandListener { public $statisticManager = null; public $updateManager = null; public $errorHandler = null; + public $timerManager = null; /** * Private properties @@ -83,6 +85,7 @@ class ManiaControl implements CommandListener { // Load ManiaControl Modules $this->database = new Database($this); $this->callbackManager = new CallbackManager($this); + $this->timerManager = new TimerManager($this); $this->settingManager = new SettingManager($this); $this->statisticManager = new StatisticManager($this); $this->manialinkManager = new ManialinkManager($this); @@ -277,7 +280,8 @@ class ManiaControl implements CommandListener { // Yield for next tick $loopEnd = microtime(true); - $sleepTime = 1000 - $loopEnd + $loopStart; + + $sleepTime = (int) (1000 - $loopEnd + $loopStart); if ($sleepTime > 0) { usleep($sleepTime); } diff --git a/application/core/Players/PlayerList.php b/application/core/Players/PlayerList.php index 9cf23830..9ce87443 100644 --- a/application/core/Players/PlayerList.php +++ b/application/core/Players/PlayerList.php @@ -18,6 +18,7 @@ use FML\Script\Script; use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; +use ManiaControl\Callbacks\TimerListener; use ManiaControl\Formatter; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\ManialinkManager; @@ -28,7 +29,7 @@ use ManiaControl\Manialinks\ManialinkPageAnswerListener; * * @author steeffeen & kremsy */ -class PlayerList implements ManialinkPageAnswerListener, CallbackListener { +class PlayerList implements ManialinkPageAnswerListener, CallbackListener, TimerListener { /** * 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_PLAYERJOINED, $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) {