maniaControl = $maniaControl; } /** * Store Map Info from MX and store the mxid in the database and the mx info in the map object * * @param $mxMapInfos */ public function updateMapObjectsWithManiaExchangeIds($mxMapInfos) { $mysqli = $this->maniaControl->database->mysqli; // Save map data $saveMapQuery = "UPDATE `" . MapManager::TABLE_MAPS . "` SET `mxid` = ? WHERE `uid` = ?;"; $saveMapStatement = $mysqli->prepare($saveMapQuery); if ($mysqli->error) { trigger_error($mysqli->error); return; } foreach($mxMapInfos as $mxMapInfo) { /** @var MXMapInfo $mxMapInfo */ $saveMapStatement->bind_param('is', $mxMapInfo->id, $mxMapInfo->uid); $saveMapStatement->execute(); if ($saveMapStatement->error) { trigger_error($saveMapStatement->error); } //Take the uid out of the vektor if (isset($this->mxIdUidVector[$mxMapInfo->id])) { $uid = $this->mxIdUidVector[$mxMapInfo->id]; } else { $uid = $mxMapInfo->uid; } $map = $this->maniaControl->mapManager->getMapByUid($uid); /** @var Map $map */ $map->mx = $mxMapInfo; } $saveMapStatement->close(); } /** * Unset Map by Mx Id * * @param $mxId */ public function unsetMap($mxId) { unset($this->mxIdUidVector[$mxId]); } /** * Fetch Map Information from Mania Exchange * * @param null $map */ public function fetchManiaExchangeMapInformations($map = null) { if (!$map) { //Fetch Informations for whole Maplist $maps = $this->maniaControl->mapManager->getMaps(); } else { //Fetch Information for a single map $maps[] = $map; } $mysqli = $this->maniaControl->database->mysqli; $mapIdString = ''; // Fetch mx ids $fetchMapQuery = "SELECT `mxid`, `changed` FROM `" . MapManager::TABLE_MAPS . "` WHERE `index` = ?;"; $fetchMapStatement = $mysqli->prepare($fetchMapQuery); if ($mysqli->error) { trigger_error($mysqli->error); return; } $id = 0; foreach($maps as $map) { /** @var Map $map */ $fetchMapStatement->bind_param('i', $map->index); $fetchMapStatement->execute(); if ($fetchMapStatement->error) { trigger_error($fetchMapStatement->error); continue; } $fetchMapStatement->store_result(); $fetchMapStatement->bind_result($mxId, $changed); $fetchMapStatement->fetch(); $fetchMapStatement->free_result(); //Set changed time into the map object $map->lastUpdate = strtotime($changed); if ($mxId != 0) { $appendString = $mxId . ','; //Set the mx id to the mxidmapvektor $this->mxIdUidVector[$mxId] = $map->uid; } else { $appendString = $map->uid . ','; } $id++; //If Max Maplimit is reached, or string gets too long send the request if ($id % self::MAPS_PER_MX_FETCH == 0) { $mapIdString = substr($mapIdString, 0, -1); $this->getMaplistByMixedUidIdString($mapIdString); $mapIdString = ''; } $mapIdString .= $appendString; } if ($mapIdString != '') { $mapIdString = substr($mapIdString, 0, -1); $this->getMaplistByMixedUidIdString($mapIdString); } $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"); } /** * Get the Whole MapList from MX by Mixed Uid and Id String fetch * * @param $string * @return array|null */ public function getMaplistByMixedUidIdString($string) { // 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=' . $string; $success = $this->maniaControl->fileReader->loadFile($url, function ($mapInfo, $error) use ($titlePrefix, $url) { if ($error) { trigger_error($error . " " . $url); return null; } $mxMapList = json_decode($mapInfo); if ($mxMapList === null) { trigger_error('Cannot decode searched JSON data from ' . $url); return null; } $maps = array(); foreach($mxMapList as $map) { if ($map != null) { array_push($maps, new MXMapInfo($titlePrefix, $map)); } } $this->updateMapObjectsWithManiaExchangeIds($maps); return true; }, "application/json"); return $success; } /** * Fetch a MapList Asynchronously * * @param $function * @param string $name * @param string $author * @param string $env * @param int $maxMapsReturned * @param int $searchOrder * @return bool */ public function getMapsAsync($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)); // 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; // Get MapTypes try { $scriptInfos = $this->maniaControl->client->getModeScriptInfo(); $mapTypes = $scriptInfos->compatibleMapTypes; $url .= '&mtype=' . $mapTypes; } catch(Exception $e) { if ($e->getMessage() != 'Not in script mode.') { throw $e; } //dont append map type } $fileFunc = function ($mapInfo, $error) use (&$function, $titlePrefix) { if ($error) { trigger_error($error); return null; } $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; }; $success = $this->maniaControl->fileReader->loadFile($url, $fileFunc, "application/json"); return $success; } /** * Gets the Current Environemnt by String * * @param $env * @return int */ private function getEnvironment($env) { switch($env) { case 'TMCanyon': case 'SMStorm': return 1; case 'TMStadium': return 2; case 'TMValley': return 3; default: return -1; } } }