TrackManiaControl/application/core/Maps/MapManager.php

364 lines
11 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-01-08 18:07:09 +01:00
use ManiaControl\FileUtil;
use ManiaControl\ManiaControl;
require_once __DIR__ . '/Map.php';
require_once __DIR__ . '/MapCommands.php';
require_once __DIR__ . '/MapList.php';
require_once __DIR__ . '/MapQueue.php';
/**
2014-01-05 14:13:18 +01:00
* Manager for Maps
*
* @author kremsy & steeffeen
*/
class MapManager implements CallbackListener {
/**
* Constants
*/
2014-01-09 18:45:39 +01:00
const TABLE_MAPS = 'mc_maps';
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-08 18:07:09 +01:00
/**
2014-01-06 18:50:26 +01:00
* Public Properties
*/
2014-01-06 18:50:26 +01:00
public $mapQueue = null;
public $mapCommands = null;
public $mapList = null;
2014-01-08 21:03:59 +01:00
public $mxInfoSearcher = null;
2014-01-08 18:07:09 +01:00
2013-12-28 19:48:06 +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();
private $mapsUids = array();
private $currentMap = null;
2013-12-28 19:48:06 +01:00
/**
* Construct map manager
*
2013-12-27 21:10:53 +01:00
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
2014-01-08 18:07:09 +01:00
// Create map commands instance
2014-01-08 21:03:59 +01:00
$this->mapList = new MapList($this->maniaControl);
$this->mapCommands = new MapCommands($maniaControl);
$this->mapQueue = new MapQueue($this->maniaControl);
$this->mxInfoSearcher = new ManiaExchangeInfoSearcher($this->maniaControl);
2014-01-08 18:07:09 +01:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap');
2014-01-06 18:50:26 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapsModified');
2014-01-09 18:45:39 +01:00
//Define Rights
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_ADD_MAP,AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_REMOVE_MAP,AuthenticationManager::AUTH_LEVEL_ADMIN);
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
2014-01-08 18:07:09 +01:00
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_MAPS . "` (
`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;";
$result = $mysqli->query($query);
2014-01-08 18:07:09 +01:00
if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
return $result;
}
/**
2014-01-06 18:50:26 +01:00
* Save a Map in the Database
*
2013-12-27 21:10:53 +01:00
* @param \ManiaControl\Maps\Map $map
2014-01-06 18:50:26 +01:00
* @return bool
*/
private function saveMap(Map &$map) {
2014-01-08 18:07:09 +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);
2014-01-08 18:07:09 +01:00
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();
2014-01-08 18:07:09 +01:00
if($mapStatement->error) {
trigger_error($mapStatement->error);
$mapStatement->close();
return false;
}
$map->index = $mapStatement->insert_id;
$mapStatement->close();
return true;
}
/**
2014-01-06 18:50:26 +01:00
* Remove a Map
2014-01-05 14:13:18 +01:00
*
2014-01-08 18:07:09 +01:00
* @param int $id
2014-01-06 18:50:26 +01:00
* @param string $uid
2014-01-08 18:07:09 +01:00
* @param bool $eraseFile
2013-12-16 13:14:26 +01:00
*/
2014-01-08 18:07:09 +01:00
public function removeMap($id, $uid, $eraseFile = false) { //TODO erasefile?
2014-01-06 18:50:26 +01:00
$map = $this->mapsUids[$uid];
2013-12-16 13:14:26 +01:00
$this->maniaControl->client->query('RemoveMap', $map->fileName);
2014-01-05 14:13:18 +01:00
$this->maniaControl->chat->sendSuccess('Map $<' . $map->name . '$> removed!');
// TODO specified message, who done it?
$this->maniaControl->log('Map $<' . $map->name . '$> removed!', true);
2014-01-06 18:50:26 +01:00
unset($this->mapsUids[$uid]);
unset($this->maps[$id]);
2013-12-16 13:14:26 +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() {
2014-01-08 18:07:09 +01:00
if(!$this->maniaControl->client->query('GetMapList', 100, 0)) {
trigger_error("Couldn't fetch mapList. " . $this->maniaControl->getClientErrorText());
return null;
}
2014-01-08 18:07:09 +01:00
$tempList = array();
2014-01-08 18:07:09 +01:00
2014-01-06 18:50:26 +01:00
$maps = $this->maniaControl->client->getResponse();
2014-01-08 18:07:09 +01:00
foreach($maps as $rpcMap) {
if(array_key_exists($rpcMap["UId"], $this->mapsUids)) {
2014-01-06 18:50:26 +01:00
// Map already exists, only update index
$tempList[] = $this->mapsUids[$rpcMap["UId"]];
2014-01-08 18:07:09 +01:00
} else { // Insert Map Object
$map = new Map($this->maniaControl, $rpcMap);
$this->saveMap($map);
2014-01-08 18:07:09 +01:00
$tempList[] = $map;
2014-01-06 18:50:26 +01:00
$this->mapsUids[$map->uid] = $map;
}
}
2014-01-08 18:07:09 +01:00
2014-01-05 14:13:18 +01:00
// restore Sorted Maplist
2014-01-06 18:50:26 +01:00
$this->maps = $tempList;
2014-01-08 18:07:09 +01:00
2013-12-28 23:24:54 +01:00
// Trigger own callback
2014-01-06 18:50:26 +01:00
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAPS_UPDATED, array(self::CB_MAPS_UPDATED));
}
2013-12-15 15:13:56 +01:00
/**
2014-01-06 18:50:26 +01:00
* Fetch current Map
*
2014-01-06 18:50:26 +01:00
* @return bool
*/
2014-01-06 18:50:26 +01:00
private function fetchCurrentMap() {
2014-01-08 18:07:09 +01:00
if(!$this->maniaControl->client->query('GetCurrentMapInfo')) {
trigger_error("Couldn't fetch map info. " . $this->maniaControl->getClientErrorText());
2014-01-06 18:50:26 +01:00
return false;
}
$rpcMap = $this->maniaControl->client->getResponse();
2014-01-08 18:07:09 +01:00
if(!array_key_exists($rpcMap["UId"], $this->mapsUids)) {
2014-01-06 18:50:26 +01:00
$this->currentMap = $this->mapsUids[$rpcMap["UId"]];
return true;
2013-12-16 12:16:33 +01:00
}
2014-01-06 18:50:26 +01:00
$map = new Map($this->maniaControl, $rpcMap);
$this->saveMap($map);
$this->mapsUids[$map->uid] = $map;
2014-01-08 18:07:09 +01:00
$this->maps[] = $map;
$this->currentMap = $map;
2014-01-06 18:50:26 +01:00
return true;
}
/**
* Handle OnInit callback
*
2013-12-27 21:10:53 +01:00
* @param array $callback
*/
public function handleOnInit(array $callback) {
$this->updateFullMapList();
2014-01-06 18:50:26 +01:00
$this->fetchCurrentMap();
}
/**
2014-01-05 14:13:18 +01:00
* Get Current Map
*
2013-12-30 16:45:26 +01:00
* @return Map currentMap
*/
2014-01-05 14:13:18 +01:00
public function getCurrentMap() {
return $this->currentMap;
}
2013-12-28 19:48:06 +01:00
/**
* Returns map By UID
2014-01-05 14:13:18 +01:00
*
2013-12-28 19:48:06 +01:00
* @param $uid
* @return mixed
*/
2014-01-05 14:13:18 +01:00
public function getMapByUid($uid) {
2014-01-08 18:07:09 +01:00
if(!isset($this->mapsUids[$uid])) {
2014-01-06 18:50:26 +01:00
return null;
}
return $this->mapsUids[$uid];
2013-12-28 19:48:06 +01:00
}
/**
* Handle BeginMap callback
*
2013-12-27 21:10:53 +01:00
* @param array $callback
*/
public function handleBeginMap(array $callback) {
2014-01-08 18:07:09 +01:00
if(array_key_exists($callback[1][0]["UId"], $this->mapsUids)) {
2014-01-06 18:50:26 +01:00
// Map already exists, only update index
$this->currentMap = $this->mapsUids[$callback[1][0]["UId"]];
2014-01-08 18:07:09 +01:00
} else {
2014-01-06 18:50:26 +01:00
// can this ever happen?
$this->fetchCurrentMap();
}
}
2013-12-14 22:00:59 +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-14 22:00:59 +01:00
/**
2014-01-05 14:13:18 +01:00
*
2013-12-14 22:00:59 +01:00
* @return array
*/
2014-01-06 18:50:26 +01:00
public function getMaps() {
return $this->maps;
2013-12-14 22:00:59 +01:00
}
2013-12-15 15:13:56 +01:00
/**
* Adds a Map from Mania Exchange
2014-01-05 14:13:18 +01:00
*
* @param $mapId
* @param $login
2013-12-15 15:13:56 +01:00
*/
2014-01-05 14:13:18 +01:00
public function addMapFromMx($mapId, $login) {
2013-12-15 15:13:56 +01:00
// Check if ManiaControl can even write to the maps dir
2014-01-08 18:07:09 +01:00
if(!$this->maniaControl->client->query('GetMapsDirectory')) {
2013-12-15 15:13:56 +01:00
trigger_error("Couldn't get map directory. " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("ManiaControl couldn't retrieve the maps directory.", $login);
return;
}
2014-01-08 18:07:09 +01:00
2013-12-15 15:13:56 +01:00
$mapDir = $this->maniaControl->client->getResponse();
2014-01-08 18:07:09 +01:00
if(!is_dir($mapDir)) {
2013-12-15 15:13:56 +01:00
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
2014-01-08 18:07:09 +01:00
if(!is_dir($mapDir . $downloadDirectory) && !mkdir($mapDir . $downloadDirectory)) {
2013-12-15 15:13:56 +01:00
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 . '/';
2014-01-08 18:07:09 +01:00
2013-12-15 15:13:56 +01:00
// Download the map
2014-01-08 18:07:09 +01:00
if(is_numeric($mapId)) {
2013-12-15 15:13:56 +01:00
// Load from MX
$serverInfo = $this->maniaControl->server->getSystemInfo();
2014-01-08 18:07:09 +01:00
$title = strtolower(substr($serverInfo['TitleId'], 0, 2));
2013-12-15 15:13:56 +01:00
// Check if map exists
2013-12-16 14:21:30 +01:00
$url = "http://api.mania-exchange.com/{$title}/maps/{$mapId}?format=json";
2014-01-08 18:07:09 +01:00
2013-12-27 21:10:53 +01:00
$mapInfo = FileUtil::loadFile($url, "application/json");
2014-01-08 18:07:09 +01:00
if(!$mapInfo || strlen($mapInfo) <= 0) {
2013-12-15 15:13:56 +01:00
// Invalid id
$this->maniaControl->chat->sendError('Invalid MX-Id!', $login);
return;
}
2014-01-08 18:07:09 +01:00
2013-12-15 15:13:56 +01:00
$mapInfo = json_decode($mapInfo, true);
2013-12-27 21:10:53 +01:00
$mapInfo = $mapInfo[0];
2014-01-08 18:07:09 +01:00
$url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}";
2013-12-15 15:13:56 +01:00
$file = FileUtil::loadFile($url);
2014-01-08 18:07:09 +01:00
if(!$file) {
2013-12-15 15:13:56 +01:00
// Download error
$this->maniaControl->chat->sendError('Download failed!', $login);
return;
}
// Save map
2013-12-27 21:10:53 +01:00
$fileName = $mapId . '_' . $mapInfo['Name'] . '.Map.Gbx';
2013-12-15 15:13:56 +01:00
$fileName = FileUtil::getClearedFileName($fileName);
2014-01-08 18:07:09 +01:00
if(!file_put_contents($mapDir . $fileName, $file)) {
2013-12-15 15:13:56 +01:00
// Save error
$this->maniaControl->chat->sendError('Saving map failed!', $login);
return;
}
// Check for valid map
$mapFileName = $downloadDirectory . '/' . $fileName;
2014-01-08 18:07:09 +01:00
if(!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $mapFileName)) {
2013-12-15 15:13:56 +01:00
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();
2014-01-08 18:07:09 +01:00
if(!$response) {
2013-12-15 15:13:56 +01:00
// Invalid map type
$this->maniaControl->chat->sendError("Invalid map type.", $login);
return;
}
// Add map to map list
2014-01-08 18:07:09 +01:00
if(!$this->maniaControl->client->query('InsertMap', $mapFileName)) {
2014-01-06 18:50:26 +01:00
// TODO irgendein bug?
2013-12-15 15:13:56 +01:00
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $login);
return;
}
$this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!');
2014-01-08 18:07:09 +01:00
$this->updateFullMapList();
2014-01-08 18:07:09 +01:00
2014-01-05 14:13:18 +01:00
// Queue requested Map
2013-12-31 12:25:03 +01:00
$this->maniaControl->mapManager->mapQueue->addMapToMapQueue($login, $mapInfo['MapUID']);
2013-12-15 15:13:56 +01:00
}
// TODO: add local map by filename
}
}