page answer action regex draft

This commit is contained in:
Steffen Schröder 2014-04-28 17:00:53 +02:00
parent 8b83825344
commit f0e7a96e0e

View File

@ -41,6 +41,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
*/ */
private $maniaControl = null; private $maniaControl = null;
private $pageAnswerListeners = array(); private $pageAnswerListeners = array();
private $pageAnswerRegexListener = array();
/** /**
* Create a new manialink manager * Create a new manialink manager
@ -69,19 +70,45 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) { public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) {
if (!method_exists($listener, $method)) { if (!method_exists($listener, $method)) {
trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!"); trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!");
return false; return false;
} }
if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) { if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) {
// Init listeners array // Init listeners array
$this->pageAnswerListeners[$actionId] = array(); $this->pageAnswerListeners[$actionId] = array();
} }
// Register page answer listener // Register page answer listener
array_push($this->pageAnswerListeners[$actionId], array($listener, $method)); array_push($this->pageAnswerListeners[$actionId], array($listener, $method));
return true; return true;
} }
/**
* Register a new manialink page answer reg ex listener
*
* @param string $actionIdRegex
* @param ManialinkPageAnswerListener $listener
* @param string $method
* @return bool
*/
public function registerManialinkPageAnswerRegexListener($actionIdRegex, ManialinkPageAnswerListener $listener, $method) {
if (!method_exists($listener, $method)) {
trigger_error("Given listener for actionIdRegex '{$actionIdRegex}' doesn't have callback method '{$method}'!");
return false;
}
if (!array_key_exists($actionIdRegex, $this->pageAnswerRegexListener) || !is_array($this->pageAnswerRegexListener[$actionIdRegex])) {
// Init regex listeners array
$this->pageAnswerRegexListener[$actionIdRegex] = array();
}
// Register page answer reg exlistener
array_push($this->pageAnswerRegexListener[$actionIdRegex], array($listener, $method));
return true;
}
/** /**
* Remove a Manialink Page Answer Listener * Remove a Manialink Page Answer Listener
* *
@ -90,9 +117,10 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
*/ */
public function unregisterManialinkPageAnswerListener(ManialinkPageAnswerListener $listener) { public function unregisterManialinkPageAnswerListener(ManialinkPageAnswerListener $listener) {
$removed = false; $removed = false;
foreach($this->pageAnswerListeners as &$listeners) { $allListeners = array_merge($this->pageAnswerListeners, $this->pageAnswerRegexListener);
foreach ($allListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) { foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] != $listener) { if ($listenerCallback[0] !== $listener) {
continue; continue;
} }
unset($listeners[$key]); unset($listeners[$key]);
@ -111,13 +139,22 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
$actionId = $callback[1][2]; $actionId = $callback[1][2];
$login = $callback[1][1]; $login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) {
// No page answer listener registered if (array_key_exists($actionId, $this->pageAnswerListeners) && is_array($this->pageAnswerListeners[$actionId])) {
return;
}
// Inform page answer listeners // Inform page answer listeners
foreach ($this->pageAnswerListeners[$actionId] as $listener) { foreach ($this->pageAnswerListeners[$actionId] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback, $player); call_user_func($listener, $callback, $player);
}
}
// Check regex listeners
foreach ($this->pageAnswerRegexListener as $actionIdRegex => $pageAnswerRegexListeners) {
if (preg_match($actionIdRegex, $actionId)) {
// Inform page answer regex listeners
foreach ($pageAnswerRegexListeners as $listener) {
call_user_func($listener, $callback, $player);
}
}
} }
} }
@ -160,7 +197,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
return $success; return $success;
} }
} catch(LoginUnknownException $e) { }
catch (LoginUnknownException $e) {
return false; return false;
} }
@ -176,7 +214,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
public function enableAltMenu(Player $player) { public function enableAltMenu(Player $player) {
try { try {
$success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_EnableAltMenu', $player->login); $success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_EnableAltMenu', $player->login);
} catch(NotInScriptModeException $e) { }
catch (NotInScriptModeException $e) {
return false; return false;
} }
return $success; return $success;
@ -191,7 +230,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
public function disableAltMenu(Player $player) { public function disableAltMenu(Player $player) {
try { try {
$success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_DisableAltMenu', $player->login); $success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_DisableAltMenu', $player->login);
} catch(NotInScriptModeException $e) { }
catch (NotInScriptModeException $e) {
return false; return false;
} }
return $success; return $success;
@ -240,7 +280,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
// Trigger callback // Trigger callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_CLOSED, $player); $this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_CLOSED, $player);
} else { }
else {
$emptyManialink = new ManiaLink($widgetId); $emptyManialink = new ManiaLink($widgetId);
$this->sendManialink($emptyManialink, $player->login); $this->sendManialink($emptyManialink, $player->login);
} }