infosearcher imrpove

This commit is contained in:
kremsy 2014-01-08 21:52:42 +01:00
parent 132fa4cf01
commit 8854d4ac58
5 changed files with 182 additions and 142 deletions

View File

@ -26,7 +26,7 @@ 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;

View File

@ -1,7 +1,6 @@
<?php
namespace ManiaControl\Maps;
use ManiaControl\FileUtil;
use ManiaControl\ManiaControl;
class ManiaExchangeInfoSearcher {
@ -34,10 +33,10 @@ class ManiaExchangeInfoSearcher {
$this->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');
@ -50,7 +49,7 @@ class ManiaExchangeInfoSearcher {
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,18 +83,57 @@ 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) {
@ -109,4 +148,99 @@ class ManiaExchangeInfoSearcher {
return -1;
}
}
}
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('<br/>', '<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<i>', '</i>');
$this->acomment = str_ireplace($search, $replace, $this->acomment);
$this->acomment = preg_replace('/\[url=.*\]/', '<i>', $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

View File

@ -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('<br/>', '<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<i>', '</i>');
$this->acomment = str_ireplace($search, $replace, $this->acomment);
$this->acomment = preg_replace('/\[url=.*\]/', '<i>', $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

View File

@ -48,7 +48,9 @@ class Map {
$this->maniaControl = $maniaControl;
$this->startTime = time();
if (!$rpc_infos) return;
if(!$rpc_infos) {
return;
}
$this->name = $rpc_infos['Name'];
$this->uid = $rpc_infos['UId'];
$this->fileName = $rpc_infos['FileName'];
@ -58,14 +60,14 @@ class Map {
$this->copperPrice = $rpc_infos['CopperPrice'];
$this->mapType = $rpc_infos['MapType'];
$this->mapStyle = $rpc_infos['MapStyle'];
if (isset($rpc_infos['NbCheckpoints'])) {
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);
@ -73,15 +75,14 @@ class Map {
$this->authorEInfo = $mapFetcher->authorEInfo;
$this->authorZone = $mapFetcher->authorZone;
$this->comment = $mapFetcher->comment;
}
catch (\Exception $e) {
} 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));
$serverInfo = $this->maniaControl->server->titleId;
$title = strtoupper(substr($serverInfo, 0, 2));
$this->mx = new \MXInfoFetcher($title, $this->uid, false);
}
}

View File

@ -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);