diff --git a/application/core/Callbacks/TimerManager.php b/application/core/Callbacks/TimerManager.php index cc229a3f..a46c70e1 100644 --- a/application/core/Callbacks/TimerManager.php +++ b/application/core/Callbacks/TimerManager.php @@ -1,11 +1,9 @@ maniaControl = $maniaControl; } + + /** + * Registers a One Time Listening + * + * @param TimerListener $listener + * @param $method + * @param $time + */ + public function registerOneTimeListening(TimerListener $listener, $method, $time) { + $this->registerTimerListening($listener, $method, $time, true); + } + /** * Registers a Timing Listening, note < 10ms it can get inaccurate * @@ -27,14 +42,41 @@ class TimerManager { * @param $time * @return bool */ - public function registerTimerListening(TimerListener $listener, $method, $time) { + public function registerTimerListening(TimerListener $listener, $method, $time, $oneTime = false) { 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; + //Init the Timer Listening + $listening = new \stdClass(); + $listening->listener = $listener; + $listening->method = $method; + $listening->deltaTime = $time / 1000; + $listening->lastTrigger = -1; + $listening->oneTime = $oneTime; + + array_push($this->timerListenings, $listening); + + return true; + } + + /** + * Remove a Script Callback Listener + * + * @param CallbackListener $listener + * @return bool + */ + public function unregisterTimerListenings(CallbackListener $listener) { + $removed = false; + foreach($this->timerListenings as $key => &$listening) { + if ($listening->listener != $listener) { + continue; + } + unset($this->timerListenings[$key]); + $removed = true; + } + return $removed; } /** @@ -42,12 +84,23 @@ class TimerManager { */ 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"]); + foreach($this->timerListenings as $key => &$listening) { + if (($listening->lastTrigger + $listening->deltaTime) <= $time) { + call_user_func(array($listening->listener, $listening->method), $time); + + //Unregister one time Listening + if ($listening->oneTime == true) { + unset($this->timerListenings[$key]); + continue; + } + + if ($listening->lastTrigger != -1) { + $listening->lastTrigger += $listening->deltaTime; + } else { + //Initial Time Initialize (self increment needed to improve accuracy) + $listening->lastTrigger = microtime(true); + } } } } - } \ No newline at end of file diff --git a/application/core/Players/PlayerList.php b/application/core/Players/PlayerList.php index 9ce87443..37fc0974 100644 --- a/application/core/Players/PlayerList.php +++ b/application/core/Players/PlayerList.php @@ -76,13 +76,8 @@ class PlayerList implements ManialinkPageAnswerListener, CallbackListener, Timer $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) { $this->playersListShown[$player->login] = $showStatus;