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-01-08 18:07:09 +01:00
|
|
|
use ManiaControl\FileUtil;
|
2014-01-12 20:56:25 +01:00
|
|
|
use ManiaControl\Formatter;
|
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-01-28 17:12:23 +01:00
|
|
|
use ManiaControl\ManiaExchange\MXMapInfo;
|
2014-01-10 12:22:37 +01:00
|
|
|
use ManiaControl\Players\Player;
|
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
|
|
|
*
|
|
|
|
* @author kremsy & steeffeen
|
|
|
|
*/
|
|
|
|
class MapManager implements CallbackListener {
|
2013-11-12 16:52:23 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-01-11 17:06:32 +01:00
|
|
|
const TABLE_MAPS = 'mc_maps';
|
|
|
|
const CB_BEGINMAP = 'MapManager.BeginMap';
|
|
|
|
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-01-08 18:07:09 +01:00
|
|
|
|
2013-11-12 15:48:25 +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-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();
|
2014-01-15 18:31:06 +01:00
|
|
|
/** @var Map $currentMap */
|
2014-01-06 18:50:26 +01:00
|
|
|
private $currentMap = null;
|
2013-12-28 19:48:06 +01:00
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
/**
|
|
|
|
* Construct map manager
|
|
|
|
*
|
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-01-08 18:07:09 +01:00
|
|
|
|
2013-11-25 02:01:15 +01:00
|
|
|
// Create map commands instance
|
2014-01-28 16:54:23 +01:00
|
|
|
$this->mxManager = new ManiaExchangeManager($this->maniaControl);
|
2014-01-14 15:15:13 +01:00
|
|
|
$this->mapList = new MapList($this->maniaControl);
|
2014-01-28 16:54:23 +01:00
|
|
|
$this->mxList = new ManiaExchangeList($this->maniaControl);
|
2014-01-14 15:15:13 +01:00
|
|
|
$this->mapCommands = new MapCommands($maniaControl);
|
|
|
|
$this->mapQueue = new MapQueue($this->maniaControl);
|
2014-01-08 18:07:09 +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');
|
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
|
2014-01-09 19:00:37 +01:00
|
|
|
$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);
|
2014-01-11 16:34:05 +01:00
|
|
|
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SHUFFLE_MAPS, AuthenticationManager::AUTH_LEVEL_ADMIN);
|
2014-01-28 20:14:21 +01:00
|
|
|
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_CHECK_UPDATE, AuthenticationManager::AUTH_LEVEL_MODERATOR);
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize necessary database tables
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
2013-11-12 16:52:23 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-08 18:07:09 +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,
|
2014-01-09 21:32:17 +01:00
|
|
|
`mxid` int(11),
|
2013-11-12 16:52:23 +01:00
|
|
|
`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);
|
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-01-06 18:50:26 +01:00
|
|
|
* Save a Map in the Database
|
2013-11-12 16:52:23 +01:00
|
|
|
*
|
2013-12-27 21:10:53 +01:00
|
|
|
* @param \ManiaControl\Maps\Map $map
|
2014-01-06 18:50:26 +01:00
|
|
|
* @return bool
|
2013-11-12 16:52:23 +01:00
|
|
|
*/
|
2013-11-25 02:01:15 +01:00
|
|
|
private function saveMap(Map &$map) {
|
2014-01-08 18:07:09 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$mapQuery = "INSERT INTO `" . self::TABLE_MAPS . "` (
|
2013-11-12 16:52:23 +01:00
|
|
|
`uid`,
|
|
|
|
`name`,
|
|
|
|
`authorLogin`,
|
|
|
|
`fileName`,
|
|
|
|
`environment`,
|
|
|
|
`mapType`
|
|
|
|
) VALUES (
|
|
|
|
?, ?, ?, ?, ?, ?
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`index` = LAST_INSERT_ID(`index`);";
|
|
|
|
$mapStatement = $mysqli->prepare($mapQuery);
|
2014-01-27 21:38:30 +01:00
|
|
|
if ($mysqli->error) {
|
2013-11-12 16:52:23 +01:00
|
|
|
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-27 21:38:30 +01:00
|
|
|
if ($mapStatement->error) {
|
2013-11-12 16:52:23 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:40:14 +01:00
|
|
|
/**
|
|
|
|
* Updates the Timestamp of a map
|
|
|
|
*
|
|
|
|
* @param $map
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function updateMapTimestamp($uid) {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
|
|
|
$mapQuery = "UPDATE `" . self::TABLE_MAPS . "` SET mxid = 0, changed = NOW() WHERE 'uid' = ?";
|
|
|
|
|
|
|
|
$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-12 21:39:27 +01:00
|
|
|
/**
|
|
|
|
* Updates a Map from Mania Exchange
|
|
|
|
*
|
|
|
|
* @param Player $admin
|
|
|
|
* @param $mxId
|
|
|
|
* @param $uid
|
|
|
|
*/
|
|
|
|
public function updateMap(Player $admin, $uid) {
|
2014-01-15 20:40:14 +01:00
|
|
|
$this->updateMapTimestamp($uid);
|
2014-01-12 21:39:27 +01:00
|
|
|
|
|
|
|
$mapsDirectory = $this->maniaControl->server->getMapsDirectory();
|
2014-01-27 21:38:30 +01:00
|
|
|
if (!$this->maniaControl->server->checkAccess($mapsDirectory)) {
|
2014-01-12 21:39:27 +01:00
|
|
|
$this->maniaControl->chat->sendError("ManiaControl doesn't have access to the maps directory.", $admin->login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-27 21:38:30 +01:00
|
|
|
if (!isset($uid)) {
|
2014-01-25 22:29:25 +01:00
|
|
|
trigger_error("Error while updating Map, unkown UID: " . $uid);
|
|
|
|
$this->maniaControl->chat->sendError("Error while updating Map.", $admin->login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-12 21:39:27 +01:00
|
|
|
$map = $this->maps[$uid];
|
|
|
|
/** @var Map $map */
|
|
|
|
$mxId = $map->mx->id;
|
2014-01-15 20:40:14 +01:00
|
|
|
$this->removeMap($admin, $uid, true, false);
|
|
|
|
$this->addMapFromMx($mxId, $admin->login, true);
|
2014-01-12 21:39:27 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param string $uid
|
|
|
|
* @param bool $eraseFile
|
2014-01-15 20:40:14 +01:00
|
|
|
* @param bool $message
|
2013-12-16 13:14:26 +01:00
|
|
|
*/
|
2014-01-15 20:40:14 +01:00
|
|
|
public function removeMap(Player $admin, $uid, $eraseFile = false, $message = true) { //TODO erasefile?
|
2014-01-10 12:22:37 +01:00
|
|
|
$map = $this->maps[$uid];
|
|
|
|
|
2014-01-15 20:40:14 +01:00
|
|
|
//Unset the Map everywhere
|
2014-01-28 20:09:09 +01:00
|
|
|
$this->mapQueue->removeFromMapQueue($admin, $map->uid);
|
2014-01-15 20:40:14 +01:00
|
|
|
$this->mxManager->unsetMap($map->mx->id);
|
|
|
|
|
2014-01-10 12:22:37 +01:00
|
|
|
// Remove map
|
2014-01-20 20:51:03 +01:00
|
|
|
try {
|
|
|
|
$this->maniaControl->client->removeMap($map->fileName);
|
2014-01-27 21:38:30 +01:00
|
|
|
} catch(\Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Couldn't remove current map. " . $e->getMessage());
|
2014-01-10 12:22:37 +01:00
|
|
|
$this->maniaControl->chat->sendError("Couldn't remove map.", $admin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:40:14 +01: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-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
|
|
|
|
|
|
|
/**
|
|
|
|
* Restructures the Maplist
|
|
|
|
*/
|
|
|
|
public function restructureMapList() {
|
|
|
|
$currentIndex = $this->getMapIndex($this->currentMap);
|
|
|
|
|
|
|
|
//No RestructureNeeded
|
2014-01-27 21:38:30 +01:00
|
|
|
if ($currentIndex < 14) {
|
2014-01-15 18:31:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lowerMapArray = array();
|
|
|
|
$higherMapArray = array();
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach($this->maps as $map) {
|
|
|
|
/** @var Map $map */
|
2014-01-27 21:38:30 +01:00
|
|
|
if ($i < $currentIndex) {
|
2014-01-15 18:31:06 +01:00
|
|
|
$lowerMapArray[] = $map->fileName;
|
|
|
|
} else {
|
|
|
|
$higherMapArray[] = $map->fileName;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mapArray = array_merge($higherMapArray, $lowerMapArray);
|
|
|
|
|
2014-01-20 20:51:03 +01:00
|
|
|
try {
|
|
|
|
$this->maniaControl->client->chooseNextMapList($mapArray);
|
2014-01-27 21:38:30 +01:00
|
|
|
} catch(\Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Error while restructuring the Maplist. " . $e->getMessage());
|
2014-01-15 18:31:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-11 16:34:05 +01:00
|
|
|
/**
|
|
|
|
* Shuffles the MapList
|
|
|
|
*
|
2014-01-15 18:31:06 +01:00
|
|
|
* @param Player $admin
|
2014-01-11 16:34:05 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-14 15:45:36 +01:00
|
|
|
public function shuffleMapList($admin = null) {
|
2014-01-15 18:31:06 +01:00
|
|
|
$shuffledMaps = $this->maps;
|
|
|
|
shuffle($shuffledMaps);
|
2014-01-11 16:34:05 +01:00
|
|
|
|
2014-01-11 17:06:32 +01:00
|
|
|
$mapArray = array();
|
2014-01-11 16:34:05 +01:00
|
|
|
|
2014-01-15 18:31:06 +01:00
|
|
|
foreach($shuffledMaps as $map) {
|
2014-01-11 17:06:32 +01:00
|
|
|
/** @var Map $map */
|
|
|
|
$mapArray[] = $map->fileName;
|
|
|
|
}
|
|
|
|
|
2014-01-20 20:51:03 +01:00
|
|
|
try {
|
|
|
|
$this->maniaControl->client->chooseNextMapList($mapArray);
|
2014-01-27 21:38:30 +01:00
|
|
|
} catch(\Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Couldn't shuffle mapList. " . $e->getMessage());
|
2014-01-11 16:34:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-11 17:06:32 +01:00
|
|
|
|
|
|
|
$this->fetchCurrentMap();
|
|
|
|
|
2014-01-27 21:38:30 +01:00
|
|
|
if ($admin != null) {
|
2014-01-14 15:45:36 +01:00
|
|
|
$message = '$<' . $admin->nickname . '$> shuffled the Maplist!';
|
|
|
|
$this->maniaControl->chat->sendSuccess($message);
|
|
|
|
$this->maniaControl->log($message, true);
|
|
|
|
}
|
|
|
|
|
2014-01-15 18:31:06 +01:00
|
|
|
//Restructure if needed
|
|
|
|
$this->restructureMapList();
|
2014-01-11 16:34:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-12 20:56:25 +01:00
|
|
|
/**
|
|
|
|
* Initializes a Map
|
|
|
|
*
|
|
|
|
* @param $rpcMap
|
|
|
|
* @return Map
|
|
|
|
*/
|
|
|
|
public function initializeMap($rpcMap) {
|
|
|
|
$map = new Map($rpcMap);
|
|
|
|
$this->saveMap($map);
|
|
|
|
|
|
|
|
$mapsDirectory = $this->maniaControl->server->getMapsDirectory();
|
2014-01-27 21:38:30 +01:00
|
|
|
if (is_readable($mapsDirectory . $map->fileName)) {
|
2014-01-12 20:56:25 +01:00
|
|
|
$mapFetcher = new \GBXChallMapFetcher(true);
|
|
|
|
try {
|
|
|
|
$mapFetcher->processFile($mapsDirectory . $map->fileName);
|
|
|
|
$map->authorNick = FORMATTER::stripDirtyCodes($mapFetcher->authorNick);
|
|
|
|
$map->authorEInfo = $mapFetcher->authorEInfo;
|
|
|
|
$map->authorZone = $mapFetcher->authorZone;
|
|
|
|
$map->comment = $mapFetcher->comment;
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
trigger_error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $map;
|
|
|
|
}
|
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() {
|
2014-01-20 20:51:03 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$maps = $this->maniaControl->client->getMapList(100, 0);
|
2014-01-27 21:38:30 +01:00
|
|
|
} catch(\Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Couldn't fetch mapList. " . $e->getMessage());
|
2013-12-15 17:52:16 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-01-08 18:07:09 +01:00
|
|
|
|
2013-12-15 17:52:16 +01:00
|
|
|
$tempList = array();
|
2014-01-08 18:07:09 +01:00
|
|
|
|
|
|
|
foreach($maps as $rpcMap) {
|
2014-01-27 21:38:30 +01:00
|
|
|
if (array_key_exists($rpcMap->uId, $this->maps)) {
|
2014-01-06 18:50:26 +01:00
|
|
|
// Map already exists, only update index
|
2014-01-16 18:08:32 +01:00
|
|
|
$tempList[$rpcMap->uId] = $this->maps[$rpcMap->uId];
|
2014-01-08 18:07:09 +01:00
|
|
|
} else { // Insert Map Object
|
2014-01-12 20:56:25 +01:00
|
|
|
$map = $this->initializeMap($rpcMap);
|
2014-01-10 12:22:37 +01:00
|
|
|
$tempList[$map->uid] = $map;
|
2013-12-15 17:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
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 17:52:16 +01:00
|
|
|
}
|
2013-12-15 15:13:56 +01:00
|
|
|
|
2013-11-12 16:52:23 +01:00
|
|
|
/**
|
2014-01-06 18:50:26 +01:00
|
|
|
* Fetch current Map
|
2013-11-12 16:52:23 +01:00
|
|
|
*
|
2014-01-06 18:50:26 +01:00
|
|
|
* @return bool
|
2013-11-12 16:52:23 +01:00
|
|
|
*/
|
2014-01-06 18:50:26 +01:00
|
|
|
private function fetchCurrentMap() {
|
2014-01-20 20:51:03 +01:00
|
|
|
try {
|
|
|
|
$rpcMap = $this->maniaControl->client->getCurrentMapInfo();
|
2014-01-27 21:38:30 +01:00
|
|
|
} catch(\Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Couldn't fetch map info. " . $e->getMessage());
|
2014-01-06 18:50:26 +01:00
|
|
|
return false;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
2014-01-20 20:51:03 +01:00
|
|
|
|
2014-01-27 21:38:30 +01:00
|
|
|
if (array_key_exists($rpcMap->uId, $this->maps)) {
|
2014-01-16 18:08:32 +01:00
|
|
|
$this->currentMap = $this->maps[$rpcMap->uId];
|
2014-01-06 18:50:26 +01:00
|
|
|
return true;
|
2013-12-16 12:16:33 +01:00
|
|
|
}
|
2014-01-12 20:56:25 +01:00
|
|
|
$map = $this->initializeMap($rpcMap);
|
2014-01-10 12:22:37 +01:00
|
|
|
$this->maps[$map->uid] = $map;
|
|
|
|
$this->currentMap = $map;
|
2014-01-06 18:50:26 +01:00
|
|
|
return true;
|
2013-11-12 16:52:23 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:49:36 +01:00
|
|
|
/**
|
|
|
|
* Handle OnInit callback
|
|
|
|
*
|
2013-12-27 21:10:53 +01:00
|
|
|
* @param array $callback
|
2013-11-26 13:49:36 +01:00
|
|
|
*/
|
|
|
|
public function handleOnInit(array $callback) {
|
2013-12-15 17:52:16 +01:00
|
|
|
$this->updateFullMapList();
|
2014-01-06 18:50:26 +01:00
|
|
|
$this->fetchCurrentMap();
|
2014-01-15 18:31:06 +01:00
|
|
|
|
|
|
|
//Fetch Mx Infos
|
2014-01-14 15:15:13 +01:00
|
|
|
$this->mxManager->fetchManiaExchangeMapInformations();
|
2014-01-15 18:31:06 +01:00
|
|
|
|
|
|
|
//Restructure Maplist
|
|
|
|
$this->restructureMapList();
|
|
|
|
|
2013-11-26 13:49:36 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2013-12-15 17:52:16 +01:00
|
|
|
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
|
2014-01-11 22:06:52 +01:00
|
|
|
* @return Map array
|
2013-12-28 19:48:06 +01:00
|
|
|
*/
|
2014-01-05 14:13:18 +01:00
|
|
|
public function getMapByUid($uid) {
|
2014-01-27 21:38:30 +01:00
|
|
|
if (!isset($this->maps[$uid])) {
|
2014-01-06 18:50:26 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-01-10 12:22:37 +01:00
|
|
|
return $this->maps[$uid];
|
2013-12-28 19:48:06 +01:00
|
|
|
}
|
2013-12-15 17:52:16 +01:00
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
/**
|
|
|
|
* Handle BeginMap callback
|
|
|
|
*
|
2013-12-27 21:10:53 +01:00
|
|
|
* @param array $callback
|
2013-11-12 15:48:25 +01:00
|
|
|
*/
|
|
|
|
public function handleBeginMap(array $callback) {
|
2014-01-27 21:38:30 +01:00
|
|
|
if (!isset($callback[1][0]["UId"])) { //TODO why this can happen?
|
2014-01-23 22:08:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-27 21:38:30 +01:00
|
|
|
if (array_key_exists($callback[1][0]["UId"], $this->maps)) {
|
2014-01-06 18:50:26 +01:00
|
|
|
// Map already exists, only update index
|
2014-01-10 12:22:37 +01:00
|
|
|
$this->currentMap = $this->maps[$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-11-12 16:52:23 +01:00
|
|
|
}
|
2014-01-09 19:00:37 +01:00
|
|
|
|
2014-01-15 18:31:06 +01:00
|
|
|
//Restructure MapList if id is over 15
|
|
|
|
$this->restructureMapList();
|
|
|
|
|
|
|
|
//Update the mx of the map (for update checks, etc.)
|
|
|
|
$this->mxManager->fetchManiaExchangeMapInformations($this->currentMap);
|
|
|
|
|
2014-01-09 19:00:37 +01:00
|
|
|
// Trigger own BeginMap callback
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_BEGINMAP, array(self::CB_BEGINMAP, $this->currentMap));
|
2014-01-15 18:31:06 +01:00
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
}
|
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-15 17:52:16 +01:00
|
|
|
|
2013-12-14 22:00:59 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-01-06 18:50:26 +01:00
|
|
|
public function getMaps() {
|
2014-01-10 12:32:11 +01:00
|
|
|
return array_values($this->maps);
|
2013-12-14 22:00:59 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 18:31:06 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
/**
|
|
|
|
* Adds a Map from Mania Exchange
|
2014-01-05 14:13:18 +01:00
|
|
|
*
|
2014-01-15 20:40:14 +01:00
|
|
|
* @param $mapId
|
|
|
|
* @param $login
|
|
|
|
* @param bool $update
|
2013-12-15 15:13:56 +01:00
|
|
|
*/
|
2014-01-15 20:40:14 +01:00
|
|
|
public function addMapFromMx($mapId, $login, $update = false) {
|
2013-12-15 15:13:56 +01:00
|
|
|
// Check if ManiaControl can even write to the maps dir
|
2014-01-20 20:51:03 +01:00
|
|
|
try {
|
|
|
|
$mapDir = $this->maniaControl->client->getMapsDirectory();
|
2014-01-27 21:38:30 +01:00
|
|
|
} catch(\Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Couldn't get map directory. " . $e->getMessage());
|
2013-12-15 15:13:56 +01:00
|
|
|
$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
|
|
|
// Download the map
|
2014-01-27 21:38:30 +01:00
|
|
|
if (is_numeric($mapId)) {
|
2013-12-15 15:13:56 +01:00
|
|
|
// Load from MX
|
|
|
|
$serverInfo = $this->maniaControl->server->getSystemInfo();
|
2014-01-16 20:00:03 +01:00
|
|
|
$title = strtolower(substr($serverInfo->titleId, 0, 2));
|
2014-01-08 18:07:09 +01:00
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
// Check if map exists
|
2014-01-14 15:15:13 +01:00
|
|
|
$mxMapInfos = $this->maniaControl->mapManager->mxManager->getMaplistByMixedUidIdString($mapId);
|
2014-01-12 17:15:59 +01:00
|
|
|
$mapInfo = $mxMapInfos[0];
|
|
|
|
/** @var MXMapInfo $mapInfo */
|
2014-01-08 18:07:09 +01:00
|
|
|
|
2014-01-27 21:38:30 +01:00
|
|
|
if (!$mapInfo || !isset($mapInfo->uploaded)) {
|
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
|
|
|
|
|
|
|
$url = "http://{$title}.mania-exchange.com/tracks/download/{$mapId}";
|
2013-12-15 15:13:56 +01:00
|
|
|
$file = FileUtil::loadFile($url);
|
2014-01-27 21:38:30 +01:00
|
|
|
if (!$file) {
|
2013-12-15 15:13:56 +01:00
|
|
|
// Download error
|
|
|
|
$this->maniaControl->chat->sendError('Download failed!', $login);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-15 16:49:50 +01:00
|
|
|
|
|
|
|
//Check if map is already on the server
|
2014-01-27 21:38:30 +01:00
|
|
|
if ($this->getMapByUid($mapInfo->uid) != null) {
|
2014-01-15 16:49:50 +01:00
|
|
|
// Download error
|
|
|
|
$this->maniaControl->chat->sendError('Map is already on the server!', $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
// Save map
|
2014-01-12 17:15:59 +01:00
|
|
|
$fileName = $mapId . '_' . $mapInfo->name . '.Map.Gbx';
|
2013-12-15 15:13:56 +01:00
|
|
|
$fileName = FileUtil::getClearedFileName($fileName);
|
2014-01-29 22:44:06 +01:00
|
|
|
|
|
|
|
$downloadDirectory = $this->maniaControl->settingManager->getSetting($this, 'MapDownloadDirectory', 'MX');
|
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
$mapFileName = $downloadDirectory . '/' . $fileName;
|
2014-01-08 18:07:09 +01:00
|
|
|
|
2014-01-29 22:44:06 +01:00
|
|
|
//Check if it can get locally Written
|
|
|
|
if (is_dir($mapDir)) {
|
|
|
|
// 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 . '/';
|
|
|
|
|
|
|
|
if (!file_put_contents($mapDir . $fileName, $file)) {
|
|
|
|
// Save error
|
|
|
|
$this->maniaControl->chat->sendError('Saving map failed!', $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Write via Write File Method
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$this->maniaControl->client->writeFileFromString($mapFileName, $file);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
$this->maniaControl->chat->sendError("Map is too big for a remote save.", $login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for valid map
|
2014-01-20 20:51:03 +01:00
|
|
|
try {
|
2014-01-25 19:00:36 +01:00
|
|
|
$this->maniaControl->client->checkMapForCurrentServerParams($mapFileName);
|
2014-01-27 21:38:30 +01:00
|
|
|
} catch(\Exception $e) {
|
2014-01-20 20:51:03 +01:00
|
|
|
trigger_error("Couldn't check if map is valid ('{$mapFileName}'). " . $e->getMessage());
|
2014-01-25 19:00:36 +01:00
|
|
|
$this->maniaControl->chat->sendError('Wrong MapType or not validated!', $login);
|
2013-12-15 15:13:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-15 16:49:50 +01:00
|
|
|
|
2013-12-15 15:13:56 +01:00
|
|
|
// Add map to map list
|
2014-01-27 22:01:42 +01:00
|
|
|
try {
|
|
|
|
$this->maniaControl->client->insertMap($mapFileName);
|
|
|
|
} catch(\Exception $e) {
|
2013-12-15 15:13:56 +01:00
|
|
|
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $login);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-28 16:54:23 +01:00
|
|
|
|
2013-12-15 17:52:16 +01:00
|
|
|
$this->updateFullMapList();
|
2014-01-08 18:07:09 +01:00
|
|
|
|
2014-01-12 17:15:59 +01:00
|
|
|
//Update Mx MapInfo
|
2014-01-14 15:15:13 +01:00
|
|
|
$this->maniaControl->mapManager->mxManager->updateMapObjectsWithManiaExchangeIds($mxMapInfos);
|
2014-01-12 17:15:59 +01:00
|
|
|
|
2014-01-15 20:40:14 +01:00
|
|
|
//Update last updated time
|
|
|
|
$map = $this->maps[$mapInfo->uid];
|
|
|
|
/** @var Map $map */
|
|
|
|
$map->lastUpdate = time();
|
|
|
|
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
|
|
|
|
2014-01-27 21:38:30 +01:00
|
|
|
if (!$update) {
|
2014-01-15 20:40:14 +01:00
|
|
|
//Message
|
2014-01-25 17:14:26 +01:00
|
|
|
$message = '$<' . $player->nickname . '$> added $<' . $mapInfo->name . '$>!';
|
|
|
|
$this->maniaControl->chat->sendSuccess($message);
|
|
|
|
$this->maniaControl->log($message, true);
|
2014-01-15 20:40:14 +01:00
|
|
|
// Queue requested Map
|
|
|
|
$this->maniaControl->mapManager->mapQueue->addMapToMapQueue($login, $mapInfo->uid);
|
|
|
|
} else {
|
2014-01-25 17:14:26 +01:00
|
|
|
$message = '$<' . $player->nickname . '$> updated $<' . $mapInfo->name . '$>!';
|
|
|
|
$this->maniaControl->chat->sendSuccess($message);
|
|
|
|
$this->maniaControl->log($message, true);
|
2014-01-15 20:40:14 +01:00
|
|
|
}
|
2013-12-15 15:13:56 +01:00
|
|
|
}
|
|
|
|
// TODO: add local map by filename
|
|
|
|
}
|
2013-11-25 02:01:15 +01:00
|
|
|
}
|