improvments mx communication

This commit is contained in:
kremsy 2014-01-09 23:21:44 +01:00
parent 161ef44508
commit fceb0aa9e0
3 changed files with 226 additions and 157 deletions

View File

@ -9,7 +9,7 @@ use ManiaControl\ManiaControl;
*
* @author steeffeen & kremsy
*/
class ManiaExchangeInfoSearcher {
class ManiaExchangeInfoSearcher { //TODO rename to ManiaExchangeManager
/**
* Constants
*/
@ -30,7 +30,7 @@ class ManiaExchangeInfoSearcher {
const SEARCH_ORDER_DIFFICULTY_HARDEST = 13;
const SEARCH_ORDER_LENGHT_SHORTEST = 14;
const SEARCH_ORDER_LENGHT_LONGEST = 15;
const MAPS_PER_MX_FETCH = 10;
/**
* Private Propertieswc
*/
@ -45,44 +45,14 @@ class ManiaExchangeInfoSearcher {
$this->maniaControl = $maniaControl;
}
public function updateMapObjectWithManiaExchangeIds() {
$maps = $this->maniaControl->mapManager->getMaps();
$mysqli = $this->maniaControl->database->mysqli;
$mapIds = array();
// Fetch mx ids
$fetchMapQuery = "SELECT `mxid` FROM `" . MapManager::TABLE_MAPS . "`
WHERE `index` = ?;";
$fetchMapStatement = $mysqli->prepare($fetchMapQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$fetchMapStatement->bind_param('i', $mapIndex);
foreach ($maps as $map) {
/**
* Store Map Info from MX and store the mxid in the database and the mx info in the map object
*
* @var Map $map
* @param $mxMapInfos
*/
$mapIndex = $map->index;
$fetchMapStatement->execute();
if ($fetchMapStatement->error) {
trigger_error($fetchMapStatement->error);
continue;
}
$fetchMapStatement->store_result();
$fetchMapStatement->bind_result($mxId);
$fetchMapStatement->fetch();
$fetchMapStatement->free_result();
// FIXME: save ids like that?
$mapIds[$map->index] = $mxId;
}
$fetchMapStatement->close();
// Fetch mx data
// TODO: fetch mx info
private function updateMapObjectWithManiaExchangeIds($mxMapInfos) {
$mysqli = $this->maniaControl->database->mysqli;
// Save map data
$saveMapQuery = "UPDATE `" . MapManager::TABLE_MAPS . "`
SET `mxid` = ?
@ -92,23 +62,112 @@ class ManiaExchangeInfoSearcher {
trigger_error($mysqli->error);
return;
}
$saveMapStatement->bind_param('ii', $mxId, $mapIndex);
foreach ($maps as $map) {
/**
*
* @var Map $map
*/
// FIXME: set $mxId
$mxId = 1337;
$mapIndex = $map->index;
foreach($mxMapInfos as $mxMapInfo) {
/** @var MXMapInfo $mxMapInfo */
$saveMapStatement->bind_param('is', $mxMapInfo->id, $mxMapInfo->uid);
$saveMapStatement->execute();
if($saveMapStatement->error) {
trigger_error($saveMapStatement->error);
}
$map = $this->maniaControl->mapManager->getMapByUid($mxMapInfo->uid);
/** @var Map $map */
$map->mx = $mxMapInfo;
}
$saveMapStatement->close();
}
/**
* Fetch Map Information from Mania Exchange
*/
public function fetchManiaExchangeMapInformations() {
$maps = $this->maniaControl->mapManager->getMaps();
$mysqli = $this->maniaControl->database->mysqli;
$mapIdString = '';
// Fetch mx ids
$fetchMapQuery = "SELECT `mxid`, `changed` FROM `" . MapManager::TABLE_MAPS . "`
WHERE `index` = ?;";
$fetchMapStatement = $mysqli->prepare($fetchMapQuery);
if($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$id = 0;
foreach($maps as $map) {
/** @var Map $map */
$fetchMapStatement->bind_param('i', $map->index);
$fetchMapStatement->execute();
if($fetchMapStatement->error) {
trigger_error($fetchMapStatement->error);
continue;
}
$fetchMapStatement->store_result();
$fetchMapStatement->bind_result($mxId, $changed);
$fetchMapStatement->fetch();
$fetchMapStatement->free_result();
//Set changed time into the map object
$map->lastUpdate = strtotime($changed);
//if($mxId != null) { //TODO not working due a fail on mx
// $mapIdString .= $mxId . ',';
//} else {
$mapIdString .= $map->uid . ',';
//}
$id++;
//if($id % self::MAPS_PER_MX_FETCH == 0) {
if($id % 6 == 0) { //TODO 6 is temporary
$maps = $this->getMaplistByMixedUidIdString($mapIdString);
$this->updateMapObjectWithManiaExchangeIds($maps);
$mapIdString = '';
}
}
if($mapIdString != '') {
$maps = $this->getMaplistByMixedUidIdString($mapIdString);
$this->updateMapObjectWithManiaExchangeIds($maps);
}
$fetchMapStatement->close();
}
public function getMaplistByMixedUidIdString($string) {
// Get Title Id
$titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtolower(substr($titleId, 0, 2));
// compile search URL
$url = 'http://api.mania-exchange.com/' . $titlePrefix . '/maps/' . $string;
// $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mc fileutil
$mapInfo = $this->get_file($url);
// 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; } }
*/
$mxMapList = json_decode($mapInfo);
if($mxMapList === null) {
trigger_error('Cannot decode searched JSON data from ' . $url);
return null;
}
$maps = array();
foreach($mxMapList as $map) {
if(!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map));
}
}
return $maps;
}
/**
* Gets a Maplist from Mania Exchange
*
@ -182,9 +241,7 @@ class ManiaExchangeInfoSearcher {
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");
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;
@ -196,8 +253,7 @@ class ManiaExchangeInfoSearcher {
if($info['timed_out']) {
return -1;
}
else {
} else {
if(substr($res, 9, 3) != '200') {
return false;
}
@ -220,8 +276,13 @@ class ManiaExchangeInfoSearcher {
}
}
//TODO put in own file
class MXMapInfo {
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;
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
@ -235,22 +296,20 @@ class MXMapInfo {
if($mx) {
if($this->prefix == 'tm') {
$dir = 'tracks';
}
else // 'sm' || 'qm'
} else // 'sm' || 'qm'
{
$dir = 'maps';
}
// temporary fix
if($this->prefix == 'tm' || !property_exists($mx, "MapID")) {
$this->id = $mx->TrackID;
}
else {
} 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;
@ -285,8 +344,7 @@ class MXMapInfo {
if($this->trkvalue == 0 && $this->lbrating > 0) {
$this->trkvalue = $this->lbrating;
}
elseif ($this->lbrating == 0 && $this->trkvalue > 0) {
} elseif($this->lbrating == 0 && $this->trkvalue > 0) {
$this->lbrating = $this->trkvalue;
}
@ -302,12 +360,11 @@ class MXMapInfo {
if($this->prefix == 'tm' && $this->replayid > 0) {
$this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid;
}
else {
} else {
$this->replayurl = '';
}
}
} // MXInfo
} // class MXInfo
} // MXMapInfo
} // class MXMapInfo

View File

@ -24,6 +24,7 @@ class Map {
public $mapType = '';
public $mapStyle = '';
public $nbCheckpoints = -1;
/** @var MXMapInfo $mx */
public $mx = null;
public $authorLogin = '';
public $authorNick = '';
@ -32,6 +33,7 @@ class Map {
public $comment = '';
public $titleUid = '';
public $startTime = -1;
public $lastUpdate = 0;
/**
* Private Properties
@ -60,6 +62,7 @@ class Map {
$this->copperPrice = $rpc_infos['CopperPrice'];
$this->mapType = $rpc_infos['MapType'];
$this->mapStyle = $rpc_infos['MapStyle'];
if(isset($rpc_infos['NbCheckpoints'])) {
$this->nbCheckpoints = $rpc_infos['NbCheckpoints'];
}
@ -79,10 +82,18 @@ class Map {
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->titleId;
$title = strtoupper(substr($serverInfo, 0, 2));
$this->mx = new \MXInfoFetcher($title, $this->uid, false);
/**
* Checks if a map Update is available
*
* @return bool
*/
public function updateAvailable() {
if($this->lastUpdate < $this->mx->updated || $this->uid != $this->mx->uid) {
return true;
} else {
return false;
}
}
}

View File

@ -213,6 +213,7 @@ class MapManager implements CallbackListener {
public function handleOnInit(array $callback) {
$this->updateFullMapList();
$this->fetchCurrentMap();
$this->mxInfoSearcher->fetchManiaExchangeMapInformations();
}
/**