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); $fsock = fsockopen($urlData['host'], $port);
stream_set_timeout($fsock, 3); 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 .= 'Host: ' . $urlData['host'] . PHP_EOL;
$query .= 'Content-Type: ' . $contentType . PHP_EOL; $query .= 'Content-Type: ' . $contentType . PHP_EOL;
$query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL; $query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL;

View File

@ -1,7 +1,6 @@
<?php <?php
namespace ManiaControl\Maps; namespace ManiaControl\Maps;
use ManiaControl\FileUtil;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
class ManiaExchangeInfoSearcher { class ManiaExchangeInfoSearcher {
@ -34,23 +33,23 @@ class ManiaExchangeInfoSearcher {
$this->maniaControl = $maniaControl; $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 //Get Title Id
$titleId = $this->maniaControl->server->titleId; $titleId = $this->maniaControl->server->titleId;
$title = strtoupper(substr($titleId, 0, 2)); $titlePrefix = strtolower(substr($titleId, 0, 2));
//Get MapTypes //Get MapTypes
$this->maniaControl->client->query('GetModeScriptInfo'); $this->maniaControl->client->query('GetModeScriptInfo');
$scriptInfos = $this->maniaControl->client->getResponse(); $scriptInfos = $this->maniaControl->client->getResponse();
$mapTypes = $scriptInfos["CompatibleMapTypes"]; $mapTypes = $scriptInfos["CompatibleMapTypes"];
$mapTypeArray = explode(",", $mapTypes); $mapTypeArray = explode(",", $mapTypes);
var_dump($mapTypes); var_dump($mapTypes);
var_dump($mapTypeArray); var_dump($mapTypeArray);
// compile search URL // compile search URL
$url = 'http://' . $title . '.mania-exchange.com/tracksearch?api=on'; $url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch?api=on';
/* if ($name != '') /* if ($name != '')
$url .= '&trackname=' . $name; $url .= '&trackname=' . $name;
if ($author != '') if ($author != '')
@ -61,12 +60,13 @@ class ManiaExchangeInfoSearcher {
} }
$url .= '&priord=' . $searchOrder; $url .= '&priord=' . $searchOrder;
$url .= '&limit=' . $maxMapsReturned; $url .= '&limit=' . 1; //TODO
$url .= '&mtype=' . $mapTypeArray[0]; $url .= '&mtype=' . $mapTypeArray[0];
// $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mp fileutil
$mapInfo = $this->get_file($url);
var_dump($url); var_dump($url);
$mapInfo = FileUtil::loadFile($url, "application/json"); return;
//TODO errors //TODO errors
/*if ($file === false) { /*if ($file === false) {
$this->error = 'Connection or response error on ' . $url; $this->error = 'Connection or response error on ' . $url;
@ -83,19 +83,58 @@ class ManiaExchangeInfoSearcher {
} }
}*/ }*/
$mx = json_decode($mapInfo);
if($mx === null) { $mxMapList = json_decode($mapInfo);
$this->error = 'Cannot decode searched JSON data from ' . $url; if($mxMapList === null) {
return array(); trigger_error('Cannot decode searched JSON data from ' . $url);
return null;
} }
var_dump($mx);
// return list of maps as array of MX objects $maps = array();
//return $maps; 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) { private function getEnvironment($env) {
switch($env) { switch($env) {
case 'TMCanyon': case 'TMCanyon':
@ -109,4 +148,99 @@ class ManiaExchangeInfoSearcher {
return -1; 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 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

@ -42,46 +42,47 @@ class Map {
* Create a new Map Object from Rpc Data * Create a new Map Object from Rpc Data
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
* @param array $rpc_infos * @param array $rpc_infos
*/ */
public function __construct(ManiaControl $maniaControl, $rpc_infos = null) { public function __construct(ManiaControl $maniaControl, $rpc_infos = null) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->startTime = time(); $this->startTime = time();
if (!$rpc_infos) return; if(!$rpc_infos) {
$this->name = $rpc_infos['Name']; return;
$this->uid = $rpc_infos['UId']; }
$this->fileName = $rpc_infos['FileName']; $this->name = $rpc_infos['Name'];
$this->uid = $rpc_infos['UId'];
$this->fileName = $rpc_infos['FileName'];
$this->authorLogin = $rpc_infos['Author']; $this->authorLogin = $rpc_infos['Author'];
$this->environment = $rpc_infos['Environnement']; $this->environment = $rpc_infos['Environnement'];
$this->goldTime = $rpc_infos['GoldTime']; $this->goldTime = $rpc_infos['GoldTime'];
$this->copperPrice = $rpc_infos['CopperPrice']; $this->copperPrice = $rpc_infos['CopperPrice'];
$this->mapType = $rpc_infos['MapType']; $this->mapType = $rpc_infos['MapType'];
$this->mapStyle = $rpc_infos['MapStyle']; $this->mapStyle = $rpc_infos['MapStyle'];
if (isset($rpc_infos['NbCheckpoints'])) { if(isset($rpc_infos['NbCheckpoints'])) {
$this->nbCheckpoints = $rpc_infos['NbCheckpoints']; $this->nbCheckpoints = $rpc_infos['NbCheckpoints'];
} }
$this->authorNick = $this->authorLogin; $this->authorNick = $this->authorLogin;
$mapsDirectory = $this->maniaControl->server->getMapsDirectory(); $mapsDirectory = $this->maniaControl->server->getMapsDirectory();
if ($this->maniaControl->server->checkAccess($mapsDirectory)) { if($this->maniaControl->server->checkAccess($mapsDirectory)) {
$mapFetcher = new \GBXChallMapFetcher(true); $mapFetcher = new \GBXChallMapFetcher(true);
try { try {
$mapFetcher->processFile($mapsDirectory . $this->fileName); $mapFetcher->processFile($mapsDirectory . $this->fileName);
$this->authorNick = FORMATTER::stripDirtyCodes($mapFetcher->authorNick); $this->authorNick = FORMATTER::stripDirtyCodes($mapFetcher->authorNick);
$this->authorEInfo = $mapFetcher->authorEInfo; $this->authorEInfo = $mapFetcher->authorEInfo;
$this->authorZone = $mapFetcher->authorZone; $this->authorZone = $mapFetcher->authorZone;
$this->comment = $mapFetcher->comment; $this->comment = $mapFetcher->comment;
} } catch(\Exception $e) {
catch (\Exception $e) {
trigger_error($e->getMessage()); trigger_error($e->getMessage());
} }
} }
// TODO: define timeout if mx is down,todo fetch all map infos at once (maybe way faster) // TODO: define timeout if mx is down,todo fetch all map infos at once (maybe way faster)
$serverInfo = $this->maniaControl->server->getSystemInfo(); $serverInfo = $this->maniaControl->server->titleId;
$title = strtoupper(substr($serverInfo['TitleId'], 0, 2)); $title = strtoupper(substr($serverInfo, 0, 2));
$this->mx = new \MXInfoFetcher($title, $this->uid, false); $this->mx = new \MXInfoFetcher($title, $this->uid, false);
} }
} }

View File

@ -120,13 +120,12 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$recent = false; $recent = false;
} }
$this->maniaControl->mapManager->mxInfoSearcher->getList('');
// search for matching maps // 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 // 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); $this->maniaControl->chat->sendError('No maps found, or MX is down!', $player->login);
if($maps->error != '') { if($maps->error != '') {
trigger_error($maps->error, E_USER_WARNING); trigger_error($maps->error, E_USER_WARNING);
@ -158,6 +157,8 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$i = 0; $i = 0;
$y -= 10; $y -= 10;
foreach($maps as $map) { foreach($maps as $map) {
var_dump($map);
return;
$mapFrame = new Frame(); $mapFrame = new Frame();
$frame->add($mapFrame); $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); $array = array($map->id => $x + 5, $map->name => $x + 17, $map->author => $x + 65, $map->mood => $x + 100, $map->maptype => $x + 115);