TrackManiaControl/application/core/Commands/CommandManager.php

170 lines
4.4 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-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-01-14 19:47:40 +01:00
* @property mixed commandListeners
2013-11-10 17:38:54 +01:00
* @author steeffeen & kremsy
2013-11-09 17:24:03 +01:00
*/
class CommandManager implements CallbackListener {
2013-11-09 17:24:03 +01:00
/**
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();
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
/**
2014-01-06 16:51:57 +01:00
* Construct a new Commands Manager
*
2014-01-06 16:51:57 +01:00
* @param \ManiaControl\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
//Create help manager instance
$this->helpManager = new HelpManager($this->maniaControl, $this);
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
* @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) {
2014-01-14 19:47:40 +01:00
if(is_array($commandName)) {
2014-01-06 18:32:36 +01:00
$success = true;
2014-01-14 19:47:40 +01:00
foreach($commandName as $command) {
if(!$this->registerCommandListener($command, $listener, $method, $adminCommand)) {
2014-01-06 18:32:36 +01:00
$success = false;
}
}
return $success;
}
2013-11-09 17:24:03 +01:00
$command = strtolower($commandName);
2014-01-14 19:47:40 +01:00
if(!method_exists($listener, $method)) {
trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
return false;
2013-11-09 17:24:03 +01:00
}
2014-01-14 19:47:40 +01:00
if($adminCommand) {
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));
2014-01-14 19:47:40 +01:00
} else {
if(!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
2013-12-09 22:12:57 +01:00
// 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
}
2014-01-14 19:47:40 +01:00
//TODO description
$this->helpManager->registerCommand($command, $adminCommand, '');
return true;
2013-11-09 17:24:03 +01:00
}
2013-12-14 23:27:15 +01:00
/**
* Remove 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-01-14 19:47:40 +01:00
foreach($this->commandListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
if($listenerCallback[0] == $listener) {
2013-12-14 23:27:15 +01:00
unset($listeners[$key]);
$removed = true;
}
}
}
2014-01-14 19:47:40 +01:00
foreach($this->adminCommandListeners as &$listeners) {
foreach($listeners as $key => &$listenerCallback) {
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
*
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-01-14 19:47:40 +01:00
if(!$callback[1][3]) {
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-01-14 19:47:40 +01:00
if(!$player) {
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]), '/');
if(!$command) {
return;
}
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
if($command == 'admin') {
2014-01-06 16:51:57 +01:00
// Strip 'admin' keyword
$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-01-14 19:47:40 +01: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
if(!array_key_exists($command, $commandListeners) || !is_array($commandListeners[$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-01-14 19:47:40 +01:00
foreach($commandListeners[$command] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback, $player);
2013-11-09 17:24:03 +01:00
}
}
}