TrackManiaControl/application/core/ManiaExchange/ManiaExchangeInfoSearcher.php

258 lines
8.0 KiB
PHP
Raw Normal View History

2014-01-08 21:03:59 +01:00
<?php
namespace ManiaControl\Maps;
use ManiaControl\ManiaControl;
2014-01-08 22:49:09 +01:00
/**
* Mania Exchange Info Searcher Class
*
* @author steeffeen & kremsy
*/
2014-01-08 21:03:59 +01:00
class ManiaExchangeInfoSearcher {
/**
* Constants
*/
2014-01-08 22:49:09 +01:00
const SEARCH_ORDER_NONE = -1;
const SEARCH_ORDER_TRACK_NAME = 0;
const SEARCH_ORDER_AUTHOR = 1;
const SEARCH_ORDER_UPLOADED_NEWEST = 2;
const SEARCH_ORDER_UPLOADED_OLDEST = 3;
const SEARCH_ORDER_UPDATED_NEWEST = 4;
const SEARCH_ORDER_UPDATED_OLDEST = 5;
const SEARCH_ORDER_ACTIVITY_LATEST = 6;
const SEARCH_ORDER_ACTIVITY_OLDEST = 7;
const SEARCH_ORDER_AWARDS_MOST = 8;
const SEARCH_ORDER_AWARDS_LEAST = 9;
const SEARCH_ORDER_COMMENTS_MOST = 10;
const SEARCH_ORDER_COMMENTS_LEAST = 11;
const SEARCH_ORDER_DIFFICULTY_EASIEST = 12;
const SEARCH_ORDER_DIFFICULTY_HARDEST = 13;
const SEARCH_ORDER_LENGHT_SHORTEST = 14;
const SEARCH_ORDER_LENGHT_LONGEST = 15;
2014-01-08 21:03:59 +01:00
/**
2014-01-08 22:49:09 +01:00
* Private Propertieswc
2014-01-08 21:03:59 +01:00
*/
private $maniaControl = null;
/**
* Construct map manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
2014-01-08 22:49:09 +01:00
/**
* Gets a Maplist from Mania Exchange
*
* @param string $name
* @param string $author
* @param string $env
* @param int $maxMapsReturned
* @param int $searchOrder
* @return array|null
*/
public function getMaps($name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
2014-01-08 21:03:59 +01:00
//Get Title Id
2014-01-08 22:15:36 +01:00
$titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtolower(substr($titleId, 0, 2));
2014-01-08 21:03:59 +01:00
//Get MapTypes
$this->maniaControl->client->query('GetModeScriptInfo');
$scriptInfos = $this->maniaControl->client->getResponse();
2014-01-09 19:29:25 +01:00
$mapTypes = $scriptInfos["CompatibleMapTypes"];
2014-01-08 21:03:59 +01:00
// compile search URL
2014-01-08 21:52:42 +01:00
$url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch?api=on';
2014-01-08 21:03:59 +01:00
if($env != '') {
$url .= '&environments=' . $this->getEnvironment($env);
}
2014-01-08 22:49:09 +01:00
if($name != '') {
$url .= '&trackname=' . $name;
}
if($author != '') {
$url .= '&author=' . $author;
}
2014-01-08 21:03:59 +01:00
$url .= '&priord=' . $searchOrder;
2014-01-08 22:49:09 +01:00
$url .= '&limit=' . $maxMapsReturned;
2014-01-09 19:29:25 +01:00
$url .= '&mtype=' . $mapTypes;
2014-01-08 21:03:59 +01:00
2014-01-09 19:29:25 +01:00
// $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mc fileutil
2014-01-08 21:52:42 +01:00
$mapInfo = $this->get_file($url);
2014-01-09 19:30:50 +01:00
2014-01-08 21:03:59 +01:00
//TODO errors
/*if ($file === false) {
$this->error = 'Connection or response error on ' . $url;
return array();
} elseif ($file === -1) {
$this->error = 'Timed out while reading data from ' . $url;
return array();
} elseif ($file == '') {
if (empty($maps)) {
$this->error = 'No data returned from ' . $url;
return array();
} else {
break;
}
}*/
2014-01-08 21:52:42 +01:00
$mxMapList = json_decode($mapInfo);
if($mxMapList === null) {
trigger_error('Cannot decode searched JSON data from ' . $url);
return null;
2014-01-08 21:03:59 +01:00
}
2014-01-08 21:52:42 +01:00
$maps = array();
2014-01-08 22:15:36 +01:00
foreach($mxMapList as $map) {
if(!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map));
2014-01-08 21:52:42 +01:00
}
}
return $maps;
2014-01-08 21:03:59 +01:00
}
2014-01-08 21:52:42 +01:00
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
2014-01-08 21:03:59 +01:00
private function getEnvironment($env) {
switch($env) {
case 'TMCanyon':
case 'SMStorm':
return 1;
case 'TMStadium':
return 2;
case 'TMValley':
return 3;
default:
return -1;
}
}
2014-01-08 21:52:42 +01:00
}
2014-01-08 22:15:36 +01:00
class MXMapInfo {
2014-01-08 21:52:42 +01:00
2014-01-08 22:15:36 +01:00
public $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;
2014-01-08 21:52:42 +01:00
/**
* 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
2014-01-08 22:15:36 +01:00
* @return MXMapInfo
2014-01-08 21:52:42 +01:00
*/
2014-01-08 22:15:36 +01:00
public function __construct($prefix, $mx) {
$this->prefix = $prefix;
if($mx) {
if($this->prefix == 'tm') {
2014-01-08 21:52:42 +01:00
$dir = 'tracks';
2014-01-08 22:15:36 +01:00
} else // 'sm' || 'qm'
{
2014-01-08 21:52:42 +01:00
$dir = 'maps';
2014-01-08 22:15:36 +01:00
}
2014-01-08 21:52:42 +01:00
//temporary fix
2014-01-08 22:15:36 +01:00
if($this->prefix == 'tm' || !property_exists($mx, "MapID")) {
2014-01-08 21:52:42 +01:00
$this->id = $mx->TrackID;
2014-01-08 22:15:36 +01:00
} else {
2014-01-08 21:52:42 +01:00
$this->id = $mx->MapID;
2014-01-08 22:15:36 +01:00
}
2014-01-08 21:52:42 +01:00
2014-01-08 22:15:36 +01:00
$this->name = $mx->Name;
2014-01-08 21:52:42 +01:00
$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;
2014-01-08 22:15:36 +01:00
if($this->trkvalue == 0 && $this->lbrating > 0) {
2014-01-08 21:52:42 +01:00
$this->trkvalue = $this->lbrating;
2014-01-08 22:15:36 +01:00
} elseif($this->lbrating == 0 && $this->trkvalue > 0) {
2014-01-08 21:52:42 +01:00
$this->lbrating = $this->trkvalue;
2014-01-08 22:15:36 +01:00
}
2014-01-08 21:52:42 +01:00
2014-01-08 22:15:36 +01:00
$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);
2014-01-08 21:52:42 +01:00
2014-01-08 22:15:36 +01:00
$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;
2014-01-08 21:52:42 +01:00
2014-01-08 22:15:36 +01:00
if($this->prefix == 'tm' && $this->replayid > 0) {
2014-01-08 21:52:42 +01:00
$this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid;
} else {
$this->replayurl = '';
}
}
2014-01-08 22:15:36 +01:00
} // MXInfo
} // class MXInfo
2014-01-08 21:52:42 +01:00