TrackManiaControl/core/Maps/MapManager.php

1117 lines
31 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-04-24 21:55:47 +02:00
use ManiaControl\Callbacks\Callbacks;
2015-07-11 13:04:34 +02:00
use ManiaControl\Communication\CommunicationAnswer;
use ManiaControl\Communication\CommunicationListener;
use ManiaControl\Communication\CommunicationMethods;
2017-03-29 20:38:46 +02:00
use ManiaControl\Files\AsyncHttpRequest;
2014-02-07 00:26:32 +01:00
use ManiaControl\Files\FileUtil;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\Logger;
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-06-23 15:45:40 +02:00
use ManiaControl\Utils\Formatter;
2014-02-17 00:34:20 +01:00
use Maniaplanet\DedicatedServer\InvalidArgumentException;
2014-06-23 16:04:00 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\AlreadyInListException;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
2017-05-18 17:29:23 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\FaultException;
use Maniaplanet\DedicatedServer\Xmlrpc\FileException;
use Maniaplanet\DedicatedServer\Xmlrpc\IndexOutOfBoundException;
use Maniaplanet\DedicatedServer\Xmlrpc\InvalidMapException;
use Maniaplanet\DedicatedServer\Xmlrpc\NotInListException;
2014-06-14 18:16:45 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\UnavailableFeatureException;
/**
* ManiaControl Map Manager Class
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
2014-04-19 22:44:59 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class MapManager implements CallbackListener, CommunicationListener, UsageInformationAble {
use UsageInformationTrait;
2017-03-26 19:51:22 +02:00
/*
* Constants
*/
const TABLE_MAPS = 'mc_maps';
2014-04-19 22:44:59 +02:00
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';
const SETTING_PERMISSION_ERASE_MAP = 'Erase 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';
const SETTING_WRITE_OWN_MAPLIST_FILE = 'Write a own Maplist File for every Server called serverlogin.txt';
2014-04-19 22:44:59 +02:00
const SEARCH_BY_AUTHOR = 'Author';
const SEARCH_BY_MAP_NAME = 'Mapname';
2014-04-19 22:44:59 +02:00
/*
* Private properties
2013-12-28 19:48:06 +01:00
*/
/** @var MapQueue $mapQueue */
private $mapQueue = null;
/** @var MapCommands $mapCommands */
private $mapCommands = null;
/** @var MapActions $mapActions */
private $mapActions = null;
/** @var MapList $mapList */
private $mapList = null;
/** @var DirectoryBrowser $directoryBrowser */
private $directoryBrowser = null;
/** @var ManiaExchangeList $mxList */
private $mxList = null;
/** @var ManiaExchangeManager $mxManager */
private $mxManager = null;
/** @var ManiaControl $maniaControl */
2014-01-06 18:50:26 +01:00
private $maniaControl = null;
/** @var Map[] $maps */
2014-01-06 18:50:26 +01:00
private $maps = array();
2014-05-09 16:58:57 +02:00
/** @var Map $currentMap */
2014-01-06 18:50:26 +01:00
private $currentMap = null;
2014-10-24 20:08:21 +02:00
private $mapEnded = false;
private $mapBegan = false;
2013-12-28 19:48:06 +01:00
/**
* Construct a new map manager instance
*
2014-05-09 16:58:57 +02:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
2014-04-19 22:44:59 +02:00
// Children
2014-06-29 23:51:01 +02:00
$this->mxManager = new ManiaExchangeManager($this->maniaControl);
$this->mapList = new MapList($this->maniaControl);
$this->directoryBrowser = new DirectoryBrowser($this->maniaControl);
$this->mxList = new ManiaExchangeList($this->maniaControl);
$this->mapCommands = new MapCommands($maniaControl);
$this->mapQueue = new MapQueue($this->maniaControl);
$this->mapActions = new MapActions($maniaControl);
2014-04-19 22:44:59 +02:00
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::ONINIT, $this, 'handleOnInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_MAPLISTMODIFIED, $this, 'mapsModified');
2014-04-19 22:44:59 +02:00
// Permissions
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_ADD_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_REMOVE_MAP, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_ERASE_MAP, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_SHUFFLE_MAPS, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHECK_UPDATE, AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_SKIP_MAP, AuthenticationManager::AUTH_LEVEL_MODERATOR);
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_RESTART_MAP, AuthenticationManager::AUTH_LEVEL_MODERATOR);
2014-04-19 22:44:59 +02:00
// Settings
2014-08-13 11:05:52 +02:00
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_AUTOSAVE_MAPLIST, true);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MAPLIST_FILE, "MatchSettings/tracklist.txt");
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WRITE_OWN_MAPLIST_FILE, false);
2015-07-11 13:04:34 +02:00
2015-07-11 13:41:46 +02:00
//Initlaize Communication Listenings
$this->initalizeCommunicationListenings();
}
/**
* Return the map commands
*
* @return MapCommands
*/
public function getMapCommands() {
return $this->mapCommands;
}
/**
* Return the map actions
*
* @return MapActions
*/
public function getMapActions() {
return $this->mapActions;
}
/**
* Return the map list
*
* @return MapList
*/
public function getMapList() {
return $this->mapList;
}
/**
* Return the directory browser
*
* @return DirectoryBrowser
*/
public function getDirectoryBrowser() {
return $this->directoryBrowser;
}
/**
* Return the mx list
*
* @return ManiaExchangeList
*/
public function getMXList() {
return $this->mxList;
}
/**
2014-05-02 17:50:30 +02:00
* Update a Map from Mania Exchange
*
2015-07-11 13:04:34 +02:00
* @param Player|null $admin
* @param string $uid
*/
2015-07-11 13:04:34 +02:00
public function updateMap($admin, $uid) {
2014-05-02 17:50:30 +02:00
$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])) {
2015-07-11 13:04:34 +02:00
if ($admin) {
$message = $this->maniaControl->getChat()->formatMessage(
'Error updating Map, unknown UID %s!',
$uid
);
$this->maniaControl->getChat()->sendError($message, $admin);
2015-07-11 13:04:34 +02:00
}
2014-05-02 17:50:30 +02:00
return;
}
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);
2015-07-11 13:04:34 +02:00
if ($admin) {
$this->addMapFromMx($mxId, $admin->login, true);
} else {
$this->addMapFromMx($mxId, null, true);
}
}
2014-01-15 20:40:14 +01:00
/**
* Update the Timestamp of a Map
2014-01-15 20:40:14 +01:00
*
* @param string $uid
2014-01-15 20:40:14 +01:00
* @return bool
*/
public function updateMapTimestamp($uid) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
//TODO mxid was set to 0, verify what for
2014-06-14 11:32:09 +02:00
$mapQuery = "UPDATE `" . self::TABLE_MAPS . "` SET
`changed` = NOW()
WHERE `uid` LIKE ?";
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;
}
/**
2014-01-06 18:50:26 +01:00
* Remove a Map
2014-01-05 14:13:18 +01:00
*
2015-07-11 13:04:34 +02:00
* @param Player|null $admin
* @param string $uid
* @param bool $eraseFile
* @param bool $message
* @return bool
2013-12-16 13:14:26 +01:00
*/
2015-07-11 13:04:34 +02:00
public function removeMap($admin, $uid, $eraseFile = false, $message = true) {
2014-04-19 22:44:59 +02:00
if (!isset($this->maps[$uid])) {
2015-07-11 13:04:34 +02:00
if ($admin) {
$this->maniaControl->getChat()->sendError('Map does not exist!', $admin);
}
return false;
2014-04-09 10:21:34 +02:00
}
/** @var Map $map */
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-08-13 11:05:52 +02:00
$this->getMapQueue()->removeFromMapQueue($admin, $map->uid);
2014-04-19 22:44:59 +02:00
2014-03-19 11:11:25 +01:00
if ($map->mx) {
2014-08-13 11:05:52 +02:00
$this->getMXManager()->unsetMap($map->mx->id);
2014-01-31 15:56:57 +01:00
}
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-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->removeMap($map->fileName);
} catch (NotInListException $e) {
2014-09-01 03:26:21 +02:00
} catch (FileException $e) {
2014-04-21 23:14:06 +02:00
}
unset($this->maps[$uid]);
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-08-13 11:05:52 +02:00
$mapDir = $this->maniaControl->getClient()->getMapsDirectory();
2015-07-11 13:04:34 +02:00
if ($this->maniaControl->getServer()->checkAccess($mapDir)) {
$mapFile = $mapDir . $map->fileName;
// Delete map file
if (!@unlink($mapFile)) {
2015-07-11 13:04:34 +02:00
if ($admin) {
$message = $this->maniaControl->getChat()->formatMessage(
'Could not erase the map file %s.',
$mapFile
);
$this->maniaControl->getChat()->sendError($message, $admin);
2015-07-11 13:04:34 +02:00
}
$eraseFile = false;
}
} else {
2015-07-11 13:04:34 +02:00
if ($admin) {
$this->maniaControl->getChat()->sendError('Could not erase the map file (no access to Maps-Directory).', $admin);
2015-07-11 13:04:34 +02:00
}
$eraseFile = false;
2014-01-31 15:56:57 +01:00
}
}
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) {
2015-07-11 13:41:46 +02:00
$action = ($eraseFile ? 'erased' : 'removed');
if ($admin) {
$message = $this->maniaControl->getChat()->formatMessage(
"%s {$action} %s!",
$admin,
$map
);
2015-07-11 13:41:46 +02:00
} else {
$message = $this->maniaControl->getChat()->formatMessage(
"%s got {$action}!",
$map
);
2015-07-11 13:41:46 +02:00
}
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
2014-01-15 20:40:14 +01:00
}
2015-07-11 13:04:34 +02:00
return true;
2013-12-16 13:14:26 +01:00
}
2014-08-25 17:34:25 +02:00
/**
* Return the map queue
*
* @return MapQueue
*/
public function getMapQueue() {
return $this->mapQueue;
}
/**
* Return the mx manager
*
* @return ManiaExchangeManager
*/
public function getMXManager() {
return $this->mxManager;
}
2014-01-15 18:31:06 +01:00
/**
2014-05-02 17:50:30 +02:00
* Adds a Map from Mania Exchange
*
2014-05-09 16:58:57 +02:00
* @param int $mapId
* @param string $login
* @param bool $update
2015-07-11 13:04:34 +02:00
* @param bool $displayMessage
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
2014-08-13 11:05:52 +02:00
$this->maniaControl->getMapManager()->getMXManager()->fetchMapInfo($mapId, function (MXMapInfo $mapInfo = null) use (
2014-06-23 15:45:40 +02:00
&$login, &$update
) {
2014-05-02 17:50:30 +02:00
if (!$mapInfo || !isset($mapInfo->uploaded)) {
2015-07-11 13:04:34 +02:00
if ($login) {
// Invalid id
$this->maniaControl->getChat()->sendError('Invalid MX-Id!', $login);
}
2014-05-02 17:50:30 +02:00
return;
}
2014-04-19 22:44:59 +02:00
2017-03-06 19:03:09 +01:00
$url = $mapInfo->downloadurl;
2017-03-29 20:38:46 +02:00
if ($key = $this->maniaControl->getSettingManager()->getSettingValue($this->getMXManager(), ManiaExchangeManager::SETTING_MX_KEY)) {
$url .= "?key=" . $key;
2017-03-06 19:03:09 +01:00
}
2017-03-29 20:38:46 +02:00
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setContentType(AsyncHttpRequest::CONTENT_TYPE_UTF8);
$asyncHttpRequest->setHeaders(array("X-ManiaPlanet-ServerLogin: " . $this->maniaControl->getServer()->login));
$asyncHttpRequest->setCallable(function ($file, $error) use (
2014-06-23 15:45:40 +02:00
&$login, &$mapInfo, &$update
) {
2014-05-02 18:21:38 +02:00
if (!$file || $error) {
2015-07-11 13:04:34 +02:00
if ($login) {
// Download error
$this->maniaControl->getChat()->sendError("Download failed: {$error}!", $login);
2015-07-11 13:04:34 +02:00
}
2014-05-02 17:50:30 +02:00
return;
}
$this->processMapFile($file, $mapInfo, $login, $update);
2017-03-29 20:38:46 +02:00
});
$asyncHttpRequest->getData();
2014-05-02 17:50:30 +02:00
});
2014-01-15 18:31:06 +01:00
}
2015-07-11 13:04:34 +02:00
return;
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-09 16:58:57 +02:00
* @param string $file
2014-05-02 17:50:30 +02:00
* @param MXMapInfo $mapInfo
2014-05-09 16:58:57 +02:00
* @param string $login
* @param bool $update
2014-05-13 16:03:26 +02:00
* @throws InvalidArgumentException
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)) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Map is already on the server!', $login);
2014-05-02 17:50:30 +02:00
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-08-13 11:05:52 +02:00
$downloadFolderName = $this->maniaControl->getSettingManager()->getSettingValue($this, 'MapDownloadDirectory', 'MX');
2014-05-03 21:37:28 +02:00
$relativeMapFileName = $downloadFolderName . DIRECTORY_SEPARATOR . $fileName;
2014-08-13 11:05:52 +02:00
$mapDir = $this->maniaControl->getServer()->getDirectory()->getMapsFolder();
2014-06-14 11:32:09 +02:00
$downloadDirectory = $mapDir . $downloadFolderName . DIRECTORY_SEPARATOR;
2014-05-02 17:50:30 +02:00
$fullMapFileName = $downloadDirectory . $fileName;
// Check if it can get written locally
2014-08-25 17:34:25 +02:00
if ($this->maniaControl->getServer()->checkAccess($mapDir)) {
2014-05-02 17:50:30 +02:00
// Create download directory if necessary
2014-06-23 16:04:00 +02:00
if (!is_dir($downloadDirectory) && !mkdir($downloadDirectory) || !is_writable($downloadDirectory)) {
$message = $this->maniaControl->getChat()->formatMessage(
'ManiaControl does not have to rights to save maps in %s!',
$downloadDirectory
);
$this->maniaControl->getChat()->sendError($message, $login);
2014-05-02 17:50:30 +02:00
return;
}
2014-10-24 20:28:11 +02:00
if (!@file_put_contents($fullMapFileName, $file)) {
2014-05-02 17:50:30 +02:00
// Save error
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Saving map failed!', $login);
2014-05-02 17:50:30 +02:00
return;
}
} else {
// Write map via write file method
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->writeFile($relativeMapFileName, $file);
2014-05-02 17:50:30 +02:00
} catch (InvalidArgumentException $e) {
if ($e->getMessage() === 'data are too big') {
$this->maniaControl->getChat()->sendError('Map is too big for a remote save!', $login);
2014-05-02 17:50:30 +02:00
return;
}
throw $e;
} catch (FileException $e) {
$this->maniaControl->getChat()->sendError('Could not write file!', $login);
2017-03-16 18:20:30 +01:00
return;
2014-05-02 17:50:30 +02:00
}
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-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->checkMapForCurrentServerParams($relativeMapFileName);
} catch (InvalidMapException $e) {
$this->maniaControl->getChat()->sendException($e, $login);
2014-05-02 17:50:30 +02:00
return;
} catch (FileException $e) {
$this->maniaControl->getChat()->sendException($e, $login);
return;
}catch (FaultException $e){
$this->maniaControl->getChat()->sendException($e, $login);
2017-05-18 17:29:23 +02:00
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
2014-06-23 16:04:00 +02:00
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->insertMap($relativeMapFileName);
} catch (AlreadyInListException $e) {
$this->maniaControl->getChat()->sendException($e, $login);
2014-06-23 16:04:00 +02:00
return;
} catch (InvalidMapException $e) {
$this->maniaControl->getChat()->sendException($e, $login);
if ($e->getMessage() != 'Map lightmap is not up to date. (will still load for now)') {
2017-03-26 19:51:22 +02:00
return;
}
2014-06-23 16:04:00 +02:00
}
2015-01-12 13:22:51 +01:00
2014-05-02 17:50:30 +02:00
$this->updateFullMapList();
2014-04-19 22:44:59 +02:00
2014-05-02 17:50:30 +02:00
// Update Mx MapInfo
2014-08-13 11:05:52 +02:00
$this->maniaControl->getMapManager()->getMXManager()->updateMapObjectsWithManiaExchangeIds(array($mapInfo));
2014-05-02 17:50:30 +02:00
// Update last updated time
2014-05-15 14:42:01 +02:00
$map = $this->getMapByUid($mapInfo->uid);
2017-03-26 20:21:23 +02:00
2014-05-15 14:42:01 +02:00
if (!$map) {
// TODO: improve this - error reports about not existing maps
2014-08-13 11:05:52 +02:00
$this->maniaControl->getErrorHandler()->triggerDebugNotice('Map not in List after Insert!');
$this->maniaControl->getChat()->sendError('Server Error!', $login);
2014-05-15 14:42:01 +02:00
return;
}
2014-05-02 17:50:30 +02:00
$map->lastUpdate = time();
//Update TimeStamp in Database
$this->updateMapTimestamp($mapInfo->uid);
2014-05-02 17:50:30 +02:00
2015-07-11 13:41:46 +02:00
//TODO messages for communication
if ($login) {
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
if (!$update) {
$message = $this->maniaControl->getChat()->formatMessage(
'%s added %s from MX!',
$player,
$map
);
2015-07-11 13:41:46 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
2015-07-11 13:41:46 +02:00
$this->maniaControl->getMapManager()->getMapQueue()->addMapToMapQueue($login, $mapInfo->uid);
} else {
$message = $this->maniaControl->getChat()->formatMessage(
'%s updated %s from MX!',
$player,
$map
);
2015-07-11 13:41:46 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
}
2014-01-14 15:45:36 +01:00
}
2014-01-11 16:34:05 +01:00
}
2014-01-12 20:56:25 +01:00
/**
* Get Map by UID
2014-01-12 20:56:25 +01:00
*
* @param string $uid
* @return Map
2014-01-12 20:56:25 +01:00
*/
2014-05-02 17:50:30 +02:00
public function getMapByUid($uid) {
2014-05-15 14:42:01 +02:00
if (isset($this->maps[$uid])) {
return $this->maps[$uid];
2014-05-02 17:50:30 +02:00
}
2014-05-15 14:42:01 +02:00
return null;
2014-01-12 20:56:25 +01:00
}
2014-01-11 16:34:05 +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() {
$tempList = array();
2014-04-19 22:44:59 +02:00
try {
$offset = 0;
2017-05-13 19:27:38 +02:00
while ($this->maniaControl->getClient() && $offset < 5000) {
2014-08-13 11:05:52 +02:00
$maps = $this->maniaControl->getClient()->getMapList(150, $offset);
2014-04-19 22:44:59 +02:00
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];
2014-05-15 14:42:01 +02:00
} else {
// Insert Map Object
2014-04-19 22:44:59 +02:00
$map = $this->initializeMap($rpcMap);
$tempList[$map->uid] = $map;
}
}
$offset += 150;
}
2017-05-13 19:27:38 +02:00
} catch (IndexOutOfBoundException $e) {
}
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-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_MAPS_UPDATED);
2014-04-19 22:44:59 +02:00
2014-03-31 21:04:15 +02:00
// Write MapList
2015-07-11 13:04:34 +02:00
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_AUTOSAVE_MAPLIST)) {
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WRITE_OWN_MAPLIST_FILE)) {
2014-08-05 02:17:41 +02:00
$serverLogin = $this->maniaControl->getServer()->login;
$matchSettingsFileName = "MatchSettings/{$serverLogin}.txt";
} else {
2014-08-13 11:05:52 +02:00
$matchSettingsFileName = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MAPLIST_FILE);
}
2014-03-13 18:25:29 +01:00
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->saveMatchSettings($matchSettingsFileName);
} catch (FileException $e) {
2016-04-11 17:14:25 +02:00
Logger::logError("Unable to write the playlist file, please checkout your Matchsetting folder File permissions!");
2014-03-13 18:25:29 +01:00
}
2014-03-07 14:43:09 +01:00
}
}
2013-12-15 15:13:56 +01:00
/**
2014-05-02 17:50:30 +02:00
* Initializes a Map
*
* @param mixed $rpcMap
* @return Map
*/
2014-05-02 17:50:30 +02:00
public function initializeMap($rpcMap) {
$map = new Map($this->maniaControl, $rpcMap);
2014-05-02 17:50:30 +02:00
$this->saveMap($map);
return $map;
}
/**
* Save a Map in the Database
*
2014-05-09 16:58:57 +02:00
* @param Map $map
2014-05-02 17:50:30 +02:00
* @return bool
*/
private function saveMap(Map &$map) {
//TODO saveMaps for whole maplist at once (usage of prepared statements)
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-02 17:50:30 +02:00
$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;
}
2014-08-25 17:34:25 +02:00
/**
* Get's a Map by it's Mania-Exchange Id
*
2014-09-01 03:28:51 +02:00
* @param int $mxId
2014-08-25 17:34:25 +02:00
* @return Map
*/
public function getMapByMxId($mxId) {
foreach ($this->maps as $map) {
2014-09-01 03:28:51 +02:00
if ($map->mx && $map->mx->id == $mxId) {
2014-08-25 17:34:25 +02:00
return $map;
}
}
return null;
}
2014-05-02 17:50:30 +02:00
/**
* 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 */
2014-05-02 17:50:30 +02:00
$mapArray[] = $map->fileName;
}
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->chooseNextMapList($mapArray);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
//TODO temp added 19.04.2014
$this->maniaControl->getErrorHandler()->triggerDebugNotice('Exception line 331 MapManager' . $e->getMessage());
trigger_error('Could not shuffle mapList. ' . $e->getMessage());
2014-05-02 17:50:30 +02:00
return false;
}
$this->fetchCurrentMap();
if ($admin) {
$message = $this->maniaControl->getChat()->formatMessage(
'%s shuffled the Maplist!',
$admin
);
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($message);
Logger::logInfo($message, true);
2014-05-02 17:50:30 +02:00
}
// Restructure if needed
$this->restructureMapList();
return true;
}
/**
* Freshly fetch current Map
*
* @return Map
*/
private function fetchCurrentMap() {
2014-06-14 18:16:45 +02:00
try {
2014-08-13 11:05:52 +02:00
$rpcMap = $this->maniaControl->getClient()->getCurrentMapInfo();
2014-06-14 18:16:45 +02:00
} catch (UnavailableFeatureException $exception) {
return null;
}
2014-05-02 17:50:30 +02:00
if (array_key_exists($rpcMap->uId, $this->maps)) {
$this->currentMap = $this->maps[$rpcMap->uId];
$this->currentMap->authorTime = $rpcMap->authorTime;
$this->currentMap->goldTime = $rpcMap->goldTime;
$this->currentMap->silverTime = $rpcMap->silverTime;
$this->currentMap->bronzeTime = $rpcMap->bronzeTime;
2014-05-02 17:50:30 +02:00
$this->currentMap->nbCheckpoints = $rpcMap->nbCheckpoints;
2014-04-19 22:44:59 +02:00
$this->currentMap->nbLaps = $rpcMap->nbLaps;
return $this->currentMap;
2013-12-16 12:16:33 +01:00
}
2014-04-19 22:44:59 +02:00
$this->currentMap = $this->initializeMap($rpcMap);
$this->maps[$this->currentMap->uid] = $this->currentMap;
return $this->currentMap;
}
2014-05-02 17:50:30 +02:00
/**
* Restructures the Maplist
*/
public function restructureMapList() {
2014-06-30 18:48:31 +02:00
$currentIndex = $this->getMapIndex($this->getCurrentMap());
2014-05-02 17:50:30 +02:00
// No RestructureNeeded
if ($currentIndex < Maplist::MAX_MAPS_PER_PAGE - 1) {
return true;
}
$lowerMapArray = array();
$higherMapArray = array();
$index = 0;
2014-05-02 17:50:30 +02:00
foreach ($this->maps as $map) {
if ($index < $currentIndex) {
2014-05-02 17:50:30 +02:00
$lowerMapArray[] = $map->fileName;
} else {
$higherMapArray[] = $map->fileName;
}
$index++;
2014-05-02 17:50:30 +02:00
}
$mapArray = array_merge($higherMapArray, $lowerMapArray);
array_shift($mapArray);
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->chooseNextMapList($mapArray);
2014-05-02 17:50:30 +02:00
} catch (Exception $e) {
trigger_error('Error restructuring the Maplist. ' . $e->getMessage());
2014-05-02 17:50:30 +02:00
return false;
}
return true;
}
2017-03-12 11:26:20 +01:00
/**
* Returns the Map by it's given Array Index
*
* @param int $index The index starts at 0
* @return Map|null
*/
public function getMapByIndex($index) {
$maps = $this->getMaps();
if ($index > sizeof($maps)) {
return null;
} else {
return $maps[$index];
}
}
2014-05-02 17:50:30 +02:00
/**
* Returns the MapIndex of a given map
*
* @param Map $map
2014-05-27 22:32:54 +02:00
* @return int
2014-05-02 17:50:30 +02:00
*/
public function getMapIndex(Map $map) {
$maps = $this->getMaps();
return array_search($map, $maps);
}
/**
* Get all Maps
*
* @param int $offset
* @param int $length
2014-05-27 22:32:54 +02:00
* @return Map[]
2014-05-02 17:50:30 +02:00
*/
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);
}
2014-06-30 18:48:31 +02:00
/**
* Get Current Map
*
* @return Map
*/
public function getCurrentMap() {
if (!$this->currentMap) {
return $this->fetchCurrentMap();
}
return $this->currentMap;
}
/**
* Handle OnInit callback
*/
public function handleOnInit() {
$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();
}
/**
* Handle AfterInit callback
*/
public function handleAfterInit() {
// Fetch MX infos
2014-08-13 11:05:52 +02:00
$this->getMXManager()->fetchManiaExchangeMapInformation();
}
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
2014-06-23 15:45:40 +02:00
* @param string $restart
2013-12-28 19:48:06 +01:00
*/
2014-05-09 12:14:25 +02:00
public function handleScriptBeginMap($mapUid, $restart) {
//TODO remove parseBoolean as soon the mp3 callbacks get removed
2014-06-23 15:45:40 +02:00
$this->beginMap($mapUid, Formatter::parseBoolean($restart));
2013-12-28 19:48:06 +01:00
}
2014-05-09 16:58:57 +02:00
/**
* Manage the Begin of a Map
*
* @param string $uid
* @param bool $restart
*/
private function beginMap($uid, $restart = false) {
2014-05-13 16:03:26 +02:00
//If a restart occurred, first call the endMap to set variables back
2014-05-09 16:58:57 +02:00
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) {
2014-08-13 11:05:52 +02:00
$rpcMap = $this->maniaControl->getClient()->getCurrentMapInfo();
$this->currentMap->authorTime = $rpcMap->authorTime;
$this->currentMap->goldTime = $rpcMap->goldTime;
$this->currentMap->silverTime = $rpcMap->silverTime;
$this->currentMap->bronzeTime = $rpcMap->bronzeTime;
2014-05-09 16:58:57 +02:00
$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.)
2014-08-13 11:05:52 +02:00
$this->getMXManager()->fetchManiaExchangeMapInformation($this->currentMap);
2014-05-09 16:58:57 +02:00
// Trigger own BeginMap callback
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::BEGINMAP, $this->currentMap);
2014-05-09 16:58:57 +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
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::ENDMAP, $this->currentMap);
2014-05-09 16:58:57 +02:00
}
2014-07-05 14:05:23 +02:00
/**
* Fetch a map by its file path
*
* @param string $relativeFileName
* @return Map
*/
2014-07-05 13:58:35 +02:00
public function fetchMapByFileName($relativeFileName) {
2014-08-13 11:05:52 +02:00
$mapInfo = $this->maniaControl->getClient()->getMapInfo($relativeFileName);
2014-07-05 13:58:35 +02:00
if (!$mapInfo) {
2014-07-05 14:05:23 +02:00
return null;
2014-07-05 13:58:35 +02:00
}
return $this->initializeMap($mapInfo);
}
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-05-02 17:50:30 +02:00
* Handle Script EndMap Callback
*/
public function handleScriptEndMap() {
2014-05-09 12:18:25 +02:00
$this->endMap();
}
2014-02-20 13:06:11 +01:00
/**
* Handle EndMap Callback
2014-02-20 13:06:11 +01:00
*
* @param array $callback
*/
public function handleEndMap(array $callback) {
2014-05-09 12:18:25 +02:00
$this->endMap();
}
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();
}
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
}
2015-07-11 13:41:46 +02:00
/**
* Initializes the Communication Listenings
*/
2015-07-11 13:41:46 +02:00
private function initalizeCommunicationListenings() {
// Communication Listenings
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_CURRENT_MAP, $this, function ($data) {
return new CommunicationAnswer($this->getCurrentMap());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP_LIST, $this, function ($data) {
return new CommunicationAnswer($this->getMaps());
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::GET_MAP, $this, function ($data) {
if (!is_object($data)) {
return new CommunicationAnswer("Error in provided Data", true);
}
if (property_exists($data, "mxId")) {
return new CommunicationAnswer($this->getMapByMxId($data->mxId));
} else if (property_exists($data, "mapUid")) {
return new CommunicationAnswer($this->getMapByUid($data->mapUid));
} else {
return new CommunicationAnswer("No mxId or mapUid provided.", true);
}
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::ADD_MAP, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "mxId")) {
return new CommunicationAnswer("No valid mxId provided.", true);
}
$this->addMapFromMx($data->mxId, null);
return new CommunicationAnswer();
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::REMOVE_MAP, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
if (!$this->getMapByUid($data->mapUid)) {
return new CommunicationAnswer("Map not found.", true);
}
$erase = false;
if (property_exists($data, "eraseMapFile")) {
$erase = $data->eraseMapFile;
}
$showMessage = true;
if (property_exists($data, "showChatMessage")) {
$showMessage = $data->showChatMessage;
}
$success = $this->removeMap(null, $data->mapUid, $erase, $showMessage);
return new CommunicationAnswer(array("success" => $success));
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::UPDATE_MAP, $this, function ($data) {
if (!is_object($data) || !property_exists($data, "mapUid")) {
return new CommunicationAnswer("No valid mapUid provided.", true);
}
$this->updateMap(null, $data->mapUid);
return new CommunicationAnswer();
});
}
2017-04-11 15:03:45 +02:00
/**
* Searches the current map list for an author
2017-04-11 15:03:45 +02:00
*
* @param $searchString
* @return array
*/
public function searchMapsByAuthor($searchString) {
return $this->searchMaps($searchString, self::SEARCH_BY_AUTHOR);
}
/**
* Searches the current map list for a map name
*
* @param $searchString
* @return array
*/
public function searchMapsByMapName($searchString) {
return $this->searchMaps($searchString, self::SEARCH_BY_MAP_NAME);
}
/**
* Searches the current map list
*
* @param $searchString
* @param string $searchBy
* @return array
*/
private function searchMaps($searchString, $searchBy = self::SEARCH_BY_MAP_NAME) {
$result = array();
$searchString = strtolower($searchString);
2017-05-06 10:54:19 +02:00
if($searchString == ''){
return $result;
}
foreach ($this->maps as $map) {
switch ($searchBy) {
case self::SEARCH_BY_MAP_NAME:
$mapName = strtolower(Formatter::stripCodes($map->name));
if (strpos($mapName, $searchString) !== false) {
array_push($result, $map);
}
break;
case self::SEARCH_BY_AUTHOR:
if (strpos(strtolower($map->authorLogin), $searchString) !== false) {
array_push($result, $map);
}
2017-04-11 15:03:45 +02:00
}
}
return $result;
}
2015-07-11 13:41:46 +02:00
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_MAPS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`mxid` int(11),
`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,
`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);
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
return $result;
}
2014-05-03 19:53:34 +02:00
}