map files scanning

This commit is contained in:
steeffeen 2014-06-30 00:18:57 +02:00
parent a948dd57c1
commit 4df544cea6

View File

@ -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);
}
}