2013-11-12 16:52:23 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Maps;
|
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
2013-11-13 01:43:12 +01:00
|
|
|
use ManiaControl\Admin\AuthenticationManager;
|
2013-11-12 16:52:23 +01:00
|
|
|
use ManiaControl\Commands\CommandListener;
|
2013-11-27 02:42:39 +01:00
|
|
|
use ManiaControl\FileUtil;
|
2013-11-12 16:52:23 +01:00
|
|
|
use ManiaControl\Players\Player;
|
2013-12-14 22:00:59 +01:00
|
|
|
use ManiaControl\Players\PlayerManager;
|
2013-11-12 16:52:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class offering commands to manage maps
|
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
|
|
|
*/
|
|
|
|
class MapCommands implements CommandListener {
|
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create MapCommands instance
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
2013-12-15 09:00:30 +01:00
|
|
|
$this->maniaControl->commandManager->registerCommandListener('nextmap', $this, 'command_NextMap', true);
|
|
|
|
$this->maniaControl->commandManager->registerCommandListener('restartmap', $this, 'command_RestartMap', true);
|
|
|
|
$this->maniaControl->commandManager->registerCommandListener('addmap', $this, 'command_AddMap', true);
|
|
|
|
$this->maniaControl->commandManager->registerCommandListener('removemap', $this, 'command_RemoveMap', true);
|
|
|
|
|
|
|
|
// Register for chat commands
|
|
|
|
$this->maniaControl->commandManager->registerCommandListener('list', $this, 'command_List');
|
|
|
|
$this->maniaControl->commandManager->registerCommandListener('maps', $this, 'command_List');
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle removemap command
|
|
|
|
*
|
|
|
|
* @param array $chat
|
|
|
|
* @param \ManiaControl\Players\Player $player
|
|
|
|
*/
|
|
|
|
public function command_RemoveMap(array $chat, Player $player) {
|
2013-11-13 01:43:12 +01:00
|
|
|
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) {
|
|
|
|
$this->maniaControl->authenticationManager->sendNotAllowed($player);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
2013-11-27 02:42:39 +01:00
|
|
|
// Get map
|
|
|
|
$map = $this->maniaControl->mapManager->getCurrentMap();
|
2013-11-12 16:52:23 +01:00
|
|
|
if (!$map) {
|
|
|
|
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
// Remove map
|
2013-11-27 02:42:39 +01:00
|
|
|
if (!$this->maniaControl->client->query('RemoveMap', $map->fileName)) {
|
2013-11-12 16:52:23 +01:00
|
|
|
trigger_error("Couldn't remove current map. " . $this->maniaControl->getClientErrorText());
|
|
|
|
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
$this->maniaControl->chat->sendSuccess('Map removed.', $player->login);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle addmap command
|
|
|
|
*
|
2013-11-27 02:42:39 +01:00
|
|
|
* @param array $chatCallback
|
2013-11-12 16:52:23 +01:00
|
|
|
* @param \ManiaControl\Players\Player $player
|
|
|
|
*/
|
2013-11-27 02:42:39 +01:00
|
|
|
public function command_AddMap(array $chatCallback, Player $player) {
|
2013-11-13 01:43:12 +01:00
|
|
|
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) {
|
|
|
|
$this->maniaControl->authenticationManager->sendNotAllowed($player);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
2013-12-09 13:45:58 +01:00
|
|
|
// TODO: user mx fetcher
|
2013-11-27 02:42:39 +01:00
|
|
|
$params = explode(' ', $chatCallback[1][2], 2);
|
2013-11-12 16:52:23 +01:00
|
|
|
if (count($params) < 2) {
|
2013-11-19 20:29:37 +01:00
|
|
|
$this->maniaControl->chat->sendUsageInfo('Usage example: //addmap 1234', $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
// Check if ManiaControl can even write to the maps dir
|
|
|
|
if (!$this->maniaControl->client->query('GetMapsDirectory')) {
|
|
|
|
trigger_error("Couldn't get map directory. " . $this->maniaControl->getClientErrorText());
|
|
|
|
$this->maniaControl->chat->sendError("ManiaControl couldn't retrieve the maps directory.", $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
$mapDir = $this->maniaControl->client->getResponse();
|
|
|
|
if (!is_dir($mapDir)) {
|
|
|
|
trigger_error("ManiaControl doesn't have have access to the maps directory in '{$mapDir}'.");
|
|
|
|
$this->maniaControl->chat->sendError("ManiaControl doesn't have access to the maps directory.", $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
2013-11-27 02:42:39 +01:00
|
|
|
$downloadDirectory = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX');
|
2013-11-12 16:52:23 +01:00
|
|
|
// 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}'.");
|
|
|
|
$this->maniaControl->chat->sendError("ManiaControl doesn't have the rights to save maps.", $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
$mapDir .= $downloadDirectory . '/';
|
|
|
|
// Download the map
|
|
|
|
$mapId = $params[1];
|
|
|
|
if (is_numeric($mapId)) {
|
|
|
|
// Load from MX
|
|
|
|
$serverInfo = $this->maniaControl->server->getSystemInfo();
|
|
|
|
$title = strtolower(substr($serverInfo['TitleId'], 0, 2));
|
|
|
|
// Check if map exists
|
|
|
|
$url = "http://{$title}.mania-exchange.com/api/tracks/get_track_info/id/{$mapId}?format=json";
|
|
|
|
$mapInfo = FileUtil::loadFile($url);
|
|
|
|
if (!$mapInfo || strlen($mapInfo) <= 0) {
|
|
|
|
// Invalid id
|
|
|
|
$this->maniaControl->chat->sendError('Invalid MX-Id!', $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
$mapInfo = json_decode($mapInfo, true);
|
|
|
|
$url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}";
|
|
|
|
$file = FileUtil::loadFile($url);
|
|
|
|
if (!$file) {
|
|
|
|
// Download error
|
|
|
|
$this->maniaControl->chat->sendError('Download failed!', $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
// Save map
|
2013-11-27 02:42:39 +01:00
|
|
|
$fileName = $mapInfo['TrackID'] . '_' . $mapInfo['Name'] . '.Map.Gbx';
|
|
|
|
$fileName = FileUtil::getClearedFileName($fileName);
|
|
|
|
if (!file_put_contents($mapDir . $fileName, $file)) {
|
2013-11-12 16:52:23 +01:00
|
|
|
// Save error
|
|
|
|
$this->maniaControl->chat->sendError('Saving map failed!', $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
// Check for valid map
|
2013-11-27 02:42:39 +01:00
|
|
|
$mapFileName = $downloadDirectory . '/' . $fileName;
|
|
|
|
if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $mapFileName)) {
|
|
|
|
trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $this->maniaControl->getClientErrorText());
|
2013-11-12 16:52:23 +01:00
|
|
|
$this->maniaControl->chat->sendError('Error checking map!', $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
$response = $this->maniaControl->client->getResponse();
|
|
|
|
if (!$response) {
|
2013-12-09 13:45:58 +01:00
|
|
|
// Invalid map type
|
2013-11-12 16:52:23 +01:00
|
|
|
$this->maniaControl->chat->sendError("Invalid map type.", $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
// Add map to map list
|
2013-11-27 02:42:39 +01:00
|
|
|
if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) {
|
2013-11-12 16:52:23 +01:00
|
|
|
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $player->login);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
$this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!');
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
// TODO: add local map by filename
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle nextmap command
|
|
|
|
*
|
|
|
|
* @param array $chat
|
|
|
|
* @param \ManiaControl\Players\Player $player
|
|
|
|
*/
|
|
|
|
public function command_NextMap(array $chat, Player $player) {
|
2013-11-13 01:43:12 +01:00
|
|
|
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) {
|
|
|
|
$this->maniaControl->authenticationManager->sendNotAllowed($player);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
2013-12-15 09:00:30 +01:00
|
|
|
$this->maniaControl->client->query('NextMap');
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-15 09:00:30 +01:00
|
|
|
* Handle restartmap command
|
2013-11-12 16:52:23 +01:00
|
|
|
*
|
|
|
|
* @param array $chat
|
|
|
|
* @param \ManiaControl\Players\Player $player
|
|
|
|
*/
|
|
|
|
public function command_RestartMap(array $chat, Player $player) {
|
2013-11-13 01:43:12 +01:00
|
|
|
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR)) {
|
|
|
|
$this->maniaControl->authenticationManager->sendNotAllowed($player);
|
2013-12-15 09:00:30 +01:00
|
|
|
return;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
2013-12-15 09:00:30 +01:00
|
|
|
$this->maniaControl->client->query('RestartMap');
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
2013-12-14 22:00:59 +01:00
|
|
|
|
2013-12-15 09:00:30 +01:00
|
|
|
/**
|
|
|
|
* Handle list maps command
|
|
|
|
*
|
|
|
|
* @param array $chatCallback
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function command_List(array $chatCallback, Player $player) {
|
|
|
|
// var_dump($chatCallback);
|
2013-12-14 22:00:59 +01:00
|
|
|
$mapList = new MapList($this->maniaControl);
|
|
|
|
$mapList->showMapList($player);
|
|
|
|
}
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|