non working commit help manager

This commit is contained in:
kremsy 2014-01-14 19:47:40 +01:00 committed by Steffen Schröder
parent 476e3464e5
commit 4c0d8270b3
3 changed files with 107 additions and 41 deletions

View File

@ -2,13 +2,14 @@
namespace ManiaControl\Commands; namespace ManiaControl\Commands;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
/** /**
* Class for handling Chat Commands * Class for handling Chat Commands
* *
* @property mixed commandListeners
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
class CommandManager implements CallbackListener { class CommandManager implements CallbackListener {
@ -16,7 +17,7 @@ class CommandManager implements CallbackListener {
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $commandListeners = array(); private $helpManager = array();
private $adminCommandListeners = array(); private $adminCommandListeners = array();
/** /**
@ -27,6 +28,9 @@ class CommandManager implements CallbackListener {
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
//Create help manager instance
$this->helpManager = new HelpManager($this->maniaControl, $this);
// Register for callback // Register for callback
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback');
} }
@ -41,36 +45,39 @@ class CommandManager implements CallbackListener {
* @return bool * @return bool
*/ */
public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false) { public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false) {
if (is_array($commandName)) { if(is_array($commandName)) {
$success = true; $success = true;
foreach ($commandName as $command) { foreach($commandName as $command) {
if (!$this->registerCommandListener($command, $listener, $method, $adminCommand)) { if(!$this->registerCommandListener($command, $listener, $method, $adminCommand)) {
$success = false; $success = false;
} }
} }
return $success; return $success;
} }
$command = strtolower($commandName); $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}')!"); trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
return false; return false;
} }
if ($adminCommand) { if($adminCommand) {
if (!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) { if(!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) {
// Init admin listeners array // Init admin listeners array
$this->adminCommandListeners[$command] = array(); $this->adminCommandListeners[$command] = array();
} }
// Register admin command listener // Register admin command listener
array_push($this->adminCommandListeners[$command], array($listener, $method)); array_push($this->adminCommandListeners[$command], array($listener, $method));
} } else {
else { if(!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
// Init listeners array // Init listeners array
$this->commandListeners[$command] = array(); $this->commandListeners[$command] = array();
} }
// Register command listener // Register command listener
array_push($this->commandListeners[$command], array($listener, $method)); array_push($this->commandListeners[$command], array($listener, $method));
} }
//TODO description
$this->helpManager->registerCommand($command, $adminCommand, '');
return true; return true;
} }
@ -82,17 +89,17 @@ class CommandManager implements CallbackListener {
*/ */
public function unregisterCommandListener(CommandListener $listener) { public function unregisterCommandListener(CommandListener $listener) {
$removed = false; $removed = false;
foreach ($this->commandListeners as &$listeners) { foreach($this->commandListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) { foreach($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] == $listener) { if($listenerCallback[0] == $listener) {
unset($listeners[$key]); unset($listeners[$key]);
$removed = true; $removed = true;
} }
} }
} }
foreach ($this->adminCommandListeners as &$listeners) { foreach($this->adminCommandListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) { foreach($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] == $listener) { if($listenerCallback[0] == $listener) {
unset($listeners[$key]); unset($listeners[$key]);
$removed = true; $removed = true;
} }
@ -108,24 +115,30 @@ class CommandManager implements CallbackListener {
*/ */
public function handleChatCallback(array $callback) { public function handleChatCallback(array $callback) {
// Check for command // Check for command
if (!$callback[1][3]) return; if(!$callback[1][3]) {
return;
}
// Check for valid player // Check for valid player
$login = $callback[1][1]; $login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player) return; if(!$player) {
return;
}
// Parse command // Parse command
$message = $callback[1][2]; $message = $callback[1][2];
$commandArray = explode(' ', $message); $commandArray = explode(' ', $message);
$command = ltrim(strtolower($commandArray[0]), '/'); $command = ltrim(strtolower($commandArray[0]), '/');
if (!$command) return; if(!$command) {
return;
}
if (substr($message, 0, 2) == '//' || $command == 'admin') { if(substr($message, 0, 2) == '//' || $command == 'admin') {
// Admin command // Admin command
$commandListeners = $this->adminCommandListeners; $commandListeners = $this->adminCommandListeners;
if ($command == 'admin') { if($command == 'admin') {
// Strip 'admin' keyword // Strip 'admin' keyword
$command = $commandArray[1]; $command = $commandArray[1];
unset($commandArray[1]); unset($commandArray[1]);
@ -134,23 +147,22 @@ class CommandManager implements CallbackListener {
// Compose uniformed message // Compose uniformed message
$message = '//' . $command; $message = '//' . $command;
foreach ($commandArray as $commandPart) { foreach($commandArray as $commandPart) {
$message .= ' ' . $commandPart; $message .= ' ' . $commandPart;
} }
$callback[1][2] = $message; $callback[1][2] = $message;
} } else {
else {
// User command // User command
$commandListeners = $this->commandListeners; $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 // No command listener registered
return; return;
} }
// Inform command listeners // Inform command listeners
foreach ($commandListeners[$command] as $listener) { foreach($commandListeners[$command] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback, $player); call_user_func(array($listener[0], $listener[1]), $callback, $player);
} }
} }

View File

@ -0,0 +1,53 @@
<?php
namespace ManiaControl\Commands;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
class HelpManager implements CommandListener {
/**
* Private Properties
*/
private $maniaControl = null;
private $playerCommands = array();
private $adminCommands = array();
/**
* Construct a new Commands Manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl, CommandManager $commandManager) {
$this->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));
}
}
}

View File

@ -25,6 +25,7 @@ require_once __DIR__ . '/Callbacks/CallbackManager.php';
require_once __DIR__ . '/Chat.php'; require_once __DIR__ . '/Chat.php';
require_once __DIR__ . '/ColorUtil.php'; require_once __DIR__ . '/ColorUtil.php';
require_once __DIR__ . '/Commands/CommandManager.php'; require_once __DIR__ . '/Commands/CommandManager.php';
require_once __DIR__ . '/Commands/HelpManager.php';
require_once __DIR__ . '/Configurators/Configurator.php'; require_once __DIR__ . '/Configurators/Configurator.php';
require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/Database.php';
require_once __DIR__ . '/FileUtil.php'; require_once __DIR__ . '/FileUtil.php';