map files scanning
This commit is contained in:
		| @@ -5,6 +5,7 @@ namespace ManiaControl\Maps; | |||||||
| use FML\ManiaLink; | use FML\ManiaLink; | ||||||
| use ManiaControl\ManiaControl; | use ManiaControl\ManiaControl; | ||||||
| use ManiaControl\Manialinks\ManialinkManager; | use ManiaControl\Manialinks\ManialinkManager; | ||||||
|  | use ManiaControl\Manialinks\ManialinkPageAnswerListener; | ||||||
| use ManiaControl\Players\Player; | use ManiaControl\Players\Player; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -14,7 +15,13 @@ use ManiaControl\Players\Player; | |||||||
|  * @copyright 2014 ManiaControl Team |  * @copyright 2014 ManiaControl Team | ||||||
|  * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3 |  * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3 | ||||||
|  */ |  */ | ||||||
| class DirectoryBrowser { | class DirectoryBrowser implements ManialinkPageAnswerListener { | ||||||
|  | 	/* | ||||||
|  | 	 * Constants | ||||||
|  | 	 */ | ||||||
|  | 	const ACTION_SHOW = 'Maps.DirectoryBrowser.Show'; | ||||||
|  | 	const WIDGET_NAME = 'Maps.DirectoryBrowser.Widget'; | ||||||
|  |  | ||||||
| 	/* | 	/* | ||||||
| 	 * Private properties | 	 * Private properties | ||||||
| 	 */ | 	 */ | ||||||
| @@ -27,6 +34,19 @@ class DirectoryBrowser { | |||||||
| 	 */ | 	 */ | ||||||
| 	public function __construct(ManiaControl $maniaControl) { | 	public function __construct(ManiaControl $maniaControl) { | ||||||
| 		$this->maniaControl = $maniaControl; | 		$this->maniaControl = $maniaControl; | ||||||
|  |  | ||||||
|  | 		// Register for ManiaLink Actions | ||||||
|  | 		$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_SHOW, $this, 'handleActionShow'); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Handle Show Page Answer | ||||||
|  | 	 * | ||||||
|  | 	 * @param array  $actionCallback | ||||||
|  | 	 * @param Player $player | ||||||
|  | 	 */ | ||||||
|  | 	public function handleActionShow(array $actionCallback, Player $player) { | ||||||
|  | 		$this->showManiaLink($player); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| @@ -39,4 +59,74 @@ class DirectoryBrowser { | |||||||
|  |  | ||||||
| 		$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player); | 		$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Get the list of not yet added Map files | ||||||
|  | 	 * | ||||||
|  | 	 * @return array|bool | ||||||
|  | 	 */ | ||||||
|  | 	protected function getFilteredMapFiles() { | ||||||
|  | 		$mapFiles = $this->getMapFiles(); | ||||||
|  | 		if (!is_array($mapFiles)) { | ||||||
|  | 			return false; | ||||||
|  | 		} | ||||||
|  | 		$filteredMapFiles = array(); | ||||||
|  | 		foreach ($mapFiles as $mapFile) { | ||||||
|  | 			// TODO: filter already added maps | ||||||
|  | 			array_push($filteredMapFiles, $mapFile); | ||||||
|  | 		} | ||||||
|  | 		return $filteredMapFiles; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Get the list of Map files | ||||||
|  | 	 * | ||||||
|  | 	 * @return array|bool | ||||||
|  | 	 */ | ||||||
|  | 	protected function getMapFiles() { | ||||||
|  | 		$mapsDirectory = $this->maniaControl->server->directory->getMapsFolder(); | ||||||
|  | 		return $this->scanMapFiles($mapsDirectory); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Scan the given directory for Map files | ||||||
|  | 	 * | ||||||
|  | 	 * @param string $directory | ||||||
|  | 	 * @return array|bool | ||||||
|  | 	 */ | ||||||
|  | 	protected function scanMapFiles($directory) { | ||||||
|  | 		if (!is_readable($directory) || !is_dir($directory)) { | ||||||
|  | 			return false; | ||||||
|  | 		} | ||||||
|  | 		$mapFiles = array(); | ||||||
|  | 		$dirFiles = scandir($directory); | ||||||
|  | 		foreach ($dirFiles as $fileName) { | ||||||
|  | 			var_dump($fileName); | ||||||
|  | 			if (is_dir($fileName)) { | ||||||
|  | 				$subDirectory = $directory . $fileName . DIRECTORY_SEPARATOR; | ||||||
|  | 				$subMapFiles  = $this->scanMapFiles($subDirectory); | ||||||
|  | 				if (is_array($subMapFiles)) { | ||||||
|  | 					$mapFiles = array_merge($mapFiles, $subMapFiles); | ||||||
|  | 				} | ||||||
|  | 			} else { | ||||||
|  | 				if ($this->isMapFileName($fileName)) { | ||||||
|  | 					$fullFileName = $directory . $fileName; | ||||||
|  | 					array_push($mapFiles, $fullFileName); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return $mapFiles; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Check if the given file name represents a Map file | ||||||
|  | 	 * | ||||||
|  | 	 * @param string $fileName | ||||||
|  | 	 * @return bool | ||||||
|  | 	 */ | ||||||
|  | 	protected function isMapFileName($fileName) { | ||||||
|  | 		$mapFileNameEnding = '.map.gbx'; | ||||||
|  | 		$fileNameEnding    = strtolower(substr($fileName, -strlen($mapFileNameEnding))); | ||||||
|  | 		return ($fileNameEnding === $mapFileNameEnding); | ||||||
|  | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user