From 8854d4ac58abb4f26b7656d904bca86965e9164e Mon Sep 17 00:00:00 2001 From: kremsy Date: Wed, 8 Jan 2014 21:52:42 +0100 Subject: [PATCH] infosearcher imrpove --- application/core/FileUtil.php | 4 +- .../ManiaExchangeInfoSearcher.php | 166 ++++++++++++++++-- .../core/ManiaExchange/mxinfosearcher.inc.php | 96 ---------- application/core/Maps/Map.php | 49 +++--- application/core/Maps/MapList.php | 9 +- 5 files changed, 182 insertions(+), 142 deletions(-) diff --git a/application/core/FileUtil.php b/application/core/FileUtil.php index fa3503bc..350174d1 100644 --- a/application/core/FileUtil.php +++ b/application/core/FileUtil.php @@ -26,12 +26,12 @@ abstract class FileUtil { $fsock = fsockopen($urlData['host'], $port); stream_set_timeout($fsock, 3); - $query = 'GET ' . $urlData['path'] . ' HTTP/1.0' . PHP_EOL; + $query = 'GET ' . $urlData['path'] . ' HTTP/1.1' . PHP_EOL; $query .= 'Host: ' . $urlData['host'] . PHP_EOL; $query .= 'Content-Type: ' . $contentType . PHP_EOL; $query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL; $query .= PHP_EOL; - + fwrite($fsock, $query); $buffer = ''; diff --git a/application/core/ManiaExchange/ManiaExchangeInfoSearcher.php b/application/core/ManiaExchange/ManiaExchangeInfoSearcher.php index 7e09c482..27502543 100644 --- a/application/core/ManiaExchange/ManiaExchangeInfoSearcher.php +++ b/application/core/ManiaExchange/ManiaExchangeInfoSearcher.php @@ -1,7 +1,6 @@ maniaControl = $maniaControl; } - public function getList($maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPLOADED_NEWEST, $env = '') { + public function getMaps($maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPLOADED_NEWEST, $env = '') { //Get Title Id $titleId = $this->maniaControl->server->titleId; - $title = strtoupper(substr($titleId, 0, 2)); + $titlePrefix = strtolower(substr($titleId, 0, 2)); //Get MapTypes $this->maniaControl->client->query('GetModeScriptInfo'); $scriptInfos = $this->maniaControl->client->getResponse(); - $mapTypes = $scriptInfos["CompatibleMapTypes"]; + $mapTypes = $scriptInfos["CompatibleMapTypes"]; $mapTypeArray = explode(",", $mapTypes); var_dump($mapTypes); var_dump($mapTypeArray); // compile search URL - $url = 'http://' . $title . '.mania-exchange.com/tracksearch?api=on'; + $url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch?api=on'; /* if ($name != '') $url .= '&trackname=' . $name; if ($author != '') @@ -61,12 +60,13 @@ class ManiaExchangeInfoSearcher { } $url .= '&priord=' . $searchOrder; - $url .= '&limit=' . $maxMapsReturned; + $url .= '&limit=' . 1; //TODO $url .= '&mtype=' . $mapTypeArray[0]; + // $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mp fileutil + $mapInfo = $this->get_file($url); var_dump($url); - $mapInfo = FileUtil::loadFile($url, "application/json"); - + return; //TODO errors /*if ($file === false) { $this->error = 'Connection or response error on ' . $url; @@ -83,19 +83,58 @@ class ManiaExchangeInfoSearcher { } }*/ - $mx = json_decode($mapInfo); - if($mx === null) { - $this->error = 'Cannot decode searched JSON data from ' . $url; - return array(); + + $mxMapList = json_decode($mapInfo); + if($mxMapList === null) { + trigger_error('Cannot decode searched JSON data from ' . $url); + return null; } - var_dump($mx); - // return list of maps as array of MX objects - //return $maps; + $maps = array(); + foreach($mxMapList as $map){ + var_dump($map); + if (!empty($map)) { + array_push($maps, new MXInfo($titlePrefix, $map)); + } + } + var_dump($maps); + return $maps; } + private function get_file($url) { + + $url = parse_url($url); + $port = isset($url['port']) ? $url['port'] : 80; + $query = isset($url['query']) ? "?" . $url['query'] : ""; + + $fp = @fsockopen($url['host'], $port, $errno, $errstr, 4); + if(!$fp) { + return false; + } + + fwrite($fp, 'GET ' . $url['path'] . $query . " HTTP/1.0\r\n" . 'Host: ' . $url['host'] . "\r\n" . 'Content-Type: application/json' . "\r\n" . 'User-Agent: ManiaControl v' . ManiaControl::VERSION . "\r\n\r\n"); + stream_set_timeout($fp, 2); + $res = ''; + $info['timed_out'] = false; + while(!feof($fp) && !$info['timed_out']) { + $res .= fread($fp, 512); + $info = stream_get_meta_data($fp); + } + fclose($fp); + + if($info['timed_out']) { + return -1; + } else { + if(substr($res, 9, 3) != '200') { + return false; + } + $page = explode("\r\n\r\n", $res, 2); + return trim($page[1]); + } + } // get_file + private function getEnvironment($env) { switch($env) { case 'TMCanyon': @@ -109,4 +148,99 @@ class ManiaExchangeInfoSearcher { return -1; } } -} \ No newline at end of file + +} + +class MXInfo { + + public $section, $prefix, $id, + $name, $userid, $author, $uploaded, $updated, + $type, $maptype, $titlepack, $style, $envir, $mood, + $dispcost, $lightmap, $modname, + $exever, $exebld, $routes, $length, $unlimiter, $laps, $diffic, + $lbrating, $trkvalue, $replaytyp, $replayid, $replaycnt, + $acomment, $awards, $comments, $rating, $ratingex, $ratingcnt, + $pageurl, $replayurl, $imageurl, $thumburl, $dloadurl; + + /** + * Returns map object with all available data from MX map data + * + * @param String $prefix + * MX URL prefix + * @param Object $map + * The MX map data from MXInfoSearcher + * @return MXInfo + */ + public function MXInfo($prefix, $mx) { + $this->prefix = $prefix; + if ($mx) { + if ($this->prefix == 'tm') + $dir = 'tracks'; + else // 'sm' || 'qm' + $dir = 'maps'; + + //temporary fix + if($this->prefix == 'tm' || !property_exists($mx, "MapID")) + $this->id = $mx->TrackID; + else + $this->id = $mx->MapID; + // $this->id = ($this->prefix == 'tm') ? $mx->TrackID : $mx->MapID; + + $this->name = $mx->Name; + $this->userid = $mx->UserID; + $this->author = $mx->Username; + $this->uploaded = $mx->UploadedAt; + $this->updated = $mx->UpdatedAt; + $this->type = $mx->TypeName; + $this->maptype = isset($mx->MapType) ? $mx->MapType : ''; + $this->titlepack = isset($mx->TitlePack) ? $mx->TitlePack : ''; + $this->style = isset($mx->StyleName) ? $mx->StyleName : ''; + $this->envir = $mx->EnvironmentName; + $this->mood = $mx->Mood; + $this->dispcost = $mx->DisplayCost; + $this->lightmap = $mx->Lightmap; + $this->modname = isset($mx->ModName) ? $mx->ModName : ''; + $this->exever = $mx->ExeVersion; + $this->exebld = $mx->ExeBuild; + $this->routes = isset($mx->RouteName) ? $mx->RouteName : ''; + $this->length = isset($mx->LengthName) ? $mx->LengthName : ''; + $this->unlimiter = isset($mx->UnlimiterRequired) ? $mx->UnlimiterRequired : false; + $this->laps = isset($mx->Laps) ? $mx->Laps : 0; + $this->diffic = $mx->DifficultyName; + $this->lbrating = isset($mx->LBRating) ? $mx->LBRating : 0; + $this->trkvalue = isset($mx->TrackValue) ? $mx->TrackValue : 0; + $this->replaytyp = isset($mx->ReplayTypeName) ? $mx->ReplayTypeName : ''; + $this->replayid = isset($mx->ReplayWRID) ? $mx->ReplayWRID : 0; + $this->replaycnt = isset($mx->ReplayCount) ? $mx->ReplayCount : 0; + $this->acomment = $mx->Comments; + $this->awards = isset($mx->AwardCount) ? $mx->AwardCount : 0; + $this->comments = $mx->CommentCount; + $this->rating = isset($mx->Rating) ? $mx->Rating : 0.0; + $this->ratingex = isset($mx->RatingExact) ? $mx->RatingExact : 0.0; + $this->ratingcnt = isset($mx->RatingCount) ? $mx->RatingCount : 0; + + if ($this->trkvalue == 0 && $this->lbrating > 0) + $this->trkvalue = $this->lbrating; + elseif ($this->lbrating == 0 && $this->trkvalue > 0) + $this->lbrating = $this->trkvalue; + + $search = array(chr(31), '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[url]', '[/url]'); + $replace = array('
', '', '', '', '', '', '', '', ''); + $this->acomment = str_ireplace($search, $replace, $this->acomment); + $this->acomment = preg_replace('/\[url=.*\]/', '', $this->acomment); + + $this->pageurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/view/' . $this->id; + $this->imageurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/screenshot/normal/' . $this->id; + $this->thumburl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/screenshot/small/' . $this->id; + $this->dloadurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/download/' . $this->id; + + if ($this->prefix == 'tm' && $this->replayid > 0) { + $this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid; + } else { + $this->replayurl = ''; + } + } + } // MXInfo +} // class MXInfo + + diff --git a/application/core/ManiaExchange/mxinfosearcher.inc.php b/application/core/ManiaExchange/mxinfosearcher.inc.php index 19216423..d1d6a667 100644 --- a/application/core/ManiaExchange/mxinfosearcher.inc.php +++ b/application/core/ManiaExchange/mxinfosearcher.inc.php @@ -227,99 +227,3 @@ class MXInfoSearcher implements Iterator,Countable { } // class MXInfoSearcher -class MXInfo { - - public $section, $prefix, $id, - $name, $userid, $author, $uploaded, $updated, - $type, $maptype, $titlepack, $style, $envir, $mood, - $dispcost, $lightmap, $modname, - $exever, $exebld, $routes, $length, $unlimiter, $laps, $diffic, - $lbrating, $trkvalue, $replaytyp, $replayid, $replaycnt, - $acomment, $awards, $comments, $rating, $ratingex, $ratingcnt, - $pageurl, $replayurl, $imageurl, $thumburl, $dloadurl; - - /** - * Returns map object with all available data from MX map data - * - * @param String $section - * MX section - * @param String $prefix - * MX URL prefix - * @param Object $map - * The MX map data from MXInfoSearcher - * @return MXInfo - */ - public function MXInfo($section, $prefix, $mx) { - - $this->section = $section; - $this->prefix = $prefix; - if ($mx) { - if ($this->prefix == 'tm') - $dir = 'tracks'; - else // 'sm' || 'qm' - $dir = 'maps'; - - //temporary fix - if($this->prefix == 'tm' || !property_exists($mx, "MapID")) - $this->id = $mx->TrackID; - else - $this->id = $mx->MapID; - // $this->id = ($this->prefix == 'tm') ? $mx->TrackID : $mx->MapID; - - $this->name = $mx->Name; - $this->userid = $mx->UserID; - $this->author = $mx->Username; - $this->uploaded = $mx->UploadedAt; - $this->updated = $mx->UpdatedAt; - $this->type = $mx->TypeName; - $this->maptype = isset($mx->MapType) ? $mx->MapType : ''; - $this->titlepack = isset($mx->TitlePack) ? $mx->TitlePack : ''; - $this->style = isset($mx->StyleName) ? $mx->StyleName : ''; - $this->envir = $mx->EnvironmentName; - $this->mood = $mx->Mood; - $this->dispcost = $mx->DisplayCost; - $this->lightmap = $mx->Lightmap; - $this->modname = isset($mx->ModName) ? $mx->ModName : ''; - $this->exever = $mx->ExeVersion; - $this->exebld = $mx->ExeBuild; - $this->routes = isset($mx->RouteName) ? $mx->RouteName : ''; - $this->length = isset($mx->LengthName) ? $mx->LengthName : ''; - $this->unlimiter = isset($mx->UnlimiterRequired) ? $mx->UnlimiterRequired : false; - $this->laps = isset($mx->Laps) ? $mx->Laps : 0; - $this->diffic = $mx->DifficultyName; - $this->lbrating = isset($mx->LBRating) ? $mx->LBRating : 0; - $this->trkvalue = isset($mx->TrackValue) ? $mx->TrackValue : 0; - $this->replaytyp = isset($mx->ReplayTypeName) ? $mx->ReplayTypeName : ''; - $this->replayid = isset($mx->ReplayWRID) ? $mx->ReplayWRID : 0; - $this->replaycnt = isset($mx->ReplayCount) ? $mx->ReplayCount : 0; - $this->acomment = $mx->Comments; - $this->awards = isset($mx->AwardCount) ? $mx->AwardCount : 0; - $this->comments = $mx->CommentCount; - $this->rating = isset($mx->Rating) ? $mx->Rating : 0.0; - $this->ratingex = isset($mx->RatingExact) ? $mx->RatingExact : 0.0; - $this->ratingcnt = isset($mx->RatingCount) ? $mx->RatingCount : 0; - - if ($this->trkvalue == 0 && $this->lbrating > 0) - $this->trkvalue = $this->lbrating; - elseif ($this->lbrating == 0 && $this->trkvalue > 0) - $this->lbrating = $this->trkvalue; - - $search = array(chr(31), '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[url]', '[/url]'); - $replace = array('
', '', '', '', '', '', '', '', ''); - $this->acomment = str_ireplace($search, $replace, $this->acomment); - $this->acomment = preg_replace('/\[url=.*\]/', '', $this->acomment); - - $this->pageurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/view/' . $this->id; - $this->imageurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/screenshot/normal/' . $this->id; - $this->thumburl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/screenshot/small/' . $this->id; - $this->dloadurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/download/' . $this->id; - - if ($this->prefix == 'tm' && $this->replayid > 0) { - $this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid; - } else { - $this->replayurl = ''; - } - } - } // MXInfo -} // class MXInfo - diff --git a/application/core/Maps/Map.php b/application/core/Maps/Map.php index 1c076947..71b28859 100644 --- a/application/core/Maps/Map.php +++ b/application/core/Maps/Map.php @@ -32,7 +32,7 @@ class Map { public $comment = ''; public $titleUid = ''; public $startTime = -1; - + /** * Private Properties */ @@ -42,46 +42,47 @@ class Map { * Create a new Map Object from Rpc Data * * @param \ManiaControl\ManiaControl $maniaControl - * @param array $rpc_infos + * @param array $rpc_infos */ public function __construct(ManiaControl $maniaControl, $rpc_infos = null) { $this->maniaControl = $maniaControl; - $this->startTime = time(); - - if (!$rpc_infos) return; - $this->name = $rpc_infos['Name']; - $this->uid = $rpc_infos['UId']; - $this->fileName = $rpc_infos['FileName']; + $this->startTime = time(); + + if(!$rpc_infos) { + return; + } + $this->name = $rpc_infos['Name']; + $this->uid = $rpc_infos['UId']; + $this->fileName = $rpc_infos['FileName']; $this->authorLogin = $rpc_infos['Author']; $this->environment = $rpc_infos['Environnement']; - $this->goldTime = $rpc_infos['GoldTime']; + $this->goldTime = $rpc_infos['GoldTime']; $this->copperPrice = $rpc_infos['CopperPrice']; - $this->mapType = $rpc_infos['MapType']; - $this->mapStyle = $rpc_infos['MapStyle']; - if (isset($rpc_infos['NbCheckpoints'])) { + $this->mapType = $rpc_infos['MapType']; + $this->mapStyle = $rpc_infos['MapStyle']; + if(isset($rpc_infos['NbCheckpoints'])) { $this->nbCheckpoints = $rpc_infos['NbCheckpoints']; } - + $this->authorNick = $this->authorLogin; - + $mapsDirectory = $this->maniaControl->server->getMapsDirectory(); - if ($this->maniaControl->server->checkAccess($mapsDirectory)) { + if($this->maniaControl->server->checkAccess($mapsDirectory)) { $mapFetcher = new \GBXChallMapFetcher(true); try { $mapFetcher->processFile($mapsDirectory . $this->fileName); - $this->authorNick = FORMATTER::stripDirtyCodes($mapFetcher->authorNick); + $this->authorNick = FORMATTER::stripDirtyCodes($mapFetcher->authorNick); $this->authorEInfo = $mapFetcher->authorEInfo; - $this->authorZone = $mapFetcher->authorZone; - $this->comment = $mapFetcher->comment; - } - catch (\Exception $e) { + $this->authorZone = $mapFetcher->authorZone; + $this->comment = $mapFetcher->comment; + } catch(\Exception $e) { trigger_error($e->getMessage()); } } - + // TODO: define timeout if mx is down,todo fetch all map infos at once (maybe way faster) - $serverInfo = $this->maniaControl->server->getSystemInfo(); - $title = strtoupper(substr($serverInfo['TitleId'], 0, 2)); - $this->mx = new \MXInfoFetcher($title, $this->uid, false); + $serverInfo = $this->maniaControl->server->titleId; + $title = strtoupper(substr($serverInfo, 0, 2)); + $this->mx = new \MXInfoFetcher($title, $this->uid, false); } } \ No newline at end of file diff --git a/application/core/Maps/MapList.php b/application/core/Maps/MapList.php index 6510be2e..0ca88991 100644 --- a/application/core/Maps/MapList.php +++ b/application/core/Maps/MapList.php @@ -120,13 +120,12 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { $recent = false; } - $this->maniaControl->mapManager->mxInfoSearcher->getList(''); - // search for matching maps - $maps = new MXInfoSearcher($title, $searchString, $author, $environment, $recent); + //$maps = new MXInfoSearcher($title, $searchString, $author, $environment, $recent); + $maps = $this->maniaControl->mapManager->mxInfoSearcher->getMaps(15); // check if there are any results - if(!$maps->valid()) { + if($maps == null) { $this->maniaControl->chat->sendError('No maps found, or MX is down!', $player->login); if($maps->error != '') { trigger_error($maps->error, E_USER_WARNING); @@ -158,6 +157,8 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { $i = 0; $y -= 10; foreach($maps as $map) { + var_dump($map); + return; $mapFrame = new Frame(); $frame->add($mapFrame); $array = array($map->id => $x + 5, $map->name => $x + 17, $map->author => $x + 65, $map->mood => $x + 100, $map->maptype => $x + 115);