- added map commands class

- added saving of map in database
This commit is contained in:
Steffen Schröder 2013-11-12 16:52:23 +01:00
parent 9bb0d0bd45
commit 26c3dc3bcb
8 changed files with 276 additions and 179 deletions

View File

@ -145,7 +145,6 @@ class CallbackManager {
}
default:
{
var_dump($callback);
$this->triggerCallback($callbackName, $callback);
break;
}
@ -175,7 +174,6 @@ class CallbackManager {
}
default:
{
var_dump($callback);
break;
}
}

View File

@ -41,7 +41,7 @@ class CommandManager implements CallbackListener, CommandListener {
// Register basic commands
$commands = array('help', 'version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'getplanets', 'donate',
'pay', 'kick', 'nextmap', 'restartmap', 'addmap', 'removemap');
'pay', 'kick');
foreach ($commands as $command) {
$this->registerCommandListener($command, $this, 'command_' . $command);
}
@ -387,155 +387,6 @@ class CommandManager implements CallbackListener, CommandListener {
return $this->maniaControl->client->query('Kick', $target->login, $message);
}
/**
* Handle removemap command
*
* @param array $chat
* @param Player $player
* @return bool
*/
private function command_removemap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
// Get map name
$map = $this->maniaControl->server->getMap();
if (!$map) {
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
return false;
}
$mapName = $map['FileName'];
// Remove map
if (!$this->maniaControl->client->query('RemoveMap', $mapName)) {
trigger_error("Couldn't remove current map. " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
return false;
}
$this->maniaControl->chat->sendSuccess('Map removed.', $player->login);
return true;
}
/**
* Handle addmap command
*
* @param array $chat
* @param Player $player
* @return bool
*/
private function command_addmap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
$params = explode(' ', $chat[1][2], 2);
if (count($params) < 2) {
// TODO: show usage
return false;
}
// 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.", $player->login);
return false;
}
$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.", $player->login);
return false;
}
$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.", $player->login);
return false;
}
$mapDir .= $downloadDirectory . '/';
// Download the map
$mapId = $params[1];
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!', $player->login);
return false;
}
$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!', $player->login);
return false;
}
// Save map
$fileName = $mapDir . $mapInfo['TrackID'] . '_' . $mapInfo['Name'] . '.Map.Gbx';
if (!file_put_contents($fileName, $file)) {
// Save error
$this->maniaControl->chat->sendError('Saving map failed!', $player->login);
return false;
}
// Check for valid map
if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $fileName)) {
trigger_error("Couldn't check if map is valid. " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError('Error checking map!', $player->login);
return false;
}
$response = $this->maniaControl->client->getResponse();
if (!$response) {
// Inalid map type
$this->maniaControl->chat->sendError("Invalid map type.", $player->login);
return false;
}
// Add map to map list
if (!$this->maniaControl->client->query('InsertMap', $fileName)) {
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $player->login);
return false;
}
$this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!');
return true;
}
// TODO: add local map by filename
// TODO: load map from direct url
}
/**
* Handle nextmap command
*
* @param array $chat
* @param \ManiaControl\Players\Player $player
* @return bool
*/
private function command_nextmap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
return $this->maniaControl->client->query('NextMap');
}
/**
* Handle retartmap command
*
* @param array $chat
* @param Player $player
* @return bool
*/
private function command_restartmap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
return $this->maniaControl->client->query('RestartMap');
}
/**
* Handle setservername command
*

View File

@ -81,8 +81,8 @@ class ManiaControl {
$this->server = new Server($this);
$this->authentication = new Authentication($this);
$this->playerManager = new PlayerManager($this);
$this->mapManager = new MapManager($this);
$this->commandManager = new CommandManager($this);
$this->mapManager = new MapManager($this);
$this->pluginManager = new PluginManager($this);
}

View File

@ -0,0 +1,184 @@
<?php
namespace ManiaControl\Maps;
use ManiaControl\Authentication;
use ManiaControl\ManiaControl;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Players\Player;
/**
* Class offering commands to manage maps
*
* @author steeffeen & kremsy
*/
class MapCommands implements CommandListener {
/**
* Private properties
*/
private $maniaControl = null;
/**
* Create MapCommands instance
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->maniaControl->commandManager->registerCommandListener('nextmap', $this, 'command_NextMap');
$this->maniaControl->commandManager->registerCommandListener('restartmap', $this, 'command_RestartMap');
$this->maniaControl->commandManager->registerCommandListener('addmap', $this, 'command_AddMap');
$this->maniaControl->commandManager->registerCommandListener('removemap', $this, 'command_RemoveMap');
}
/**
* Handle removemap command
*
* @param array $chat
* @param \ManiaControl\Players\Player $player
* @return bool
*/
public function command_RemoveMap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
// Get map name
$map = $this->maniaControl->server->getMap();
if (!$map) {
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
return false;
}
$mapName = $map['FileName'];
// Remove map
if (!$this->maniaControl->client->query('RemoveMap', $mapName)) {
trigger_error("Couldn't remove current map. " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("Couldn't remove map.", $player->login);
return false;
}
$this->maniaControl->chat->sendSuccess('Map removed.', $player->login);
return true;
}
/**
* Handle addmap command
*
* @param array $chat
* @param \ManiaControl\Players\Player $player
* @return bool
*/
public function command_AddMap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
$params = explode(' ', $chat[1][2], 2);
if (count($params) < 2) {
// TODO: show usage
return false;
}
// 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.", $player->login);
return false;
}
$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.", $player->login);
return false;
}
$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.", $player->login);
return false;
}
$mapDir .= $downloadDirectory . '/';
// Download the map
$mapId = $params[1];
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!', $player->login);
return false;
}
$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!', $player->login);
return false;
}
// Save map
$fileName = $mapDir . $mapInfo['TrackID'] . '_' . $mapInfo['Name'] . '.Map.Gbx';
if (!file_put_contents($fileName, $file)) {
// Save error
$this->maniaControl->chat->sendError('Saving map failed!', $player->login);
return false;
}
// Check for valid map
if (!$this->maniaControl->client->query('CheckMapForCurrentServerParams', $fileName)) {
trigger_error("Couldn't check if map is valid. " . $this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError('Error checking map!', $player->login);
return false;
}
$response = $this->maniaControl->client->getResponse();
if (!$response) {
// Inalid map type
$this->maniaControl->chat->sendError("Invalid map type.", $player->login);
return false;
}
// Add map to map list
if (!$this->maniaControl->client->query('InsertMap', $fileName)) {
$this->maniaControl->chat->sendError("Couldn't add map to match settings!", $player->login);
return false;
}
$this->maniaControl->chat->sendSuccess('Map $<' . $mapInfo['Name'] . '$> added!');
return true;
}
// TODO: add local map by filename
}
/**
* Handle nextmap command
*
* @param array $chat
* @param \ManiaControl\Players\Player $player
* @return bool
*/
public function command_NextMap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
return $this->maniaControl->client->query('NextMap');
}
/**
* Handle retartmap command
*
* @param array $chat
* @param \ManiaControl\Players\Player $player
* @return bool
*/
public function command_RestartMap(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_OPERATOR)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
return $this->maniaControl->client->query('RestartMap');
}
}
?>

View File

@ -3,22 +3,30 @@
namespace ManiaControl\Maps;
require_once __DIR__ . '/Map.php';
require_once __DIR__ . '/MapCommands.php';
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
// TODO: xlist command
/**
* Manager for maps
*
* @author kremsy & steeffeen
*/
class MapManager implements CallbackListener {
/**
* Constants
*/
const TABLE_MAPS = 'mc_maps';
/**
* Private properties
*/
private $maniaControl = null;
private $mapCommands = null;
private $mapList = array();
/**
@ -26,15 +34,10 @@ class MapManager implements CallbackListener {
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
// TODO: database init
// TODO: erasemap from server
// TODO: implement of a method which are called by xlist command and results maplists from maniaexcahnge (or extra class for it)
// TODO: admin add from maniaexchange, would handle it here
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
$this->mapCommands = new MapCommands($maniaControl);
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap');
}
@ -45,7 +48,65 @@ class MapManager implements CallbackListener {
* @return bool
*/
private function initTables() {
// TODO: Initialize database table
$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();
return true;
}
@ -56,22 +117,36 @@ class MapManager implements CallbackListener {
* @return bool
*/
private function addMap(Map $map) {
if (!$map) {
return false;
}
// TODO: Save map in database
$this->saveMap($map);
$this->mapList[$map->uid] = $map;
return true;
}
/**
* 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;
}
/**
* Handle BeginMap callback
*
* @param array $callback
*/
public function handleBeginMap(array $callback) {
$rpcMap = $this->maniaControl->server->getCurrentMap();
$map = new Map($this->maniaControl, $rpcMap);
$map = $this->getCurrentMap();
if (!$map) {
return;
}
$this->addMap($map);
}
}

View File

@ -88,6 +88,7 @@ class PluginManager {
$pluginStatement->execute();
if ($pluginStatement->error) {
trigger_error($pluginStatement->error);
$pluginStatement->close();
return false;
}
$pluginStatement->close();

View File

@ -266,19 +266,6 @@ class Server {
return $ghostReplay;
}
/**
* Fetch current map
*
* @return array
*/
public function getCurrentMap() {
if (!$this->maniaControl->client->query('GetCurrentMapInfo')) {
trigger_error("Couldn't fetch map info. " . $this->maniaControl->getClientErrorText());
return null;
}
return $this->maniaControl->client->getResponse();
}
/**
* Waits for the server to have the given status
*

View File

@ -275,6 +275,7 @@ class SettingManager {
$settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$settingStatement->close();