From 5ada096e0a15fcdd5a5433352bef30180c9f5b6d Mon Sep 17 00:00:00 2001 From: kremsy Date: Fri, 7 Feb 2014 17:53:09 +0100 Subject: [PATCH] asynchronous mapadd --- .../ManiaExchange/ManiaExchangeManager.php | 30 +++++++++++++ application/core/Maps/MapManager.php | 45 +++++++++---------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/application/core/ManiaExchange/ManiaExchangeManager.php b/application/core/ManiaExchange/ManiaExchangeManager.php index b9628bd2..3821e37c 100644 --- a/application/core/ManiaExchange/ManiaExchangeManager.php +++ b/application/core/ManiaExchange/ManiaExchangeManager.php @@ -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 diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 49cec79f..abd161db 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -517,33 +517,31 @@ 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); - - 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}"; - - //Download the file - $function = function ($file, $error) use (&$login, &$mapInfo, &$mapDir, &$update) { - if (!$file) { - // Download error - $this->maniaControl->chat->sendError('Download failed!', $login); + $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; } - $this->processMapFile($file, $mapInfo, $mapDir, $login, $update); - }; - $this->maniaControl->fileReader->loadFile($url, $function); + + $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) { + if (!$file) { + // Download error + $this->maniaControl->chat->sendError('Download failed!', $login); + return; + } + $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 - }