/** * Set relevant properties from the key values * * @param string[] $info Associative array of server properties */ private function setServerInfo(array $info) { $this->gs->setName($info['hostname']); $this->gs->setMapName($info['map']); $this->gs->setPlayerCount((int) $info['numplayers']); $this->gs->setMaxPlayers((int) $info['maxplayers']); }
/** * Read and parse the response * * @param resource $fp Handle to an open socket * @throws \Exception */ private function readResponse($fp) { $res = fread($fp, 3); if (strpos($res, 0xff) !== 0) { throw new \Exception('Invalid ping response'); } $res = fread($fp, 2048); $data = explode(pack('n', 0), $res); $this->gs->setName(self::decodeUTF16BEString($data[3])); $this->gs->setPlayerCount((int) self::decodeUTF16BEString($data[4])); $this->gs->setMaxPlayers((int) self::decodeUTF16BEString($data[5])); }
/** * Parse the JSON string and set server status properties * * @param string $jsonString * @throws \Exception */ private function parseJSON($jsonString) { $json = json_decode($jsonString); if (!$json) { throw new \Exception('Invalid JSON string'); } $name = $json->description; $this->gs->setName(is_string($name) ? $name : $name->text); $this->gs->setPlayerCount($json->players->online); $this->gs->setMaxPlayers($json->players->max); if (property_exists($json->players, 'sample')) { $players = array(); foreach ($json->players->sample as $player) { $players[] = $player->name; } $this->gs->setPlayerList($players); } }