From 9e5e4445529854d8d0e6c0e444b7a7d3deaa1db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Tue, 12 Nov 2013 15:48:25 +0100 Subject: [PATCH] - subfolders for different classes - Improved file and class naming - "handler"-> "listener" (callbacks, commands) --- application/ManiaControl.php | 2 +- .../core/Callbacks/CallbackListener.php | 13 + .../CallbackManager.php} | 232 ++++++++++-------- application/core/Commands/CommandListener.php | 13 + .../CommandManager.php} | 60 +++-- .../ManialinkIdHandler.php} | 5 +- .../ManialinkUtil.php} | 2 +- application/core/Maps/Map.php | 84 +++++++ application/core/Maps/MapManager.php | 77 ++++++ .../core/{player.php => Players/Player.php} | 2 +- .../PlayerManager.php} | 60 +++-- .../core/{plugin.php => Plugins/Plugin.php} | 20 +- .../PluginManager.php} | 14 +- .../core/Settings/SettingConfigurator.php | 43 ++++ .../SettingManager.php} | 11 +- application/core/authentication.php | 8 +- application/core/maniaControl.php | 76 +++--- application/core/map.php | 79 ------ application/core/mapHandler.php | 78 ------ application/core/settingConfigurator.php | 21 -- application/plugins/testPlugin.php | 9 +- 21 files changed, 515 insertions(+), 394 deletions(-) create mode 100644 application/core/Callbacks/CallbackListener.php rename application/core/{callbacks.php => Callbacks/CallbackManager.php} (51%) create mode 100644 application/core/Commands/CommandListener.php rename application/core/{commands.php => Commands/CommandManager.php} (89%) rename application/core/{manialinkIdHandler.php => Manialinks/ManialinkIdHandler.php} (92%) rename application/core/{manialinkUtil.php => Manialinks/ManialinkUtil.php} (98%) create mode 100644 application/core/Maps/Map.php create mode 100644 application/core/Maps/MapManager.php rename application/core/{player.php => Players/Player.php} (98%) rename application/core/{playerHandler.php => Players/PlayerManager.php} (77%) rename application/core/{plugin.php => Plugins/Plugin.php} (65%) rename application/core/{pluginHandler.php => Plugins/PluginManager.php} (94%) create mode 100644 application/core/Settings/SettingConfigurator.php rename application/core/{settingManager.php => Settings/SettingManager.php} (96%) delete mode 100644 application/core/map.php delete mode 100644 application/core/mapHandler.php delete mode 100644 application/core/settingConfigurator.php diff --git a/application/ManiaControl.php b/application/ManiaControl.php index ec5e7951..05f300d7 100644 --- a/application/ManiaControl.php +++ b/application/ManiaControl.php @@ -19,7 +19,7 @@ if (!is_dir('logs')) { ini_set('error_log', 'logs/ManiaControl_' . getmypid() . '.log'); // Load ManiaControl class -require_once __DIR__ . '/core/maniaControl.php'; +require_once __DIR__ . '/core/ManiaControl.php'; // Start ManiaControl error_log('Loading ManiaControl v' . ManiaControl::VERSION . '...'); diff --git a/application/core/Callbacks/CallbackListener.php b/application/core/Callbacks/CallbackListener.php new file mode 100644 index 00000000..dacadef4 --- /dev/null +++ b/application/core/Callbacks/CallbackListener.php @@ -0,0 +1,13 @@ + diff --git a/application/core/callbacks.php b/application/core/Callbacks/CallbackManager.php similarity index 51% rename from application/core/callbacks.php rename to application/core/Callbacks/CallbackManager.php index 83bc22ee..44007d9d 100644 --- a/application/core/callbacks.php +++ b/application/core/Callbacks/CallbackManager.php @@ -1,13 +1,17 @@ maniaControl = $maniaControl; - - // Init values $this->last1Second = time(); $this->last5Second = time(); $this->last1Minute = time(); @@ -72,89 +72,24 @@ class Callbacks { } /** - * Perform OnInit callback + * Register a new callback listener + * + * @param string $callbackName + * @param \ManiaControl\Callbacks\CallbackListener $listener + * @param string $method + * @return bool */ - public function onInit() { - // On init callback - $this->triggerCallback(self::CB_MC_ONINIT, array(self::CB_MC_ONINIT)); - - // Simulate begin map - $map = $this->maniaControl->server->getCurrentMap(); - if ($map) { - $this->triggerCallback(self::CB_MC_BEGINMAP, array(self::CB_MC_BEGINMAP, array($map))); + public function registerCallbackListener($callbackName, CallbackListener $listener, $method) { + if (!method_exists($listener, $method)) { + trigger_error( + "Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!"); + return false; } - } - - /** - * Handles the given array of callbacks - */ - public function handleCallbacks() { - // Perform ManiaControl callbacks - if ($this->last1Second <= time() - 1) { - $this->last1Second = time(); - - // 1 second - $this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND)); - - if ($this->last5Second <= time() - 5) { - $this->last5Second = time(); - - // 5 second - $this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND)); - - if ($this->last1Minute <= time() - 60) { - $this->last1Minute = time(); - - // 1 minute - $this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE)); - - if ($this->last3Minute <= time() - 180) { - $this->last3Minute = time(); - - // 3 minute - $this->triggerCallback(self::CB_MC_3_MINUTE, array(self::CB_MC_3_MINUTE)); - } - } - } - } - - // Get server callbacks - if (!$this->maniaControl->client) { - return; - } - $this->maniaControl->client->resetError(); - $this->maniaControl->client->readCB(); - $callbacks = $this->maniaControl->client->getCBResponses(); - if (!is_array($callbacks) || $this->maniaControl->client->isError()) { - trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText()); - return; - } - - // Handle callbacks - foreach ($callbacks as $index => $callback) { - $callbackName = $callback[0]; - switch ($callbackName) { - case self::CB_MP_BEGINMAP: - { - // Map begin - $this->triggerCallback($callbackName, $callback); - $this->triggerCallback(self::CB_MC_BEGINMAP, $callback); - break; - } - case self::CB_MP_ENDMAP: - { - // Map end - $this->triggerCallback($callbackName, $callback); - $this->triggerCallback(self::CB_MC_ENDMAP, $callback); - break; - } - default: - { - $this->triggerCallback($callbackName, $callback); - break; - } - } + if (!array_key_exists($callbackName, $this->callbackListeners)) { + $this->callbackListeners[$callbackName] = array(); } + array_push($this->callbackListeners[$callbackName], array($listener, $method)); + return true; } /** @@ -164,28 +99,119 @@ class Callbacks { * @param array $data */ public function triggerCallback($callbackName, array $callback) { - if (!array_key_exists($callbackName, $this->callbackHandlers)) { + if (!array_key_exists($callbackName, $this->callbackListeners)) { return; } - foreach ($this->callbackHandlers[$callbackName] as $handler) { - call_user_func(array($handler[0], $handler[1]), $callback); + foreach ($this->callbackListeners[$callbackName] as $listener) { + call_user_func(array($listener[0], $listener[1]), $callback); } } /** - * Add a new callback handler + * Trigger internal and manage server callbacks */ - public function registerCallbackHandler($callback, $handler, $method) { - if (!is_object($handler) || !method_exists($handler, $method)) { - trigger_error("Given handler can't handle callback '{$callback}' (no method '{$method}')!"); + public function manageCallbacks() { + $this->manageTimedCallbacks(); + + // Get server callbacks + if (!$this->maniaControl->client) { return; } - if (!array_key_exists($callback, $this->callbackHandlers) || !is_array($this->callbackHandlers[$callback])) { - // Init callback handler array - $this->callbackHandlers[$callback] = array(); + $this->maniaControl->client->readCB(); + $callbacks = $this->maniaControl->client->getCBResponses(); + if (!is_array($callbacks)) { + trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText()); + return; } - // Register callback handler - array_push($this->callbackHandlers[$callback], array($handler, $method)); + + // Handle callbacks + foreach ($callbacks as $index => $callback) { + $callbackName = $callback[0]; + switch ($callbackName) { + case 'ManiaPlanet.BeginMap': + { + $this->triggerCallback(self::CB_MC_BEGINMAP, $callback); + break; + } + case 'ManiaPlanet.EndMap': + { + $this->triggerCallback(self::CB_MC_ENDMAP, $callback); + break; + } + case self::CB_MP_MODESCRIPTCALLBACK: + { + $this->handleScriptCallback($callback); + break; + } + default: + { + var_dump($callback); + $this->triggerCallback($callbackName, $callback); + break; + } + } + } + } + + /** + * Handle the given script callback + * + * @param array $callback + */ + private function handleScriptCallback(array $callback) { + $scriptCallbackName = $callback[1][0]; + switch ($scriptCallbackName) { + case 'EndMap': + { + $this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback); + $this->triggerCallback(self::CB_MC_ENDMAP, $callback); + break; + } + case 'LibXmlRpc_EndMap': + { + $this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback); + $this->triggerCallback(self::CB_MC_ENDMAP, $callback); + break; + } + default: + { + var_dump($callback); + break; + } + } + } + + /** + * Manage recurring timed callbacks + */ + private function manageTimedCallbacks() { + // 1 second + if ($this->last1Second > time() - 1) { + return; + } + $this->last1Second = time(); + $this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND)); + + // 5 second + if ($this->last5Second > time() - 5) { + return; + } + $this->last5Second = time(); + $this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND)); + + // 1 minute + if ($this->last1Minute > time() - 60) { + return; + } + $this->last1Minute = time(); + $this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE)); + + // 3 minute + if ($this->last3Minute > time() - 180) { + return; + } + $this->last3Minute = time(); + $this->triggerCallback(self::CB_MC_3_MINUTE, array(self::CB_MC_3_MINUTE)); } } diff --git a/application/core/Commands/CommandListener.php b/application/core/Commands/CommandListener.php new file mode 100644 index 00000000..b3b0faf2 --- /dev/null +++ b/application/core/Commands/CommandListener.php @@ -0,0 +1,13 @@ + diff --git a/application/core/commands.php b/application/core/Commands/CommandManager.php similarity index 89% rename from application/core/commands.php rename to application/core/Commands/CommandManager.php index 665c11cd..7c5ecd4c 100644 --- a/application/core/commands.php +++ b/application/core/Commands/CommandManager.php @@ -1,6 +1,14 @@ maniaControl = $maniaControl; - // Register for callbacks - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_5_SECOND, $this, 'each5Seconds'); - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_PLAYERCHAT, $this, 'handleChatCallback'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_5_SECOND, $this, 'each5Seconds'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback'); // Register basic commands $commands = array('help', 'version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'getplanets', 'donate', 'pay', 'kick', 'nextmap', 'restartmap', 'addmap', 'removemap'); foreach ($commands as $command) { - $this->registerCommandHandler($command, $this, 'command_' . $command); + $this->registerCommandListener($command, $this, 'command_' . $command); } } /** - * Register a command handler + * Register a command listener * * @param string $commandName - * @param object $handler + * @param CommandListener $listener * @param string $method * @return bool */ - public function registerCommandHandler($commandName, $handler, $method) { + public function registerCommandListener($commandName, CommandListener $listener, $method) { $command = strtolower($commandName); - if (!is_object($handler) || !method_exists($handler, $method)) { - trigger_error("Given handler can't handle command '{$command}' (no method '{$method}')!"); + if (!method_exists($listener, $method)) { + trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!"); return false; } - if (!array_key_exists($command, $this->commandHandlers) || !is_array($this->commandHandlers[$command])) { - // Init handlers array - $this->commandHandlers[$command] = array(); + if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) { + // Init listeners array + $this->commandListeners[$command] = array(); } - // Register command handler - array_push($this->commandHandlers[$command], array($handler, $method)); + // Register command listener + array_push($this->commandListeners[$command], array($listener, $method)); return true; } @@ -68,26 +77,25 @@ class Commands { * @return bool */ public function handleChatCallback(array $callback) { - $chat = $callback[1]; // Check for command - if (!$chat[3]) { + if (!$callback[1][3]) { return false; } // Check for valid player - $player = $this->maniaControl->playerHandler->getPlayer($login); + $player = $this->maniaControl->playerManager->getPlayer($callback[1][1]); if (!$player) { return false; } // Handle command - $command = explode(" ", substr($chat[2], 1)); + $command = explode(" ", substr($callback[1][2], 1)); $command = strtolower($command[0]); - if (!array_key_exists($command, $this->commandHandlers) || !is_array($this->commandHandlers[$command])) { + if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) { // No command handler registered return true; } // Inform command handlers - foreach ($this->commandHandlers[$command] as $handler) { - call_user_func(array($handler[0], $handler[1]), $callback, $player); + foreach ($this->commandListeners[$command] as $listener) { + call_user_func(array($listener[0], $listener[1]), $callback, $player); } return true; } @@ -502,7 +510,7 @@ class Commands { * Handle nextmap command * * @param array $chat - * @param Player $player + * @param \ManiaControl\Players\Player $player * @return bool */ private function command_nextmap(array $chat, Player $player) { diff --git a/application/core/manialinkIdHandler.php b/application/core/Manialinks/ManialinkIdHandler.php similarity index 92% rename from application/core/manialinkIdHandler.php rename to application/core/Manialinks/ManialinkIdHandler.php index dff7ce76..48458163 100644 --- a/application/core/manialinkIdHandler.php +++ b/application/core/Manialinks/ManialinkIdHandler.php @@ -1,6 +1,6 @@ \ No newline at end of file + +?> diff --git a/application/core/manialinkUtil.php b/application/core/Manialinks/ManialinkUtil.php similarity index 98% rename from application/core/manialinkUtil.php rename to application/core/Manialinks/ManialinkUtil.php index 5eca8c28..6feb1732 100644 --- a/application/core/manialinkUtil.php +++ b/application/core/Manialinks/ManialinkUtil.php @@ -1,6 +1,6 @@ maniaControl = $maniaControl; + $this->startTime = time(); + + if (!$rpc_infos) { + return; + } + $this->name = $rpc_infos['Name']; // in aseco stripped new lines on name + $this->uid = $rpc_infos['UId']; + $this->fileName = $rpc_infos['FileName']; + $this->authorLogin = $rpc_infos['Author']; + $this->environment = $rpc_infos['Environnement']; + $this->goldTime = $rpc_infos['GoldTime']; + $this->copperPrice = $rpc_infos['CopperPrice']; + $this->mapType = $rpc_infos['MapType']; + $this->mapStyle = $rpc_infos['MapStyle']; + + $mapsDirectory = $this->maniaControl->server->getMapsDirectory(); + if ($this->maniaControl->server->checkAccess($mapsDirectory)) { + $this->mapFetcher = new \GBXChallMapFetcher(true); + try { + $this->mapFetcher->processFile($mapsDirectory . $this->fileName); + } + catch (Exception $e) { + trigger_error($e->getMessage(), E_USER_WARNING); + } + $this->authorNick = $this->mapFetcher->authorNick; + $this->authorEInfo = $this->mapFetcher->authorEInfo; + $this->authorZone = $this->mapFetcher->authorZone; + $this->comment = $this->mapFetcher->comment; + } + + // TODO: define timeout if mx is down + $serverInfo = $this->maniaControl->server->getSystemInfo(); + $title = strtoupper(substr($serverInfo['TitleId'], 0, 2)); + $this->mx = new \MXInfoFetcher($title, $this->uid, false); + } +} \ No newline at end of file diff --git a/application/core/Maps/MapManager.php b/application/core/Maps/MapManager.php new file mode 100644 index 00000000..f2ee0aab --- /dev/null +++ b/application/core/Maps/MapManager.php @@ -0,0 +1,77 @@ +maniaControl = $maniaControl; + + $this->initTables(); + + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap'); + } + + /** + * Initialize necessary database tables + * + * @return bool + */ + private function initTables() { + // TODO: Initialize database table + return true; + } + + /** + * Add a map to the MapList + * + * @param \ManiaControl\Maps\Map $map + * @return bool + */ + private function addMap(Map $map) { + if (!$map) { + return false; + } + // TODO: Save map in database + $this->mapList[$map->uid] = $map; + return true; + } + + /** + * Handle BeginMap callback + * + * @param array $callback + */ + public function handleBeginMap(array $callback) { + $rpcMap = $this->maniaControl->server->getCurrentMap(); + $map = new Map($this->maniaControl, $rpcMap); + $this->addMap($map); + } +} \ No newline at end of file diff --git a/application/core/player.php b/application/core/Players/Player.php similarity index 98% rename from application/core/player.php rename to application/core/Players/Player.php index ecf2874c..4c5c99e7 100644 --- a/application/core/player.php +++ b/application/core/Players/Player.php @@ -1,6 +1,6 @@ maniaControl = $maniaControl; $this->initTables(); - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_ONINIT, $this, 'onInit'); - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_PLAYERCONNECT, $this, 'playerConnect'); - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_PLAYERDISCONNECT, $this, 'playerDisconnect'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this, + 'playerDisconnect'); } /** @@ -73,17 +81,19 @@ class PlayerHandler { * @param array $callback */ public function onInit(array $callback) { - //register settings - $this->maniaControl->settingManager->initSetting($this, "Leave Join Messages",true); - + // register settings + $this->maniaControl->settingManager->initSetting($this, "Leave Join Messages", true); + $this->maniaControl->client->query('GetPlayerList', 300, 0, 2); $playerList = $this->maniaControl->client->getResponse(); - foreach ($playerList as $player) { - if ($player['PlayerId'] <= 0) { + foreach ($playerList as $playerItem) { + if ($playerItem['PlayerId'] <= 0) { continue; } - $callback = array(Callbacks::CB_MP_PLAYERCONNECT, array($player['Login'])); - $this->playerConnect($callback); + $this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']); + $playerInfo = $this->maniaControl->client->getResponse(); + $player = new Player($playerInfo); + $this->addPlayer($player); } } @@ -98,14 +108,18 @@ class PlayerHandler { $playerInfo = $this->maniaControl->client->getResponse(); $player = new Player($playerInfo); $this->addPlayer($player); - - if($this->maniaControl->settingManager->getSetting($this,"Leave Join Messages")){ - $string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); - $this->maniaControl->chat->sendChat('$ff0'.$string[$player->authLevel].': '. $player->nickname . '$z $ff0Nation:$fff ' . $player->getCountry() . ' $ff0Ladder: $fff' . $player->ladderRank); + + if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_LEAVE_JOIN_MESSAGES)) { + return; } - //TODO: remove $w, $l and stuff out of nick - //TODO: postfix playerConnect callBack as soon as needed - //Todo: Better style colours of the message or anything else + $string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); + $this->maniaControl->chat->sendChat( + '$ff0' . $string[$player->authLevel] . ': ' . $player->nickname . '$z $ff0Nation:$fff ' . $player->getCountry() . + ' $ff0Ladder: $fff' . $player->ladderRank); + + // TODO: remove $w, $l and stuff out of nick + // TODO: postfix playerConnect callBack as soon as needed + // TODO: Better style colours of the message or anything else } /** @@ -116,8 +130,8 @@ class PlayerHandler { public function playerDisconnect(array $callback) { $login = $callback[1][0]; $player = $this->removePlayer($login); - - if($this->maniaControl->settingManager->getSetting($this,"Leave Join Messages")){ + + if ($this->maniaControl->settingManager->getSetting($this, "Leave Join Messages")) { $played = TimeFormatter::formatTime(time() - $player->joinTime); $this->maniaControl->chat->sendChat($player->nickname . '$z $ff0has left the game. Played:$fff ' . $played); } @@ -264,4 +278,4 @@ class PlayerHandler { $playedStatement->close(); return true; } -} \ No newline at end of file +} diff --git a/application/core/plugin.php b/application/core/Plugins/Plugin.php similarity index 65% rename from application/core/plugin.php rename to application/core/Plugins/Plugin.php index 3d113d4b..814a41b3 100644 --- a/application/core/plugin.php +++ b/application/core/Plugins/Plugin.php @@ -1,27 +1,29 @@ maniaControl = $maniaControl; diff --git a/application/core/Settings/SettingConfigurator.php b/application/core/Settings/SettingConfigurator.php new file mode 100644 index 00000000..d01f4122 --- /dev/null +++ b/application/core/Settings/SettingConfigurator.php @@ -0,0 +1,43 @@ +maniaControl = $maniaControl; + + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit'); + + $this->maniaControl->manialinkIdHandler->reserveManiaLinkIds(100); + } + + /** + * Handle OnInit callback + * + * @param array $callback + */ + public function onInit(array $callback) { + // TODO: handle callback + // $this->maniaControl->manialinkUtil-> + // $this->maniaControl->chat->sendChat("test"); + } +} \ No newline at end of file diff --git a/application/core/settingManager.php b/application/core/Settings/SettingManager.php similarity index 96% rename from application/core/settingManager.php rename to application/core/Settings/SettingManager.php index f6883052..ea99ea35 100644 --- a/application/core/settingManager.php +++ b/application/core/Settings/SettingManager.php @@ -2,6 +2,10 @@ namespace ManiaControl; +require_once __DIR__ . '/SettingConfigurator.php'; + +use ManiaControl\Settings\SettingConfigurator; + /** * Class managing settings and configurations * @@ -22,6 +26,7 @@ class SettingManager { * Private properties */ private $maniaControl = null; + private $configurator = null; private $arrayDelimiter = ';;'; /** @@ -33,6 +38,8 @@ class SettingManager { $this->maniaControl = $maniaControl; $this->initTables(); + + $this->configurator = new SettingConfigurator($maniaControl); } /** @@ -187,8 +194,8 @@ class SettingManager { @value := ?, @value ) ON DUPLICATE KEY UPDATE - `type` = VALUE(`type`), - `default` = VALUE(`default`);"; + `type` = VALUES(`type`), + `default` = VALUES(`default`);"; $settingStatement = $mysqli->prepare($settingQuery); if ($mysqli->error) { trigger_error($mysqli->error); diff --git a/application/core/authentication.php b/application/core/authentication.php index e126f472..66601973 100644 --- a/application/core/authentication.php +++ b/application/core/authentication.php @@ -2,6 +2,8 @@ namespace ManiaControl; +use ManiaControl\Players\Player; + /** * Class handling authentication levels * @@ -42,7 +44,7 @@ class Authentication { $mysqli = $this->maniaControl->database->mysqli; // Remove all XSuperadmins - $adminQuery = "UPDATE `" . PlayerHandler::TABLE_PLAYERS . "` + $adminQuery = "UPDATE `" . Players\PlayerManager::TABLE_PLAYERS . "` SET `authLevel` = ? WHERE `authLevel` = ?;"; $adminStatement = $mysqli->prepare($adminQuery); @@ -61,7 +63,7 @@ class Authentication { // Set XSuperAdmins $xAdmins = $config->xsuperadmins->xpath('login'); - $adminQuery = "INSERT INTO `" . PlayerHandler::TABLE_PLAYERS . "` ( + $adminQuery = "INSERT INTO `" . Players\PlayerManager::TABLE_PLAYERS . "` ( `login`, `authLevel` ) VALUES ( @@ -138,7 +140,7 @@ class Authentication { /** * Check if the player has enough rights * - * @param Player $login + * @param \ManiaControl\Players\Player $login * @param int $neededAuthLevel * @return bool */ diff --git a/application/core/maniaControl.php b/application/core/maniaControl.php index d537bfb4..5220eda3 100644 --- a/application/core/maniaControl.php +++ b/application/core/maniaControl.php @@ -2,31 +2,30 @@ namespace ManiaControl; -/** - * Needed includes - * - * @author steeffeen & kremsy - */ -require_once __DIR__ . '/authentication.php'; -require_once __DIR__ . '/callbacks.php'; -require_once __DIR__ . '/chat.php'; -require_once __DIR__ . '/commands.php'; -require_once __DIR__ . '/database.php'; -require_once __DIR__ . '/fileUtil.php'; -require_once __DIR__ . '/manialinkIdHandler.php'; -require_once __DIR__ . '/player.php'; -require_once __DIR__ . '/playerHandler.php'; -require_once __DIR__ . '/plugin.php'; -require_once __DIR__ . '/pluginHandler.php'; -require_once __DIR__ . '/server.php'; -require_once __DIR__ . '/settingManager.php'; -require_once __DIR__ . '/settingConfigurator.php'; -require_once __DIR__ . '/map.php'; -require_once __DIR__ . '/mapHandler.php'; +use ManiaControl\Players\PlayerManager; +use ManiaControl\Callbacks\CallbackManager; +use ManiaControl\Commands\CommandManager; +use ManiaControl\Manialinks\ManialinkIdHandler; +use ManiaControl\Maps\MapManager; +use ManiaControl\Plugins\PluginManager; + +require_once __DIR__ . '/Authentication.php'; +require_once __DIR__ . '/Callbacks/CallbackManager.php'; +require_once __DIR__ . '/Chat.php'; +require_once __DIR__ . '/Commands/CommandManager.php'; +require_once __DIR__ . '/Database.php'; +require_once __DIR__ . '/FileUtil.php'; +require_once __DIR__ . '/Manialinks/ManialinkIdHandler.php'; +require_once __DIR__ . '/Manialinks/ManialinkUtil.php'; +require_once __DIR__ . '/Maps/Map.php'; +require_once __DIR__ . '/Maps/MapManager.php'; +require_once __DIR__ . '/Players/PlayerManager.php'; +require_once __DIR__ . '/Plugins/PluginManager.php'; +require_once __DIR__ . '/Server.php'; +require_once __DIR__ . '/Settings/SettingManager.php'; require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfosearcher.inc.php'; - list($endiantest) = array_values(unpack('L1L', pack('V', 1))); if ($endiantest == 1) { require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php'; @@ -38,7 +37,7 @@ else { /** * ManiaControl Server Controller for ManiaPlanet Server * - * @author steeffeen and Lukas + * @author steeffeen & kremsy */ class ManiaControl { /** @@ -52,17 +51,19 @@ class ManiaControl { * Public properties */ public $authentication = null; - public $callbacks = null; + public $callbackManager = null; public $chat = null; public $client = null; - public $commands = null; + public $commandManager = null; public $database = null; public $manialinkIdHandler = null; - public $playerHandler = null; - public $pluginHandler = null; + public $mapManager = null; + public $playerManager = null; + public $pluginManager = null; public $server = null; public $settingConfigurator = null; public $settingManager = null; + /** * Private properties */ @@ -73,17 +74,16 @@ class ManiaControl { */ public function __construct() { $this->database = new Database($this); + $this->callbackManager = new CallbackManager($this); + $this->manialinkIdHandler = new ManialinkIdHandler(); $this->settingManager = new SettingManager($this); $this->chat = new Chat($this); - $this->callbacks = new Callbacks($this); $this->server = new Server($this); $this->authentication = new Authentication($this); - $this->playerHandler = new PlayerHandler($this); - $this->mapHandler = new MapHandler($this); - $this->manialinkIdHandler = new ManialinkIdHandler(); - $this->commands = new Commands($this); - $this->pluginHandler = new PluginHandler($this); - $this->settingConfigurator = new SettingConfigurator($this); + $this->playerManager = new PlayerManager($this); + $this->mapManager = new MapManager($this); + $this->commandManager = new CommandManager($this); + $this->pluginManager = new PluginManager($this); } /** @@ -134,7 +134,7 @@ class ManiaControl { error_log('Starting ManiaControl v' . self::VERSION . '!'); // Load plugins - $this->pluginHandler->loadPlugins(); + $this->pluginManager->loadPlugins(); // Connect to server $this->connect(); @@ -146,7 +146,7 @@ class ManiaControl { $this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!'); // OnInit - $this->callbacks->onInit(); + $this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONINIT, array(CallbackManager::CB_MC_ONINIT)); // Main loop while (!$this->shutdownRequested) { @@ -155,8 +155,8 @@ class ManiaControl { // Disable script timeout set_time_limit(30); - // Handle server callbacks - $this->callbacks->handleCallbacks(); + // Manager callbacks + $this->callbackManager->manageCallbacks(); // Yield for next tick $loopEnd = microtime(true); diff --git a/application/core/map.php b/application/core/map.php deleted file mode 100644 index 5c2813c9..00000000 --- a/application/core/map.php +++ /dev/null @@ -1,79 +0,0 @@ -maniaControl = $maniaControl; - - if ($rpc_infos) { - $this->name = $rpc_infos['Name']; //in aseco stripped new lines on name - $this->uid = $rpc_infos['UId']; - $this->fileName = $rpc_infos['FileName']; - $this->authorLogin = $rpc_infos['Author']; - $this->environment = $rpc_infos['Environnement']; - $this->goldTime = $rpc_infos['GoldTime']; - $this->copperPrice = $rpc_infos['CopperPrice']; - $this->mapType = $rpc_infos['MapType']; - $this->mapStyle = $rpc_infos['MapStyle']; - - $this->mapFetcher = new \GBXChallMapFetcher(true); - - try{ - $this->mapFetcher->processFile($this->maniaControl->server->getMapsDirectory() . $this->fileName); - } catch (Exception $e){ - trigger_error($e->getMessage(), E_USER_WARNING); - } - $this->authorNick = $this->mapFetcher->authorNick; - $this->authorEInfo = $this->mapFetcher->authorEInfo; - $this->authorZone = $this->mapFetcher->authorZone; - $this->comment = $this->mapFetcher->comment; - //additional properties anyway in the mapfetcher object - - //TODO: change to SM to gameerkennung - //TODO: define timeout if mx is down - $this->mx = new \MXInfoFetcher('SM', $this->uid, false); //SM -> change to gameerkennung - } else { - $this->name = 'undefined'; - } - - $this->starttime = time(); - } -} \ No newline at end of file diff --git a/application/core/mapHandler.php b/application/core/mapHandler.php deleted file mode 100644 index 8cb3638f..00000000 --- a/application/core/mapHandler.php +++ /dev/null @@ -1,78 +0,0 @@ -maniaControl = $maniaControl; - - $this->initTables(); - - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_ONINIT, $this, 'onInit'); - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_BEGINMAP, $this, 'beginMap'); - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_ENDMAP, $this, 'endMap'); - } - - /** - * Initialize all necessary tables - * - * @return bool - */ - private function initTables() { - //TODO: Initialize database table - - } - - - /** - * Handle OnInit callback - * - * @param array $callback - */ - public function onInit(){ - $this->maniaControl->client->query('GetMapList', 300, 0); - $mapList = $this->maniaControl->client->getResponse(); - foreach ($mapList as $map) { - $map = new Map($this->maniaControl, $map); - $this->addMap($map); - } - } - - /** - * Add a map to the MapList - * - * @param Map $map - * @return bool - */ - private function addMap(Map $map) { - //TODO: ADD Maps to database - if (!$map) { - return false; - } - $this->mapList[$map->uid] = $map; - return true; - } - -} \ No newline at end of file diff --git a/application/core/settingConfigurator.php b/application/core/settingConfigurator.php deleted file mode 100644 index 02f88920..00000000 --- a/application/core/settingConfigurator.php +++ /dev/null @@ -1,21 +0,0 @@ -maniaControl = $maniaControl; - $this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_ONINIT, $this, 'onInit'); - $this->maniaControl->manialinkIdHandler->reserveManiaLinkIds(100); - } - - public function onInit(array $callback){ - // $this->maniaControl->manialinkUtil-> - // $this->maniaControl->chat->sendChat("test"); - } -} \ No newline at end of file diff --git a/application/plugins/testPlugin.php b/application/plugins/testPlugin.php index 622d409e..d0db4962 100644 --- a/application/plugins/testPlugin.php +++ b/application/plugins/testPlugin.php @@ -1,7 +1,12 @@