added unregister methods

This commit is contained in:
Steffen Schröder 2013-12-14 23:27:15 +01:00
parent 30717ce343
commit bebda8f69a
3 changed files with 99 additions and 20 deletions

View File

@ -112,11 +112,49 @@ class CallbackManager {
return true;
}
/**
* Remove a Callback Listener
*
* @param CallbackListener $listener
* @return bool
*/
public function unregisterCallbackListener(CallbackListener $listener) {
$removed = false;
foreach ($this->callbackListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] == $listener) {
unset($listeners[$key]);
$removed = true;
}
}
}
return $removed;
}
/**
* Remove a Script Callback Listener
*
* @param CallbackListener $listener
* @return bool
*/
public function unregisterScriptCallbackListener(CallbackListener $listener) {
$removed = false;
foreach ($this->scriptCallbackListener as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] == $listener) {
unset($listeners[$key]);
$removed = true;
}
}
}
return $removed;
}
/**
* Trigger a specific callback
*
* @param string $callbackName
* @param array $callback
* @param array $callback
*/
public function triggerCallback($callbackName, array $callback) {
if (!array_key_exists($callbackName, $this->callbackListeners)) {
@ -249,5 +287,3 @@ class CallbackManager {
$this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE));
}
}
?>

View File

@ -19,6 +19,7 @@ class CommandManager implements CallbackListener {
private $maniaControl = null;
private $commandListeners = array();
private $adminCommandListeners = array();
/**
* Construct commands manager
*
@ -32,10 +33,10 @@ class CommandManager implements CallbackListener {
/**
* Register a command listener
*
* @param string $commandName
* @param CommandListener $listener
* @param string $method
* @param bool $adminCommand
* @param string $commandName
* @param CommandListener $listener
* @param string $method
* @param bool $adminCommand
* @return bool
*/
public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false) {
@ -44,14 +45,15 @@ class CommandManager implements CallbackListener {
trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
return false;
}
if($adminCommand){
if ($adminCommand) {
if (!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) {
// Init listeners array
// Init admin listeners array
$this->adminCommandListeners[$command] = array();
}
// Register admin command listener
array_push($this->adminCommandListeners[$command], array($listener, $method));
}else{
}
else {
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
// Init listeners array
$this->commandListeners[$command] = array();
@ -62,6 +64,33 @@ class CommandManager implements CallbackListener {
return true;
}
/**
* 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;
}
/**
* Handle chat callback
*
@ -80,28 +109,28 @@ class CommandManager implements CallbackListener {
// Handle command
$commandArray = explode(" ", substr($callback[1][2], 1));
$command = strtolower($commandArray[0]);
if(substr($command,0,1) == "/" || $command == "admin"){ //admin command
if (substr($command, 0, 1) == "/" || $command == "admin") { // admin command
$commandListeners = $this->adminCommandListeners;
if($command == "admin"){
if ($command == "admin") {
$command = strtolower($commandArray[1]);
}else{
$command = substr($command, 1); //remove /
}
}else{ //user command
else {
$command = substr($command, 1); // remove /
}
}
else { // user command
$commandListeners = $this->commandListeners;
}
if (!array_key_exists($command, $commandListeners) || !is_array($commandListeners[$command])) {
// No command listener registered
return;
}
// Inform command listeners
foreach ($commandListeners[$command] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback, $player);
}
}
}
?>

View File

@ -63,6 +63,20 @@ class ManialinkManager implements CallbackListener {
return true;
}
/**
* Remove a Manialink Page Answer Listener
*
* @param ManialinkPageAnswerListener $listener
* @return bool
*/
public function unregisterManialinkPageAnswerListener(ManialinkPageAnswerListener $listener) {
$keys = array_keys($this->pageAnswerListeners, $listener);
foreach ($keys as $key) {
unset($this->pageAnswerListeners[$key]);
}
return true;
}
/**
* Reserve manialink ids
*