TrackManiaControl/application/core/Maps/MapManager.php

852 lines
24 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Maps;
2014-01-09 18:45:39 +01:00
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
2014-04-24 21:55:47 +02:00
use ManiaControl\Callbacks\Callbacks;
2014-02-07 00:26:32 +01:00
use ManiaControl\Files\FileUtil;
use ManiaControl\Logger;
2014-01-08 18:07:09 +01:00
use ManiaControl\ManiaControl;
2014-01-28 16:54:23 +01:00
use ManiaControl\ManiaExchange\ManiaExchangeList;
2014-01-28 15:56:50 +01:00
use ManiaControl\ManiaExchange\ManiaExchangeManager;
2014-02-07 17:27:09 +01:00
use ManiaControl\ManiaExchange\MXMapInfo;
2014-01-10 12:22:37 +01:00
use ManiaControl\Players\Player;
2014-06-23 15:45:40 +02:00
use ManiaControl\Utils\Formatter;
2014-02-17 00:34:20 +01:00
use Maniaplanet\DedicatedServer\InvalidArgumentException;
2014-06-23 16:04:00 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\AlreadyInListException;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
use Maniaplanet\DedicatedServer\Xmlrpc\FileException;
use Maniaplanet\DedicatedServer\Xmlrpc\IndexOutOfBoundException;
use Maniaplanet\DedicatedServer\Xmlrpc\InvalidMapException;
use Maniaplanet\DedicatedServer\Xmlrpc\NotInListException;
2014-06-14 18:16:45 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\UnavailableFeatureException;
/**
* ManiaControl Map Manager Class
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
2014-04-19 22:44:59 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class MapManager implements CallbackListener {
/*
* Constants
*/
const TABLE_MAPS = 'mc_maps';
2014-04-19 22:44:59 +02:00
const CB_MAPS_UPDATED = 'MapManager.MapsUpdated';
const CB_KARMA_UPDATED = 'MapManager.KarmaUpdated';
const SETTING_PERMISSION_ADD_MAP = 'Add Maps';
const SETTING_PERMISSION_REMOVE_MAP = 'Remove Maps';
const SETTING_PERMISSION_ERASE_MAP = 'Erase Maps';
2014-01-11 16:34:05 +01:00
const SETTING_PERMISSION_SHUFFLE_MAPS = 'Shuffle Maps';
2014-01-28 20:14:21 +01:00
const SETTING_PERMISSION_CHECK_UPDATE = 'Check Map Update';
2014-04-19 22:44:59 +02:00
const SETTING_PERMISSION_SKIP_MAP = 'Skip Map';
const SETTING_PERMISSION_RESTART_MAP = 'Restart Map';
const SETTING_AUTOSAVE_MAPLIST = 'Autosave Maplist file';
const SETTING_MAPLIST_FILE = 'File to write Maplist in';
const SETTING_WRITE_OWN_MAPLIST_FILE = 'Write a own Maplist File for every Server called serverlogin.txt';
2014-04-19 22:44:59 +02:00
/*
* Public properties
*/
/** @var MapQueue $mapQueue */
/** @deprecated see getMapQueue() */
2014-01-06 18:50:26 +01:00
public $mapQueue = null;
/** @var MapCommands $mapCommands */
/** @deprecated see getMapCommands() */
2014-01-06 18:50:26 +01:00
public $mapCommands = null;
/** @var MapActions $mapActions */
/** @deprecated see getMapActions() */
public $mapActions = null;
/** @var MapList $mapList */
/** @deprecated see getMapList() */
2014-01-06 18:50:26 +01:00
public $mapList = null;
/** @var DirectoryBrowser $directoryBrowser */
/** @deprecated see getDirectoryBrowser() */
2014-06-29 23:51:01 +02:00
public $directoryBrowser = null;
/** @var ManiaExchangeList $mxList */
/** @deprecated see getMXList() */
2014-01-28 16:54:23 +01:00
public $mxList = null;
/** @var ManiaExchangeManager $mxManager */
/** @deprecated see getMXManager() */
2014-01-14 15:28:22 +01:00
public $mxManager = null;
2014-04-19 22:44:59 +02:00
/*
* Private properties
2013-12-28 19:48:06 +01:00
*/
/** @var ManiaControl $maniaControl */
2014-01-06 18:50:26 +01:00
private $maniaControl = null;
/** @var Map[] $maps */
2014-01-06 18:50:26 +01:00
private $maps = array();
2014-05-09 16:58:57 +02:00
/** @var Map $currentMap */
2014-01-06 18:50:26 +01:00
private $currentMap = null;
private $mapEnded = false;
private $mapBegan = false;
2013-12-28 19:48:06 +01:00
/**
* Construct a new map manager instance
*
2014-05-09 16:58:57 +02:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
2014-04-19 22:44:59 +02:00
// Children
2014-06-29 23:51:01 +02:00
$this->mxManager = new ManiaExchangeManager($this->maniaControl);
$this->mapList = new MapList($this->maniaControl);
$this->directoryBrowser = new DirectoryBrowser($this->maniaControl);
$this->mxList = new ManiaExchangeList($this->maniaControl);
$this->mapCommands = new MapCommands($maniaControl);
$this->mapQueue = new MapQueue($this->maniaControl);
$this->mapActions = new MapActions($maniaControl);
2014-04-19 22:44:59 +02:00
// Callbacks
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'handleOnInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapsModified');
2014-04-19 22:44:59 +02:00
// Permissions
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_ADD_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_REMOVE_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_ERASE_MAP, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_SHUFFLE_MAPS, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHECK_UPDATE, AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_SKIP_MAP, AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_RESTART_MAP, AuthenticationManager::AUTH_LEVEL_MODERATOR);
2014-04-19 22:44:59 +02:00
// Settings
$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_WRITE_OWN_MAPLIST_FILE, false);
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-04-19 22:44:59 +02:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_MAPS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
2014-01-09 21:32:17 +01:00
`mxid` int(11),
`uid` varchar(50) NOT NULL,
`name` varchar(150) NOT NULL,
`authorLogin` varchar(100) NOT NULL,
`fileName` varchar(100) NOT NULL,
`environment` varchar(50) NOT NULL,
`mapType` varchar(50) NOT NULL,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`),
UNIQUE KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Map Data' AUTO_INCREMENT=1;";
$result = $mysqli->query($query);
2014-01-27 21:38:30 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
return $result;
}
/**
* Return the map queue
*
* @return MapQueue
*/
public function getMapQueue() {
return $this->mapQueue;
}
/**
* Return the map commands
*
* @return MapCommands
*/
public function getMapCommands() {
return $this->mapCommands;
}
/**
* Return the map actions
*
* @return MapActions
*/
public function getMapActions() {
return $this->mapActions;
}
/**
* Return the map list
*
* @return MapList
*/
public function getMapList() {
return $this->mapList;
}
/**
* Return the directory browser
*
* @return DirectoryBrowser
*/
public function getDirectoryBrowser() {
return $this->directoryBrowser;
}
/**
* Return the mx list
*
* @return ManiaExchangeList
*/
public function getMXList() {
return $this->mxList;
}
/**
* Return the mx manager
*
* @return ManiaExchangeManager
*/
public function getMXManager() {
return $this->mxManager;
}
/**
2014-05-02 17:50:30 +02:00
* Update a Map from Mania Exchange
*
2014-05-09 16:58:57 +02:00
* @param Player $admin
* @param string $uid
*/
2014-05-02 17:50:30 +02:00
public function updateMap(Player $admin, $uid) {
$this->updateMapTimestamp($uid);
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
if (!isset($uid) || !isset($this->maps[$uid])) {
$this->maniaControl->getChat()->sendError("Error updating Map: Unknown UID '{$uid}'!", $admin);
2014-05-02 17:50:30 +02:00
return;
}
2014-05-02 17:50:30 +02:00
/** @var Map $map */
$map = $this->maps[$uid];
$mxId = $map->mx->id;
$this->removeMap($admin, $uid, true, false);
$this->addMapFromMx($mxId, $admin->login, true);
}
2014-01-15 20:40:14 +01:00
/**
* Update the Timestamp of a Map
2014-01-15 20:40:14 +01:00
*
* @param string $uid
2014-01-15 20:40:14 +01:00
* @return bool
*/
private function updateMapTimestamp($uid) {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-06-14 11:32:09 +02:00
$mapQuery = "UPDATE `" . self::TABLE_MAPS . "` SET
mxid = 0,
changed = NOW()
WHERE 'uid' = ?";
2014-01-15 20:40:14 +01:00
$mapStatement = $mysqli->prepare($mapQuery);
2014-01-27 21:38:30 +01:00
if ($mysqli->error) {
2014-01-15 20:40:14 +01:00
trigger_error($mysqli->error);
return false;
}
$mapStatement->bind_param('s', $uid);
$mapStatement->execute();
2014-01-27 21:38:30 +01:00
if ($mapStatement->error) {
2014-01-15 20:40:14 +01:00
trigger_error($mapStatement->error);
$mapStatement->close();
return false;
}
$mapStatement->close();
return true;
}
/**
2014-01-06 18:50:26 +01:00
* Remove a Map
2014-01-05 14:13:18 +01:00
*
2014-05-09 16:58:57 +02:00
* @param Player $admin
* @param string $uid
* @param bool $eraseFile
* @param bool $message
2013-12-16 13:14:26 +01:00
*/
2014-01-31 15:56:57 +01:00
public function removeMap(Player $admin, $uid, $eraseFile = false, $message = true) {
2014-04-19 22:44:59 +02:00
if (!isset($this->maps[$uid])) {
$this->maniaControl->getChat()->sendError('Map does not exist!', $admin);
2014-04-09 10:21:34 +02:00
return;
}
/** @var Map $map */
2014-04-09 10:22:28 +02:00
$map = $this->maps[$uid];
2014-03-31 21:04:15 +02:00
// Unset the Map everywhere
$this->getMapQueue()->removeFromMapQueue($admin, $map->uid);
2014-04-19 22:44:59 +02:00
2014-03-19 11:11:25 +01:00
if ($map->mx) {
$this->getMXManager()->unsetMap($map->mx->id);
2014-01-31 15:56:57 +01:00
}
2014-04-19 22:44:59 +02:00
2014-01-10 12:22:37 +01:00
// Remove map
2014-04-23 15:11:46 +02:00
try {
$this->maniaControl->getClient()->removeMap($map->fileName);
} catch (NotInListException $e) {
2014-04-21 23:14:06 +02:00
}
unset($this->maps[$uid]);
2014-04-19 22:44:59 +02:00
2014-01-31 15:56:57 +01:00
if ($eraseFile) {
// Check if ManiaControl can even write to the maps dir
$mapDir = $this->maniaControl->getClient()->getMapsDirectory();
if ($this->maniaControl->getServer()->checkAccess($mapDir)) {
// Delete map file
if (!@unlink($mapDir . $map->fileName)) {
$this->maniaControl->getChat()->sendError("Couldn't erase the map file.", $admin);
$eraseFile = false;
}
} else {
$this->maniaControl->getChat()->sendError("Couldn't erase the map file (no access).", $admin);
$eraseFile = false;
2014-01-31 15:56:57 +01:00
}
}
2014-04-19 22:44:59 +02:00
2014-03-31 21:04:15 +02:00
// Show Message
2014-01-27 21:38:30 +01:00
if ($message) {
$action = ($eraseFile ? 'erased' : 'removed');
$message = $admin->getEscapedNickname() . ' ' . $action . ' ' . $map->getEscapedName() . '!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true, true, true);
2014-01-15 20:40:14 +01:00
}
2013-12-16 13:14:26 +01:00
}
2014-01-15 18:31:06 +01:00
/**
2014-05-02 17:50:30 +02:00
* Adds a Map from Mania Exchange
*
2014-05-09 16:58:57 +02:00
* @param int $mapId
* @param string $login
* @param bool $update
2014-01-15 18:31:06 +01:00
*/
2014-05-02 17:50:30 +02:00
public function addMapFromMx($mapId, $login, $update = false) {
if (is_numeric($mapId)) {
// Check if map exists
$this->maniaControl->getMapManager()->getMXManager()->fetchMapInfo($mapId, function (MXMapInfo $mapInfo = null) use (
2014-06-23 15:45:40 +02:00
&$login, &$update
) {
2014-05-02 17:50:30 +02:00
if (!$mapInfo || !isset($mapInfo->uploaded)) {
// Invalid id
$this->maniaControl->getChat()->sendError('Invalid MX-Id!', $login);
2014-05-02 17:50:30 +02:00
return;
}
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
// Download the file
$this->maniaControl->getFileReader()->loadFile($mapInfo->downloadurl, function ($file, $error) use (
2014-06-23 15:45:40 +02:00
&$login, &$mapInfo, &$update
) {
2014-05-02 18:21:38 +02:00
if (!$file || $error) {
2014-05-02 17:50:30 +02:00
// Download error
$this->maniaControl->getChat()->sendError("Download failed: '{$error}'!", $login);
2014-05-02 17:50:30 +02:00
return;
}
$this->processMapFile($file, $mapInfo, $login, $update);
2014-05-02 17:50:30 +02:00
});
});
2014-01-15 18:31:06 +01:00
}
}
2014-01-11 16:34:05 +01:00
/**
2014-05-02 17:50:30 +02:00
* Process the MapFile
2014-01-11 16:34:05 +01:00
*
2014-05-09 16:58:57 +02:00
* @param string $file
2014-05-02 17:50:30 +02:00
* @param MXMapInfo $mapInfo
2014-05-09 16:58:57 +02:00
* @param string $login
* @param bool $update
2014-05-13 16:03:26 +02:00
* @throws InvalidArgumentException
2014-01-11 16:34:05 +01:00
*/
2014-05-02 17:50:30 +02:00
private function processMapFile($file, MXMapInfo $mapInfo, $login, $update) {
// Check if map is already on the server
if ($this->getMapByUid($mapInfo->uid)) {
$this->maniaControl->getChat()->sendError('Map is already on the server!', $login);
2014-05-02 17:50:30 +02:00
return;
}
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
// Save map
$fileName = $mapInfo->id . '_' . $mapInfo->name . '.Map.Gbx';
$fileName = FileUtil::getClearedFileName($fileName);
2014-04-19 22:44:59 +02:00
$downloadFolderName = $this->maniaControl->getSettingManager()->getSettingValue($this, 'MapDownloadDirectory', 'MX');
2014-05-03 21:37:28 +02:00
$relativeMapFileName = $downloadFolderName . DIRECTORY_SEPARATOR . $fileName;
$mapDir = $this->maniaControl->getServer()->getDirectory()
->getMapsFolder();
2014-06-14 11:32:09 +02:00
$downloadDirectory = $mapDir . $downloadFolderName . DIRECTORY_SEPARATOR;
2014-05-02 17:50:30 +02:00
$fullMapFileName = $downloadDirectory . $fileName;
// Check if it can get written locally
if ($this->maniaControl->getServer()->checkAccess($mapDir)) {
2014-05-02 17:50:30 +02:00
// Create download directory if necessary
2014-06-23 16:04:00 +02:00
if (!is_dir($downloadDirectory) && !mkdir($downloadDirectory) || !is_writable($downloadDirectory)) {
$this->maniaControl->getChat()->sendError("ManiaControl doesn't have to rights to save maps in '{$downloadDirectory}'.", $login);
2014-05-02 17:50:30 +02:00
return;
}
if (!file_put_contents($fullMapFileName, $file)) {
// Save error
$this->maniaControl->getChat()->sendError('Saving map failed!', $login);
2014-05-02 17:50:30 +02:00
return;
}
} else {
// Write map via write file method
try {
$this->maniaControl->getClient()->writeFile($relativeMapFileName, $file);
2014-05-02 17:50:30 +02:00
} catch (InvalidArgumentException $e) {
if ($e->getMessage() === 'data are too big') {
$this->maniaControl->getChat()->sendError("Map is too big for a remote save.", $login);
2014-05-02 17:50:30 +02:00
return;
}
throw $e;
}
2014-01-11 17:06:32 +01:00
}
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
// Check for valid map
2014-01-20 20:51:03 +01:00
try {
$this->maniaControl->getClient()->checkMapForCurrentServerParams($relativeMapFileName);
2014-06-23 16:04:00 +02:00
} catch (InvalidMapException $exception) {
$this->maniaControl->getChat()->sendException($exception, $login);
2014-05-02 17:50:30 +02:00
return;
} catch (FileException $exception) {
$this->maniaControl->getChat()->sendException($exception, $login);
return;
2014-01-11 16:34:05 +01:00
}
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
// Add map to map list
2014-06-23 16:04:00 +02:00
try {
$this->maniaControl->getClient()->insertMap($relativeMapFileName);
2014-06-23 16:04:00 +02:00
} catch (AlreadyInListException $exception) {
$this->maniaControl->getChat()->sendException($exception, $login);
2014-06-23 16:04:00 +02:00
return;
}
2014-05-02 17:50:30 +02:00
$this->updateFullMapList();
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
// Update Mx MapInfo
$this->maniaControl->getMapManager()->getMXManager()->updateMapObjectsWithManiaExchangeIds(array($mapInfo));
2014-05-02 17:50:30 +02:00
// Update last updated time
2014-05-15 14:42:01 +02:00
$map = $this->getMapByUid($mapInfo->uid);
if (!$map) {
// TODO: improve this - error reports about not existing maps
$this->maniaControl->getErrorHandler()->triggerDebugNotice('Map not in List after Insert!');
$this->maniaControl->getChat()->sendError('Server Error!', $login);
2014-05-15 14:42:01 +02:00
return;
}
2014-05-02 17:50:30 +02:00
$map->lastUpdate = time();
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
2014-05-02 17:50:30 +02:00
if (!$update) {
// Message
$message = $player->getEscapedNickname() . ' added $<' . $mapInfo->name . '$>!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true, true, true);
2014-05-02 17:50:30 +02:00
// Queue requested Map
$this->maniaControl->getMapManager()->getMapQueue()->addMapToMapQueue($login, $mapInfo->uid);
2014-05-02 17:50:30 +02:00
} else {
$message = $player->getEscapedNickname() . ' updated $<' . $mapInfo->name . '$>!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true, true, true);
2014-01-14 15:45:36 +01:00
}
2014-01-11 16:34:05 +01:00
}
2014-01-12 20:56:25 +01:00
/**
* Get Map by UID
2014-01-12 20:56:25 +01:00
*
* @param string $uid
* @return Map
2014-01-12 20:56:25 +01:00
*/
2014-05-02 17:50:30 +02:00
public function getMapByUid($uid) {
2014-05-15 14:42:01 +02:00
if (isset($this->maps[$uid])) {
return $this->maps[$uid];
2014-05-02 17:50:30 +02:00
}
2014-05-15 14:42:01 +02:00
return null;
2014-01-12 20:56:25 +01:00
}
2014-01-11 16:34:05 +01:00
/**
* Updates the full Map list, needed on Init, addMap and on ShuffleMaps
*/
2014-01-05 14:13:18 +01:00
private function updateFullMapList() {
$tempList = array();
2014-04-19 22:44:59 +02:00
try {
$offset = 0;
while ($this->maniaControl->getClient()) {
$maps = $this->maniaControl->getClient()->getMapList(150, $offset);
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
foreach ($maps as $rpcMap) {
2014-04-19 22:44:59 +02:00
if (array_key_exists($rpcMap->uId, $this->maps)) {
// Map already exists, only update index
$tempList[$rpcMap->uId] = $this->maps[$rpcMap->uId];
2014-05-15 14:42:01 +02:00
} else {
// Insert Map Object
2014-04-19 22:44:59 +02:00
$map = $this->initializeMap($rpcMap);
$tempList[$map->uid] = $map;
}
}
$offset += 150;
}
} catch (IndexOutOfBoundException $e) {
}
2014-04-19 22:44:59 +02:00
// restore Sorted MapList
2014-01-06 18:50:26 +01:00
$this->maps = $tempList;
2014-04-19 22:44:59 +02:00
2013-12-28 23:24:54 +01:00
// Trigger own callback
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_MAPS_UPDATED);
2014-04-19 22:44:59 +02:00
2014-03-31 21:04:15 +02:00
// Write MapList
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_AUTOSAVE_MAPLIST)) {
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WRITE_OWN_MAPLIST_FILE)) {
$serverLogin = $this->maniaControl->getServer()->login;
$matchSettingsFileName = "MatchSettings/{$serverLogin}.txt";
} else {
$matchSettingsFileName = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MAPLIST_FILE);
}
2014-03-13 18:25:29 +01:00
try {
$this->maniaControl->getClient()->saveMatchSettings($matchSettingsFileName);
} catch (FileException $e) {
Logger::logError("Unable to write the playlist file, please checkout your MX-Folders File permissions!");
2014-03-13 18:25:29 +01:00
}
2014-03-07 14:43:09 +01:00
}
}
2013-12-15 15:13:56 +01:00
/**
2014-05-02 17:50:30 +02:00
* Initializes a Map
*
* @param mixed $rpcMap
* @return Map
*/
2014-05-02 17:50:30 +02:00
public function initializeMap($rpcMap) {
$map = new Map($rpcMap);
$this->saveMap($map);
return $map;
}
/**
* Save a Map in the Database
*
2014-05-09 16:58:57 +02:00
* @param Map $map
2014-05-02 17:50:30 +02:00
* @return bool
*/
private function saveMap(Map &$map) {
//TODO saveMaps for whole maplist at once (usage of prepared statements)
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-02 17:50:30 +02:00
$mapQuery = "INSERT INTO `" . self::TABLE_MAPS . "` (
`uid`,
`name`,
`authorLogin`,
`fileName`,
`environment`,
`mapType`
) VALUES (
?, ?, ?, ?, ?, ?
) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`),
`fileName` = VALUES(`fileName`),
`environment` = VALUES(`environment`),
`mapType` = VALUES(`mapType`);";
$mapStatement = $mysqli->prepare($mapQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$mapStatement->bind_param('ssssss', $map->uid, $map->rawName, $map->authorLogin, $map->fileName, $map->environment, $map->mapType);
$mapStatement->execute();
if ($mapStatement->error) {
trigger_error($mapStatement->error);
$mapStatement->close();
return false;
}
$map->index = $mapStatement->insert_id;
$mapStatement->close();
return true;
}
/**
* Shuffles the MapList
*
* @param Player $admin
* @return bool
*/
public function shuffleMapList($admin = null) {
$shuffledMaps = $this->maps;
shuffle($shuffledMaps);
$mapArray = array();
foreach ($shuffledMaps as $map) {
/** @var Map $map */
2014-05-02 17:50:30 +02:00
$mapArray[] = $map->fileName;
}
try {
$this->maniaControl->getClient()->chooseNextMapList($mapArray);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
//TODO temp added 19.04.2014
$this->maniaControl->getErrorHandler()->triggerDebugNotice("Exception line 331 MapManager" . $e->getMessage());
2014-05-02 17:50:30 +02:00
trigger_error("Couldn't shuffle mapList. " . $e->getMessage());
return false;
}
$this->fetchCurrentMap();
if ($admin) {
$message = $admin->getEscapedNickname() . ' shuffled the Maplist!';
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true, true, true);
2014-05-02 17:50:30 +02:00
}
// Restructure if needed
$this->restructureMapList();
return true;
}
/**
* Freshly fetch current Map
*
* @return Map
*/
private function fetchCurrentMap() {
2014-06-14 18:16:45 +02:00
try {
$rpcMap = $this->maniaControl->getClient()->getCurrentMapInfo();
2014-06-14 18:16:45 +02:00
} catch (UnavailableFeatureException $exception) {
return null;
}
2014-05-02 17:50:30 +02:00
if (array_key_exists($rpcMap->uId, $this->maps)) {
$this->currentMap = $this->maps[$rpcMap->uId];
$this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints;
2014-04-19 22:44:59 +02:00
$this->currentMap->nbLaps = $rpcMap->nbLaps;
return $this->currentMap;
2013-12-16 12:16:33 +01:00
}
2014-04-19 22:44:59 +02:00
$this->currentMap = $this->initializeMap($rpcMap);
$this->maps[$this->currentMap->uid] = $this->currentMap;
return $this->currentMap;
}
2014-05-02 17:50:30 +02:00
/**
* Restructures the Maplist
*/
public function restructureMapList() {
2014-06-30 18:48:31 +02:00
$currentIndex = $this->getMapIndex($this->getCurrentMap());
2014-05-02 17:50:30 +02:00
// No RestructureNeeded
if ($currentIndex < Maplist::MAX_MAPS_PER_PAGE - 1) {
return true;
}
$lowerMapArray = array();
$higherMapArray = array();
$index = 0;
2014-05-02 17:50:30 +02:00
foreach ($this->maps as $map) {
if ($index < $currentIndex) {
2014-05-02 17:50:30 +02:00
$lowerMapArray[] = $map->fileName;
} else {
$higherMapArray[] = $map->fileName;
}
$index++;
2014-05-02 17:50:30 +02:00
}
$mapArray = array_merge($higherMapArray, $lowerMapArray);
array_shift($mapArray);
try {
$this->maniaControl->getClient()->chooseNextMapList($mapArray);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
trigger_error("Error restructuring the Maplist. " . $e->getMessage());
2014-05-02 17:50:30 +02:00
return false;
}
return true;
}
/**
* Returns the MapIndex of a given map
*
* @param Map $map
2014-05-27 22:32:54 +02:00
* @return int
2014-05-02 17:50:30 +02:00
*/
public function getMapIndex(Map $map) {
$maps = $this->getMaps();
return array_search($map, $maps);
}
/**
* Get all Maps
*
* @param int $offset
* @param int $length
2014-05-27 22:32:54 +02:00
* @return Map[]
2014-05-02 17:50:30 +02:00
*/
public function getMaps($offset = null, $length = null) {
if ($offset === null) {
return array_values($this->maps);
}
if ($length === null) {
return array_slice($this->maps, $offset);
}
return array_slice($this->maps, $offset, $length);
}
2014-06-30 18:48:31 +02:00
/**
* Get Current Map
*
* @return Map
*/
public function getCurrentMap() {
if (!$this->currentMap) {
return $this->fetchCurrentMap();
}
return $this->currentMap;
}
/**
* Handle OnInit callback
*/
public function handleOnInit() {
$this->updateFullMapList();
2014-01-06 18:50:26 +01:00
$this->fetchCurrentMap();
2014-04-19 22:44:59 +02:00
2014-03-31 21:04:15 +02:00
// Restructure Maplist
2014-01-15 18:31:06 +01:00
$this->restructureMapList();
}
/**
* Handle AfterInit callback
*/
public function handleAfterInit() {
// Fetch MX infos
$this->getMXManager()->fetchManiaExchangeMapInformation();
}
2013-12-28 19:48:06 +01:00
/**
2014-05-02 17:50:30 +02:00
* Handle Script BeginMap callback
2014-01-05 14:13:18 +01:00
*
2014-05-09 12:18:25 +02:00
* @param string $mapUid
2014-06-23 15:45:40 +02:00
* @param string $restart
2013-12-28 19:48:06 +01:00
*/
2014-05-09 12:14:25 +02:00
public function handleScriptBeginMap($mapUid, $restart) {
2014-06-23 15:45:40 +02:00
$this->beginMap($mapUid, Formatter::parseBoolean($restart));
2013-12-28 19:48:06 +01:00
}
2014-05-09 16:58:57 +02:00
/**
* Manage the Begin of a Map
*
* @param string $uid
* @param bool $restart
*/
private function beginMap($uid, $restart = false) {
2014-05-13 16:03:26 +02:00
//If a restart occurred, first call the endMap to set variables back
2014-05-09 16:58:57 +02:00
if ($restart) {
$this->endMap();
}
if ($this->mapBegan) {
return;
}
$this->mapBegan = true;
$this->mapEnded = false;
if (array_key_exists($uid, $this->maps)) {
// Map already exists, only update index
$this->currentMap = $this->maps[$uid];
if (!$this->currentMap->nbCheckpoints || !$this->currentMap->nbLaps) {
$rpcMap = $this->maniaControl->getClient()->getCurrentMapInfo();
2014-05-09 16:58:57 +02:00
$this->currentMap->nbLaps = $rpcMap->nbLaps;
$this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints;
}
}
// Restructure MapList if id is over 15
$this->restructureMapList();
// Update the mx of the map (for update checks, etc.)
$this->getMXManager()->fetchManiaExchangeMapInformation($this->currentMap);
2014-05-09 16:58:57 +02:00
// Trigger own BeginMap callback
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::BEGINMAP, $this->currentMap);
2014-05-09 16:58:57 +02:00
}
/**
* Manage the End of a Map
*/
private function endMap() {
if ($this->mapEnded) {
return;
}
$this->mapEnded = true;
$this->mapBegan = false;
// Trigger own EndMap callback
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::ENDMAP, $this->currentMap);
2014-05-09 16:58:57 +02:00
}
2014-07-05 14:05:23 +02:00
/**
* Fetch a map by its file path
*
* @param string $relativeFileName
* @return Map
*/
2014-07-05 13:58:35 +02:00
public function fetchMapByFileName($relativeFileName) {
$mapInfo = $this->maniaControl->getClient()->getMapInfo($relativeFileName);
2014-07-05 13:58:35 +02:00
if (!$mapInfo) {
2014-07-05 14:05:23 +02:00
return null;
2014-07-05 13:58:35 +02:00
}
return $this->initializeMap($mapInfo);
}
2014-05-09 12:14:25 +02:00
/**
* Handle BeginMap callback
*
* @param array $callback
*/
public function handleBeginMap(array $callback) {
$this->beginMap($callback[1][0]["UId"]);
}
/**
2014-05-02 17:50:30 +02:00
* Handle Script EndMap Callback
*/
public function handleScriptEndMap() {
2014-05-09 12:18:25 +02:00
$this->endMap();
}
2014-02-20 13:06:11 +01:00
/**
* Handle EndMap Callback
2014-02-20 13:06:11 +01:00
*
* @param array $callback
*/
public function handleEndMap(array $callback) {
2014-05-09 12:18:25 +02:00
$this->endMap();
}
2014-02-20 13:06:11 +01:00
2013-12-16 14:21:30 +01:00
/**
2014-01-06 18:50:26 +01:00
* Handle Maps Modified Callback
2014-01-05 14:13:18 +01:00
*
2013-12-16 14:21:30 +01:00
* @param array $callback
*/
2014-01-06 18:50:26 +01:00
public function mapsModified(array $callback) {
2013-12-16 14:21:30 +01:00
$this->updateFullMapList();
}
2014-04-28 16:59:55 +02:00
/**
* Get the Number of Maps
2014-04-30 00:17:57 +02:00
*
2014-04-28 16:59:55 +02:00
* @return int
*/
public function getMapsCount() {
return count($this->maps);
2013-12-14 22:00:59 +01:00
}
2014-05-03 19:53:34 +02:00
}