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;
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);
}
}

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__ . '/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';