From f40b60e065c951e872488596d606bc6073839be5 Mon Sep 17 00:00:00 2001 From: kremsy Date: Fri, 7 Feb 2014 13:12:53 +0100 Subject: [PATCH] small fixes --- .../ManiaExchange/ManiaExchangeManager.php | 111 ++++++++++++++++-- application/core/Maps/MapList.php | 1 + application/core/Maps/MapManager.php | 7 +- 3 files changed, 107 insertions(+), 12 deletions(-) diff --git a/application/core/ManiaExchange/ManiaExchangeManager.php b/application/core/ManiaExchange/ManiaExchangeManager.php index 575734d3..af4ce216 100644 --- a/application/core/ManiaExchange/ManiaExchangeManager.php +++ b/application/core/ManiaExchange/ManiaExchangeManager.php @@ -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 * @@ -188,15 +226,10 @@ class ManiaExchangeManager { $url = 'http://api.mania-exchange.com/' . $titlePrefix . '/maps/?ids=' . $string; try { //FIXME exceptions get not caught here? - $success = $this->maniaControl->fileReader->loadFile($url, function ($mapInfo) { - - // Get Title Id - $titleId = $this->maniaControl->server->titleId; - $titlePrefix = strtolower(substr($titleId, 0, 2)); - + $success = $this->maniaControl->fileReader->loadFile($url, function ($mapInfo) use ($titlePrefix, $url) { $mxMapList = json_decode($mapInfo); if ($mxMapList === null) { - trigger_error('Cannot decode searched JSON data'); + trigger_error('Cannot decode searched JSON data from ' . $url); return null; } @@ -218,6 +251,70 @@ class ManiaExchangeManager { 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 * diff --git a/application/core/Maps/MapList.php b/application/core/Maps/MapList.php index 00728d2e..fa79ea8a 100644 --- a/application/core/Maps/MapList.php +++ b/application/core/Maps/MapList.php @@ -90,6 +90,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { public function checkUpdates(array $chatCallback, Player $player) { //Update Mx Infos $this->maniaControl->mapManager->mxManager->fetchManiaExchangeMapInformations(); + //Reshow the Maplist $this->showMapList($player); } diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 7f648602..5d07a286 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -10,7 +10,6 @@ use ManiaControl\Formatter; use ManiaControl\ManiaControl; use ManiaControl\ManiaExchange\ManiaExchangeList; use ManiaControl\ManiaExchange\ManiaExchangeManager; -use ManiaControl\ManiaExchange\MXMapInfo; use ManiaControl\Players\Player; /** @@ -524,9 +523,7 @@ class MapManager implements CallbackListener { $title = strtolower(substr($serverInfo->titleId, 0, 2)); // Check if map exists - $mxMapInfos = $this->maniaControl->mapManager->mxManager->getMaplistByMixedUidIdString($mapId); - $mapInfo = $mxMapInfos[0]; - /** @var MXMapInfo $mapInfo */ + $mapInfo = $this->maniaControl->mapManager->mxManager->getMap($mapId); if (!$mapInfo || !isset($mapInfo->uploaded)) { // Invalid id @@ -603,7 +600,7 @@ class MapManager implements CallbackListener { $this->updateFullMapList(); //Update Mx MapInfo - $this->maniaControl->mapManager->mxManager->updateMapObjectsWithManiaExchangeIds($mxMapInfos); + $this->maniaControl->mapManager->mxManager->updateMapObjectsWithManiaExchangeIds(array($mapInfo)); //Update last updated time $map = $this->maps[$mapInfo->uid];