2013-11-12 15:48:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Maps;
|
|
|
|
|
2014-01-09 18:45:39 +01:00
|
|
|
use ManiaControl\Admin\AuthenticationManager;
|
2013-11-12 15:48:25 +01:00
|
|
|
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;
|
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-02-17 00:34:20 +01:00
|
|
|
use Maniaplanet\DedicatedServer\InvalidArgumentException;
|
2014-04-19 22:44:59 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\CouldNotWritePlaylistFileException;
|
2014-02-13 14:21:25 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
|
2014-04-22 21:56:18 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\MapNotCompatibleOrCompleteException;
|
2014-04-21 23:14:06 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\MapNotInCurrentSelectionException;
|
2014-04-19 22:44:59 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\StartIndexOutOfBoundException;
|
2013-11-12 15:48:25 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-05 14:13:18 +01:00
|
|
|
* Manager for Maps
|
2013-11-12 15:48:25 +01:00
|
|
|
*
|
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
|
2013-11-12 15:48:25 +01:00
|
|
|
*/
|
|
|
|
class MapManager implements CallbackListener {
|
2014-02-19 16:27:56 +01:00
|
|
|
/*
|
2013-11-12 16:52:23 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-04-24 21:55:47 +02:00
|
|
|
const TABLE_MAPS = 'mc_maps';
|
|
|
|
/** @deprecated CB_BEGINMAP */
|
|
|
|
const CB_BEGINMAP = 'MapManager.BeginMap';
|
|
|
|
/** @deprecated CB_ENDMAP */
|
2014-04-19 22:44:59 +02:00
|
|
|
const CB_ENDMAP = 'MapManager.EndMap';
|
|
|
|
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';
|
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';
|
|
|
|
|
2014-02-19 16:27:56 +01:00
|
|
|
/*
|
2014-01-06 18:50:26 +01:00
|
|
|
* Public Properties
|
2013-11-12 15:48:25 +01:00
|
|
|
*/
|
2014-01-06 18:50:26 +01:00
|
|
|
public $mapQueue = null;
|
|
|
|
public $mapCommands = null;
|
|
|
|
public $mapList = null;
|
2014-01-28 16:54:23 +01:00
|
|
|
public $mxList = null;
|
2014-01-14 15:28:22 +01:00
|
|
|
public $mxManager = null;
|
2014-05-09 11:58:33 +02:00
|
|
|
public $mapActions = null;
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-02-19 16:27:56 +01:00
|
|
|
/*
|
2014-01-06 18:50:26 +01:00
|
|
|
* Private Properties
|
2013-12-28 19:48:06 +01:00
|
|
|
*/
|
2014-01-06 18:50:26 +01:00
|
|
|
private $maniaControl = null;
|
|
|
|
private $maps = array();
|
2014-03-31 21:04:15 +02:00
|
|
|
/**
|
|
|
|
* @var Map $currentMap
|
|
|
|
*/
|
2014-01-06 18:50:26 +01:00
|
|
|
private $currentMap = null;
|
2014-02-19 16:27:56 +01:00
|
|
|
private $mapEnded = false;
|
|
|
|
private $mapBegan = false;
|
2013-12-28 19:48:06 +01:00
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
/**
|
2014-02-19 16:27:56 +01:00
|
|
|
* Construct a new Map Manager
|
2013-11-12 15:48:25 +01:00
|
|
|
*
|
2013-12-27 21:10:53 +01:00
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
2013-11-12 15:48:25 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->initTables();
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2013-11-25 02:01:15 +01:00
|
|
|
// Create map commands instance
|
2014-04-19 22:44:59 +02:00
|
|
|
$this->mxManager = new ManiaExchangeManager($this->maniaControl);
|
|
|
|
$this->mapList = new MapList($this->maniaControl);
|
|
|
|
$this->mxList = new ManiaExchangeList($this->maniaControl);
|
2014-01-14 15:15:13 +01:00
|
|
|
$this->mapCommands = new MapCommands($maniaControl);
|
2014-04-19 22:44:59 +02:00
|
|
|
$this->mapQueue = new MapQueue($this->maniaControl);
|
2014-05-09 11:58:33 +02:00
|
|
|
$this->mapActions = new MapActions($maniaControl);
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2013-11-25 02:01:15 +01:00
|
|
|
// Register for callbacks
|
2014-02-19 12:53:06 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'handleOnInit');
|
2014-04-19 13:04:30 +02:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit');
|
2014-01-06 18:50:26 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapsModified');
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-02-19 16:27:56 +01:00
|
|
|
// Define Rights
|
2014-01-09 19:00:37 +01:00
|
|
|
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_ADD_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN);
|
2014-04-19 22:44:59 +02:00
|
|
|
$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);
|
|
|
|
|
2014-03-07 14:43:09 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_AUTOSAVE_MAPLIST, true);
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt");
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-19 16:27:56 +01:00
|
|
|
* Initialize necessary Database Tables
|
2013-11-12 15:48:25 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2013-11-12 16:52:23 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-04-19 22:44:59 +02:00
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_MAPS . "` (
|
2013-11-12 16:52:23 +01:00
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
2014-01-09 21:32:17 +01:00
|
|
|
`mxid` int(11),
|
2014-02-13 14:58:04 +01:00
|
|
|
`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,
|
2013-11-12 16:52:23 +01:00
|
|
|
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
|
PRIMARY KEY (`index`),
|
|
|
|
UNIQUE KEY `uid` (`uid`)
|
2014-02-13 14:58:04 +01:00
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Map Data' AUTO_INCREMENT=1;";
|
2013-11-25 02:01:15 +01:00
|
|
|
$result = $mysqli->query($query);
|
2014-01-27 21:38:30 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-12 16:52:23 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-25 02:01:15 +01:00
|
|
|
return $result;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-02 17:50:30 +02:00
|
|
|
* Update a Map from Mania Exchange
|
2013-11-12 16:52:23 +01:00
|
|
|
*
|
2014-05-02 17:50:30 +02:00
|
|
|
* @param Player $admin
|
|
|
|
* @param string $uid
|
2013-11-12 16:52:23 +01:00
|
|
|
*/
|
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])) {
|
|
|
|
trigger_error("Error while updating Map, unknown UID: " . $uid);
|
|
|
|
$this->maniaControl->chat->sendError("Error while updating Map.", $admin->login);
|
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
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);
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:40:14 +01:00
|
|
|
/**
|
2014-05-02 16:13:45 +02:00
|
|
|
* Update the Timestamp of a Map
|
2014-01-15 20:40:14 +01:00
|
|
|
*
|
2014-05-02 16:13:45 +02:00
|
|
|
* @param string $uid
|
2014-01-15 20:40:14 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function updateMapTimestamp($uid) {
|
2014-04-19 22:44:59 +02:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-05-02 16:13:45 +02:00
|
|
|
$mapQuery = "UPDATE `" . self::TABLE_MAPS . "` SET
|
|
|
|
mxid = 0,
|
|
|
|
changed = NOW()
|
|
|
|
WHERE 'uid' = ?";
|
2014-04-19 22:44:59 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
/**
|
2014-01-06 18:50:26 +01:00
|
|
|
* Remove a Map
|
2014-01-05 14:13:18 +01:00
|
|
|
*
|
2014-01-15 18:31:06 +01:00
|
|
|
* @param \ManiaControl\Players\Player $admin
|
2014-04-19 22:44:59 +02:00
|
|
|
* @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])) {
|
2014-04-09 10:22:28 +02:00
|
|
|
$this->maniaControl->chat->sendError("Map is not existing!", $admin->login);
|
2014-04-09 10:21:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-09 10:22:28 +02:00
|
|
|
$map = $this->maps[$uid];
|
|
|
|
|
2014-03-31 21:04:15 +02:00
|
|
|
// Unset the Map everywhere
|
2014-01-28 20:09:09 +01:00
|
|
|
$this->mapQueue->removeFromMapQueue($admin, $map->uid);
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-03-19 11:11:25 +01:00
|
|
|
if ($map->mx) {
|
2014-01-31 15:56:57 +01:00
|
|
|
$this->mxManager->unsetMap($map->mx->id);
|
|
|
|
}
|
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 {
|
2014-04-21 23:14:06 +02:00
|
|
|
$this->maniaControl->client->removeMap($map->fileName);
|
2014-05-02 17:50:30 +02:00
|
|
|
} catch (MapNotInCurrentSelectionException $e) {
|
2014-04-21 23:14:06 +02:00
|
|
|
}
|
|
|
|
|
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
|
2014-02-13 14:21:25 +01:00
|
|
|
$mapDir = $this->maniaControl->client->getMapsDirectory();
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-02-13 14:21:25 +01:00
|
|
|
// Delete map file
|
2014-01-31 15:56:57 +01:00
|
|
|
if (!@unlink($mapDir . $map->fileName)) {
|
|
|
|
trigger_error("Couldn't remove Map '{$mapDir}{$map->fileName}'.");
|
|
|
|
$this->maniaControl->chat->sendError("ManiaControl couldn't remove the MapFile.", $admin->login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2014-01-15 20:40:14 +01:00
|
|
|
$message = '$<' . $admin->nickname . '$> removed $<' . $map->name . '$>!';
|
|
|
|
$this->maniaControl->chat->sendSuccess($message);
|
|
|
|
$this->maniaControl->log($message, true);
|
|
|
|
}
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-01-10 12:22:37 +01:00
|
|
|
unset($this->maps[$uid]);
|
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
|
|
|
|
*
|
|
|
|
* @param $mapId
|
|
|
|
* @param $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
|
|
|
|
$self = $this;
|
|
|
|
$this->maniaControl->mapManager->mxManager->getMapInfo($mapId, function (MXMapInfo $mapInfo) use (&$self, &$login, &$update) {
|
|
|
|
if (!$mapInfo || !isset($mapInfo->uploaded)) {
|
|
|
|
// Invalid id
|
|
|
|
$self->maniaControl->chat->sendError('Invalid MX-Id!', $login);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
// Download the file
|
|
|
|
$self->maniaControl->fileReader->loadFile($mapInfo->downloadurl, function ($file, $error) use (&$self, &$login, &$mapInfo, &$update) {
|
2014-05-02 18:21:38 +02:00
|
|
|
if (!$file || $error) {
|
2014-05-02 17:50:30 +02:00
|
|
|
// Download error
|
2014-05-02 18:21:38 +02:00
|
|
|
$self->maniaControl->chat->sendError("Download failed: '{$error}'!", $login);
|
2014-05-02 17:50:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$self->processMapFile($file, $mapInfo, $login, $update);
|
|
|
|
});
|
|
|
|
});
|
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-02 17:50:30 +02:00
|
|
|
* @param $file
|
|
|
|
* @param MXMapInfo $mapInfo
|
|
|
|
* @param $login
|
|
|
|
* @param $update
|
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)) {
|
|
|
|
// Download error
|
|
|
|
$this->maniaControl->chat->sendError('Map is already on the server!', $login);
|
|
|
|
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
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
$downloadFolderName = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX');
|
2014-05-03 21:37:28 +02:00
|
|
|
$relativeMapFileName = $downloadFolderName . DIRECTORY_SEPARATOR . $fileName;
|
2014-05-02 17:50:30 +02:00
|
|
|
$mapDir = $this->maniaControl->client->getMapsDirectory();
|
2014-05-03 21:37:28 +02:00
|
|
|
$downloadDirectory = $mapDir . DIRECTORY_SEPARATOR . $downloadFolderName . DIRECTORY_SEPARATOR;
|
2014-05-02 17:50:30 +02:00
|
|
|
$fullMapFileName = $downloadDirectory . $fileName;
|
|
|
|
|
|
|
|
// Check if it can get written locally
|
|
|
|
if (is_dir($mapDir)) {
|
|
|
|
// Create download directory if necessary
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_put_contents($fullMapFileName, $file)) {
|
|
|
|
// Save error
|
|
|
|
$this->maniaControl->chat->sendError('Saving map failed!', $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Write map via write file method
|
|
|
|
try {
|
2014-05-03 19:53:34 +02:00
|
|
|
$this->maniaControl->client->writeFile($relativeMapFileName, $file);
|
2014-05-02 17:50:30 +02:00
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
if ($e->getMessage() == 'data are too big') {
|
|
|
|
$this->maniaControl->chat->sendError("Map is too big for a remote save.", $login);
|
|
|
|
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 {
|
2014-05-02 17:50:30 +02:00
|
|
|
$this->maniaControl->client->checkMapForCurrentServerParams($relativeMapFileName);
|
|
|
|
} catch (MapNotCompatibleOrCompleteException $e) {
|
|
|
|
trigger_error("Couldn't check if map is valid ('{$relativeMapFileName}'). " . $e->getMessage());
|
|
|
|
$this->maniaControl->chat->sendError('Wrong MapType or not validated!', $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
|
|
|
|
$this->maniaControl->client->insertMap($relativeMapFileName);
|
|
|
|
$this->updateFullMapList();
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
// Update Mx MapInfo
|
|
|
|
$this->maniaControl->mapManager->mxManager->updateMapObjectsWithManiaExchangeIds(array($mapInfo));
|
|
|
|
|
|
|
|
// Update last updated time
|
|
|
|
$map = $this->maps[$mapInfo->uid];
|
|
|
|
/**
|
|
|
|
* @var Map $map
|
|
|
|
*/
|
|
|
|
$map->lastUpdate = time();
|
|
|
|
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
|
|
|
|
|
|
|
if (!$update) {
|
|
|
|
// 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 {
|
|
|
|
$message = '$<' . $player->nickname . '$> updated $<' . $mapInfo->name . '$>!';
|
2014-01-14 15:45:36 +01:00
|
|
|
$this->maniaControl->chat->sendSuccess($message);
|
|
|
|
$this->maniaControl->log($message, true);
|
|
|
|
}
|
2014-01-11 16:34:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 20:56:25 +01:00
|
|
|
/**
|
2014-05-02 17:50:30 +02:00
|
|
|
* Returns map By UID
|
2014-01-12 20:56:25 +01:00
|
|
|
*
|
2014-05-02 17:50:30 +02:00
|
|
|
* @param $uid
|
|
|
|
* @return Map array
|
2014-01-12 20:56:25 +01:00
|
|
|
*/
|
2014-05-02 17:50:30 +02:00
|
|
|
public function getMapByUid($uid) {
|
|
|
|
if (!isset($this->maps[$uid])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->maps[$uid];
|
2014-01-12 20:56:25 +01:00
|
|
|
}
|
2014-01-11 16:34:05 +01:00
|
|
|
|
2013-12-15 17:52:16 +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() {
|
2013-12-15 17:52:16 +01:00
|
|
|
$tempList = array();
|
2014-04-19 22:44:59 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$i = 0;
|
2014-05-02 17:50:30 +02:00
|
|
|
while (true) {
|
2014-04-19 22:44:59 +02:00
|
|
|
$maps = $this->maniaControl->client->getMapList(150, $i);
|
|
|
|
|
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];
|
|
|
|
} else { // Insert Map Object
|
|
|
|
$map = $this->initializeMap($rpcMap);
|
|
|
|
$tempList[$map->uid] = $map;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$i += 150;
|
2013-12-15 17:52:16 +01:00
|
|
|
}
|
2014-05-02 17:50:30 +02:00
|
|
|
} catch (StartIndexOutOfBoundException $e) {
|
2013-12-15 17:52:16 +01:00
|
|
|
}
|
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
|
2014-02-19 15:44:00 +01:00
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPS_UPDATED);
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-03-31 21:04:15 +02:00
|
|
|
// Write MapList
|
2014-03-13 18:25:29 +01:00
|
|
|
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_AUTOSAVE_MAPLIST)) {
|
|
|
|
try {
|
|
|
|
$this->maniaControl->client->saveMatchSettings($this->maniaControl->settingManager->getSetting($this, self::SETTING_MAPLIST_FILE));
|
2014-05-02 17:50:30 +02:00
|
|
|
} catch (CouldNotWritePlaylistFileException $e) {
|
2014-04-19 22:44:59 +02:00
|
|
|
$this->maniaControl->log("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 17:52:16 +01:00
|
|
|
}
|
2013-12-15 15:13:56 +01:00
|
|
|
|
2013-11-12 16:52:23 +01:00
|
|
|
/**
|
2014-05-02 17:50:30 +02:00
|
|
|
* Initializes a Map
|
2013-11-12 16:52:23 +01:00
|
|
|
*
|
2014-05-02 17:50:30 +02:00
|
|
|
* @param $rpcMap
|
2014-02-19 16:27:56 +01:00
|
|
|
* @return Map
|
2013-11-12 16:52:23 +01:00
|
|
|
*/
|
2014-05-02 17:50:30 +02:00
|
|
|
public function initializeMap($rpcMap) {
|
|
|
|
$map = new Map($rpcMap);
|
|
|
|
$this->saveMap($map);
|
2014-04-19 22:44:59 +02:00
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/*$mapsDirectory = $this->maniaControl->server->getMapsDirectory();
|
|
|
|
if (is_readable($mapsDirectory . $map->fileName)) {
|
|
|
|
$mapFetcher = new \GBXChallMapFetcher(true);
|
|
|
|
$mapFetcher->processFile($mapsDirectory . $map->fileName);
|
|
|
|
$map->authorNick = FORMATTER::stripDirtyCodes($mapFetcher->authorNick);
|
|
|
|
$map->authorEInfo = $mapFetcher->authorEInfo;
|
|
|
|
$map->authorZone = $mapFetcher->authorZone;
|
|
|
|
$map->comment = $mapFetcher->comment;
|
|
|
|
}*/
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a Map in the Database
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\Maps\Map $map
|
|
|
|
* @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`,
|
|
|
|
`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
|
|
|
|
*/
|
|
|
|
$mapArray[] = $map->fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->maniaControl->client->chooseNextMapList($mapArray);
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fetchCurrentMap();
|
|
|
|
|
|
|
|
if ($admin) {
|
|
|
|
$message = '$<' . $admin->nickname . '$> shuffled the Maplist!';
|
|
|
|
$this->maniaControl->chat->sendSuccess($message);
|
|
|
|
$this->maniaControl->log($message, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restructure if needed
|
|
|
|
$this->restructureMapList();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Freshly fetch current Map
|
|
|
|
*
|
|
|
|
* @return Map
|
|
|
|
*/
|
|
|
|
private function fetchCurrentMap() {
|
|
|
|
$rpcMap = $this->maniaControl->client->getCurrentMapInfo();
|
|
|
|
|
|
|
|
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;
|
2014-02-19 16:27:56 +01:00
|
|
|
return $this->currentMap;
|
2013-12-16 12:16:33 +01:00
|
|
|
}
|
2014-04-19 22:44:59 +02:00
|
|
|
|
|
|
|
$this->currentMap = $this->initializeMap($rpcMap);
|
2014-02-19 16:27:56 +01:00
|
|
|
$this->maps[$this->currentMap->uid] = $this->currentMap;
|
|
|
|
return $this->currentMap;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 17:50:30 +02:00
|
|
|
/**
|
|
|
|
* Restructures the Maplist
|
|
|
|
*/
|
|
|
|
public function restructureMapList() {
|
|
|
|
$currentIndex = $this->getMapIndex($this->currentMap);
|
|
|
|
|
|
|
|
// No RestructureNeeded
|
|
|
|
if ($currentIndex < Maplist::MAX_MAPS_PER_PAGE - 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lowerMapArray = array();
|
|
|
|
$higherMapArray = array();
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach ($this->maps as $map) {
|
|
|
|
if ($i < $currentIndex) {
|
|
|
|
$lowerMapArray[] = $map->fileName;
|
|
|
|
} else {
|
|
|
|
$higherMapArray[] = $map->fileName;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mapArray = array_merge($higherMapArray, $lowerMapArray);
|
|
|
|
array_shift($mapArray);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->maniaControl->client->chooseNextMapList($mapArray);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
trigger_error("Error while restructuring the Maplist. " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the MapIndex of a given map
|
|
|
|
*
|
|
|
|
* @param Map $map
|
|
|
|
* @internal param $uid
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getMapIndex(Map $map) {
|
|
|
|
$maps = $this->getMaps();
|
|
|
|
return array_search($map, $maps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all Maps
|
|
|
|
*
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $length
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:49:36 +01:00
|
|
|
/**
|
|
|
|
* Handle OnInit callback
|
|
|
|
*/
|
2014-02-19 15:44:00 +01:00
|
|
|
public function handleOnInit() {
|
2013-12-15 17:52:16 +01:00
|
|
|
$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();
|
2013-11-26 13:49:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 13:04:30 +02:00
|
|
|
/**
|
|
|
|
* Handle AfterInit callback
|
|
|
|
*/
|
|
|
|
public function handleAfterInit() {
|
|
|
|
// Fetch MX infos
|
|
|
|
$this->mxManager->fetchManiaExchangeMapInformations();
|
|
|
|
}
|
|
|
|
|
2013-12-15 17:52:16 +01:00
|
|
|
/**
|
2014-01-05 14:13:18 +01:00
|
|
|
* Get Current Map
|
|
|
|
*
|
2013-12-30 16:45:26 +01:00
|
|
|
* @return Map currentMap
|
2013-12-15 17:52:16 +01:00
|
|
|
*/
|
2014-01-05 14:13:18 +01:00
|
|
|
public function getCurrentMap() {
|
2014-02-19 16:53:09 +01:00
|
|
|
if (!$this->currentMap) {
|
|
|
|
return $this->fetchCurrentMap();
|
|
|
|
}
|
2013-12-15 17:52:16 +01:00
|
|
|
return $this->currentMap;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param bool $restart
|
2013-12-28 19:48:06 +01:00
|
|
|
*/
|
2014-05-09 12:14:25 +02:00
|
|
|
public function handleScriptBeginMap($mapUid, $restart) {
|
|
|
|
$this->beginMap($mapUid, $restart);
|
2013-12-28 19:48:06 +01:00
|
|
|
}
|
2013-12-15 17:52:16 +01:00
|
|
|
|
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-02-19 16:27:56 +01:00
|
|
|
/**
|
2014-05-02 17:50:30 +02:00
|
|
|
* Handle Script EndMap Callback
|
2014-02-19 16:27:56 +01:00
|
|
|
*
|
2014-05-09 12:18:25 +02:00
|
|
|
* @param string $mapUid
|
2014-02-19 16:27:56 +01:00
|
|
|
*/
|
2014-05-09 12:18:25 +02:00
|
|
|
public function handleScriptEndMap($mapUid) {
|
|
|
|
$this->endMap();
|
2014-02-19 16:27:56 +01:00
|
|
|
}
|
2014-02-20 13:06:11 +01:00
|
|
|
|
2014-02-19 16:27:56 +01:00
|
|
|
/**
|
|
|
|
* Handle EndMap Callback
|
2014-02-20 13:06:11 +01:00
|
|
|
*
|
2014-02-19 16:27:56 +01:00
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleEndMap(array $callback) {
|
2014-05-09 12:18:25 +02:00
|
|
|
$this->endMap();
|
2014-02-19 16:27:56 +01:00
|
|
|
}
|
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();
|
|
|
|
}
|
2013-12-15 17:52:16 +01:00
|
|
|
|
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-02-07 17:27:09 +01:00
|
|
|
// TODO: add local map by filename
|
2014-05-09 12:18:25 +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->callbackManager->triggerCallback(self::CB_ENDMAP, $this->currentMap);
|
|
|
|
//TODO remove deprecated callback later
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(Callbacks::ENDMAP, $this->currentMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage the Begin of a Map
|
|
|
|
*
|
|
|
|
* @param $uid
|
|
|
|
* @param bool $restart
|
|
|
|
*/
|
|
|
|
private function beginMap($uid, $restart = false) {
|
|
|
|
//If a restart occured, first call the endMap to set variables back
|
|
|
|
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->client->getCurrentMapInfo();
|
|
|
|
$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->mxManager->fetchManiaExchangeMapInformations($this->currentMap);
|
|
|
|
|
|
|
|
// Trigger own BeginMap callback (
|
|
|
|
//TODO remove deprecated callback later
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_BEGINMAP, $this->currentMap);
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(Callbacks::BEGINMAP, $this->currentMap);
|
|
|
|
}
|
2014-05-03 19:53:34 +02:00
|
|
|
}
|