function onPreLoop() { $time = time(); if ($time - $this->time >= 1) { $this->time = $time; Dispatcher::dispatch(new Event()); } }
function onTick() { ++$this->tickCount; $time = microtime(true); Dispatcher::dispatch(new Event(Event::ON_NEW_CPU_VALUE, round($this->loopCount / ($time - $this->loopStart), 2))); if ($this->tickCount % 3 == 0) { Dispatcher::dispatch(new Event(Event::ON_NEW_MEMORY_VALUE, memory_get_usage())); } Dispatcher::dispatch(new Event(Event::ON_NEW_NETWORK_VALUE, array(Client::$received - $this->lastNetwork[0], Client::$sent - $this->lastNetwork[1]))); $this->loopCount = 0; $this->loopStart = $time; $this->lastNetwork = array(Client::$received, Client::$sent); }
protected function updateRanking($rankings) { foreach ($rankings as $ranking) { if ($ranking->rank == 0) { continue; } $player = $this->getPlayerObject($ranking->login); if (!$player) { continue; } $rankOld = $player->rank; $player->rank = $ranking->rank; $player->bestTime = $ranking->bestTime; $player->bestCheckpoints = $ranking->bestCheckpoints; $player->score = $ranking->score; $player->nbrLapsFinished = $ranking->nbrLapsFinished; $player->ladderScore = $ranking->ladderScore; if (!$player->isSpectator && $rankOld != $player->rank) { Dispatcher::dispatch(new Event(Event::ON_PLAYER_NEW_RANK, $player, $rankOld, $player->rank)); } } }
private function prepare($plugin) { $this->checkDependencies($plugin); $plugin->onLoad(); Dispatcher::dispatch(new Event(Event::ON_PLUGIN_LOADED, $plugin->getId())); }
private function restartThread($threadId) { if (!$this->enabled || !isset($this->threads[$threadId])) { return; } $commandDiscarded = false; if (empty($this->pendings[$threadId])) { Logger::debug('Thread #' . $threadId . ' died...', true, array('Process #' . getmypid())); Dispatcher::dispatch(new Event(Event::ON_THREAD_DIES, $threadId)); } else { Logger::debug('Thread #' . $threadId . ' timed out...', true, array('Process #' . getmypid())); Dispatcher::dispatch(new Event(Event::ON_THREAD_TIMES_OUT, $threadId)); // If we already tried this command too many times, we discard it... $command = reset($this->pendings[$threadId]); $lastCommandId = $command->getId(); if (++$this->tries[$lastCommandId] > Config::getInstance()->maxTries) { $this->database->execute('DELETE FROM ThreadingCommands WHERE commandId=%d AND parentId=%d', $lastCommandId, getmypid()); unset($this->pendings[$threadId][$lastCommandId]); unset($this->tries[$lastCommandId]); Logger::debug('Command #' . $lastCommandId . ' has been discarded after ' . Config::getInstance()->maxTries . ' unsuccessful tries...', true, array('Process #' . getmypid())); $commandDiscarded = true; } } // Respawning the thread $threadHandle = $this->threads[$threadId]; proc_terminate($threadHandle); proc_close($threadHandle); ++$this->deadThreadsCount; $this->threads[$threadId] = $this->spawnThread($threadId); $this->lastTick[$threadId] = $this->tick; Dispatcher::dispatch(new Event(Event::ON_THREAD_RESTART, $threadId)); Logger::debug('Thread #' . $threadId . ' restarted!', true, array('Process #' . getmypid())); if ($commandDiscarded) { $command->fail(); } }
protected function dispatch($event, $param) { \ManiaLive\Event\Dispatcher::dispatch(new Event($event, $param)); }
final function run() { try { $this->init(); Dispatcher::dispatch(new Event(Event::ON_RUN)); self::$startTime = microtime(true); $nextCycleStart = self::$startTime; $cycleTime = 1 / static::CYCLES_PER_SECOND; } catch (\Exception $e) { ErrorHandling::processRuntimeException($e); } try { while ($this->running) { Dispatcher::dispatch(new Event(Event::ON_PRE_LOOP)); $calls = $this->connection->executeCallbacks(); if (!empty($calls)) { foreach ($calls as $call) { $method = preg_replace('/^[[:alpha:]]+\\./', '', $call[0]); // remove trailing "Whatever." $params = (array) $call[1]; Dispatcher::dispatch(new \ManiaLive\DedicatedApi\Callback\Event($method, $params)); } } $this->connection->executeMulticall(); Dispatcher::dispatch(new Event(Event::ON_POST_LOOP)); $endCycleTime = microtime(true) + $cycleTime / 10; do { $nextCycleStart += $cycleTime; } while ($nextCycleStart < $endCycleTime); @time_sleep_until($nextCycleStart); } } catch (\Exception $e) { ErrorHandling::processRuntimeException($e); } Dispatcher::dispatch(new Event(Event::ON_TERMINATE)); }
protected function fireEvent($playerLogin) { Dispatcher::dispatch(new AllyEvent($playerLogin)); }