TrackManiaControl/application/core/Commands/CommandManager.php
Steffen Schröder a9b1842a6f - Improved Join/Leave messages
- Moved Server Commands into separate class
- Added \Server namespace
2013-11-19 20:29:37 +01:00

91 lines
2.4 KiB
PHP

<?php
namespace ManiaControl\Commands;
require_once __DIR__ . '/CommandListener.php';
use ManiaControl\ManiaControl;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Players\Player;
/**
* Class for handling chat commands
*
* @author steeffeen & kremsy
*/
class CommandManager implements CallbackListener {
/**
* Private properties
*/
private $maniaControl = null;
private $commandListeners = array();
/**
* Construct commands manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback');
}
/**
* Register a command listener
*
* @param string $commandName
* @param CommandListener $listener
* @param string $method
* @return bool
*/
public function registerCommandListener($commandName, CommandListener $listener, $method) {
$command = strtolower($commandName);
if (!method_exists($listener, $method)) {
trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
return false;
}
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));
return true;
}
/**
* Handle chat callback
*
* @param array $callback
* @return bool
*/
public function handleChatCallback(array $callback) {
// Check for command
if (!$callback[1][3]) {
return false;
}
// Check for valid player
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1]);
if (!$player) {
return false;
}
// Handle command
$command = explode(" ", substr($callback[1][2], 1));
$command = strtolower($command[0]);
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
// No command listener registered
return true;
}
// Inform command listeners
foreach ($this->commandListeners[$command] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback, $player);
}
return true;
}
}
?>