From 4c0d8270b3c9b1d046f8703a39494f63d572bdf5 Mon Sep 17 00:00:00 2001 From: kremsy Date: Tue, 14 Jan 2014 19:47:40 +0100 Subject: [PATCH] non working commit help manager --- application/core/Commands/CommandManager.php | 94 +++++++++++--------- application/core/Commands/HelpManager.php | 53 +++++++++++ application/core/ManiaControl.php | 1 + 3 files changed, 107 insertions(+), 41 deletions(-) create mode 100644 application/core/Commands/HelpManager.php diff --git a/application/core/Commands/CommandManager.php b/application/core/Commands/CommandManager.php index 6921b670..94d09fd9 100644 --- a/application/core/Commands/CommandManager.php +++ b/application/core/Commands/CommandManager.php @@ -2,13 +2,14 @@ namespace ManiaControl\Commands; -use ManiaControl\ManiaControl; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; +use ManiaControl\ManiaControl; /** * Class for handling Chat Commands * + * @property mixed commandListeners * @author steeffeen & kremsy */ class CommandManager implements CallbackListener { @@ -16,7 +17,7 @@ class CommandManager implements CallbackListener { * Private Properties */ private $maniaControl = null; - private $commandListeners = array(); + private $helpManager = array(); private $adminCommandListeners = array(); /** @@ -26,7 +27,10 @@ class CommandManager implements CallbackListener { */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; - + + //Create help manager instance + $this->helpManager = new HelpManager($this->maniaControl, $this); + // Register for callback $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback'); } @@ -34,43 +38,46 @@ class CommandManager implements CallbackListener { /** * Register a command listener * - * @param string $commandName + * @param string $commandName * @param CommandListener $listener - * @param string $method - * @param bool $adminCommand + * @param string $method + * @param bool $adminCommand * @return bool */ public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false) { - if (is_array($commandName)) { + if(is_array($commandName)) { $success = true; - foreach ($commandName as $command) { - if (!$this->registerCommandListener($command, $listener, $method, $adminCommand)) { + foreach($commandName as $command) { + if(!$this->registerCommandListener($command, $listener, $method, $adminCommand)) { $success = false; } } return $success; } $command = strtolower($commandName); - if (!method_exists($listener, $method)) { + if(!method_exists($listener, $method)) { trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!"); return false; } - if ($adminCommand) { - if (!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) { + if($adminCommand) { + if(!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) { // Init admin listeners array $this->adminCommandListeners[$command] = array(); } // Register admin command listener array_push($this->adminCommandListeners[$command], array($listener, $method)); - } - else { - if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) { + } else { + if(!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) { // Init listeners array $this->commandListeners[$command] = array(); } // Register command listener array_push($this->commandListeners[$command], array($listener, $method)); } + + //TODO description + $this->helpManager->registerCommand($command, $adminCommand, ''); + return true; } @@ -82,17 +89,17 @@ class CommandManager implements CallbackListener { */ public function unregisterCommandListener(CommandListener $listener) { $removed = false; - foreach ($this->commandListeners as &$listeners) { - foreach ($listeners as $key => &$listenerCallback) { - if ($listenerCallback[0] == $listener) { + foreach($this->commandListeners as &$listeners) { + foreach($listeners as $key => &$listenerCallback) { + if($listenerCallback[0] == $listener) { unset($listeners[$key]); $removed = true; } } } - foreach ($this->adminCommandListeners as &$listeners) { - foreach ($listeners as $key => &$listenerCallback) { - if ($listenerCallback[0] == $listener) { + foreach($this->adminCommandListeners as &$listeners) { + foreach($listeners as $key => &$listenerCallback) { + if($listenerCallback[0] == $listener) { unset($listeners[$key]); $removed = true; } @@ -108,49 +115,54 @@ class CommandManager implements CallbackListener { */ public function handleChatCallback(array $callback) { // Check for command - if (!$callback[1][3]) return; - + if(!$callback[1][3]) { + return; + } + // Check for valid player - $login = $callback[1][1]; + $login = $callback[1][1]; $player = $this->maniaControl->playerManager->getPlayer($login); - if (!$player) return; - + if(!$player) { + return; + } + // Parse command - $message = $callback[1][2]; + $message = $callback[1][2]; $commandArray = explode(' ', $message); - $command = ltrim(strtolower($commandArray[0]), '/'); - if (!$command) return; - - if (substr($message, 0, 2) == '//' || $command == 'admin') { + $command = ltrim(strtolower($commandArray[0]), '/'); + if(!$command) { + return; + } + + if(substr($message, 0, 2) == '//' || $command == 'admin') { // Admin command $commandListeners = $this->adminCommandListeners; - - if ($command == 'admin') { + + if($command == 'admin') { // Strip 'admin' keyword $command = $commandArray[1]; unset($commandArray[1]); } unset($commandArray[0]); - + // Compose uniformed message $message = '//' . $command; - foreach ($commandArray as $commandPart) { + foreach($commandArray as $commandPart) { $message .= ' ' . $commandPart; } $callback[1][2] = $message; - } - else { + } else { // User command $commandListeners = $this->commandListeners; } - - if (!array_key_exists($command, $commandListeners) || !is_array($commandListeners[$command])) { + + if(!array_key_exists($command, $commandListeners) || !is_array($commandListeners[$command])) { // No command listener registered return; } - + // Inform command listeners - foreach ($commandListeners[$command] as $listener) { + foreach($commandListeners[$command] as $listener) { call_user_func(array($listener[0], $listener[1]), $callback, $player); } } diff --git a/application/core/Commands/HelpManager.php b/application/core/Commands/HelpManager.php new file mode 100644 index 00000000..b3b577e6 --- /dev/null +++ b/application/core/Commands/HelpManager.php @@ -0,0 +1,53 @@ +maniaControl = $maniaControl; + + //Register the help command + $commandManager->registerCommandListener('help', $this, 'command_playerHelp', false); + } + + + public function command_playerHelp(array $chat, Player $player) { + $string = ''; + + foreach($this->playerCommands as $key => $value) { + var_dump($key, $value); + } + } + + /** + * Registers a new Command + * + * @param $name + * @param $adminCommand + * @param $description + */ + public function registerCommand($name, $adminCommand = false, $description = '') { + if($adminCommand) { + array_push($this->adminCommands, array("Name" => $name, "Description" => $description)); + } else { + array_push($this->playerCommands, array("Name" => $name, "Description" => $description)); + } + } + +} \ No newline at end of file diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 5b953c40..54148316 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -25,6 +25,7 @@ require_once __DIR__ . '/Callbacks/CallbackManager.php'; require_once __DIR__ . '/Chat.php'; require_once __DIR__ . '/ColorUtil.php'; require_once __DIR__ . '/Commands/CommandManager.php'; +require_once __DIR__ . '/Commands/HelpManager.php'; require_once __DIR__ . '/Configurators/Configurator.php'; require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/FileUtil.php';