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;
use ManiaControl\ManiaControl;
use ManiaControl\Formatter;
/**
* File utility class
@ -87,6 +88,8 @@ abstract class FileUtil {
* @return string
*/
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 \ManiaControl\Players\Player $player

View File

@ -52,7 +52,9 @@ class MapManager implements CallbackListener {
*/
private $maniaControl = null;
private $maps = array();
/** @var Map $currentMap */
/**
* @var Map $currentMap
*/
private $currentMap = null;
private $mapEnded = false;
private $mapBegan = false;
@ -79,11 +81,16 @@ class MapManager implements CallbackListener {
// Define Rights
$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_SHUFFLE_MAPS, AuthenticationManager::AUTH_LEVEL_ADMIN);
$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->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_REMOVE_MAP,
AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SHUFFLE_MAPS,
AuthenticationManager::AUTH_LEVEL_ADMIN);
$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_MAPLIST_FILE, "MatchSettings/tracklist.txt");
@ -200,7 +207,9 @@ class MapManager implements CallbackListener {
}
$map = $this->maps[$uid];
/** @var Map $map */
/**
* @var Map $map
*/
$mxId = $map->mx->id;
$this->removeMap($admin, $uid, true, false);
$this->addMapFromMx($mxId, $admin->login, true);
@ -217,7 +226,7 @@ class MapManager implements CallbackListener {
public function removeMap(Player $admin, $uid, $eraseFile = false, $message = true) {
$map = $this->maps[$uid];
//Unset the Map everywhere
// Unset the Map everywhere
$this->mapQueue->removeFromMapQueue($admin, $map->uid);
if ($map->mx) {
@ -239,7 +248,7 @@ class MapManager implements CallbackListener {
}
}
//Show Message
// Show Message
if ($message) {
$message = '$<' . $admin->nickname . '$> removed $<' . $map->name . '$>!';
$this->maniaControl->chat->sendSuccess($message);
@ -255,7 +264,7 @@ class MapManager implements CallbackListener {
public function restructureMapList() {
$currentIndex = $this->getMapIndex($this->currentMap);
//No RestructureNeeded
// No RestructureNeeded
if ($currentIndex < Maplist::MAX_MAPS_PER_PAGE - 1) {
return true;
}
@ -264,10 +273,11 @@ class MapManager implements CallbackListener {
$higherMapArray = array();
$i = 0;
foreach($this->maps as $map) {
foreach ($this->maps as $map) {
if ($i < $currentIndex) {
$lowerMapArray[] = $map->fileName;
} else {
}
else {
$higherMapArray[] = $map->fileName;
}
$i++;
@ -278,7 +288,8 @@ class MapManager implements CallbackListener {
try {
$this->maniaControl->client->chooseNextMapList($mapArray);
} catch(Exception $e) {
}
catch (Exception $e) {
trigger_error("Error while restructuring the Maplist. " . $e->getMessage());
return false;
}
@ -297,14 +308,17 @@ class MapManager implements CallbackListener {
$mapArray = array();
foreach($shuffledMaps as $map) {
/** @var Map $map */
foreach ($shuffledMaps as $map) {
/**
* @var Map $map
*/
$mapArray[] = $map->fileName;
}
try {
$this->maniaControl->client->chooseNextMapList($mapArray);
} catch(Exception $e) {
}
catch (Exception $e) {
trigger_error("Couldn't shuffle mapList. " . $e->getMessage());
return false;
}
@ -317,7 +331,7 @@ class MapManager implements CallbackListener {
$this->maniaControl->log($message, true);
}
//Restructure if needed
// Restructure if needed
$this->restructureMapList();
return true;
}
@ -351,11 +365,12 @@ class MapManager implements CallbackListener {
$maps = $this->maniaControl->client->getMapList(100, 0);
$tempList = array();
foreach($maps as $rpcMap) {
foreach ($maps as $rpcMap) {
if (array_key_exists($rpcMap->uId, $this->maps)) {
// Map already exists, only update index
$tempList[$rpcMap->uId] = $this->maps[$rpcMap->uId];
} else { // Insert Map Object
}
else { // Insert Map Object
$map = $this->initializeMap($rpcMap);
$tempList[$map->uid] = $map;
}
@ -367,14 +382,16 @@ class MapManager implements CallbackListener {
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPS_UPDATED);
//Write MapList
// Write MapList
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_AUTOSAVE_MAPLIST)) {
try {
$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.') {
$this->maniaControl->log("Unable to write the playlist file, please checkout your MX-Folders File permissions!");
} else {
}
else {
throw $e;
}
}
@ -408,10 +425,10 @@ class MapManager implements CallbackListener {
$this->updateFullMapList();
$this->fetchCurrentMap();
//Fetch Mx Infos
// Fetch Mx Infos
$this->mxManager->fetchManiaExchangeMapInformations();
//Restructure Maplist
// Restructure Maplist
$this->restructureMapList();
}
@ -465,7 +482,8 @@ class MapManager implements CallbackListener {
$this->currentMap->nbLaps = $rpcMap->nbLaps;
$this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints;
}
} else {
}
else {
$rpcMap = \Maniaplanet\DedicatedServer\Structures\Map::fromArray($callback[1][0]);
$this->currentMap = $this->initializeMap($rpcMap);
// TODO: can this ever happen?
@ -563,21 +581,23 @@ class MapManager implements CallbackListener {
public function addMapFromMx($mapId, $login, $update = false) {
if (is_numeric($mapId)) {
// 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)) {
// Invalid id
$this->maniaControl->chat->sendError('Invalid MX-Id!', $login);
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;
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';
}
//Download the file
$this->maniaControl->fileReader->loadFile($url, function ($file, $error) use (&$login, &$mapInfo, &$update) {
// Download the file
$this->maniaControl->fileReader->loadFile($url,
function ($file, $error) use(&$login, &$mapInfo, &$update) {
if (!$file) {
// Download error
$this->maniaControl->chat->sendError('Download failed!', $login);
@ -598,9 +618,7 @@ class MapManager implements CallbackListener {
* @param $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) {
// Download error
$this->maniaControl->chat->sendError('Map is already on the server!', $login);
@ -611,31 +629,33 @@ class MapManager implements CallbackListener {
$fileName = $mapInfo->id . '_' . $mapInfo->name . '.Map.Gbx';
$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 locally Written
// Check if it can get written locally
if (is_dir($mapDir)) {
// 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}'.");
if (!is_dir($downloadDirectory) && !mkdir($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);
return;
}
$mapDir .= $downloadDirectory . '/';
if (!file_put_contents($mapDir . $fileName, $file)) {
if (!file_put_contents($fullMapFileName, $file)) {
// Save error
$this->maniaControl->chat->sendError('Saving map failed!', $login);
return;
}
//Write via Write File Method
} else {
}
else {
// Write map via write file method
try {
$this->maniaControl->client->writeFileFromString($mapFileName, $file);
} catch(InvalidArgumentException $e) {
$this->maniaControl->client->writeFileFromString($relativeMapFileName, $file);
}
catch (InvalidArgumentException $e) {
if ($e->getMessage() == 'data are too big') {
$this->maniaControl->chat->sendError("Map is too big for a remote save.", $login);
return;
@ -646,35 +666,39 @@ class MapManager implements CallbackListener {
// Check for valid map
try {
$this->maniaControl->client->checkMapForCurrentServerParams($mapFileName);
} catch(Exception $e) {
trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $e->getMessage());
$this->maniaControl->client->checkMapForCurrentServerParams($relativeMapFileName);
}
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);
return;
}
// Add map to map list
$this->maniaControl->client->insertMap($mapFileName);
$this->maniaControl->client->insertMap($relativeMapFileName);
$this->updateFullMapList();
//Update Mx MapInfo
// Update Mx MapInfo
$this->maniaControl->mapManager->mxManager->updateMapObjectsWithManiaExchangeIds(array($mapInfo));
//Update last updated time
// Update last updated time
$map = $this->maps[$mapInfo->uid];
/** @var Map $map */
/**
* @var Map $map
*/
$map->lastUpdate = time();
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$update) {
//Message
// Message
$message = '$<' . $player->nickname . '$> added $<' . $mapInfo->name . '$>!';
$this->maniaControl->chat->sendSuccess($message);
$this->maniaControl->log($message, true);
// Queue requested Map
$this->maniaControl->mapManager->mapQueue->addMapToMapQueue($login, $mapInfo->uid);
} else {
}
else {
$message = '$<' . $player->nickname . '$> updated $<' . $mapInfo->name . '$>!';
$this->maniaControl->chat->sendSuccess($message);
$this->maniaControl->log($message, true);