mapmanager cleanup

This commit is contained in:
kremsy 2014-04-19 22:44:59 +02:00 committed by Steffen Schröder
parent be4d96fa22
commit 02b0a0dec0
2 changed files with 163 additions and 168 deletions

View File

@ -15,6 +15,10 @@ class FaultException extends Exception
{
case 'Login unknown.':
return new LoginUnknownException($faultString, $faultCode);
case 'Unable to write the playlist file.':
return new CouldNotWritePlaylistFileException($faultString, $faultCode);
case 'Start index out of bound.':
return new StartIndexOutOfBoundException($faultString, $faultCode);
}
return new self($faultString, $faultCode);
@ -22,5 +26,6 @@ class FaultException extends Exception
}
class LoginUnknownException extends FaultException {}
class CouldNotWritePlaylistFileException extends FaultException {}
class StartIndexOutOfBoundException extends FaultException {}
?>

View File

@ -6,14 +6,15 @@ use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Files\FileUtil;
use ManiaControl\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\ManiaExchange\ManiaExchangeList;
use ManiaControl\ManiaExchange\ManiaExchangeManager;
use ManiaControl\ManiaExchange\MXMapInfo;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\InvalidArgumentException;
use Maniaplanet\DedicatedServer\Xmlrpc\CouldNotWritePlaylistFileException;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
use Maniaplanet\DedicatedServer\Xmlrpc\StartIndexOutOfBoundException;
/**
* Manager for Maps
@ -84,16 +85,11 @@ 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");
@ -134,6 +130,7 @@ class MapManager implements CallbackListener {
* @return bool
*/
private function saveMap(Map &$map) {
//TODO saveMaps for whole maplist at once (usage of prepared statements)
$mysqli = $this->maniaControl->database->mysqli;
$mapQuery = "INSERT INTO `" . self::TABLE_MAPS . "` (
`uid`,
@ -284,8 +281,7 @@ class MapManager implements CallbackListener {
foreach($this->maps as $map) {
if ($i < $currentIndex) {
$lowerMapArray[] = $map->fileName;
}
else {
} else {
$higherMapArray[] = $map->fileName;
}
$i++;
@ -296,8 +292,7 @@ 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;
}
@ -325,8 +320,9 @@ class MapManager implements CallbackListener {
try {
$this->maniaControl->client->chooseNextMapList($mapArray);
}
catch (Exception $e) {
} catch(Exception $e) {
//TODO temp added 19.04.2014
$this->maniaControl->errorHandler->triggerDebugNotice("Exception line 331 MapManager" . $e->getMessage());
trigger_error("Couldn't shuffle mapList. " . $e->getMessage());
return false;
}
@ -370,20 +366,29 @@ class MapManager implements CallbackListener {
* Updates the full Map list, needed on Init, addMap and on ShuffleMaps
*/
private function updateFullMapList() {
$maps = $this->maniaControl->client->getMapList(150, 0);
$tempList = array();
try {
$i = 0;
while(true) {
$maps = $this->maniaControl->client->getMapList(150, $i);
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;
}
}
// restore Sorted Maplist
$i += 150;
}
} catch(StartIndexOutOfBoundException $e) {
}
// restore Sorted MapList
$this->maps = $tempList;
// Trigger own callback
@ -393,15 +398,9 @@ class MapManager implements CallbackListener {
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) {
if ($e->getMessage() == 'Unable to write the playlist file.') {
} catch(CouldNotWritePlaylistFileException $e) {
$this->maniaControl->log("Unable to write the playlist file, please checkout your MX-Folders File permissions!");
}
else {
throw $e;
}
}
}
}
@ -495,12 +494,6 @@ class MapManager implements CallbackListener {
$this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints;
}
}
else {
$rpcMap = \Maniaplanet\DedicatedServer\Structures\Map::fromArray($callback[1][0]);
$this->currentMap = $this->initializeMap($rpcMap);
// TODO: can this ever happen?
$this->maniaControl->errorHandler->triggerDebugNotice("new map wasn't fetched yet! " . $callback[1][0]["UId"]);
}
// Restructure MapList if id is over 15
$this->restructureMapList();
@ -593,8 +586,7 @@ 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);
@ -608,8 +600,7 @@ class MapManager implements CallbackListener {
}
// 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) {
// Download error
$this->maniaControl->chat->sendError('Download failed!', $login);
@ -661,13 +652,11 @@ class MapManager implements CallbackListener {
$this->maniaControl->chat->sendError('Saving map failed!', $login);
return;
}
}
else {
} else {
// Write map via write file method
try {
$this->maniaControl->client->writeFileFromString($relativeMapFileName, $file);
}
catch (InvalidArgumentException $e) {
} catch(InvalidArgumentException $e) {
if ($e->getMessage() == 'data are too big') {
$this->maniaControl->chat->sendError("Map is too big for a remote save.", $login);
return;
@ -679,8 +668,10 @@ class MapManager implements CallbackListener {
// Check for valid map
try {
$this->maniaControl->client->checkMapForCurrentServerParams($relativeMapFileName);
}
catch (Exception $e) {
} catch(Exception $e) {
//TODO temp added 19.04.2014
$this->maniaControl->errorHandler->triggerDebugNotice("Exception line 331 MapManager" . $e->getMessage());
trigger_error("Couldn't check if map is valid ('{$relativeMapFileName}'). " . $e->getMessage());
$this->maniaControl->chat->sendError('Wrong MapType or not validated!', $login);
return;
@ -709,8 +700,7 @@ class MapManager implements CallbackListener {
$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);