diff --git a/application/core/Maps/MapCommands.php b/application/core/Maps/MapCommands.php index 7914820e..ac933f33 100644 --- a/application/core/Maps/MapCommands.php +++ b/application/core/Maps/MapCommands.php @@ -83,78 +83,9 @@ class MapCommands implements CommandListener { $this->maniaControl->chat->sendUsageInfo('Usage example: //addmap 1234', $player->login); return; } - // Check if ManiaControl can even write to the maps dir - if (!$this->maniaControl->client->query('GetMapsDirectory')) { - trigger_error("Couldn't get map directory. " . $this->maniaControl->getClientErrorText()); - $this->maniaControl->chat->sendError("ManiaControl couldn't retrieve the maps directory.", $player->login); - return; - } - $mapDir = $this->maniaControl->client->getResponse(); - if (!is_dir($mapDir)) { - trigger_error("ManiaControl doesn't have have access to the maps directory in '{$mapDir}'."); - $this->maniaControl->chat->sendError("ManiaControl doesn't have access to the maps directory.", $player->login); - return; - } - $downloadDirectory = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX'); - // Create download directory if necessary - if (!is_dir($mapDir . $downloadDirectory) && !mkdir($mapDir . $downloadDirectory)) { - trigger_error("ManiaControl doesn't have to rights to save maps in '{$mapDir}{$downloadDirectory}'."); - $this->maniaControl->chat->sendError("ManiaControl doesn't have the rights to save maps.", $player->login); - return; - } - $mapDir .= $downloadDirectory . '/'; - // Download the map - $mapId = $params[1]; - if (is_numeric($mapId)) { - // Load from MX - $serverInfo = $this->maniaControl->server->getSystemInfo(); - $title = strtolower(substr($serverInfo['TitleId'], 0, 2)); - // Check if map exists - $url = "http://{$title}.mania-exchange.com/api/tracks/get_track_info/id/{$mapId}?format=json"; - $mapInfo = FileUtil::loadFile($url); - if (!$mapInfo || strlen($mapInfo) <= 0) { - // Invalid id - $this->maniaControl->chat->sendError('Invalid MX-Id!', $player->login); - return; - } - $mapInfo = json_decode($mapInfo, true); - $url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}"; - $file = FileUtil::loadFile($url); - if (!$file) { - // Download error - $this->maniaControl->chat->sendError('Download failed!', $player->login); - return; - } - // Save map - $fileName = $mapInfo['TrackID'] . '_' . $mapInfo['Name'] . '.Map.Gbx'; - $fileName = FileUtil::getClearedFileName($fileName); - if (!file_put_contents($mapDir . $fileName, $file)) { - // Save error - $this->maniaControl->chat->sendError('Saving map failed!', $player->login); - return; - } - // Check for valid map - $mapFileName = $downloadDirectory . '/' . $fileName; - if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $mapFileName)) { - trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $this->maniaControl->getClientErrorText()); - $this->maniaControl->chat->sendError('Error checking map!', $player->login); - return; - } - $response = $this->maniaControl->client->getResponse(); - if (!$response) { - // Invalid map type - $this->maniaControl->chat->sendError("Invalid map type.", $player->login); - return; - } - // Add map to map list - if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) { - $this->maniaControl->chat->sendError("Couldn't add map to match settings!", $player->login); - return; - } - $this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!'); - return; - } - // TODO: add local map by filename + + //add Map from Mania Exchange + $this->maniaControl->mapManager->addMapFromMx($params[1],$player->login); } /** diff --git a/application/core/Maps/MapList.php b/application/core/Maps/MapList.php index 65665763..a923e6a4 100644 --- a/application/core/Maps/MapList.php +++ b/application/core/Maps/MapList.php @@ -11,6 +11,10 @@ use FML\Controls\Control; use FML\Controls\Label; use FML\Controls\Labels\Label_Text; use FML\Controls\Quads\Quad_Icons64x64_1; +use FML\Script\Script; +use FML\Script\Tooltips; +use ManiaControl\Callbacks\CallbackListener; +use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Maps\Map; @@ -22,9 +26,13 @@ use ManiaControl\ManiaControl; use ManiaControl\Players\Player; use MXInfoSearcher; -class MapList implements ManialinkPageAnswerListener { +class MapList implements ManialinkPageAnswerListener, CallbackListener { + const ACTION_CLOSEWIDGET = 'MapList.CloseWidget'; + const ACTION_ADD_MAP = 'MapList.AddMap'; const MAX_MAPS_PER_PAGE = 15; + + /** * Private properties */ @@ -45,6 +53,8 @@ class MapList implements ManialinkPageAnswerListener { $this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET , $this, 'closeWidget'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, + 'handleManialinkPageAnswer'); //settings $this->width = 150; @@ -106,6 +116,13 @@ class MapList implements ManialinkPageAnswerListener { $frame = $this->buildMainFrame(); $maniaLink->add($frame); + // Create script and features + $script = new Script(); + $maniaLink->setScript($script); + + $tooltips = new Tooltips(); + $script->addFeature($tooltips); + //Start offsets $x = -$this->width / 2; $y = $this->height / 2; @@ -114,7 +131,7 @@ class MapList implements ManialinkPageAnswerListener { $headFrame = new Frame(); $frame->add($headFrame); $headFrame->setY($y - 3); - $array = array("Id" => $x + 5, "MapName" => $x + 15, "MapAuthor" => $x + 70, "MapMood" => $x + 90, "MapType" => $x + 105); + $array = array("Id" => $x + 5, "Name" => $x + 17, "Author" => $x + 70, "Mood" => $x + 90, "Type" => $x + 105); $this->maniaControl->manialinkManager->labelLine($headFrame,$array); $i = 0; @@ -122,11 +139,33 @@ class MapList implements ManialinkPageAnswerListener { foreach($maps as $map){ $mapFrame = new Frame(); $frame->add($mapFrame); - $array = array($map->id => $x + 5, $map->name => $x + 15, $map->author => $x + 70, $map->mood => $x + 90, $map->maptype => $x + 105); + $array = array($map->id => $x + 5, $map->name => $x + 17, $map->author => $x + 70, $map->mood => $x + 90, $map->maptype => $x + 105); $this->maniaControl->manialinkManager->labelLine($mapFrame,$array); $mapFrame->setY($y); - $y -= 4; + //TODO only for admins: + //Add-Map-Button + $addQuad = new Quad_Icons64x64_1(); + $mapFrame->add($addQuad); + $addQuad->setX($x + 15); + $addQuad->setZ(-0.1); + $addQuad->setSubStyle($addQuad::SUBSTYLE_Add); + $addQuad->setSize(4,4); + $addQuad->setAction(self::ACTION_ADD_MAP . "." .$map->id); + + //Description Label + $descriptionLabel = new Label(); + $frame->add($descriptionLabel); + $descriptionLabel->setAlign(Control::LEFT, Control::TOP); + $descriptionLabel->setPosition($x + 10, -$this->height / 2 + 5); + $descriptionLabel->setSize($this->width * 0.7, 4); + $descriptionLabel->setTextSize(2); + $descriptionLabel->setVisible(false); + $descriptionLabel->setText("Add-Map: {$map->name}"); + $tooltips->add($addQuad, $descriptionLabel); + + + $y -= 4; $i++; if($i == self::MAX_MAPS_PER_PAGE) break; @@ -187,7 +226,6 @@ class MapList implements ManialinkPageAnswerListener { //Get Maplist $mapList = $this->maniaControl->mapManager->getMapList(); - $mapList = array_slice($mapList, 0, self::MAX_MAPS_PER_PAGE); //TODO add pages $id = 1; @@ -227,63 +265,11 @@ class MapList implements ManialinkPageAnswerListener { //Display Maps $array = array($id => $x + 5, $mxId => $x + 10, $map->name => $x + 20, $map->authorNick => $x + 70); $this->maniaControl->manialinkManager->labelLine($frame,$array); - - -/* //TODO detailed mx info page with link to mx - $x +=5; - $idLabel = new Label_Text(); - $frame->add($idLabel); - $idLabel->setHAlign(Control::LEFT); - $idLabel->setX($x); - // $mxIdLabel->setSize($width * 0.5, 2); - $idLabel->setStyle($idLabel::STYLE_TextCardSmall); - $idLabel->setTextSize(1.5); - $idLabel->setText($id); - $idLabel->setTextColor('FFF'); - - //TODO detailed mx info page with link to mx - $x +=5; - $mxIdLabel = new Label_Text(); - $frame->add($mxIdLabel); - $mxIdLabel->setHAlign(Control::LEFT); - $mxIdLabel->setX($x); - // $mxIdLabel->setSize($width * 0.5, 2); - $mxIdLabel->setStyle($mxIdLabel::STYLE_TextCardSmall); - $mxIdLabel->setTextSize(1.5); - if(isset($map->mx->id)) - $mxIdLabel->setText($map->mx->id); - else - $mxIdLabel->setText("-"); - $mxIdLabel->setTextColor('FFF'); - //TODO action detailed map info - $x +=10; - $nameLabel = new Label_Text(); - $frame->add($nameLabel); - $nameLabel->setHAlign(Control::LEFT); - $nameLabel->setX($x); - //$nameLabel->setSize($width * 0.5, 2); - $nameLabel->setStyle($nameLabel::STYLE_TextCardSmall); - $nameLabel->setTextSize(1.5); - $nameLabel->setText('$fff'.$map->name); - //TODO action detailed map info - $x +=50; - $authorLabel = new Label_Text(); - $frame->add($authorLabel); - $authorLabel->setHAlign(Control::LEFT); - $authorLabel->setX($x); - //$nameLabel->setSize($width * 0.5, 2); - $authorLabel->setStyle($authorLabel::STYLE_TextCardSmall); - $authorLabel->setTextSize(1.5); - $authorLabel->setText($map->authorNick); - $authorLabel->setTextColor('FFF'); - - //TODO later add buttons for jukebox, admin control buttons (remove map, change to map) //TODO side switch - //var_dump($map);*/ } @@ -296,4 +282,17 @@ class MapList implements ManialinkPageAnswerListener { $this->maniaControl->manialinkManager->closeWidget($player); } + /** + * @param array $callback + */ + public function handleManialinkPageAnswer(array $callback){ + $actionId = $callback[1][2]; + $addMap = (strpos($actionId, self::ACTION_ADD_MAP) === 0); + if(!$addMap) + return; + + $actionArray = explode(".", $actionId); + var_dump($actionArray); + $this->maniaControl->mapManager->addMapFromMx($actionArray[2],$callback[1][1]); + } } \ No newline at end of file diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php index 01636ef0..e009e29e 100644 --- a/application/core/Maps/MapManager.php +++ b/application/core/Maps/MapManager.php @@ -5,9 +5,11 @@ namespace ManiaControl\Maps; require_once __DIR__ . '/Map.php'; require_once __DIR__ . '/MapCommands.php'; +use ManiaControl\FileUtil; use ManiaControl\ManiaControl; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; +use ManiaControl\Players\Player; // TODO: xlist command @@ -123,6 +125,7 @@ class MapManager implements CallbackListener { return true; } + /** * Fetch current map * @@ -187,4 +190,84 @@ class MapManager implements CallbackListener { return $this->mapList; } + /** + * Adds a Map from Mania Exchange + * @param $mapId + * @param $login + */ + public function addMapFromMx($mapId, $login){ + // Check if ManiaControl can even write to the maps dir + if (!$this->maniaControl->client->query('GetMapsDirectory')) { + trigger_error("Couldn't get map directory. " . $this->maniaControl->getClientErrorText()); + $this->maniaControl->chat->sendError("ManiaControl couldn't retrieve the maps directory.", $login); + return; + } + + $mapDir = $this->maniaControl->client->getResponse(); + if (!is_dir($mapDir)) { + trigger_error("ManiaControl doesn't have have access to the maps directory in '{$mapDir}'."); + $this->maniaControl->chat->sendError("ManiaControl doesn't have access to the maps directory.", $login); + return; + } + $downloadDirectory = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX'); + // Create download directory if necessary + if (!is_dir($mapDir . $downloadDirectory) && !mkdir($mapDir . $downloadDirectory)) { + trigger_error("ManiaControl doesn't have to rights to save maps in '{$mapDir}{$downloadDirectory}'."); + $this->maniaControl->chat->sendError("ManiaControl doesn't have the rights to save maps.", $login); + return; + } + $mapDir .= $downloadDirectory . '/'; + + // Download the map + if (is_numeric($mapId)) { + // Load from MX + $serverInfo = $this->maniaControl->server->getSystemInfo(); + $title = strtolower(substr($serverInfo['TitleId'], 0, 2)); + // Check if map exists + $url = "http://{$title}.mania-exchange.com/api/tracks/get_track_info/id/{$mapId}?format=json"; + $mapInfo = FileUtil::loadFile($url); + if (!$mapInfo || strlen($mapInfo) <= 0) { + // Invalid id + $this->maniaControl->chat->sendError('Invalid MX-Id!', $login); + return; + } + $mapInfo = json_decode($mapInfo, true); + $url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}"; + $file = FileUtil::loadFile($url); + if (!$file) { + // Download error + $this->maniaControl->chat->sendError('Download failed!', $login); + return; + } + // Save map + $fileName = $mapInfo['TrackID'] . '_' . $mapInfo['Name'] . '.Map.Gbx'; + $fileName = FileUtil::getClearedFileName($fileName); + if (!file_put_contents($mapDir . $fileName, $file)) { + // Save error + $this->maniaControl->chat->sendError('Saving map failed!', $login); + return; + } + // Check for valid map + $mapFileName = $downloadDirectory . '/' . $fileName; + if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $mapFileName)) { + trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $this->maniaControl->getClientErrorText()); + $this->maniaControl->chat->sendError('Error checking map!', $login); + return; + } + $response = $this->maniaControl->client->getResponse(); + if (!$response) { + // Invalid map type + $this->maniaControl->chat->sendError("Invalid map type.", $login); + return; + } + // Add map to map list + if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) { + $this->maniaControl->chat->sendError("Couldn't add map to match settings!", $login); + return; + } + $this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!'); + return; + } + // TODO: add local map by filename + } }