fixed + improved map saving/adding

This commit is contained in:
Steffen Schröder 2014-03-31 21:04:15 +02:00
parent b4bbfe6c72
commit 334b3b606c
3 changed files with 195 additions and 168 deletions

View File

@ -3,6 +3,7 @@
namespace ManiaControl\Files; namespace ManiaControl\Files;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Formatter;
/** /**
* File utility class * File utility class
@ -87,6 +88,8 @@ abstract class FileUtil {
* @return string * @return string
*/ */
public static function getClearedFileName($fileName) { public static function getClearedFileName($fileName) {
return str_replace(array('\\', '/', ':', '*', '?', '"', '<', '>', '|'), '_', $fileName); $fileName = Formatter::stripCodes($fileName);
$fileName = str_replace(array('\\', '/', ':', '*', '?', '"', '<', '>', '|'), '_', $fileName);
return $fileName;
} }
} }

View File

@ -136,7 +136,7 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
} }
/** /**
* Handle addmap command * Handle shufflemaps command
* *
* @param array $chatCallback * @param array $chatCallback
* @param \ManiaControl\Players\Player $player * @param \ManiaControl\Players\Player $player

View File

@ -52,7 +52,9 @@ class MapManager implements CallbackListener {
*/ */
private $maniaControl = null; private $maniaControl = null;
private $maps = array(); private $maps = array();
/** @var Map $currentMap */ /**
* @var Map $currentMap
*/
private $currentMap = null; private $currentMap = null;
private $mapEnded = false; private $mapEnded = false;
private $mapBegan = false; private $mapBegan = false;
@ -79,11 +81,16 @@ class MapManager implements CallbackListener {
// Define Rights // Define Rights
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_ADD_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_ADD_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_REMOVE_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_REMOVE_MAP,
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SHUFFLE_MAPS, AuthenticationManager::AUTH_LEVEL_ADMIN); AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_CHECK_UPDATE, AuthenticationManager::AUTH_LEVEL_MODERATOR); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SHUFFLE_MAPS,
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SKIP_MAP, AuthenticationManager::AUTH_LEVEL_MODERATOR); AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_RESTART_MAP, AuthenticationManager::AUTH_LEVEL_MODERATOR); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_CHECK_UPDATE,
AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SKIP_MAP,
AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_RESTART_MAP,
AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_AUTOSAVE_MAPLIST, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_AUTOSAVE_MAPLIST, true);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt"); $this->maniaControl->settingManager->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt");
@ -200,7 +207,9 @@ class MapManager implements CallbackListener {
} }
$map = $this->maps[$uid]; $map = $this->maps[$uid];
/** @var Map $map */ /**
* @var Map $map
*/
$mxId = $map->mx->id; $mxId = $map->mx->id;
$this->removeMap($admin, $uid, true, false); $this->removeMap($admin, $uid, true, false);
$this->addMapFromMx($mxId, $admin->login, true); $this->addMapFromMx($mxId, $admin->login, true);
@ -267,7 +276,8 @@ class MapManager implements CallbackListener {
foreach ($this->maps as $map) { foreach ($this->maps as $map) {
if ($i < $currentIndex) { if ($i < $currentIndex) {
$lowerMapArray[] = $map->fileName; $lowerMapArray[] = $map->fileName;
} else { }
else {
$higherMapArray[] = $map->fileName; $higherMapArray[] = $map->fileName;
} }
$i++; $i++;
@ -278,7 +288,8 @@ class MapManager implements CallbackListener {
try { try {
$this->maniaControl->client->chooseNextMapList($mapArray); $this->maniaControl->client->chooseNextMapList($mapArray);
} catch(Exception $e) { }
catch (Exception $e) {
trigger_error("Error while restructuring the Maplist. " . $e->getMessage()); trigger_error("Error while restructuring the Maplist. " . $e->getMessage());
return false; return false;
} }
@ -298,13 +309,16 @@ class MapManager implements CallbackListener {
$mapArray = array(); $mapArray = array();
foreach ($shuffledMaps as $map) { foreach ($shuffledMaps as $map) {
/** @var Map $map */ /**
* @var Map $map
*/
$mapArray[] = $map->fileName; $mapArray[] = $map->fileName;
} }
try { try {
$this->maniaControl->client->chooseNextMapList($mapArray); $this->maniaControl->client->chooseNextMapList($mapArray);
} catch(Exception $e) { }
catch (Exception $e) {
trigger_error("Couldn't shuffle mapList. " . $e->getMessage()); trigger_error("Couldn't shuffle mapList. " . $e->getMessage());
return false; return false;
} }
@ -355,7 +369,8 @@ class MapManager implements CallbackListener {
if (array_key_exists($rpcMap->uId, $this->maps)) { if (array_key_exists($rpcMap->uId, $this->maps)) {
// Map already exists, only update index // Map already exists, only update index
$tempList[$rpcMap->uId] = $this->maps[$rpcMap->uId]; $tempList[$rpcMap->uId] = $this->maps[$rpcMap->uId];
} else { // Insert Map Object }
else { // Insert Map Object
$map = $this->initializeMap($rpcMap); $map = $this->initializeMap($rpcMap);
$tempList[$map->uid] = $map; $tempList[$map->uid] = $map;
} }
@ -371,10 +386,12 @@ class MapManager implements CallbackListener {
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_AUTOSAVE_MAPLIST)) { if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_AUTOSAVE_MAPLIST)) {
try { try {
$this->maniaControl->client->saveMatchSettings($this->maniaControl->settingManager->getSetting($this, self::SETTING_MAPLIST_FILE)); $this->maniaControl->client->saveMatchSettings($this->maniaControl->settingManager->getSetting($this, self::SETTING_MAPLIST_FILE));
} catch(Exception $e) { }
catch (Exception $e) {
if ($e->getMessage() == 'Unable to write the playlist file.') { if ($e->getMessage() == 'Unable to write the playlist file.') {
$this->maniaControl->log("Unable to write the playlist file, please checkout your MX-Folders File permissions!"); $this->maniaControl->log("Unable to write the playlist file, please checkout your MX-Folders File permissions!");
} else { }
else {
throw $e; throw $e;
} }
} }
@ -465,7 +482,8 @@ class MapManager implements CallbackListener {
$this->currentMap->nbLaps = $rpcMap->nbLaps; $this->currentMap->nbLaps = $rpcMap->nbLaps;
$this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints; $this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints;
} }
} else { }
else {
$rpcMap = \Maniaplanet\DedicatedServer\Structures\Map::fromArray($callback[1][0]); $rpcMap = \Maniaplanet\DedicatedServer\Structures\Map::fromArray($callback[1][0]);
$this->currentMap = $this->initializeMap($rpcMap); $this->currentMap = $this->initializeMap($rpcMap);
// TODO: can this ever happen? // TODO: can this ever happen?
@ -563,21 +581,23 @@ class MapManager implements CallbackListener {
public function addMapFromMx($mapId, $login, $update = false) { public function addMapFromMx($mapId, $login, $update = false) {
if (is_numeric($mapId)) { if (is_numeric($mapId)) {
// Check if map exists // Check if map exists
$this->maniaControl->mapManager->mxManager->getMapInfo($mapId, function (MXMapInfo $mapInfo) use (&$login, &$update) { $this->maniaControl->mapManager->mxManager->getMapInfo($mapId,
function (MXMapInfo $mapInfo) use(&$login, &$update) {
if (!$mapInfo || !isset($mapInfo->uploaded)) { if (!$mapInfo || !isset($mapInfo->uploaded)) {
// Invalid id // Invalid id
$this->maniaControl->chat->sendError('Invalid MX-Id!', $login); $this->maniaControl->chat->sendError('Invalid MX-Id!', $login);
return; return;
} }
//TODO hardcoded whiel beta, later take just $mapInfo->url again // TODO hardcoded during closed beta, later take just $mapInfo->url again
$url = 'http://' . $mapInfo->prefix . '.mania-exchange.com/' . $mapInfo->dir . '/download/' . $mapInfo->id; $url = 'http://' . $mapInfo->prefix . '.mania-exchange.com/' . $mapInfo->dir . '/download/' . $mapInfo->id;
if ($this->maniaControl->settingManager->getSetting($this->mxManager, ManiaExchangeManager::SETTING_MP3_BETA_TESTING)) { if ($this->maniaControl->settingManager->getSetting($this->mxManager, ManiaExchangeManager::SETTING_MP3_BETA_TESTING)) {
$url .= '?key=t42kEMjzH7xpAjBFHAvEkC7rqAlw'; $url .= '?key=t42kEMjzH7xpAjBFHAvEkC7rqAlw';
} }
// Download the file // Download the file
$this->maniaControl->fileReader->loadFile($url, function ($file, $error) use (&$login, &$mapInfo, &$update) { $this->maniaControl->fileReader->loadFile($url,
function ($file, $error) use(&$login, &$mapInfo, &$update) {
if (!$file) { if (!$file) {
// Download error // Download error
$this->maniaControl->chat->sendError('Download failed!', $login); $this->maniaControl->chat->sendError('Download failed!', $login);
@ -598,8 +618,6 @@ class MapManager implements CallbackListener {
* @param $update * @param $update
*/ */
private function processMapFile($file, MXMapInfo $mapInfo, $login, $update) { private function processMapFile($file, MXMapInfo $mapInfo, $login, $update) {
$mapDir = $this->maniaControl->client->getMapsDirectory();
// Check if map is already on the server // Check if map is already on the server
if ($this->getMapByUid($mapInfo->uid) != null) { if ($this->getMapByUid($mapInfo->uid) != null) {
// Download error // Download error
@ -611,31 +629,33 @@ class MapManager implements CallbackListener {
$fileName = $mapInfo->id . '_' . $mapInfo->name . '.Map.Gbx'; $fileName = $mapInfo->id . '_' . $mapInfo->name . '.Map.Gbx';
$fileName = FileUtil::getClearedFileName($fileName); $fileName = FileUtil::getClearedFileName($fileName);
$downloadDirectory = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX'); $downloadFolderName = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX');
$relativeMapFileName = $downloadFolderName . '/' . $fileName;
$mapDir = $this->maniaControl->client->getMapsDirectory();
$downloadDirectory = $mapDir . '/' . $downloadFolderName . '/';
$fullMapFileName = $downloadDirectory . $fileName;
$mapFileName = $downloadDirectory . '/' . $fileName; // Check if it can get written locally
//Check if it can get locally Written
if (is_dir($mapDir)) { if (is_dir($mapDir)) {
// Create download directory if necessary // Create download directory if necessary
if (!is_dir($mapDir . $downloadDirectory) && !mkdir($mapDir . $downloadDirectory)) { if (!is_dir($downloadDirectory) && !mkdir($downloadDirectory)) {
trigger_error("ManiaControl doesn't have to rights to save maps in '{$mapDir}{$downloadDirectory}'."); trigger_error("ManiaControl doesn't have to rights to save maps in '{$downloadDirectory}'.");
$this->maniaControl->chat->sendError("ManiaControl doesn't have the rights to save maps.", $login); $this->maniaControl->chat->sendError("ManiaControl doesn't have the rights to save maps.", $login);
return; return;
} }
$mapDir .= $downloadDirectory . '/'; if (!file_put_contents($fullMapFileName, $file)) {
if (!file_put_contents($mapDir . $fileName, $file)) {
// Save error // Save error
$this->maniaControl->chat->sendError('Saving map failed!', $login); $this->maniaControl->chat->sendError('Saving map failed!', $login);
return; return;
} }
//Write via Write File Method }
} else { else {
// Write map via write file method
try { try {
$this->maniaControl->client->writeFileFromString($mapFileName, $file); $this->maniaControl->client->writeFileFromString($relativeMapFileName, $file);
} catch(InvalidArgumentException $e) { }
catch (InvalidArgumentException $e) {
if ($e->getMessage() == 'data are too big') { if ($e->getMessage() == 'data are too big') {
$this->maniaControl->chat->sendError("Map is too big for a remote save.", $login); $this->maniaControl->chat->sendError("Map is too big for a remote save.", $login);
return; return;
@ -646,15 +666,16 @@ class MapManager implements CallbackListener {
// Check for valid map // Check for valid map
try { try {
$this->maniaControl->client->checkMapForCurrentServerParams($mapFileName); $this->maniaControl->client->checkMapForCurrentServerParams($relativeMapFileName);
} catch(Exception $e) { }
trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $e->getMessage()); catch (Exception $e) {
trigger_error("Couldn't check if map is valid ('{$relativeMapFileName}'). " . $e->getMessage());
$this->maniaControl->chat->sendError('Wrong MapType or not validated!', $login); $this->maniaControl->chat->sendError('Wrong MapType or not validated!', $login);
return; return;
} }
// Add map to map list // Add map to map list
$this->maniaControl->client->insertMap($mapFileName); $this->maniaControl->client->insertMap($relativeMapFileName);
$this->updateFullMapList(); $this->updateFullMapList();
// Update Mx MapInfo // Update Mx MapInfo
@ -662,7 +683,9 @@ class MapManager implements CallbackListener {
// Update last updated time // Update last updated time
$map = $this->maps[$mapInfo->uid]; $map = $this->maps[$mapInfo->uid];
/** @var Map $map */ /**
* @var Map $map
*/
$map->lastUpdate = time(); $map->lastUpdate = time();
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
@ -674,7 +697,8 @@ class MapManager implements CallbackListener {
$this->maniaControl->log($message, true); $this->maniaControl->log($message, true);
// Queue requested Map // Queue requested Map
$this->maniaControl->mapManager->mapQueue->addMapToMapQueue($login, $mapInfo->uid); $this->maniaControl->mapManager->mapQueue->addMapToMapQueue($login, $mapInfo->uid);
} else { }
else {
$message = '$<' . $player->nickname . '$> updated $<' . $mapInfo->name . '$>!'; $message = '$<' . $player->nickname . '$> updated $<' . $mapInfo->name . '$>!';
$this->maniaControl->chat->sendSuccess($message); $this->maniaControl->chat->sendSuccess($message);
$this->maniaControl->log($message, true); $this->maniaControl->log($message, true);