final tested version of maniaexchange map search

This commit is contained in:
kremsy 2015-01-21 21:26:48 +01:00
parent 67208624e5
commit eeac91bd22
2 changed files with 39 additions and 85 deletions

View File

@ -272,7 +272,7 @@ class ManiaExchangeManager {
* @deprecated * @deprecated
* @see \ManiaControl\ManiaExchange\ManiaExchangeManager::fetchMapsAsync() * @see \ManiaControl\ManiaExchange\ManiaExchangeManager::fetchMapsAsync()
*/ */
public function getMapsAsync(callable $function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) { public function getMapsAsync(callable $function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = ManiaExchangeMapSearch::SEARCH_ORDER_UPDATED_NEWEST) {
$this->fetchMapsAsync($function, $name, $author, $env, $maxMapsReturned, $searchOrder); $this->fetchMapsAsync($function, $name, $author, $env, $maxMapsReturned, $searchOrder);
return true; return true;
} }
@ -286,74 +286,22 @@ class ManiaExchangeManager {
* @param string $env * @param string $env
* @param int $maxMapsReturned * @param int $maxMapsReturned
* @param int $searchOrder * @param int $searchOrder
*
* @deprecated
* @see \ManiaControl\ManiaExchange\ManiaExchangeMapSearch
*/ */
public function fetchMapsAsync(callable $function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) { public function fetchMapsAsync(callable $function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $sortOrder = ManiaExchangeMapSearch::SEARCH_ORDER_UPDATED_NEWEST) {
//TODO provide additional parameters as array, generally new structure $mapSearch = new ManiaExchangeMapSearch($this->maniaControl);
// TODO: remove $env because it's not really used? $mapSearch->setMapName($name);
$mapSearch->setAuthorName($author);
$mapSearch->setMapLimit($maxMapsReturned);
$mapSearch->setPrioritySortOrder($sortOrder);
// Get Title Id if($env){
$titleId = $this->maniaControl->getServer()->titleId; $mapSearch->setEnvironments($env);
$titlePrefix = $this->maniaControl->getMapManager()->getCurrentMap()->getGame();
// compile search URL
$url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch2/search?api=on';
$game = explode('@', $titleId);
$envNumber = $this->getEnvironment($game[0]);
if ($env || $envNumber > -1) {
$url .= '&environments=' . $envNumber;
}
if ($name) {
$url .= '&trackname=' . str_replace(" ", "%20", $name);
}
if ($author) {
$url .= '&author=' . $author;
} }
$url .= '&priord=' . $searchOrder; $mapSearch->fetchMapsAsync($function);
$url .= '&limit=' . $maxMapsReturned;
if ($titlePrefix !== "tm") {
$url .= '&minexebuild=' . self::MIN_EXE_BUILD;
}
// Get MapTypes
try {
$scriptInfos = $this->maniaControl->getClient()->getModeScriptInfo();
$mapTypes = $scriptInfos->compatibleMapTypes;
$url .= '&mtype=' . $mapTypes;
} catch (GameModeException $e) {
}
$this->maniaControl->getFileReader()->loadFile($url, function ($mapInfo, $error) use (&$function, $titlePrefix) {
if ($error) {
trigger_error($error);
return;
}
$mxMapList = json_decode($mapInfo);
if (!isset($mxMapList->results)) {
trigger_error('Cannot decode searched JSON data');
return;
}
$mxMapList = $mxMapList->results;
if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data');
return;
}
$maps = array();
foreach ($mxMapList as $map) {
if (!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map));
}
}
call_user_func($function, $maps);
}, AsynchronousFileReader::CONTENT_TYPE_JSON);
} }
/** /**

View File

@ -55,7 +55,7 @@ class ManiaExchangeMapSearch {
private $titlePrefix = ""; private $titlePrefix = "";
private $mode = null; private $mode = null;
private $trackName = null; private $mapName = null;
private $authorName = null; private $authorName = null;
private $mod = null; private $mod = null;
private $authorId = null; private $authorId = null;
@ -70,7 +70,7 @@ class ManiaExchangeMapSearch {
private $environments = null; private $environments = null;
private $vehicles = null; private $vehicles = null;
private $page = null; private $page = null;
private $limit = null; private $mapLimit = null;
private $unreleased = null; private $unreleased = null;
private $mapGroup = null; private $mapGroup = null;
private $commentsMinLength = null; private $commentsMinLength = null;
@ -83,9 +83,7 @@ class ManiaExchangeMapSearch {
/** @var ManiaControl $maniaControl */ /** @var ManiaControl $maniaControl */
private $maniaControl = null; private $maniaControl = null;
//TODO test class //TODO use class by mxlist
//TODO test booleans
//TODO use class by maniaexchangemanager and mxlist
/** /**
* Construct map manager * Construct map manager
@ -138,23 +136,23 @@ class ManiaExchangeMapSearch {
if ($this->mode) { if ($this->mode) {
$parameters .= "&mode=" . $this->mode; $parameters .= "&mode=" . $this->mode;
} }
if ($this->trackName) { if ($this->mapName) {
$parameters .= "&trackname=" . str_replace(" ", "%20", $this->trackName); //TODO test without the str_replace (URLENCODE) $parameters .= "&trackname=" . urlencode($this->mapName);
} }
if ($this->authorName) { if ($this->authorName) {
$parameters .= "&author=" . $this->authorName; $parameters .= "&author=" . urlencode($this->authorName);
} }
if ($this->mod) { if ($this->mod) {
$parameters .= "&mod=" . $this->mod; $parameters .= "&mod=" . urlencode($this->mod);
} }
if ($this->authorId) { if ($this->authorId) {
$parameters .= "&authorid= " . $this->authorId; $parameters .= "&authorid= " . $this->authorId;
} }
if ($this->maniaScriptType) { if ($this->maniaScriptType) {
$parameters .= "&mtype=" . $this->maniaScriptType; $parameters .= "&mtype=" . urlencode($this->maniaScriptType);
} }
if ($this->titlePack) { if ($this->titlePack) {
$parameters .= "&tpack=" . $this->titlePack; $parameters .= "&tpack=" . urlencode($this->titlePack);
} }
if ($this->replayType) { if ($this->replayType) {
$parameters .= "&rytpe=" . $this->replayType; $parameters .= "&rytpe=" . $this->replayType;
@ -183,11 +181,11 @@ class ManiaExchangeMapSearch {
if ($this->page) { if ($this->page) {
$parameters .= "&page=" . $this->page; $parameters .= "&page=" . $this->page;
} }
if ($this->limit) { if ($this->mapLimit) {
$parameters .= "&limit=" . $this->limit; $parameters .= "&limit=" . $this->mapLimit;
} }
if (isset($this->unreleased)) { if (isset($this->unreleased)) {
$parameters .= "&unreleased=" . $this->unreleased; $parameters .= "&unreleased=" . (int) $this->unreleased;
} }
if ($this->mapGroup) { if ($this->mapGroup) {
$parameters .= "&mapgroup=" . $this->mapGroup; $parameters .= "&mapgroup=" . $this->mapGroup;
@ -199,7 +197,7 @@ class ManiaExchangeMapSearch {
$parameters .= "&customscreenshot=" . $this->customScreenshot; $parameters .= "&customscreenshot=" . $this->customScreenshot;
} }
if ($this->minExeBuild) { if ($this->minExeBuild) {
$parameters .= "&minexebuild=" . $this->minExeBuild; $parameters .= "&minexebuild=" . urlencode($this->minExeBuild);
} }
if (isset($this->envMix)) { if (isset($this->envMix)) {
$parameters .= "&envmix=" . (int) $this->envMix; $parameters .= "&envmix=" . (int) $this->envMix;
@ -211,6 +209,7 @@ class ManiaExchangeMapSearch {
$parameters .= "&embeddedobjects=" . (int) $this->embeddedObjects; $parameters .= "&embeddedobjects=" . (int) $this->embeddedObjects;
} }
var_dump($this->url . $parameters);
$this->maniaControl->getFileReader()->loadFile($this->url . $parameters, function ($mapInfo, $error) use (&$function) { $this->maniaControl->getFileReader()->loadFile($this->url . $parameters, function ($mapInfo, $error) use (&$function) {
if ($error) { if ($error) {
trigger_error($error); trigger_error($error);
@ -270,10 +269,10 @@ class ManiaExchangeMapSearch {
} }
/** /**
* @param string $trackName * @param string $mapName
*/ */
public function setTrackName($trackName) { public function setMapName($mapName) {
$this->trackName = $trackName; $this->mapName = $mapName;
} }
/** /**
@ -342,14 +341,14 @@ class ManiaExchangeMapSearch {
/** /**
* @param int $secondaryOrder * @param int $secondaryOrder
*/ */
public function setSecondaryOrder($secondaryOrder) { public function setSecondarySortOrder($secondaryOrder) {
$this->secondaryOrder = $secondaryOrder; $this->secondaryOrder = $secondaryOrder;
} }
/** /**
* @param int $priorityOrder * @param int $priorityOrder
*/ */
public function setPriorityOrder($priorityOrder) { public function setPrioritySortOrder($priorityOrder) {
$this->priorityOrder = $priorityOrder; $this->priorityOrder = $priorityOrder;
} }
@ -429,4 +428,11 @@ class ManiaExchangeMapSearch {
public function setEmbeddedObjects($embeddedObjects) { public function setEmbeddedObjects($embeddedObjects) {
$this->embeddedObjects = $embeddedObjects; $this->embeddedObjects = $embeddedObjects;
} }
/**
* @param null $mapLimit
*/
public function setMapLimit($mapLimit) {
$this->mapLimit = $mapLimit;
}
} }