TrackManiaControl/application/core/Commands/CommandManager.php

211 lines
5.6 KiB
PHP
Raw Normal View History

2013-11-09 17:24:03 +01:00
<?php
namespace ManiaControl\Commands;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
2014-07-11 16:54:43 +02:00
use ManiaControl\Callbacks\Listening;
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
*/
class CommandManager implements CallbackListener {
/*
2014-01-06 16:51:57 +01:00
* Private Properties
2013-11-09 17:24:03 +01:00
*/
private $maniaControl = null;
2014-01-14 19:47:40 +01:00
private $helpManager = array();
2014-07-11 16:54:43 +02:00
/** @var Listening[][] $commandListenings */
private $commandListenings = array();
/** @var Listening[][] $adminCommandListenings */
private $adminCommandListenings = 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
*
2014-07-11 16:54:43 +02:00
* @param ManiaControl $maniaControl
2013-11-09 17:24:03 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-14 19:47:40 +01:00
2014-07-11 16:54:43 +02: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
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback');
2013-11-09 17:24:03 +01: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
* @return bool
2013-11-09 17:24:03 +01:00
*/
2014-07-11 16:54:43 +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-07-11 16:54:43 +02:00
$success = false;
2014-05-02 17:50:30 +02:00
foreach ($commandName as $command) {
2014-07-11 16:54:43 +02:00
if ($this->registerCommandListener($command, $listener, $method, $adminCommand, $description)) {
$success = true;
2014-01-06 18:32:36 +01:00
}
}
return $success;
}
2014-07-11 16:54:43 +02:00
if (!Listening::checkValidCallback($listener, $method)) {
$listenerClass = get_class($listener);
trigger_error("Given Listener '{$listenerClass}' can't handle Command '{$commandName}': No callable Method '{$method}'!");
return false;
2013-11-09 17:24:03 +01:00
}
2014-07-11 16:54:43 +02:00
$command = strtolower($commandName);
$listening = new Listening($listener, $method);
2014-03-13 18:47:40 +01:00
if ($adminCommand) {
2014-07-11 16:54:43 +02:00
$this->addListening($this->adminCommandListenings, $listening, $command);
2014-01-14 19:47:40 +01:00
} else {
2014-07-11 16:54:43 +02:00
$this->addListening($this->commandListenings, $listening, $command);
2013-11-09 17:24:03 +01:00
}
2014-01-14 19:47:40 +01:00
// TODO: description(?)
if ($description) {
$this->helpManager->registerCommand($command, $adminCommand, $description, get_class($listener) . '\\' . $method);
}
2014-01-14 19:47:40 +01:00
return true;
2013-11-09 17:24:03 +01:00
}
/**
2014-07-11 16:54:43 +02:00
* Add a Listening to the given Listenings Array
*
2014-07-11 16:54:43 +02:00
* @param array $listeningsArray
* @param Listening $listening
* @param string $command
*/
2014-07-11 16:54:43 +02:00
private function addListening(array &$listeningsArray, Listening $listening, $command) {
if (!array_key_exists($command, $listeningsArray) || !is_array($listeningsArray[$command])) {
// Init listenings array
$listeningsArray[$command] = array();
}
2014-07-11 16:54:43 +02:00
// Register command listening
array_push($listeningsArray[$command], $listening);
}
2013-12-14 23:27:15 +01: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-07-11 16:54:43 +02:00
if ($this->removeCommandListener($this->commandListenings, $listener)) {
$removed = true;
2013-12-14 23:27:15 +01:00
}
2014-07-11 16:54:43 +02:00
if ($this->removeCommandListener($this->adminCommandListenings, $listener)) {
$removed = true;
}
return $removed;
}
/**
2014-07-11 16:54:43 +02:00
* Remove the Command Listener from the given Listenings Array
*
2014-07-11 16:54:43 +02:00
* @param array $listeningsArray
* @param CommandListener $listener
* @return bool
*/
2014-07-11 16:54:43 +02:00
private function removeCommandListener(array &$listeningsArray, CommandListener $listener) {
$removed = false;
2014-07-11 16:54:43 +02:00
foreach ($listeningsArray as &$listenings) {
foreach ($listenings as $key => &$listening) {
if ($listening->listener === $listener) {
unset($listenings[$key]);
2013-12-14 23:27:15 +01:00
$removed = true;
}
}
}
return $removed;
}
2013-11-09 17:24:03 +01:00
/**
2014-01-06 16:51:57 +01:00
* Handle Chat Callback
*
2014-01-06 16:51:57 +01:00
* @param array $callback
2013-11-09 17:24:03 +01:00
*/
public function handleChatCallback(array $callback) {
2013-11-09 17:24:03 +01:00
// Check for command
2014-07-17 20:02:55 +02:00
if (!$this->isCommandMessage($callback)) {
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;
}
if (substr($message, 0, 2) === '//' || $command === 'admin') {
2014-01-06 16:51:57 +01:00
// Admin command
2014-07-11 16:54:43 +02:00
$commandListenings = $this->adminCommandListenings;
2014-01-14 19:47:40 +01: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
2014-07-11 16:54:43 +02:00
$message = '//' . $command . ' ' . implode(' ', $commandArray);
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
2014-07-11 16:54:43 +02:00
$commandListenings = $this->commandListenings;
2013-12-09 22:12:57 +01:00
}
2014-01-14 19:47:40 +01:00
2014-07-11 16:54:43 +02:00
if (!array_key_exists($command, $commandListenings) || !is_array($commandListenings[$command])) {
// No command listener registered
return;
2013-11-09 17:24:03 +01:00
}
2014-01-14 19:47:40 +01:00
// Inform command listeners
2014-07-11 16:54:43 +02:00
foreach ($commandListenings[$command] as $listening) {
/** @var Listening $listening */
$listening->triggerCallback($callback, $player);
2013-11-09 17:24:03 +01:00
}
}
2014-07-11 16:54:43 +02:00
/**
* Check if the given Chat Callback is a Command Message
*
* @param array $chatCallback
* @return bool
*/
private function isCommandMessage(array $chatCallback) {
return (bool)$chatCallback[1][3];
}
2013-11-09 17:24:03 +01:00
}