From 9fde082a710558476fc6947419c0c2083a1e5837 Mon Sep 17 00:00:00 2001 From: steeffeen Date: Sat, 5 Jul 2014 13:58:35 +0200 Subject: [PATCH] map adding --- application/core/Maps/DirectoryBrowser.php | 44 +++++++++++++++++++++- application/core/Maps/MapManager.php | 18 ++++----- application/core/Maps/MapQueue.php | 11 +++++- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/application/core/Maps/DirectoryBrowser.php b/application/core/Maps/DirectoryBrowser.php index 00073040..7ab5d626 100644 --- a/application/core/Maps/DirectoryBrowser.php +++ b/application/core/Maps/DirectoryBrowser.php @@ -15,6 +15,9 @@ use ManiaControl\ManiaControl; use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Players\Player; +use Maniaplanet\DedicatedServer\Xmlrpc\AlreadyInListException; +use Maniaplanet\DedicatedServer\Xmlrpc\FileException; +use Maniaplanet\DedicatedServer\Xmlrpc\InvalidMapException; /** * Maps Directory Browser @@ -362,8 +365,45 @@ class DirectoryBrowser implements ManialinkPageAnswerListener { public function handleAddFile(array $actionCallback, Player $player) { $actionName = $actionCallback[1][2]; $fileName = substr($actionName, strlen(self::ACTION_ADD_FILE)); - // TODO: add map - var_dump($fileName); + $folderPath = $player->getCache($this, self::CACHE_FOLDER_PATH); + $filePath = $folderPath . $fileName; + + $mapsFolder = $this->maniaControl->server->directory->getMapsFolder(); + $relativeFilePath = substr($filePath, strlen($mapsFolder)); + + // Check for valid map + try { + $this->maniaControl->client->checkMapForCurrentServerParams($relativeFilePath); + } catch (InvalidMapException $exception) { + $this->maniaControl->chat->sendException($exception, $player); + return; + } catch (FileException $exception) { + $this->maniaControl->chat->sendException($exception, $player); + return; + } + + // Add map to map list + try { + $this->maniaControl->client->insertMap($relativeFilePath); + } catch (AlreadyInListException $exception) { + $this->maniaControl->chat->sendException($exception, $player); + return; + } + $map = $this->maniaControl->mapManager->fetchMapByFileName($relativeFilePath); + if (!$map) { + $this->maniaControl->chat->sendError('Error occurred.', $player); + return; + } + + // Message + $message = $player->getEscapedNickname() . ' added ' . $map->getEscapedName() . '!'; + $this->maniaControl->chat->sendSuccess($message); + $this->maniaControl->log($message, true); + + // Queue requested Map + $this->maniaControl->mapManager->mapQueue->addMapToMapQueue($player, $map); + + $this->showManiaLink($player); } /** diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 3504451e..fd1b496b 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -447,16 +447,6 @@ class MapManager implements CallbackListener { public function initializeMap($rpcMap) { $map = new Map($rpcMap); $this->saveMap($map); - - /*$mapsDirectory = $this->maniaControl->server->getMapsDirectory(); - if (is_readable($mapsDirectory . $map->fileName)) { - $mapFetcher = new \GBXChallMapFetcher(true); - $mapFetcher->processFile($mapsDirectory . $map->fileName); - $map->authorNick = FORMATTER::stripDirtyCodes($mapFetcher->authorNick); - $map->authorEInfo = $mapFetcher->authorEInfo; - $map->authorZone = $mapFetcher->authorZone; - $map->comment = $mapFetcher->comment; - }*/ return $map; } @@ -723,6 +713,14 @@ class MapManager implements CallbackListener { $this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDMAP, $this->currentMap); } + public function fetchMapByFileName($relativeFileName) { + $mapInfo = $this->maniaControl->client->getMapInfo($relativeFileName); + if (!$mapInfo) { + return false; + } + return $this->initializeMap($mapInfo); + } + /** * Handle BeginMap callback * diff --git a/application/core/Maps/MapQueue.php b/application/core/Maps/MapQueue.php index d237d4fc..0b439735 100644 --- a/application/core/Maps/MapQueue.php +++ b/application/core/Maps/MapQueue.php @@ -256,7 +256,12 @@ class MapQueue implements CallbackListener, CommandListener { } } - //Check if the map is already juked + // Check if the map is already juked + $map = null; + if ($uid instanceof Map) { + $map = $uid; + $uid = $map->uid; + } if (array_key_exists($uid, $this->queuedMaps)) { $this->maniaControl->chat->sendError('That map is already in the Map-Queue!', $login); return; @@ -271,7 +276,9 @@ class MapQueue implements CallbackListener, CommandListener { } } - $map = $this->maniaControl->mapManager->getMapByUid($uid); + if (!$map) { + $map = $this->maniaControl->mapManager->getMapByUid($uid); + } $this->queuedMaps[$uid] = array($player, $map);