2013-11-12 15:48:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Maps;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/Map.php';
|
2013-11-12 16:52:23 +01:00
|
|
|
require_once __DIR__ . '/MapCommands.php';
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
use ManiaControl\FileUtil;
|
2013-11-12 15:48:25 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2013-12-15 15:13:56 +01:00
|
|
|
use ManiaControl\Players\Player;
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-11-12 16:52:23 +01:00
|
|
|
// TODO: xlist command
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
/**
|
|
|
|
* Manager for maps
|
|
|
|
*
|
|
|
|
* @author kremsy & steeffeen
|
|
|
|
*/
|
|
|
|
class MapManager implements CallbackListener {
|
2013-11-12 16:52:23 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const TABLE_MAPS = 'mc_maps';
|
2013-11-12 15:48:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
2013-11-12 16:52:23 +01:00
|
|
|
private $mapCommands = null;
|
2013-11-12 15:48:25 +01:00
|
|
|
private $mapList = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct map manager
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2013-11-25 02:01:15 +01:00
|
|
|
|
|
|
|
// Init database tables
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->initTables();
|
2013-11-25 02:01:15 +01:00
|
|
|
|
|
|
|
// Create map commands instance
|
2013-11-12 16:52:23 +01:00
|
|
|
$this->mapCommands = new MapCommands($maniaControl);
|
2013-11-12 15:48:25 +01:00
|
|
|
|
2013-11-25 02:01:15 +01:00
|
|
|
// Register for callbacks
|
2013-11-26 13:49:36 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit');
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize necessary database tables
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2013-11-12 16:52:23 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2013-11-25 02:01:15 +01: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,
|
|
|
|
`uid` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`name` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`authorLogin` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`fileName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`environment` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
|
|
|
`mapType` varchar(50) COLLATE utf8_unicode_ci 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;";
|
2013-11-25 02:01:15 +01:00
|
|
|
$result = $mysqli->query($query);
|
2013-11-12 16:52:23 +01:00
|
|
|
if ($mysqli->error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save map to the database
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\Maps\Map $map
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-11-25 02:01:15 +01:00
|
|
|
private function saveMap(Map &$map) {
|
2013-11-12 16:52:23 +01:00
|
|
|
$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`);";
|
|
|
|
$mapStatement = $mysqli->prepare($mapQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$mapStatement->bind_param('ssssss', $map->uid, $map->name, $map->authorLogin, $map->fileName, $map->environment, $map->mapType);
|
|
|
|
$mapStatement->execute();
|
|
|
|
if ($mapStatement->error) {
|
|
|
|
trigger_error($mapStatement->error);
|
|
|
|
$mapStatement->close();
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-25 02:01:15 +01:00
|
|
|
$map->index = $mapStatement->insert_id;
|
2013-11-12 16:52:23 +01:00
|
|
|
$mapStatement->close();
|
2013-11-12 15:48:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a map to the MapList
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\Maps\Map $map
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function addMap(Map $map) {
|
2013-11-12 16:52:23 +01:00
|
|
|
$this->saveMap($map);
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->mapList[$map->uid] = $map;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
|
2013-11-12 16:52:23 +01:00
|
|
|
/**
|
|
|
|
* Fetch current map
|
|
|
|
*
|
|
|
|
* @return \ManiaControl\Maps\Map
|
|
|
|
*/
|
|
|
|
public function getCurrentMap() {
|
|
|
|
if (!$this->maniaControl->client->query('GetCurrentMapInfo')) {
|
|
|
|
trigger_error("Couldn't fetch map info. " . $this->maniaControl->getClientErrorText());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$rpcMap = $this->maniaControl->client->getResponse();
|
|
|
|
$map = new Map($this->maniaControl, $rpcMap);
|
2013-11-25 02:01:15 +01:00
|
|
|
$this->addMap($map);
|
2013-11-12 16:52:23 +01:00
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:49:36 +01:00
|
|
|
/**
|
|
|
|
* Handle OnInit callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleOnInit(array $callback) {
|
|
|
|
$map = $this->getCurrentMap();
|
|
|
|
if (!$map) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-14 22:00:59 +01:00
|
|
|
$this->addMap($map); //TODO needed?
|
|
|
|
//var_dump($map);
|
|
|
|
//add Maplist on Init once
|
|
|
|
if(!$this->maniaControl->client->query('GetMapList', 100,0)){ //fetch 100 maps, first map is always current map
|
|
|
|
trigger_error("Couldn't fetch mapList. " . $this->maniaControl->getClientErrorText());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$mapList = $this->maniaControl->client->getResponse();
|
|
|
|
foreach($mapList as $rpcMap){
|
|
|
|
$map = new Map($this->maniaControl, $rpcMap);
|
|
|
|
$this->addMap($map);
|
|
|
|
}
|
2013-11-26 13:49:36 +01:00
|
|
|
}
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
/**
|
|
|
|
* Handle BeginMap callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleBeginMap(array $callback) {
|
2013-11-12 16:52:23 +01:00
|
|
|
$map = $this->getCurrentMap();
|
|
|
|
if (!$map) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->addMap($map);
|
2013-12-14 22:00:59 +01:00
|
|
|
|
2013-12-15 11:29:49 +01:00
|
|
|
|
2013-12-14 22:00:59 +01:00
|
|
|
//TODO restrucutre, make current as first
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
2013-12-14 22:00:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMapList(){
|
|
|
|
return $this->mapList;
|
|
|
|
}
|
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
/**
|
|
|
|
* Adds a Map from Mania Exchange
|
|
|
|
* @param $mapId
|
|
|
|
* @param $login
|
|
|
|
*/
|
|
|
|
public function addMapFromMx($mapId, $login){
|
|
|
|
// 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.", $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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.", $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$downloadDirectory = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX');
|
|
|
|
// 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.", $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$mapDir .= $downloadDirectory . '/';
|
|
|
|
|
|
|
|
// Download the map
|
|
|
|
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!', $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$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!', $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Save map
|
|
|
|
$fileName = $mapInfo['TrackID'] . '_' . $mapInfo['Name'] . '.Map.Gbx';
|
|
|
|
$fileName = FileUtil::getClearedFileName($fileName);
|
|
|
|
if (!file_put_contents($mapDir . $fileName, $file)) {
|
|
|
|
// Save error
|
|
|
|
$this->maniaControl->chat->sendError('Saving map failed!', $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check for valid map
|
|
|
|
$mapFileName = $downloadDirectory . '/' . $fileName;
|
|
|
|
if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $mapFileName)) {
|
|
|
|
trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $this->maniaControl->getClientErrorText());
|
|
|
|
$this->maniaControl->chat->sendError('Error checking map!', $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$response = $this->maniaControl->client->getResponse();
|
|
|
|
if (!$response) {
|
|
|
|
// Invalid map type
|
|
|
|
$this->maniaControl->chat->sendError("Invalid map type.", $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Add map to map list
|
|
|
|
if (!$this->maniaControl->client->query('InsertMap', $mapFileName)) {
|
|
|
|
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO: add local map by filename
|
|
|
|
}
|
2013-11-25 02:01:15 +01:00
|
|
|
}
|