2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-12 15:48:25 +01:00
|
|
|
namespace ManiaControl\Commands;
|
|
|
|
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for handling chat commands
|
|
|
|
*
|
2013-11-10 17:38:54 +01:00
|
|
|
* @author steeffeen & kremsy
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-19 20:29:37 +01:00
|
|
|
class CommandManager implements CallbackListener {
|
2013-11-10 14:56:37 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
2013-11-10 14:56:37 +01:00
|
|
|
private $maniaControl = null;
|
2013-11-12 15:48:25 +01:00
|
|
|
private $commandListeners = array();
|
2013-12-09 22:12:57 +01:00
|
|
|
private $adminCommandListeners = array();
|
2013-12-14 23:27:15 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
2013-11-12 19:33:25 +01:00
|
|
|
* Construct commands manager
|
2013-11-12 15:48:25 +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;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-12 15:48:25 +01:00
|
|
|
* Register a command listener
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-12-14 23:27:15 +01:00
|
|
|
* @param string $commandName
|
|
|
|
* @param CommandListener $listener
|
|
|
|
* @param string $method
|
|
|
|
* @param bool $adminCommand
|
2013-11-10 14:56:37 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-12-09 22:12:57 +01:00
|
|
|
public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false) {
|
2013-11-09 17:24:03 +01:00
|
|
|
$command = strtolower($commandName);
|
2013-11-12 15:48:25 +01:00
|
|
|
if (!method_exists($listener, $method)) {
|
|
|
|
trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
|
2013-11-10 14:56:37 +01:00
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-12-14 23:27:15 +01:00
|
|
|
if ($adminCommand) {
|
2013-12-09 22:12:57 +01:00
|
|
|
if (!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) {
|
2013-12-14 23:27:15 +01:00
|
|
|
// Init admin listeners array
|
2013-12-09 22:12:57 +01:00
|
|
|
$this->adminCommandListeners[$command] = array();
|
|
|
|
}
|
|
|
|
// Register admin command listener
|
|
|
|
array_push($this->adminCommandListeners[$command], array($listener, $method));
|
2013-12-14 23:27:15 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-12-09 22:12:57 +01:00
|
|
|
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));
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 14:56:37 +01:00
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 23:27:15 +01:00
|
|
|
/**
|
|
|
|
* Remove a Command Listener
|
|
|
|
*
|
|
|
|
* @param CommandListener $listener
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function unregisterCommandListener(CommandListener $listener) {
|
|
|
|
$removed = false;
|
|
|
|
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) {
|
|
|
|
unset($listeners[$key]);
|
|
|
|
$removed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $removed;
|
|
|
|
}
|
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Handle chat callback
|
2013-11-10 14:56:37 +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
|
2013-11-12 15:48:25 +01:00
|
|
|
if (!$callback[1][3]) {
|
2013-11-28 03:47:08 +01:00
|
|
|
return;
|
2013-11-10 14:56:37 +01:00
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
// Check for valid player
|
2013-11-12 15:48:25 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1]);
|
2013-11-10 19:30:14 +01:00
|
|
|
if (!$player) {
|
2013-11-28 03:47:08 +01:00
|
|
|
return;
|
2013-11-10 14:56:37 +01:00
|
|
|
}
|
2013-11-09 17:24:03 +01:00
|
|
|
// Handle command
|
2013-12-09 22:20:30 +01:00
|
|
|
$commandArray = explode(" ", substr($callback[1][2], 1));
|
|
|
|
$command = strtolower($commandArray[0]);
|
2013-12-14 23:27:15 +01:00
|
|
|
|
|
|
|
if (substr($command, 0, 1) == "/" || $command == "admin") { // admin command
|
2013-12-09 22:12:57 +01:00
|
|
|
$commandListeners = $this->adminCommandListeners;
|
2013-12-14 23:27:15 +01:00
|
|
|
if ($command == "admin") {
|
2013-12-09 22:20:30 +01:00
|
|
|
$command = strtolower($commandArray[1]);
|
2013-12-09 22:12:57 +01:00
|
|
|
}
|
2013-12-14 23:27:15 +01:00
|
|
|
else {
|
|
|
|
$command = substr($command, 1); // remove /
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // user command
|
2013-12-09 22:12:57 +01:00
|
|
|
$commandListeners = $this->commandListeners;
|
|
|
|
}
|
2013-12-14 23:27:15 +01:00
|
|
|
|
2013-12-09 22:12:57 +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
|
|
|
}
|
2013-12-14 23:27:15 +01:00
|
|
|
|
2013-11-12 19:33:25 +01:00
|
|
|
// Inform command listeners
|
2013-12-09 22:12:57 +01:00
|
|
|
foreach ($commandListeners[$command] as $listener) {
|
2013-11-12 15:48:25 +01:00
|
|
|
call_user_func(array($listener[0], $listener[1]), $callback, $player);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|