imrproved mxinfo

This commit is contained in:
kremsy 2014-01-28 15:56:50 +01:00 committed by Steffen Schröder
parent 10fe4c3e7e
commit 49faf10d78
8 changed files with 149 additions and 447 deletions

View File

@ -22,8 +22,6 @@ use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
require_once __DIR__ . '/Maniaplanet/DedicatedServer/Connection.php'; require_once __DIR__ . '/Maniaplanet/DedicatedServer/Connection.php';
require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php'; require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php';
require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php';
require_once __DIR__ . '/ManiaExchange/ManiaExchangeManager.php';
require_once __DIR__ . '/FML/autoload.php'; require_once __DIR__ . '/FML/autoload.php';
/** /**

View File

@ -0,0 +1,98 @@
<?php
namespace ManiaControl\ManiaExchange;
/**
* Mania Exchange Map Info Object
*
* @author xymph
* @updated lukas and steeffeen
*/
class MXMapInfo {
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;
/**
* 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 MXMapInfo
*/
public function __construct($prefix, $mx) {
$this->prefix = $prefix;
if ($mx) {
if ($this->prefix == 'tm') {
$dir = 'tracks';
} else { // 'sm' || 'qm'
$dir = 'maps';
}
if ($this->prefix == 'tm' || !property_exists($mx, "MapID")) {
$this->id = $mx->TrackID;
} else {
$this->id = $mx->MapID;
}
$this->name = $mx->Name;
$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 : '';
$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 = '';
}
}
}
}

View File

@ -1,8 +1,10 @@
<?php <?php
namespace ManiaControl\Maps; namespace ManiaControl\ManiaExchange;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Maps\Map;
use ManiaControl\Maps\MapManager;
/** /**
* Mania Exchange Info Searcher Class * Mania Exchange Info Searcher Class
@ -63,7 +65,7 @@ class ManiaExchangeManager {
SET `mxid` = ? SET `mxid` = ?
WHERE `uid` = ?;"; WHERE `uid` = ?;";
$saveMapStatement = $mysqli->prepare($saveMapQuery); $saveMapStatement = $mysqli->prepare($saveMapQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return; return;
} }
@ -71,12 +73,12 @@ class ManiaExchangeManager {
/** @var MXMapInfo $mxMapInfo */ /** @var MXMapInfo $mxMapInfo */
$saveMapStatement->bind_param('is', $mxMapInfo->id, $mxMapInfo->uid); $saveMapStatement->bind_param('is', $mxMapInfo->id, $mxMapInfo->uid);
$saveMapStatement->execute(); $saveMapStatement->execute();
if($saveMapStatement->error) { if ($saveMapStatement->error) {
trigger_error($saveMapStatement->error); trigger_error($saveMapStatement->error);
} }
//Take the uid out of the vektor //Take the uid out of the vektor
if(isset($this->mxIdUidVector[$mxMapInfo->id])) { if (isset($this->mxIdUidVector[$mxMapInfo->id])) {
$uid = $this->mxIdUidVector[$mxMapInfo->id]; $uid = $this->mxIdUidVector[$mxMapInfo->id];
} else { } else {
$uid = $mxMapInfo->uid; $uid = $mxMapInfo->uid;
@ -104,7 +106,7 @@ class ManiaExchangeManager {
* @param null $map * @param null $map
*/ */
public function fetchManiaExchangeMapInformations($map = null) { public function fetchManiaExchangeMapInformations($map = null) {
if(!$map) { if (!$map) {
//Fetch Informations for whole Maplist //Fetch Informations for whole Maplist
$maps = $this->maniaControl->mapManager->getMaps(); $maps = $this->maniaControl->mapManager->getMaps();
} else { } else {
@ -119,7 +121,7 @@ class ManiaExchangeManager {
$fetchMapQuery = "SELECT `mxid`, `changed` FROM `" . MapManager::TABLE_MAPS . "` $fetchMapQuery = "SELECT `mxid`, `changed` FROM `" . MapManager::TABLE_MAPS . "`
WHERE `index` = ?;"; WHERE `index` = ?;";
$fetchMapStatement = $mysqli->prepare($fetchMapQuery); $fetchMapStatement = $mysqli->prepare($fetchMapQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return; return;
} }
@ -129,7 +131,7 @@ class ManiaExchangeManager {
/** @var Map $map */ /** @var Map $map */
$fetchMapStatement->bind_param('i', $map->index); $fetchMapStatement->bind_param('i', $map->index);
$fetchMapStatement->execute(); $fetchMapStatement->execute();
if($fetchMapStatement->error) { if ($fetchMapStatement->error) {
trigger_error($fetchMapStatement->error); trigger_error($fetchMapStatement->error);
continue; continue;
} }
@ -141,7 +143,7 @@ class ManiaExchangeManager {
//Set changed time into the map object //Set changed time into the map object
$map->lastUpdate = strtotime($changed); $map->lastUpdate = strtotime($changed);
if($mxId != 0) { if ($mxId != 0) {
$appendString = $mxId . ','; $appendString = $mxId . ',';
//Set the mx id to the mxidmapvektor //Set the mx id to the mxidmapvektor
$this->mxIdUidVector[$mxId] = $map->uid; $this->mxIdUidVector[$mxId] = $map->uid;
@ -152,7 +154,7 @@ class ManiaExchangeManager {
$id++; $id++;
//If Max Maplimit is reached, or string gets too long send the request //If Max Maplimit is reached, or string gets too long send the request
if($id % self::MAPS_PER_MX_FETCH == 0) { if ($id % self::MAPS_PER_MX_FETCH == 0) {
$mapIdString = substr($mapIdString, 0, -1); $mapIdString = substr($mapIdString, 0, -1);
$maps = $this->getMaplistByMixedUidIdString($mapIdString); $maps = $this->getMaplistByMixedUidIdString($mapIdString);
$this->updateMapObjectsWithManiaExchangeIds($maps); $this->updateMapObjectsWithManiaExchangeIds($maps);
@ -162,7 +164,7 @@ class ManiaExchangeManager {
$mapIdString .= $appendString; $mapIdString .= $appendString;
} }
if($mapIdString != '') { if ($mapIdString != '') {
$mapIdString = substr($mapIdString, 0, -1); $mapIdString = substr($mapIdString, 0, -1);
$maps = $this->getMaplistByMixedUidIdString($mapIdString); $maps = $this->getMaplistByMixedUidIdString($mapIdString);
$this->updateMapObjectsWithManiaExchangeIds($maps); $this->updateMapObjectsWithManiaExchangeIds($maps);
@ -189,28 +191,28 @@ class ManiaExchangeManager {
$mapInfo = $this->get_file($url); $mapInfo = $this->get_file($url);
if($mapInfo === false) { if ($mapInfo === false) {
$this->error = 'Connection or response error on ' . $url; $this->error = 'Connection or response error on ' . $url;
return array(); return array();
} elseif($mapInfo === -1) { } elseif ($mapInfo === -1) {
$this->error = 'Timed out while reading data from ' . $url; $this->error = 'Timed out while reading data from ' . $url;
return array(); return array();
} elseif($mapInfo == '') { } elseif ($mapInfo == '') {
if(empty($maps)) { if (empty($maps)) {
$this->error = 'No data returned from ' . $url; $this->error = 'No data returned from ' . $url;
return array(); return array();
} }
} }
$mxMapList = json_decode($mapInfo); $mxMapList = json_decode($mapInfo);
if($mxMapList === null) { if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data from ' . $url); trigger_error('Cannot decode searched JSON data from ' . $url);
return null; return null;
} }
$maps = array(); $maps = array();
foreach($mxMapList as $map) { foreach($mxMapList as $map) {
if(!empty($map)) { if (!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map)); array_push($maps, new MXMapInfo($titlePrefix, $map));
} }
} }
@ -240,13 +242,13 @@ class ManiaExchangeManager {
// compile search URL // compile search URL
$url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch?api=on'; $url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch?api=on';
if($env != '') { if ($env != '') {
$url .= '&environments=' . $this->getEnvironment($env); $url .= '&environments=' . $this->getEnvironment($env);
} }
if($name != '') { if ($name != '') {
$url .= '&trackname=' . str_replace(" ", "%20", $name); $url .= '&trackname=' . str_replace(" ", "%20", $name);
} }
if($author != '') { if ($author != '') {
$url .= '&author=' . $author; $url .= '&author=' . $author;
} }
@ -257,28 +259,28 @@ class ManiaExchangeManager {
// $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mc fileutil // $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mc fileutil
$mapInfo = $this->get_file($url); $mapInfo = $this->get_file($url);
if($mapInfo === false) { if ($mapInfo === false) {
$this->error = 'Connection or response error on ' . $url; $this->error = 'Connection or response error on ' . $url;
return array(); return array();
} elseif($mapInfo === -1) { } elseif ($mapInfo === -1) {
$this->error = 'Timed out while reading data from ' . $url; $this->error = 'Timed out while reading data from ' . $url;
return array(); return array();
} elseif($mapInfo == '') { } elseif ($mapInfo == '') {
if(empty($maps)) { if (empty($maps)) {
$this->error = 'No data returned from ' . $url; $this->error = 'No data returned from ' . $url;
return array(); return array();
} }
} }
$mxMapList = json_decode($mapInfo); $mxMapList = json_decode($mapInfo);
if($mxMapList === null) { if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data from ' . $url); trigger_error('Cannot decode searched JSON data from ' . $url);
return null; return null;
} }
$maps = array(); $maps = array();
foreach($mxMapList as $map) { foreach($mxMapList as $map) {
if(!empty($map)) { if (!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map)); array_push($maps, new MXMapInfo($titlePrefix, $map));
} }
} }
@ -297,7 +299,7 @@ class ManiaExchangeManager {
$query = isset($url['query']) ? "?" . $url['query'] : ""; $query = isset($url['query']) ? "?" . $url['query'] : "";
$fp = @fsockopen($url['host'], $port, $errno, $errstr, 4); $fp = @fsockopen($url['host'], $port, $errno, $errstr, 4);
if(!$fp) { if (!$fp) {
return false; return false;
} }
@ -311,10 +313,10 @@ class ManiaExchangeManager {
} }
fclose($fp); fclose($fp);
if($info['timed_out']) { if ($info['timed_out']) {
return -1; return -1;
} else { } else {
if(substr($res, 9, 3) != '200') { if (substr($res, 9, 3) != '200') {
return false; return false;
} }
$page = explode("\r\n\r\n", $res, 2); $page = explode("\r\n\r\n", $res, 2);
@ -342,96 +344,3 @@ class ManiaExchangeManager {
} }
} }
} }
//TODO put in own file
class MXMapInfo {
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;
/**
* 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 MXMapInfo
*/
public function __construct($prefix, $mx) {
$this->prefix = $prefix;
if($mx) {
if($this->prefix == 'tm') {
$dir = 'tracks';
} else // 'sm' || 'qm'
{
$dir = 'maps';
}
if($this->prefix == 'tm' || !property_exists($mx, "MapID")) {
$this->id = $mx->TrackID;
} else {
$this->id = $mx->MapID;
}
$this->name = $mx->Name;
$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 : '';
$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 = '';
}
}
} // MXMapInfo
} // class MXMapInfo

View File

@ -1,306 +0,0 @@
<?php
/* vim: set noexpandtab tabstop=2 softtabstop=2 shiftwidth=2: */
/**
* MXInfoFetcher - Fetch info/records for TM2/SM/QM maps from ManiaExchange
* Created by Xymph <tm@gamers.org> based on:
* http://api.mania-exchange.com/
* http://tm.mania-exchange.com/api
* http://sm.mania-exchange.com/api
* http://tm.mania-exchange.com/threads/view/218
* Derived from TMXInfoFetcher
*
* v1.7: Added $titlepack (TM2/SM)
* v1.6: Allowed 24-char UIDs too
* v1.5: Added $maptype (TM2/SM)
* v1.4: Updated to use MX API v2.0 and add/fix support for SM; added
* $trkvalue (TM2, equals deprecated $lbrating), $unlimiter (TM2/SM),
* $rating/$ratingex/$ratingcnt (SM)
* v1.3: Added URLs to downloadable replays
* v1.2: Added the replays list in $recordlist
* v1.1: Allowed 25-char UIDs too
* v1.0: Initial release
*/
class MXInfoFetcher {
public $section, $prefix, $uid, $id, $records, $error,
$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, $recordlist;
/**
* Fetches all available data for a ManiaExchange map
*
* @param String $game
* MX section for 'TM2', 'SM', 'QM'
* @param String $id
* The map UID to search for (if a 24-27 char alphanum string),
* otherwise the MX ID to search for (if a number)
* @param Boolean $records
* If true, the script also returns the world records (max. 10)
* [not yet available]
* @return MXInfoFetcher
* If $error is not an empty string, it's an error message
*/
public function MXInfoFetcher($game, $id, $records) {
$this->section = $game;
switch ($game) {
case 'TM2':
$this->prefix = 'tm';
break;
case 'SM':
$this->prefix = 'sm';
break;
case 'QM':
$this->prefix = 'qm';
break;
default:
$this->prefix = '';
return;
}
$this->error = '';
$this->records = $records;
// check for UID string
if (preg_match('/^\w{24,27}$/', $id)) {
$this->uid = $id;
$this->getData(true);
// check for MX ID
} elseif (is_numeric($id) && $id > 0) {
$this->id = floor($id);
$this->getData(false);
}
} // MXInfoFetcher
public static function __set_state($import) {
$mx = new MXInfoFetcher('', 0, false);
$mx->section = $import['section'];
$mx->prefix = $import['prefix'];
$mx->uid = (int)$import['uid'];
$mx->id = (int)$import['id'];
$mx->records = (bool)$import['records'];
$mx->error = '';
$mx->name = $import['name'];
$mx->userid = (int)$import['userid'];
$mx->author = $import['author'];
$mx->uploaded = $import['uploaded'];
$mx->updated = $import['updated'];
$mx->type = $import['type'];
$mx->maptype = isset($import['maptype']) ? $import['maptype'] : '';
$mx->titlepack = isset($import['titlepack']) ? $import['titlepack'] : '';
$mx->style = $import['style'];
$mx->envir = $import['envir'];
$mx->mood = $import['mood'];
$mx->dispcost = (int)$import['dispcost'];
$mx->lightmap = (int)$import['lightmap'];
$mx->modname = $import['modname'];
$mx->exever = $import['exever'];
$mx->exebld = $import['exebld'];
$mx->routes = $import['routes'];
$mx->length = $import['length'];
$mx->unlimiter = isset($import['unlimiter']) ? (bool)$import['unlimiter'] : false;
$mx->laps = (int)$import['laps'];
$mx->diffic = $import['diffic'];
$mx->lbrating = isset($import['lbrating']) ? (int)$import['lbrating'] : 0;
$mx->trkvalue = isset($import['trkvalue']) ? (int)$import['trkvalue'] : 0;
$mx->replaytyp = $import['replaytyp'];
$mx->replayid = (int)$import['replayid'];
$mx->replaycnt = (int)$import['replaycnt'];
$mx->acomment = $import['acomment'];
$mx->awards = (int)$import['awards'];
$mx->comments = (int)$import['comments'];
$mx->rating = isset($import['rating']) ? (float)$import['rating'] : 0.0;
$mx->ratingex = isset($import['ratingex']) ? (float)$import['ratingex'] : 0.0;
$mx->ratingcnt = isset($import['ratingcnt']) ? (int)$import['ratingcnt'] : 0;
$mx->pageurl = $import['pageurl'];
$mx->replayurl = $import['replayurl'];
$mx->imageurl = $import['imageurl'];
$mx->thumburl = $import['thumburl'];
$mx->dloadurl = $import['dloadurl'];
$mx->recordlist = null;
if ($mx->trkvalue == 0 && $mx->lbrating > 0)
$mx->trkvalue = $mx->lbrating;
elseif ($mx->lbrating == 0 && $mx->trkvalue > 0)
$mx->lbrating = $mx->trkvalue;
return $mx;
} // __set_state
private function getData($isuid) {
// get map info
if ($this->prefix == 'tm')
$dir = 'tracks';
else // 'sm' || 'qm'
$dir = 'maps';
$url = 'http://api.mania-exchange.com/' . $this->prefix . '/' . $dir . '/' . ($isuid ? $this->uid : $this->id);
$file = $this->get_file($url);
if ($file === false) {
$this->error = 'Connection or response error on ' . $url;
return;
} elseif ($file === -1) {
$this->error = 'Timed out while reading data from ' . $url;
return;
} elseif ($file == '') {
$this->error = 'No data returned from ' . $url;
return;
}
// process map info
$mx = json_decode($file);
if ($mx === null) {
$this->error = 'Cannot decode JSON data from ' . $url;
return;
}
if (empty($mx)) {
$this->error = 'No data returned from ' . $url;
return;
}
$mx = $mx[0];
if ($isuid)
$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->replayid > 0) {
$this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid;
} else {
$this->replayurl = '';
}
// fetch records too?
$this->recordlist = array();
if ($this->prefix == 'tm' && $this->records) {
$limit = 15;
$url = 'http://api.mania-exchange.com/' . $this->prefix . '/replays/' . $this->id . '/' . $limit . '/';
$file = $this->get_file($url);
if ($file === false) {
$this->error = 'Connection or response error on ' . $url;
return;
} elseif ($file === -1) {
$this->error = 'Timed out while reading data from ' . $url;
return;
} elseif ($file == '') {
$this->error = 'No data returned from ' . $url;
return;
}
// process replays info
$mx = json_decode($file);
if ($mx === null) {
$this->error = 'Cannot decode JSON data from ' . $url;
return;
}
$i = 0;
while ($i < $limit && isset($mx[$i])) {
$this->recordlist[$i] = array(
'replayid' => $mx[$i]->ReplayID,
'userid' => $mx[$i]->UserID,
'username' => $mx[$i]->Username,
'uploadedat' => $mx[$i]->UploadedAt,
'replaytime' => $mx[$i]->ReplayTime,
'stuntscore' => $mx[$i]->StuntScore,
'respawns' => $mx[$i]->Respawns,
'beaten' => $mx[$i]->Beaten,
'percentage' => $mx[$i]->Percentage,
'replaypnts' => $mx[$i]->ReplayPoints,
'nadeopnts' => $mx[$i]->NadeoPoints,
'replayurl' => 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $mx[$i]->ReplayID,
);
$i++;
}
}
} // getData
// Simple HTTP Get function with timeout
// ok: return string || error: return false || timeout: return -1
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: MXInfoFetcher (' . PHP_OS . ")\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
} // class MXInfoFetcher

View File

@ -3,9 +3,9 @@
namespace ManiaControl\Manialinks; namespace ManiaControl\Manialinks;
use FML\CustomUI; use FML\CustomUI;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
@ -69,7 +69,9 @@ class CustomUIManager implements CallbackListener {
* @param array $callback * @param array $callback
*/ */
public function handle1Second(array $callback) { public function handle1Second(array $callback) {
if (!$this->updateManialink) return; if (!$this->updateManialink) {
return;
}
$this->updateManialink = false; $this->updateManialink = false;
$this->updateManialink(); $this->updateManialink();
} }

View File

@ -78,7 +78,7 @@ class IconManager implements CallbackListener {
* @return string * @return string
*/ */
public function getIcon($iconName) { public function getIcon($iconName) {
if(!isset($this->icons[$iconName])) { if (!isset($this->icons[$iconName])) {
return null; return null;
} }
return $this->icons[$iconName]; return $this->icons[$iconName];

View File

@ -65,12 +65,12 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
* @return bool * @return bool
*/ */
public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) { public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) {
if(!method_exists($listener, $method)) { if (!method_exists($listener, $method)) {
trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!"); trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!");
return false; return false;
} }
if(!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) { if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) {
// Init listeners array // Init listeners array
$this->pageAnswerListeners[$actionId] = array(); $this->pageAnswerListeners[$actionId] = array();
} }
@ -90,7 +90,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
$removed = false; $removed = false;
foreach($this->pageAnswerListeners as &$listeners) { foreach($this->pageAnswerListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) { foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] != $listener) { if ($listenerCallback[0] != $listener) {
continue; continue;
} }
unset($listeners[$key]); unset($listeners[$key]);
@ -109,7 +109,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
$actionId = $callback[1][2]; $actionId = $callback[1][2];
$login = $callback[1][1]; $login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if(!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) { if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) {
// No page answer listener registered // No page answer listener registered
return; return;
} }
@ -132,17 +132,17 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
$manialinkText = (string)$manialinkText; $manialinkText = (string)$manialinkText;
try { try {
if(!$logins) { if (!$logins) {
return $this->maniaControl->client->sendDisplayManialinkPage(null, $manialinkText, $timeout, $hideOnClick); return $this->maniaControl->client->sendDisplayManialinkPage(null, $manialinkText, $timeout, $hideOnClick);
} }
if(is_string($logins)) { if (is_string($logins)) {
return $this->maniaControl->client->sendDisplayManialinkPage($logins, $manialinkText, $timeout, $hideOnClick); return $this->maniaControl->client->sendDisplayManialinkPage($logins, $manialinkText, $timeout, $hideOnClick);
} }
if(is_array($logins)) { if (is_array($logins)) {
$success = true; $success = true;
foreach($logins as $login) { foreach($logins as $login) {
$subSuccess = $this->maniaControl->client->sendDisplayManialinkPage($login, $manialinkText, $timeout, $hideOnClick); $subSuccess = $this->maniaControl->client->sendDisplayManialinkPage($login, $manialinkText, $timeout, $hideOnClick);
if(!$subSuccess) { if (!$subSuccess) {
$success = false; $success = false;
} }
} }
@ -188,7 +188,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player->login); $this->maniaControl->manialinkManager->sendManialink($maniaLink, $player->login);
$this->disableAltMenu($player); $this->disableAltMenu($player);
if($widgetName != '') { if ($widgetName != '') {
// Trigger callback // Trigger callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_OPENED, array(self::CB_MAIN_WINDOW_OPENED, $player, $widgetName)); $this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_OPENED, array(self::CB_MAIN_WINDOW_OPENED, $player, $widgetName));
} }
@ -211,7 +211,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
* @param bool $widgetId * @param bool $widgetId
*/ */
public function closeWidget(Player $player, $widgetId = false) { public function closeWidget(Player $player, $widgetId = false) {
if(!$widgetId) { if (!$widgetId) {
$emptyManialink = new ManiaLink(self::MAIN_MLID); $emptyManialink = new ManiaLink(self::MAIN_MLID);
$manialinkText = $emptyManialink->render()->saveXML(); $manialinkText = $emptyManialink->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login); $this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login);
@ -256,7 +256,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
$label->setText($text); $label->setText($text);
$label->setTextColor($textColor); $label->setTextColor($textColor);
if($profile) { if ($profile) {
$script->addProfileButton($label, $profile); $script->addProfileButton($label, $profile);
} }

View File

@ -8,6 +8,7 @@ use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\FileUtil; use ManiaControl\FileUtil;
use ManiaControl\Formatter; use ManiaControl\Formatter;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\ManiaExchange\ManiaExchangeManager;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
/** /**