public function run() { global $loggerData; // Loading Logger instance Logger::getInstance(); Logger::log($this->getName()); // Initializing database $this->initDatabase(); // Loading eBot configuration Logger::log("Loading config"); Config::getInstance()->scanAdvertising(); Config::getInstance()->printConfig(); // Registring components Logger::log("Registering MatchManager"); MatchManager::getInstance(); Logger::log("Registering Messages"); MessageManager::createFromConfigFile(); Logger::log("Registering PluginsManager"); PluginsManager::getInstance(); // Starting application Logger::log("Starting eBot Application"); /* try { $this->socket = new Socket(Config::getInstance()->getBot_ip(), Config::getInstance()->getBot_port()); } catch (Exception $ex) { Logger::error("Unable to bind socket"); die(); } */ try { $this->websocket['match'] = new \WebSocket("ws://" . \eBot\Config\Config::getInstance()->getBot_ip() . ":" . \eBot\Config\Config::getInstance()->getBot_port() . "/match"); $this->websocket['match']->open(); $this->websocket['rcon'] = new \WebSocket("ws://" . \eBot\Config\Config::getInstance()->getBot_ip() . ":" . \eBot\Config\Config::getInstance()->getBot_port() . "/rcon"); $this->websocket['rcon']->open(); $this->websocket['logger'] = new \WebSocket("ws://" . \eBot\Config\Config::getInstance()->getBot_ip() . ":" . \eBot\Config\Config::getInstance()->getBot_port() . "/logger"); $this->websocket['logger']->open(); $this->websocket['livemap'] = new \WebSocket("ws://" . \eBot\Config\Config::getInstance()->getBot_ip() . ":" . \eBot\Config\Config::getInstance()->getBot_port() . "/livemap"); $this->websocket['livemap']->open(); $this->websocket['aliveCheck'] = new \WebSocket("ws://" . \eBot\Config\Config::getInstance()->getBot_ip() . ":" . \eBot\Config\Config::getInstance()->getBot_port() . "/alive"); $this->websocket['aliveCheck']->open(); } catch (Exception $ex) { Logger::error("Unable to create Websocket."); die; } PluginsManager::getInstance()->startAll(); $time = time(); while (true) { $data = ""; $ip = ""; $nbMessage = count($loggerData); if ($nbMessage > 0) { if ($nbMessage > 5) { Logger::log($nbMessage . " in queue!"); } $d = explode("---", $loggerData->shift()); $ip = array_shift($d); $data = implode("---", $d); $nbMessage--; } else { usleep(500); } //$data = $this->socket->recvfrom($ip); if ($data) { if (!preg_match("/L+\\s+\\d+\\/\\d+\\/\\d+/", $data)) { if ($data == '__true__') { $this->clientsConnected = true; } elseif ($data == '__false__') { $this->clientsConnected = false; } elseif ($data == '__aliveCheck__') { $this->websocket['aliveCheck']->sendData('__isAlive__'); } else { $data = json_decode($data, true); $authkey = \eBot\Manager\MatchManager::getInstance()->getAuthkey($data[1]); $text = \eTools\Utils\Encryption::decrypt($data[0], $authkey, 256); if ($text) { if (preg_match("!^(?<id>\\d+) stopNoRs (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminStopNoRs(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => 'stop', 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) stop (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminStop(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => 'stop', 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) executeCommand (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+) (?<command>.*)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminExecuteCommand($preg["command"]); if ($reply) { $send = json_encode(array('id' => $preg["id"], 'content' => $reply)); $this->websocket['rcon']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) passknife (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminPassKnife(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) forceknife (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminForceKnife(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) forceknifeend (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminForceKnifeEnd(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) forcestart (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminForceStart(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) stopback (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminStopBack(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) pauseunpause (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminPauseUnpause(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) fixsides (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminFixSides(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) streamerready (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminStreamerReady(); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) goBackRounds (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+) (?<round>\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminGoBackRounds($preg['round']); if ($reply) { $send = json_encode(array('message' => 'button', 'content' => $match->getStatus(), 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } } else { Logger::error($preg["ip"] . " is not managed !"); } } elseif (preg_match("!^(?<id>\\d+) skipmap (?<ip>\\d+\\.\\d+\\.\\d+\\.\\d+\\:\\d+)\$!", $text, $preg)) { $match = \eBot\Manager\MatchManager::getInstance()->getMatch($preg["ip"]); if ($match) { $reply = $match->adminSkipMap(); /* if ($reply) { $send = json_encode(array('message' => 'button', 'content' => 'stop', 'id' => $preg["id"])); $this->websocket['match']->sendData($send); } */ } else { Logger::error($preg["ip"] . " is not managed !"); } } else { Logger::error($text . " not managed"); } } } } else { $line = substr($data, 7); if (\eBot\Manager\MatchManager::getInstance()->getMatch($ip)) { file_put_contents(APP_ROOT . "/logs/{$ip}", $line, FILE_APPEND); $line = trim(substr($line, 23)); \eBot\Manager\MatchManager::getInstance()->getMatch($ip)->processMessage($line); $line = substr($data, 7, strlen($data) - 8); file_put_contents(Logger::getInstance()->getLogPathAdmin() . "/logs_" . \eBot\Manager\MatchManager::getInstance()->getMatch($ip)->getMatchId() . ".log", $line, FILE_APPEND); if ($this->clientsConnected) { $send = json_encode(array('id' => \eBot\Manager\MatchManager::getInstance()->getMatch($ip)->getMatchId(), 'content' => $line)); $this->websocket['logger']->sendData($send); } } } } if ($time + 5 < time()) { $time = time(); $this->websocket['match']->send(json_encode(array("message" => "ping"))); $this->websocket['logger']->send(json_encode(array("message" => "ping"))); $this->websocket['rcon']->send(json_encode(array("message" => "ping"))); $this->websocket['livemap']->send(json_encode(array("message" => "ping"))); $this->websocket['aliveCheck']->send(json_encode(array("message" => "ping"))); } if ($nbMessage < 100) { \eBot\Manager\MatchManager::getInstance()->sendPub(); \eTools\Task\TaskManager::getInstance()->runTask(); } } }
public function taskExecute($name) { if ($name == self::SET_LIVE) { $this->addLog("Setting live"); $this->enable = true; } elseif ($name == self::TASK_ENGAGE_MAP) { $tvTimeRemaining = $this->rcon->send("tv_time_remaining"); if (preg_match('/(?<time>\\d+\\.\\d+) seconds/', $tvTimeRemaining, $preg)) { TaskManager::getInstance()->addTask(new Task($this, self::TASK_ENGAGE_MAP, microtime(true) + floatval($preg['time']) + 1)); $this->addLog("Waiting till GOTV broadcast is finished! Mapchange in " . (intval($preg['time']) + 1) . " seconds"); } else { $this->engageMap(); } } elseif ($name == self::CHANGE_HOSTNAME) { if ($this->rcon->getState()) { $this->rcon->send('hostname "' . $this->getHostname() . '"'); } else { TaskManager::getInstance()->addTask(new Task($this, self::CHANGE_HOSTNAME, microtime(true) + 5)); } } elseif ($name == self::TEST_RCON) { if ($this->rcon->getState()) { if (!$this->rcon->send('echo eBot')) { $ip = explode(":", $this->server_ip); try { $this->rcon = new Rcon($ip[0], $ip[1], $this->rconPassword); $this->rcon->send("echo eBot"); } catch (\Exception $ex) { Logger::error("Reinit rcon failed - " . $ex->getMessage()); Logger::error("Trying to rengage in 10 seconds"); $this->addMatchLog("RCON Connection failed, trying to engage the match in 10 seconds - " . $ex->getMessage(), true); \eBot\Manager\MatchManager::getInstance()->setRetry($this->match_id, \eBot\Manager\MatchManager::getInstance()->getRetry($this->match_id) + 1); \eBot\Manager\MatchManager::getInstance()->delayServer($this->server_ip, 10); $this->needDel = true; } } } TaskManager::getInstance()->addTask(new Task($this, self::TEST_RCON, microtime(true) + 10)); } elseif ($name == self::REINIT_RCON) { $ip = explode(":", $this->server_ip); try { $this->rcon = new Rcon($ip[0], $ip[1], $this->rconPassword); $this->rcon->send("echo eBot"); } catch (\Exception $ex) { Logger::error("Reinit rcon failed - " . $ex->getMessage()); Logger::error("Trying to rengage in 10 seconds"); $this->addMatchLog("RCON Connection failed, trying to engage the match in 10 seconds - " . $ex->getMessage(), true); \eBot\Manager\MatchManager::getInstance()->delayServer($this->server_ip, 10); $this->needDel = true; } } elseif ($name == self::STOP_RECORD) { $this->needDelTask = false; $this->addLog("Stopping record & push"); $this->rcon->send("tv_stoprecord"); if (\eBot\Config\Config::getInstance()->getDemoDownload()) { $this->rcon->send('csay_tv_demo_push "' . $this->currentRecordName . '.dem" "http://' . \eBot\Config\Config::getInstance()->getBot_ip() . ':' . \eBot\Config\Config::getInstance()->getBot_port() . '/upload"'); } $this->currentRecordName = ""; $this->rcon->send("exec server.cfg;"); } elseif ($name == self::TASK_DELAY_READY) { if (\eBot\Config\Config::getInstance()->getDelayReady()) { if ($this->delay_ready_countdown > 0 && !$this->delay_ready_abort && $this->ready['ct'] && $this->ready['t']) { $this->say("Match starts in " . $this->delay_ready_countdown . " seconds. !abort to stop countdown."); $this->delay_ready_countdown--; TaskManager::getInstance()->addTask(new Task($this, self::TASK_DELAY_READY, microtime(true) + 1)); } elseif ($this->delay_ready_countdown == 0 && !$this->delay_ready_abort && $this->ready['ct'] && $this->ready['t']) { $this->delay_ready_abort = false; $this->delay_ready_countdown = 10; $this->delay_ready_inprogress = false; $this->startMatch(true); } } } elseif ($name == self::TASK_SEND_TEAM_NAMES) { $this->sendTeamNames(); } }