asynchronous mapadd

This commit is contained in:
kremsy 2014-02-07 17:53:09 +01:00 committed by Steffen Schröder
parent 4c8b247896
commit 5ada096e0a
2 changed files with 51 additions and 24 deletions

View File

@ -172,6 +172,36 @@ class ManiaExchangeManager {
$fetchMapStatement->close();
}
/**
* Get Map Info Asynchronously
*
* @param $id
* @param $function
* @return bool
*/
public function getMapInfo($id, $function) {
// 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;
return $this->maniaControl->fileReader->loadFile($url, function ($mapInfo, $error) use (&$function, $titlePrefix, $url) {
$mxMapInfo = null;
if ($error) {
trigger_error($error);
} else {
$mxMapList = json_decode($mapInfo);
if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data from ' . $url);
} else {
$mxMapInfo = new MXMapInfo($titlePrefix, $mxMapList[0]);
}
}
call_user_func($function, $mxMapInfo);
}, "application/json");
}
/**
* Gets a Single Map

View File

@ -517,22 +517,19 @@ class MapManager implements CallbackListener {
return;
}
// Download the map
if (is_numeric($mapId)) {
// Load from MX
$serverInfo = $this->maniaControl->server->getSystemInfo();
$title = strtolower(substr($serverInfo->titleId, 0, 2));
// Check if map exists
$mapInfo = $this->maniaControl->mapManager->mxManager->getMap($mapId);
$this->maniaControl->mapManager->mxManager->getMapInfo($mapId, function (MXMapInfo $mapInfo) use ($login) {
if (!$mapInfo || !isset($mapInfo->uploaded)) {
// Invalid id
$this->maniaControl->chat->sendError('Invalid MX-Id!', $login);
return;
}
$url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}";
$serverInfo = $this->maniaControl->server->getSystemInfo();
$title = strtolower(substr($serverInfo->titleId, 0, 2));
$url = "http://{$title}.mania-exchange.com/tracks/download/{$mapInfo->id}";
//Download the file
$function = function ($file, $error) use (&$login, &$mapInfo, &$mapDir, &$update) {
@ -544,6 +541,7 @@ class MapManager implements CallbackListener {
$this->processMapFile($file, $mapInfo, $mapDir, $login, $update);
};
$this->maniaControl->fileReader->loadFile($url, $function);
});
}
}
@ -641,5 +639,4 @@ class MapManager implements CallbackListener {
}
}
// TODO: add local map by filename
}