From 4df544cea6f7409e75fa21427b60c975a6403936 Mon Sep 17 00:00:00 2001 From: steeffeen Date: Mon, 30 Jun 2014 00:18:57 +0200 Subject: [PATCH] map files scanning --- application/core/Maps/DirectoryBrowser.php | 92 +++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/application/core/Maps/DirectoryBrowser.php b/application/core/Maps/DirectoryBrowser.php index 12f53d0c..0ef8e1e2 100644 --- a/application/core/Maps/DirectoryBrowser.php +++ b/application/core/Maps/DirectoryBrowser.php @@ -5,6 +5,7 @@ namespace ManiaControl\Maps; use FML\ManiaLink; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\ManialinkManager; +use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Players\Player; /** @@ -14,7 +15,13 @@ use ManiaControl\Players\Player; * @copyright 2014 ManiaControl Team * @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 */ @@ -27,6 +34,19 @@ class DirectoryBrowser { */ public function __construct(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); } + + /** + * 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); + } }