TrackManiaControl/application/core/ManiaExchange/ManiaExchangeManager.php

438 lines
13 KiB
PHP
Raw Normal View History

2014-01-08 21:03:59 +01:00
<?php
2014-01-09 22:08:34 +01:00
2014-01-08 21:03:59 +01:00
namespace ManiaControl\Maps;
use ManiaControl\ManiaControl;
2014-01-08 22:49:09 +01:00
/**
* Mania Exchange Info Searcher Class
*
* @author steeffeen & kremsy
*/
2014-01-14 15:15:13 +01:00
class ManiaExchangeManager {
2014-01-08 21:03:59 +01:00
/**
* Constants
*/
2014-01-14 15:15:13 +01:00
//Search others
2014-01-09 23:21:44 +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;
2014-01-08 22:49:09 +01:00
const SEARCH_ORDER_DIFFICULTY_EASIEST = 12;
const SEARCH_ORDER_DIFFICULTY_HARDEST = 13;
2014-01-09 23:21:44 +01:00
const SEARCH_ORDER_LENGHT_SHORTEST = 14;
const SEARCH_ORDER_LENGHT_LONGEST = 15;
2014-01-14 15:15:13 +01:00
//Maximum Maps per request
const MAPS_PER_MX_FETCH = 50;
2014-01-12 19:08:56 +01:00
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;
2014-01-11 22:06:52 +01:00
private $mxIdUidVector = array();
2014-01-08 21:03:59 +01:00
/**
* Construct map manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
2014-01-09 23:21:44 +01:00
/**
* Store Map Info from MX and store the mxid in the database and the mx info in the map object
*
* @param $mxMapInfos
*/
2014-01-12 17:15:59 +01:00
public function updateMapObjectsWithManiaExchangeIds($mxMapInfos) {
2014-01-09 22:08:34 +01:00
$mysqli = $this->maniaControl->database->mysqli;
2014-01-09 23:21:44 +01:00
// Save map data
$saveMapQuery = "UPDATE `" . MapManager::TABLE_MAPS . "`
SET `mxid` = ?
2014-01-12 21:39:27 +01:00
WHERE `uid` = ?;";
2014-01-09 23:21:44 +01:00
$saveMapStatement = $mysqli->prepare($saveMapQuery);
if($mysqli->error) {
trigger_error($mysqli->error);
return;
}
foreach($mxMapInfos as $mxMapInfo) {
/** @var MXMapInfo $mxMapInfo */
$saveMapStatement->bind_param('is', $mxMapInfo->id, $mxMapInfo->uid);
$saveMapStatement->execute();
if($saveMapStatement->error) {
trigger_error($saveMapStatement->error);
}
2014-01-11 22:06:52 +01:00
//Take the uid out of the vektor
if(isset($this->mxIdUidVector[$mxMapInfo->id])) {
$uid = $this->mxIdUidVector[$mxMapInfo->id];
} else {
$uid = $mxMapInfo->uid;
}
$map = $this->maniaControl->mapManager->getMapByUid($uid);
2014-01-09 23:21:44 +01:00
/** @var Map $map */
$map->mx = $mxMapInfo;
}
$saveMapStatement->close();
}
2014-01-15 20:40:14 +01:00
/**
* Unset Map by Mx Id
*
* @param $mxId
*/
public function unsetMap($mxId) {
unset($this->mxIdUidVector[$mxId]);
}
2014-01-15 18:31:06 +01:00
2014-01-09 23:21:44 +01:00
/**
* Fetch Map Information from Mania Exchange
2014-01-15 20:40:14 +01:00
*
2014-01-15 18:31:06 +01:00
* @param null $map
2014-01-09 23:21:44 +01:00
*/
2014-01-15 18:31:06 +01:00
public function fetchManiaExchangeMapInformations($map = null) {
2014-01-15 20:40:14 +01:00
if(!$map) {
2014-01-15 18:31:06 +01:00
//Fetch Informations for whole Maplist
2014-01-15 20:40:14 +01:00
$maps = $this->maniaControl->mapManager->getMaps();
} else {
2014-01-15 18:31:06 +01:00
//Fetch Information for a single map
$maps[] = $map;
}
2014-01-09 23:21:44 +01:00
$mysqli = $this->maniaControl->database->mysqli;
$mapIdString = '';
2014-01-09 22:08:34 +01:00
// Fetch mx ids
2014-01-09 23:21:44 +01:00
$fetchMapQuery = "SELECT `mxid`, `changed` FROM `" . MapManager::TABLE_MAPS . "`
2014-01-09 22:08:34 +01:00
WHERE `index` = ?;";
$fetchMapStatement = $mysqli->prepare($fetchMapQuery);
2014-01-09 23:21:44 +01:00
if($mysqli->error) {
2014-01-09 22:08:34 +01:00
trigger_error($mysqli->error);
return;
2014-01-09 21:37:01 +01:00
}
2014-01-09 23:21:44 +01:00
$id = 0;
foreach($maps as $map) {
/** @var Map $map */
$fetchMapStatement->bind_param('i', $map->index);
2014-01-09 22:08:34 +01:00
$fetchMapStatement->execute();
2014-01-09 23:21:44 +01:00
if($fetchMapStatement->error) {
2014-01-09 22:08:34 +01:00
trigger_error($fetchMapStatement->error);
continue;
}
$fetchMapStatement->store_result();
2014-01-09 23:21:44 +01:00
$fetchMapStatement->bind_result($mxId, $changed);
2014-01-09 22:08:34 +01:00
$fetchMapStatement->fetch();
$fetchMapStatement->free_result();
2014-01-09 23:21:44 +01:00
//Set changed time into the map object
$map->lastUpdate = strtotime($changed);
2014-01-11 22:06:52 +01:00
if($mxId != 0) {
2014-01-12 19:08:56 +01:00
$appendString = $mxId . ',';
2014-01-11 22:06:52 +01:00
//Set the mx id to the mxidmapvektor
$this->mxIdUidVector[$mxId] = $map->uid;
} else {
2014-01-12 19:08:56 +01:00
$appendString = $map->uid . ',';
2014-01-11 22:06:52 +01:00
}
2014-01-09 23:21:44 +01:00
$id++;
2014-01-12 19:08:56 +01:00
//If Max Maplimit is reached, or string gets too long send the request
2014-01-12 22:24:30 +01:00
if($id % self::MAPS_PER_MX_FETCH == 0) {
2014-01-12 22:31:50 +01:00
$mapIdString = substr($mapIdString, 0, -1);
$maps = $this->getMaplistByMixedUidIdString($mapIdString);
2014-01-12 17:15:59 +01:00
$this->updateMapObjectsWithManiaExchangeIds($maps);
2014-01-09 23:21:44 +01:00
$mapIdString = '';
}
2014-01-12 19:08:56 +01:00
$mapIdString .= $appendString;
2014-01-09 21:32:17 +01:00
}
2014-01-09 23:21:44 +01:00
if($mapIdString != '') {
2014-01-12 22:31:50 +01:00
$mapIdString = substr($mapIdString, 0, -1);
$maps = $this->getMaplistByMixedUidIdString($mapIdString);
2014-01-12 17:15:59 +01:00
$this->updateMapObjectsWithManiaExchangeIds($maps);
2014-01-09 23:21:44 +01:00
}
2014-01-09 22:08:34 +01:00
$fetchMapStatement->close();
2014-01-09 23:21:44 +01:00
}
2014-01-14 15:15:13 +01:00
/**
* Get the Whole Maplist from MX by Mixed Uid and Id String fetch
*
* @param $string
* @return array|null
*/
2014-01-09 23:21:44 +01:00
public function getMaplistByMixedUidIdString($string) {
// Get Title Id
$titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtolower(substr($titleId, 0, 2));
// compile search URL
2014-01-12 22:24:30 +01:00
$url = 'http://api.mania-exchange.com/' . $titlePrefix . '/maps/?ids=' . $string;
2014-01-09 23:21:44 +01:00
// $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mc fileutil
$mapInfo = $this->get_file($url);
2014-01-14 13:57:10 +01:00
if($mapInfo === false) {
$this->error = 'Connection or response error on ' . $url;
return array();
} elseif($mapInfo === -1) {
$this->error = 'Timed out while reading data from ' . $url;
return array();
} elseif($mapInfo == '') {
if(empty($maps)) {
$this->error = 'No data returned from ' . $url;
return array();
}
}
2014-01-09 23:21:44 +01:00
$mxMapList = json_decode($mapInfo);
if($mxMapList === null) {
trigger_error('Cannot decode searched JSON data from ' . $url);
return null;
2014-01-09 22:08:34 +01:00
}
2014-01-09 23:21:44 +01:00
$maps = array();
foreach($mxMapList as $map) {
if(!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map));
2014-01-09 22:08:34 +01:00
}
}
2014-01-09 23:21:44 +01:00
return $maps;
2014-01-09 21:32:17 +01:00
}
2014-01-08 22:49:09 +01:00
/**
* Gets a Maplist from Mania Exchange
*
* @param string $name
* @param string $author
* @param string $env
2014-01-09 23:21:44 +01:00
* @param int $maxMapsReturned
* @param int $searchOrder
2014-01-09 22:08:34 +01:00
* @return array null
2014-01-08 22:49:09 +01:00
*/
public function getMaps($name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
2014-01-09 22:08:34 +01:00
// Get Title Id
2014-01-09 23:21:44 +01:00
$titleId = $this->maniaControl->server->titleId;
2014-01-08 22:15:36 +01:00
$titlePrefix = strtolower(substr($titleId, 0, 2));
2014-01-09 23:21:44 +01:00
2014-01-09 22:08:34 +01:00
// Get MapTypes
2014-01-16 19:07:00 +01:00
$scriptInfos = $this->maniaControl->client->getModeScriptInfo();
2014-01-09 23:21:44 +01:00
2014-01-16 19:07:00 +01:00
$mapTypes = $scriptInfos->compatibleMapTypes;
2014-01-09 23:21:44 +01:00
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-09 23:21:44 +01:00
if($env != '') {
2014-01-08 21:03:59 +01:00
$url .= '&environments=' . $this->getEnvironment($env);
}
2014-01-09 23:21:44 +01:00
if($name != '') {
2014-01-09 23:58:47 +01:00
$url .= '&trackname=' . str_replace(" ", "%20", $name);
2014-01-08 22:49:09 +01:00
}
2014-01-09 23:21:44 +01:00
if($author != '') {
2014-01-08 22:49:09 +01:00
$url .= '&author=' . $author;
}
2014-01-09 23:21:44 +01:00
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-09 23:21:44 +01:00
2014-01-09 22:08:34 +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 23:21:44 +01:00
2014-01-14 13:57:10 +01:00
if($mapInfo === false) {
$this->error = 'Connection or response error on ' . $url;
return array();
} elseif($mapInfo === -1) {
$this->error = 'Timed out while reading data from ' . $url;
return array();
} elseif($mapInfo == '') {
if(empty($maps)) {
$this->error = 'No data returned from ' . $url;
return array();
}
}
2014-01-09 23:21:44 +01:00
2014-01-08 21:52:42 +01:00
$mxMapList = json_decode($mapInfo);
2014-01-09 23:21:44 +01:00
if($mxMapList === null) {
2014-01-08 21:52:42 +01:00
trigger_error('Cannot decode searched JSON data from ' . $url);
return null;
2014-01-08 21:03:59 +01:00
}
2014-01-09 23:21:44 +01:00
2014-01-08 21:52:42 +01:00
$maps = array();
2014-01-09 23:21:44 +01:00
foreach($mxMapList as $map) {
if(!empty($map)) {
2014-01-08 22:15:36 +01:00
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-14 15:15:13 +01:00
/**
* Loads an file
*
* @param $url
* @return bool|int|string
*/
2014-01-08 21:52:42 +01:00
private function get_file($url) {
2014-01-09 23:21:44 +01:00
$url = parse_url($url);
$port = isset($url['port']) ? $url['port'] : 80;
2014-01-08 21:52:42 +01:00
$query = isset($url['query']) ? "?" . $url['query'] : "";
2014-01-09 23:21:44 +01:00
2014-01-08 21:52:42 +01:00
$fp = @fsockopen($url['host'], $port, $errno, $errstr, 4);
2014-01-09 23:21:44 +01:00
if(!$fp) {
2014-01-08 21:52:42 +01:00
return false;
}
2014-01-09 23:21:44 +01:00
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");
2014-01-08 21:52:42 +01:00
stream_set_timeout($fp, 2);
2014-01-09 23:21:44 +01:00
$res = '';
2014-01-08 21:52:42 +01:00
$info['timed_out'] = false;
2014-01-09 23:21:44 +01:00
while(!feof($fp) && !$info['timed_out']) {
2014-01-08 21:52:42 +01:00
$res .= fread($fp, 512);
$info = stream_get_meta_data($fp);
}
fclose($fp);
2014-01-09 23:21:44 +01:00
if($info['timed_out']) {
2014-01-08 21:52:42 +01:00
return -1;
2014-01-09 23:21:44 +01:00
} else {
if(substr($res, 9, 3) != '200') {
2014-01-08 21:52:42 +01:00
return false;
}
$page = explode("\r\n\r\n", $res, 2);
return trim($page[1]);
}
} // get_file
2014-01-14 15:15:13 +01:00
/**
* Gets the Current Environemnt by String
*
* @param $env
* @return int
*/
2014-01-08 21:03:59 +01:00
private function getEnvironment($env) {
2014-01-09 23:21:44 +01:00
switch($env) {
2014-01-08 21:03:59 +01:00
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-09 23:21:44 +01:00
//TODO put in own file
2014-01-08 22:15:36 +01:00
class MXMapInfo {
2014-01-09 23:21:44 +01:00
public $prefix, $id, $uid, $name, $userid, $author, $uploaded, $updated, $type, $maptype;
public $titlepack, $style, $envir, $mood, $dispcost, $lightmap, $modname, $exever;
public $exebld, $routes, $length, $unlimiter, $laps, $diffic, $lbrating, $trkvalue;
public $replaytyp, $replayid, $replaycnt, $acomment, $awards, $comments, $rating;
public $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
*
2014-01-09 22:08:34 +01:00
* @param String $prefix MX URL prefix
2014-01-09 23:21:44 +01:00
* @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;
2014-01-09 23:21:44 +01:00
if($mx) {
if($this->prefix == 'tm') {
2014-01-08 21:52:42 +01:00
$dir = 'tracks';
2014-01-09 23:21:44 +01:00
} else // 'sm' || 'qm'
2014-01-08 22:15:36 +01:00
{
2014-01-08 21:52:42 +01:00
$dir = 'maps';
2014-01-08 22:15:36 +01:00
}
2014-01-09 23:21:44 +01:00
if($this->prefix == 'tm' || !property_exists($mx, "MapID")) {
2014-01-08 21:52:42 +01:00
$this->id = $mx->TrackID;
2014-01-09 23:21:44 +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-09 23:21:44 +01:00
2014-01-08 22:15:36 +01:00
$this->name = $mx->Name;
2014-01-09 23:21:44 +01:00
$this->uid = isset($mx->MapUID) ? $mx->MapUID : '';
$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 : '';
2014-01-08 21:52:42 +01:00
$this->titlepack = isset($mx->TitlePack) ? $mx->TitlePack : '';
2014-01-09 23:21:44 +01:00
$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 : '';
2014-01-08 21:52:42 +01:00
$this->unlimiter = isset($mx->UnlimiterRequired) ? $mx->UnlimiterRequired : false;
2014-01-09 23:21:44 +01:00
$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;
2014-01-08 21:52:42 +01:00
$this->replaytyp = isset($mx->ReplayTypeName) ? $mx->ReplayTypeName : '';
2014-01-09 23:21:44 +01:00
$this->replayid = isset($mx->ReplayWRID) ? $mx->ReplayWRID : 0;
2014-01-08 21:52:42 +01:00
$this->replaycnt = isset($mx->ReplayCount) ? $mx->ReplayCount : 0;
2014-01-09 23:21:44 +01:00
$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;
2014-01-08 21:52:42 +01:00
$this->ratingcnt = isset($mx->RatingCount) ? $mx->RatingCount : 0;
2014-01-09 23:21:44 +01:00
if($this->trkvalue == 0 && $this->lbrating > 0) {
2014-01-08 21:52:42 +01:00
$this->trkvalue = $this->lbrating;
2014-01-09 23:21:44 +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-09 23:21:44 +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>');
2014-01-08 22:15:36 +01:00
$this->acomment = str_ireplace($search, $replace, $this->acomment);
$this->acomment = preg_replace('/\[url=.*\]/', '<i>', $this->acomment);
2014-01-09 23:21:44 +01:00
$this->pageurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/view/' . $this->id;
2014-01-08 22:15:36 +01:00
$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-09 23:21:44 +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;
2014-01-09 23:21:44 +01:00
} else {
2014-01-08 21:52:42 +01:00
$this->replayurl = '';
}
}
2014-01-09 23:21:44 +01:00
} // MXMapInfo
} // class MXMapInfo
2014-01-08 21:52:42 +01:00