mx fetching db statements

This commit is contained in:
Steffen Schröder 2014-01-09 22:08:34 +01:00
parent 28a435ba16
commit f9041748f6

View File

@ -1,4 +1,5 @@
<?php <?php
namespace ManiaControl\Maps; namespace ManiaControl\Maps;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
@ -44,41 +45,69 @@ class ManiaExchangeInfoSearcher {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
} }
public function updateMapObjectWithManiaExchangeIds() {
public function updateMapObjectWithManiaExchangeIds(){
$maps = $this->maniaControl->mapManager->getMaps(); $maps = $this->maniaControl->mapManager->getMaps();
$mysqli = $this->maniaControl->database->mysqli;
$mapIds = array(); $mapIds = array();
foreach($maps as $map){
/** @var Map $map */
//TODO prepared statement read MX id from maps database
// 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) {
//code... /**
*
//fetch mx info * @var Map $map
*/
foreach ($maps as $map){ $mapIndex = $map->index;
/** @var Map $map */ $fetchMapStatement->execute();
//code... if ($fetchMapStatement->error) {
trigger_error($fetchMapStatement->error);
continue;
//TODO prepared statment set MX id in maps database
} }
$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
// Save map data
$saveMapQuery = "UPDATE `" . MapManager::TABLE_MAPS . "`
SET `mxid` = ?
WHERE `index` = ?;";
$saveMapStatement = $mysqli->prepare($saveMapQuery);
if ($mysqli->error) {
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;
$saveMapStatement->execute();
if ($saveMapStatement->error) {
trigger_error($saveMapStatement->error);
}
}
$saveMapStatement->close();
}
/** /**
* Gets a Maplist from Mania Exchange * Gets a Maplist from Mania Exchange
@ -88,14 +117,14 @@ class ManiaExchangeInfoSearcher {
* @param string $env * @param string $env
* @param int $maxMapsReturned * @param int $maxMapsReturned
* @param int $searchOrder * @param int $searchOrder
* @return array|null * @return array null
*/ */
public function getMaps($name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) { public function getMaps($name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
//Get Title Id // Get Title Id
$titleId = $this->maniaControl->server->titleId; $titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtolower(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();
@ -104,13 +133,13 @@ class ManiaExchangeInfoSearcher {
// 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=' . $name; $url .= '&trackname=' . $name;
} }
if($author != '') { if ($author != '') {
$url .= '&author=' . $author; $url .= '&author=' . $author;
} }
@ -121,32 +150,22 @@ class ManiaExchangeInfoSearcher {
// $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);
//TODO errors // TODO errors
/*if ($file === false) { /*
$this->error = 'Connection or response error on ' . $url; * if ($file === false) { $this->error = 'Connection or response error on ' . $url; return array(); } elseif ($file === -1) { $this->error =
return array(); * 'Timed out while reading data from ' . $url; return array(); } elseif ($file == '') { if (empty($maps)) { $this->error = 'No data returned
} elseif ($file === -1) { * from ' . $url; return array(); } else { break; } }
$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); $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));
} }
} }
@ -154,39 +173,40 @@ class ManiaExchangeInfoSearcher {
} }
private function get_file($url) { private function get_file($url) {
$url = parse_url($url); $url = parse_url($url);
$port = isset($url['port']) ? $url['port'] : 80; $port = isset($url['port']) ? $url['port'] : 80;
$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;
} }
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); stream_set_timeout($fp, 2);
$res = ''; $res = '';
$info['timed_out'] = false; $info['timed_out'] = false;
while(!feof($fp) && !$info['timed_out']) { while (!feof($fp) && !$info['timed_out']) {
$res .= fread($fp, 512); $res .= fread($fp, 512);
$info = stream_get_meta_data($fp); $info = stream_get_meta_data($fp);
} }
fclose($fp); fclose($fp);
if($info['timed_out']) { if ($info['timed_out']) {
return -1; return -1;
} else { }
if(substr($res, 9, 3) != '200') { else {
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);
return trim($page[1]); return trim($page[1]);
} }
} // get_file } // get_file
private function getEnvironment($env) { private function getEnvironment($env) {
switch($env) { switch ($env) {
case 'TMCanyon': case 'TMCanyon':
case 'SMStorm': case 'SMStorm':
return 1; return 1;
@ -198,36 +218,34 @@ class ManiaExchangeInfoSearcher {
return -1; return -1;
} }
} }
} }
class MXMapInfo { 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, $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 * Returns map object with all available data from MX map data
* *
* @param String $prefix * @param String $prefix MX URL prefix
* MX URL prefix * @param Object $map The MX map data from MXInfoSearcher
* @param Object $map
* The MX map data from MXInfoSearcher
* @return MXMapInfo * @return MXMapInfo
*/ */
public function __construct($prefix, $mx) { public function __construct($prefix, $mx) {
$this->prefix = $prefix; $this->prefix = $prefix;
if($mx) { if ($mx) {
if($this->prefix == 'tm') { if ($this->prefix == 'tm') {
$dir = 'tracks'; $dir = 'tracks';
} else // 'sm' || 'qm' }
else // 'sm' || 'qm'
{ {
$dir = 'maps'; $dir = 'maps';
} }
//temporary fix // temporary fix
if($this->prefix == 'tm' || !property_exists($mx, "MapID")) { if ($this->prefix == 'tm' || !property_exists($mx, "MapID")) {
$this->id = $mx->TrackID; $this->id = $mx->TrackID;
} else { }
else {
$this->id = $mx->MapID; $this->id = $mx->MapID;
} }
@ -265,9 +283,10 @@ class MXMapInfo {
$this->ratingex = isset($mx->RatingExact) ? $mx->RatingExact : 0.0; $this->ratingex = isset($mx->RatingExact) ? $mx->RatingExact : 0.0;
$this->ratingcnt = isset($mx->RatingCount) ? $mx->RatingCount : 0; $this->ratingcnt = isset($mx->RatingCount) ? $mx->RatingCount : 0;
if($this->trkvalue == 0 && $this->lbrating > 0) { if ($this->trkvalue == 0 && $this->lbrating > 0) {
$this->trkvalue = $this->lbrating; $this->trkvalue = $this->lbrating;
} elseif($this->lbrating == 0 && $this->trkvalue > 0) { }
elseif ($this->lbrating == 0 && $this->trkvalue > 0) {
$this->lbrating = $this->trkvalue; $this->lbrating = $this->trkvalue;
} }
@ -281,9 +300,10 @@ class MXMapInfo {
$this->thumburl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/screenshot/small/' . $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; $this->dloadurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $dir . '/download/' . $this->id;
if($this->prefix == 'tm' && $this->replayid > 0) { if ($this->prefix == 'tm' && $this->replayid > 0) {
$this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid; $this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid;
} else { }
else {
$this->replayurl = ''; $this->replayurl = '';
} }
} }