added mapmanager communicationmethods

This commit is contained in:
kremsy 2015-07-11 13:04:34 +02:00
parent 70805de32f
commit cc7c36f3d8
2 changed files with 152 additions and 25 deletions

View File

@ -16,6 +16,47 @@ interface CommunicationMethods {
*/ */
const RESTART_MANIA_CONTROL = "ManiaControl.Restart"; const RESTART_MANIA_CONTROL = "ManiaControl.Restart";
/** Adds a Map from Mania Exchange to the Server
* Required Parameters
* - mxId
* (no success returning yet because of asynchronously of adding)
*/
const ADD_MAP = "MapManager.AddMap";
/** Removes a Map from the Server
* Required Parameters
* - mapUid
* Optional Parameters
* - displayMessage (default true)
* - eraseMapFile (default false)
*/
const REMOVE_MAP = "MapManager.RemoveMap";
/** Updates a Map over Mania Exchange
* Required Parameters
* - mapUid
* (no success returning yet because of asynchronously of adding)
*/
const UPDATE_MAP = "MapManager.UpdateMap";
/** Gets the current Map
* Required Parameters
* - mxId
* OR
* - mapUid
*/
const GET_CURRENT_MAP = "MapManager.GetCurrentMap";
/** Gets the specific Map
* no Parameters
*/
const GET_MAP = "MapManager.GetMap";
/** Gets the current Map List
* no Parameters
*/
const GET_MAP_LIST = "MapManager.GetMapList";
/** Gets Mania Control PlayerList /** Gets Mania Control PlayerList
* no Parameters * no Parameters
*/ */

View File

@ -6,6 +6,9 @@ use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks; use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Communication\CommunicationAnswer;
use ManiaControl\Communication\CommunicationListener;
use ManiaControl\Communication\CommunicationMethods;
use ManiaControl\Files\FileUtil; use ManiaControl\Files\FileUtil;
use ManiaControl\Logger; use ManiaControl\Logger;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
@ -30,7 +33,7 @@ use Maniaplanet\DedicatedServer\Xmlrpc\UnavailableFeatureException;
* @copyright 2014-2015 ManiaControl Team * @copyright 2014-2015 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class MapManager implements CallbackListener { class MapManager implements CallbackListener, CommunicationListener {
/* /*
* Constants * Constants
*/ */
@ -135,6 +138,71 @@ class MapManager implements CallbackListener {
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_AUTOSAVE_MAPLIST, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_AUTOSAVE_MAPLIST, true);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt"); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt");
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WRITE_OWN_MAPLIST_FILE, false); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WRITE_OWN_MAPLIST_FILE, false);
// Communication Listenings
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_CURRENT_MAP, $this, function ($data) {
return new CommunicationAnswer($this->getCurrentMap());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP_LIST, $this, function ($data) {
return new CommunicationAnswer($this->getMapList());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP, $this, function ($data) {
if (!is_object($data)) {
return new CommunicationAnswer("Error in provided Data", true);
}
if (property_exists($data, "mxId")) {
return new CommunicationAnswer($this->getMapByMxId($data->mxId));
} else if (property_exists($data, "mapUid")) {
return new CommunicationAnswer($this->getMapByUid($data->mxUid));
} else {
return new CommunicationAnswer("No mxId or mapUid provided.", true);
}
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::ADD_MAP, $this, function ($data) {
if (!is_object($data) || property_exists($data, "mxId")) {
return new CommunicationAnswer("No valid mxId provided.", true);
}
if (!$this->getMapByMxId($data->mxId)) {
return new CommunicationAnswer("Map not found.", true);
}
$this->addMapFromMx($data->mxId, null);
return new CommunicationAnswer();
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::REMOVE_MAP, $this, function ($data) {
if (!is_object($data) || property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
$erase = false;
if (property_exists($data, "eraseMapFile")) {
$erase = $data->eraseMapFile;
}
$showMessage = true;
if (property_exists($data, "showChatMessage")) {
$showMessage = $data->showChatMessage;
}
$success = $this->removeMap(null, $data->mapUid, $erase, $showMessage);
return new CommunicationAnswer(array("success" => $success));
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::UPDATE_MAP, $this, function ($data) {
if (!is_object($data) || property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
$this->updateMap(null, $data->mapUid);
return new CommunicationAnswer();
});
} }
/** /**
@ -213,14 +281,16 @@ class MapManager implements CallbackListener {
/** /**
* Update a Map from Mania Exchange * Update a Map from Mania Exchange
* *
* @param Player $admin * @param Player|null $admin
* @param string $uid * @param string $uid
*/ */
public function updateMap(Player $admin, $uid) { public function updateMap($admin, $uid) {
$this->updateMapTimestamp($uid); $this->updateMapTimestamp($uid);
if (!isset($uid) || !isset($this->maps[$uid])) { if (!isset($uid) || !isset($this->maps[$uid])) {
$this->maniaControl->getChat()->sendError("Error updating Map: Unknown UID '{$uid}'!", $admin); if ($admin) {
$this->maniaControl->getChat()->sendError("Error updating Map: Unknown UID '{$uid}'!", $admin);
}
return; return;
} }
@ -229,7 +299,11 @@ class MapManager implements CallbackListener {
$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); if ($admin) {
$this->addMapFromMx($mxId, $admin->login, true);
} else {
$this->addMapFromMx($mxId, null, true);
}
} }
/** /**
@ -263,15 +337,18 @@ class MapManager implements CallbackListener {
/** /**
* Remove a Map * Remove a Map
* *
* @param Player $admin * @param Player|null $admin
* @param string $uid * @param string $uid
* @param bool $eraseFile * @param bool $eraseFile
* @param bool $message * @param bool $message
* @return bool
*/ */
public function removeMap(Player $admin, $uid, $eraseFile = false, $message = true) { public function removeMap($admin, $uid, $eraseFile = false, $message = true) {
if (!isset($this->maps[$uid])) { if (!isset($this->maps[$uid])) {
$this->maniaControl->getChat()->sendError('Map does not exist!', $admin); if ($admin) {
return; $this->maniaControl->getChat()->sendError('Map does not exist!', $admin);
}
return false;
} }
/** @var Map $map */ /** @var Map $map */
@ -296,15 +373,18 @@ class MapManager implements CallbackListener {
if ($eraseFile) { if ($eraseFile) {
// Check if ManiaControl can even write to the maps dir // Check if ManiaControl can even write to the maps dir
$mapDir = $this->maniaControl->getClient()->getMapsDirectory(); $mapDir = $this->maniaControl->getClient()->getMapsDirectory();
if ($this->maniaControl->getServer()->checkAccess($mapDir) if ($this->maniaControl->getServer()->checkAccess($mapDir)) {
) {
// Delete map file // Delete map file
if (!@unlink($mapDir . $map->fileName)) { if (!@unlink($mapDir . $map->fileName)) {
$this->maniaControl->getChat()->sendError("Couldn't erase the map file.", $admin); if ($admin) {
$this->maniaControl->getChat()->sendError("Couldn't erase the map file.", $admin);
}
$eraseFile = false; $eraseFile = false;
} }
} else { } else {
$this->maniaControl->getChat()->sendError("Couldn't erase the map file (no access).", $admin); if ($admin) {
$this->maniaControl->getChat()->sendError("Couldn't erase the map file (no access).", $admin);
}
$eraseFile = false; $eraseFile = false;
} }
} }
@ -316,6 +396,8 @@ class MapManager implements CallbackListener {
$this->maniaControl->getChat()->sendSuccess($message); $this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true); Logger::logInfo($message, true);
} }
return true;
} }
/** /**
@ -342,6 +424,7 @@ class MapManager implements CallbackListener {
* @param int $mapId * @param int $mapId
* @param string $login * @param string $login
* @param bool $update * @param bool $update
* @param bool $displayMessage
*/ */
public function addMapFromMx($mapId, $login, $update = false) { public function addMapFromMx($mapId, $login, $update = false) {
if (is_numeric($mapId)) { if (is_numeric($mapId)) {
@ -350,8 +433,10 @@ class MapManager implements CallbackListener {
&$login, &$update &$login, &$update
) { ) {
if (!$mapInfo || !isset($mapInfo->uploaded)) { if (!$mapInfo || !isset($mapInfo->uploaded)) {
// Invalid id if ($login) {
$this->maniaControl->getChat()->sendError('Invalid MX-Id!', $login); // Invalid id
$this->maniaControl->getChat()->sendError('Invalid MX-Id!', $login);
}
return; return;
} }
@ -360,14 +445,17 @@ class MapManager implements CallbackListener {
&$login, &$mapInfo, &$update &$login, &$mapInfo, &$update
) { ) {
if (!$file || $error) { if (!$file || $error) {
// Download error if ($login) {
$this->maniaControl->getChat()->sendError("Download failed: '{$error}'!", $login); // Download error
$this->maniaControl->getChat()->sendError("Download failed: '{$error}'!", $login);
}
return; return;
} }
$this->processMapFile($file, $mapInfo, $login, $update); $this->processMapFile($file, $mapInfo, $login, $update);
}); });
}); });
} }
return;
} }
/** /**
@ -522,10 +610,8 @@ class MapManager implements CallbackListener {
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_MAPS_UPDATED); $this->maniaControl->getCallbackManager()->triggerCallback(self::CB_MAPS_UPDATED);
// Write MapList // Write MapList
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_AUTOSAVE_MAPLIST) if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_AUTOSAVE_MAPLIST)) {
) { if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WRITE_OWN_MAPLIST_FILE)) {
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WRITE_OWN_MAPLIST_FILE)
) {
$serverLogin = $this->maniaControl->getServer()->login; $serverLogin = $this->maniaControl->getServer()->login;
$matchSettingsFileName = "MatchSettings/{$serverLogin}.txt"; $matchSettingsFileName = "MatchSettings/{$serverLogin}.txt";
} else { } else {