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
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
|
|
|
|
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;
|
|
|
|
$this->initTables();
|
2013-11-12 16:52:23 +01:00
|
|
|
$this->mapCommands = new MapCommands($maniaControl);
|
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;
|
|
|
|
$mapsTableQuery = "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;";
|
|
|
|
$mapsTableStatement = $mysqli->prepare($mapsTableQuery);
|
|
|
|
if ($mysqli->error) {
|
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$mapsTableStatement->execute();
|
|
|
|
if ($mapsTableStatement->error) {
|
|
|
|
trigger_error($mapsTableStatement->error, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$mapsTableStatement->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save map to the database
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\Maps\Map $map
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function saveMap(Map $map) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
$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-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);
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|