예제 #1
0
 private function _queryServer($queries, $id = null)
 {
     if (empty($id)) {
         $servers = SBServer::model()->findAll();
     } else {
         if (is_array($id)) {
             $servers = SBServer::model()->findAllByPk($id);
         } else {
             $servers = array(SBServer::model()->findByPk($id));
         }
     }
     $results = array();
     foreach ($servers as $server) {
         $query = new ServerQuery($server->host, $server->port);
         $result = array('id' => $server->id, 'host' => $server->host, 'port' => $server->port);
         if ($queries & self::QUERY_INFO) {
             $info = $query->getInfo();
             if (empty($info)) {
                 $result['error'] = array('code' => 'ERR_TIMEOUT', 'message' => Yii::t('sourcebans', 'components.ServerQuery.err_timeout') . ' (' . $server . ')');
             } else {
                 if ($info['hostname'] == "anned by server\n") {
                     $result['error'] = array('code' => 'ERR_BLOCKED', 'message' => Yii::t('sourcebans', 'components.ServerQuery.err_blocked') . ' (' . $server . ')');
                 } else {
                     $map_image = '/images/maps/' . $server->game->folder . '/' . $info['map'] . '.jpg';
                     $result['hostname'] = preg_replace('/[\\x00-\\x1F]/', null, $info['hostname']);
                     // Strip UTF-8 characters
                     $result['numplayers'] = $info['numplayers'];
                     $result['maxplayers'] = $info['maxplayers'];
                     $result['map'] = basename($info['map']);
                     // Strip Steam Workshop folder
                     $result['os'] = $info['os'];
                     $result['secure'] = $info['secure'];
                     $result['map_image'] = file_exists(Yii::getPathOfAlias('webroot') . $map_image) ? Yii::app()->baseUrl . $map_image : null;
                 }
             }
         }
         if ($queries & self::QUERY_PLAYERS) {
             $result['players'] = $query->getPlayers();
             usort($result['players'], array($this, '_sortPlayers'));
             foreach ($result['players'] as &$player) {
                 if ($player['time'] > 60) {
                     $player['time'] = floor($player['time'] / 60) * 60;
                 }
                 // Round off to minutes
                 $player['time'] = Yii::app()->format->formatLength($player['time']);
             }
         }
         if ($queries & self::QUERY_RULES) {
             $result['rules'] = $query->getRules();
         }
         $results[] = $result;
     }
     return empty($id) || is_array($id) ? $results : $results[0];
 }