2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
namespace ManiaControl\Commands;
|
|
|
|
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2014-01-14 19:47:40 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-06 16:51:57 +01:00
|
|
|
* Class for handling Chat Commands
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2014-05-02 17:50:30 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
|
|
* @copyright 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-19 20:29:37 +01:00
|
|
|
class CommandManager implements CallbackListener {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-01-06 16:51:57 +01:00
|
|
|
* Private Properties
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 14:56:37 +01:00
|
|
|
private $maniaControl = null;
|
2014-01-14 19:47:40 +01:00
|
|
|
private $helpManager = array();
|
2014-05-13 01:18:47 +02:00
|
|
|
// TODO: use listening class
|
2013-12-09 22:12:57 +01:00
|
|
|
private $adminCommandListeners = array();
|
2014-01-14 20:02:35 +01:00
|
|
|
private $commandListeners = array();
|
2013-12-14 23:27:15 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 16:51:57 +01:00
|
|
|
* Construct a new Commands Manager
|
2013-11-12 15:48:25 +01:00
|
|
|
*
|
2014-01-06 16:51:57 +01:00
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 14:56:37 +01:00
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2014-01-14 19:47:40 +01:00
|
|
|
|
|
|
|
//Create help manager instance
|
2014-01-14 20:02:35 +01:00
|
|
|
$this->helpManager = new HelpManager($this->maniaControl);
|
2014-01-14 19:47:40 +01:00
|
|
|
|
2014-01-06 16:51:57 +01:00
|
|
|
// Register for callback
|
2013-11-12 15:48:25 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback');
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 01:18:47 +02:00
|
|
|
* Register a Command Listener
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2014-01-14 19:47:40 +01:00
|
|
|
* @param string $commandName
|
2014-01-06 16:51:57 +01:00
|
|
|
* @param CommandListener $listener
|
2014-01-14 19:47:40 +01:00
|
|
|
* @param string $method
|
|
|
|
* @param bool $adminCommand
|
2014-05-01 01:41:19 +02:00
|
|
|
* @param string $description
|
2013-11-10 14:56:37 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2014-05-15 15:13:58 +02:00
|
|
|
public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false, $description = null) {
|
2014-03-13 18:47:40 +01:00
|
|
|
if (is_array($commandName)) {
|
2014-01-06 18:32:36 +01:00
|
|
|
$success = true;
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($commandName as $command) {
|
2014-05-01 01:41:19 +02:00
|
|
|
if (!$this->registerCommandListener($command, $listener, $method, $adminCommand, $description)) {
|
2014-01-06 18:32:36 +01:00
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $success;
|
|
|
|
}
|
2014-05-13 00:22:47 +02:00
|
|
|
|
|
|
|
$command = strtolower($commandName);
|
|
|
|
$listenerCallback = array($listener, $method);
|
|
|
|
$listenerClass = get_class($listener);
|
|
|
|
|
|
|
|
if (!is_callable($listenerCallback)) {
|
|
|
|
trigger_error("Given Listener '{$listenerClass}' can't handle Command '{$command}'! No callable Method '{$method}'!");
|
2013-11-10 14:56:37 +01:00
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-05-13 00:22:47 +02:00
|
|
|
|
2014-03-13 18:47:40 +01:00
|
|
|
if ($adminCommand) {
|
2014-05-13 00:22:47 +02:00
|
|
|
$this->addListenerCallback($this->adminCommandListeners, $listenerCallback, $command);
|
2014-01-14 19:47:40 +01:00
|
|
|
} else {
|
2014-05-13 00:22:47 +02:00
|
|
|
$this->addListenerCallback($this->commandListeners, $listenerCallback, $command);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-01-14 19:47:40 +01:00
|
|
|
|
2014-05-15 15:13:58 +02:00
|
|
|
// TODO: description(?)
|
|
|
|
if ($description) {
|
|
|
|
$this->helpManager->registerCommand($command, $adminCommand, $description, get_class($listener) . '\\' . $method);
|
|
|
|
}
|
2014-01-14 19:47:40 +01:00
|
|
|
|
2013-11-10 14:56:37 +01:00
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:22:47 +02:00
|
|
|
/**
|
|
|
|
* Add a Listener Callback to the given Listener Array
|
|
|
|
*
|
2014-05-13 16:00:25 +02:00
|
|
|
* @param array $listenerArray
|
|
|
|
* @param callable $listenerCallback
|
|
|
|
* @param string $command
|
2014-05-13 00:22:47 +02:00
|
|
|
*/
|
2014-05-13 16:00:25 +02:00
|
|
|
private function addListenerCallback(array &$listenerArray, callable $listenerCallback, $command) {
|
2014-05-13 00:22:47 +02:00
|
|
|
if (!array_key_exists($command, $listenerArray) || !is_array($listenerArray[$command])) {
|
|
|
|
// Init listeners array
|
|
|
|
$listenerArray[$command] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register command listener
|
|
|
|
array_push($listenerArray[$command], $listenerCallback);
|
|
|
|
}
|
|
|
|
|
2013-12-14 23:27:15 +01:00
|
|
|
/**
|
2014-05-13 01:18:47 +02:00
|
|
|
* Unregister a Command Listener
|
2014-01-06 16:51:57 +01:00
|
|
|
*
|
|
|
|
* @param CommandListener $listener
|
2013-12-14 23:27:15 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function unregisterCommandListener(CommandListener $listener) {
|
|
|
|
$removed = false;
|
2014-05-13 00:22:47 +02:00
|
|
|
if ($this->removeCommandListener($this->commandListeners, $listener)) {
|
|
|
|
$removed = true;
|
2013-12-14 23:27:15 +01:00
|
|
|
}
|
2014-05-13 00:22:47 +02:00
|
|
|
if ($this->removeCommandListener($this->adminCommandListeners, $listener)) {
|
|
|
|
$removed = true;
|
|
|
|
}
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the Command Listener from the given Listeners Array
|
|
|
|
*
|
|
|
|
* @param array $listenerArray
|
|
|
|
* @param CommandListener $listener
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function removeCommandListener(array &$listenerArray, CommandListener $listener) {
|
|
|
|
$removed = false;
|
|
|
|
foreach ($listenerArray as &$listeners) {
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($listeners as $key => &$listenerCallback) {
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($listenerCallback[0] === $listener) {
|
2013-12-14 23:27:15 +01:00
|
|
|
unset($listeners[$key]);
|
|
|
|
$removed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2014-01-06 16:51:57 +01:00
|
|
|
* Handle Chat Callback
|
2013-11-10 14:56:37 +01:00
|
|
|
*
|
2014-01-06 16:51:57 +01:00
|
|
|
* @param array $callback
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 14:56:37 +01:00
|
|
|
public function handleChatCallback(array $callback) {
|
2013-11-09 17:24:03 +01:00
|
|
|
// Check for command
|
2014-03-13 18:47:40 +01:00
|
|
|
if (!$callback[1][3]) {
|
2014-01-14 19:47:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
// Check for valid player
|
2014-01-14 19:47:40 +01:00
|
|
|
$login = $callback[1][1];
|
2014-01-06 16:51:57 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2014-03-13 18:47:40 +01:00
|
|
|
if (!$player) {
|
2014-01-14 19:47:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-06 16:51:57 +01:00
|
|
|
// Parse command
|
2014-01-14 19:47:40 +01:00
|
|
|
$message = $callback[1][2];
|
2014-01-06 16:51:57 +01:00
|
|
|
$commandArray = explode(' ', $message);
|
2014-01-14 19:47:40 +01:00
|
|
|
$command = ltrim(strtolower($commandArray[0]), '/');
|
2014-03-13 18:47:40 +01:00
|
|
|
if (!$command) {
|
2014-01-14 19:47:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-14 14:32:29 +02:00
|
|
|
if (substr($message, 0, 2) === '//' || $command === 'admin') {
|
2014-01-06 16:51:57 +01:00
|
|
|
// Admin command
|
2013-12-09 22:12:57 +01:00
|
|
|
$commandListeners = $this->adminCommandListeners;
|
2014-01-14 19:47:40 +01:00
|
|
|
|
2014-06-14 14:32:29 +02:00
|
|
|
if ($command === 'admin') {
|
2014-01-06 16:51:57 +01:00
|
|
|
// Strip 'admin' keyword
|
2014-03-13 18:47:40 +01:00
|
|
|
if (isset($commandArray[1])) {
|
|
|
|
$command = $commandArray[1];
|
|
|
|
unset($commandArray[1]);
|
|
|
|
}
|
2013-12-09 22:12:57 +01:00
|
|
|
}
|
2014-01-06 16:51:57 +01:00
|
|
|
unset($commandArray[0]);
|
2014-01-14 19:47:40 +01:00
|
|
|
|
2014-01-06 16:51:57 +01:00
|
|
|
// Compose uniformed message
|
|
|
|
$message = '//' . $command;
|
2014-05-02 17:50:30 +02:00
|
|
|
foreach ($commandArray as $commandPart) {
|
2014-01-06 16:51:57 +01:00
|
|
|
$message .= ' ' . $commandPart;
|
2013-12-14 23:27:15 +01:00
|
|
|
}
|
2014-01-06 16:51:57 +01:00
|
|
|
$callback[1][2] = $message;
|
2014-01-14 19:47:40 +01:00
|
|
|
} else {
|
2014-01-06 16:51:57 +01:00
|
|
|
// User command
|
2013-12-09 22:12:57 +01:00
|
|
|
$commandListeners = $this->commandListeners;
|
|
|
|
}
|
2014-01-14 19:47:40 +01:00
|
|
|
|
2014-03-13 18:47:40 +01:00
|
|
|
if (!array_key_exists($command, $commandListeners) || !is_array($commandListeners[$command])) {
|
2013-11-12 19:33:25 +01:00
|
|
|
// No command listener registered
|
2013-11-28 03:47:08 +01:00
|
|
|
return;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2014-01-14 19:47:40 +01:00
|
|
|
|
2013-11-12 19:33:25 +01:00
|
|
|
// Inform command listeners
|
2014-05-13 00:22:47 +02:00
|
|
|
foreach ($commandListeners[$command] as $listenerCallback) {
|
|
|
|
call_user_func($listenerCallback, $callback, $player);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|