small fixes

This commit is contained in:
kremsy 2014-02-07 13:12:53 +01:00 committed by Steffen Schröder
parent 928aabb4d2
commit f40b60e065
3 changed files with 107 additions and 12 deletions

View File

@ -173,6 +173,44 @@ class ManiaExchangeManager {
} }
/**
* Gets a Single Map
*
* @param $string
* @return MXMapInfo
*/
public function getMap($id) {
// 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/?ids=' . $id;
$mapInfo = FileUtil::loadFile($url, "application/json");
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();
}
}
$mxMapList = json_decode($mapInfo);
if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data from ' . $url);
return null;
}
return new MXMapInfo($titlePrefix, $mxMapList[0]);
}
/** /**
* Get the Whole Maplist from MX by Mixed Uid and Id String fetch * Get the Whole Maplist from MX by Mixed Uid and Id String fetch
* *
@ -188,15 +226,10 @@ class ManiaExchangeManager {
$url = 'http://api.mania-exchange.com/' . $titlePrefix . '/maps/?ids=' . $string; $url = 'http://api.mania-exchange.com/' . $titlePrefix . '/maps/?ids=' . $string;
try { //FIXME exceptions get not caught here? try { //FIXME exceptions get not caught here?
$success = $this->maniaControl->fileReader->loadFile($url, function ($mapInfo) { $success = $this->maniaControl->fileReader->loadFile($url, function ($mapInfo) use ($titlePrefix, $url) {
// Get Title Id
$titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtolower(substr($titleId, 0, 2));
$mxMapList = json_decode($mapInfo); $mxMapList = json_decode($mapInfo);
if ($mxMapList === null) { if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data'); trigger_error('Cannot decode searched JSON data from ' . $url);
return null; return null;
} }
@ -218,6 +251,70 @@ class ManiaExchangeManager {
return $success; return $success;
} }
/*public function getMapsAssync($function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
if (!is_callable($function)) {
$this->maniaControl->log("Function is not callable");
return false;
}
// Get Title Id
$titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtolower(substr($titleId, 0, 2));
// Get MapTypes
$scriptInfos = $this->maniaControl->client->getModeScriptInfo();
$mapTypes = $scriptInfos->compatibleMapTypes;
// compile search URL
$url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch?api=on';
if ($env != '') {
$url .= '&environments=' . $this->getEnvironment($env);
}
if ($name != '') {
$url .= '&trackname=' . str_replace(" ", "%20", $name);
}
if ($author != '') {
$url .= '&author=' . $author;
}
$url .= '&priord=' . $searchOrder;
$url .= '&limit=' . $maxMapsReturned;
$url .= '&mtype=' . $mapTypes;
try {
$success = $this->maniaControl->fileReader->loadFile($url, function ($mapInfo) use (&$function) {
// Get Title Id
$titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtolower(substr($titleId, 0, 2));
$mxMapList = json_decode($mapInfo);
if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data');
return null;
}
$maps = array();
foreach($mxMapList as $map) {
if (!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map));
}
}
call_user_func($function, $maps);
return true;
}, "application/json");
} catch(\Exception $e) {
var_dump($e);
return false;
}
return $success;
}*/
/** /**
* Gets a Maplist from Mania Exchange * Gets a Maplist from Mania Exchange
* *

View File

@ -90,6 +90,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
public function checkUpdates(array $chatCallback, Player $player) { public function checkUpdates(array $chatCallback, Player $player) {
//Update Mx Infos //Update Mx Infos
$this->maniaControl->mapManager->mxManager->fetchManiaExchangeMapInformations(); $this->maniaControl->mapManager->mxManager->fetchManiaExchangeMapInformations();
//Reshow the Maplist //Reshow the Maplist
$this->showMapList($player); $this->showMapList($player);
} }

View File

@ -10,7 +10,6 @@ use ManiaControl\Formatter;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\ManiaExchange\ManiaExchangeList; use ManiaControl\ManiaExchange\ManiaExchangeList;
use ManiaControl\ManiaExchange\ManiaExchangeManager; use ManiaControl\ManiaExchange\ManiaExchangeManager;
use ManiaControl\ManiaExchange\MXMapInfo;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
/** /**
@ -524,9 +523,7 @@ class MapManager implements CallbackListener {
$title = strtolower(substr($serverInfo->titleId, 0, 2)); $title = strtolower(substr($serverInfo->titleId, 0, 2));
// Check if map exists // Check if map exists
$mxMapInfos = $this->maniaControl->mapManager->mxManager->getMaplistByMixedUidIdString($mapId); $mapInfo = $this->maniaControl->mapManager->mxManager->getMap($mapId);
$mapInfo = $mxMapInfos[0];
/** @var MXMapInfo $mapInfo */
if (!$mapInfo || !isset($mapInfo->uploaded)) { if (!$mapInfo || !isset($mapInfo->uploaded)) {
// Invalid id // Invalid id
@ -603,7 +600,7 @@ class MapManager implements CallbackListener {
$this->updateFullMapList(); $this->updateFullMapList();
//Update Mx MapInfo //Update Mx MapInfo
$this->maniaControl->mapManager->mxManager->updateMapObjectsWithManiaExchangeIds($mxMapInfos); $this->maniaControl->mapManager->mxManager->updateMapObjectsWithManiaExchangeIds(array($mapInfo));
//Update last updated time //Update last updated time
$map = $this->maps[$mapInfo->uid]; $map = $this->maps[$mapInfo->uid];