diff --git a/application/core/Manialinks/ManialinkManager.php b/application/core/Manialinks/ManialinkManager.php index 9af3abbe..31a2baac 100644 --- a/application/core/Manialinks/ManialinkManager.php +++ b/application/core/Manialinks/ManialinkManager.php @@ -15,44 +15,45 @@ use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException; /** * Manialink Manager Class - * - * @author steeffeen & kremsy + * + * @author steeffeen & kremsy * @copyright ManiaControl Copyright © 2014 ManiaControl Team - * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener { /* * Constants */ - const MAIN_MLID = 'Main.ManiaLinkId'; - const ACTION_CLOSEWIDGET = 'ManiaLinkManager.CloseWidget'; + const MAIN_MLID = 'Main.ManiaLinkId'; + const ACTION_CLOSEWIDGET = 'ManiaLinkManager.CloseWidget'; const CB_MAIN_WINDOW_CLOSED = 'ManialinkManagerCallback.MainWindowClosed'; const CB_MAIN_WINDOW_OPENED = 'ManialinkManagerCallback.MainWindowOpened'; - + /* * Public Properties */ public $styleManager = null; public $customUIManager = null; public $iconManager = null; - + /* * Private Properties */ private $maniaControl = null; private $pageAnswerListeners = array(); + private $pageAnswerRegexListener = array(); /** * Create a new manialink manager - * + * * @param ManiaControl $maniaControl */ public function __construct(ManiaControl $maniaControl) { - $this->maniaControl = $maniaControl; - $this->styleManager = new StyleManager($maniaControl); + $this->maniaControl = $maniaControl; + $this->styleManager = new StyleManager($maniaControl); $this->customUIManager = new CustomUIManager($maniaControl); - $this->iconManager = new IconManager($maniaControl); - + $this->iconManager = new IconManager($maniaControl); + // Register for callbacks $this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); @@ -60,39 +61,66 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener /** * Register a new manialink page answer listener - * - * @param string $actionId + * + * @param string $actionId * @param ManialinkPageAnswerListener $listener - * @param string $method + * @param string $method * @return bool */ public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) { if (!method_exists($listener, $method)) { trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!"); - return false; } + if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) { // Init listeners array $this->pageAnswerListeners[$actionId] = array(); } + // Register page answer listener array_push($this->pageAnswerListeners[$actionId], array($listener, $method)); + + 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 - * + * * @param ManialinkPageAnswerListener $listener * @return bool */ public function unregisterManialinkPageAnswerListener(ManialinkPageAnswerListener $listener) { $removed = false; - foreach($this->pageAnswerListeners as &$listeners) { - foreach($listeners as $key => &$listenerCallback) { - if ($listenerCallback[0] != $listener) { + $allListeners = array_merge($this->pageAnswerListeners, $this->pageAnswerRegexListener); + foreach ($allListeners as &$listeners) { + foreach ($listeners as $key => &$listenerCallback) { + if ($listenerCallback[0] !== $listener) { continue; } unset($listeners[$key]); @@ -104,39 +132,48 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener /** * Handle ManialinkPageAnswer callback - * + * * @param array $callback */ public function handleManialinkPageAnswer(array $callback) { $actionId = $callback[1][2]; - $login = $callback[1][1]; - $player = $this->maniaControl->playerManager->getPlayer($login); - if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) { - // No page answer listener registered - return; + $login = $callback[1][1]; + $player = $this->maniaControl->playerManager->getPlayer($login); + + if (array_key_exists($actionId, $this->pageAnswerListeners) && is_array($this->pageAnswerListeners[$actionId])) { + // Inform page answer listeners + foreach ($this->pageAnswerListeners[$actionId] as $listener) { + call_user_func($listener, $callback, $player); + } } - // Inform page answer listeners - foreach($this->pageAnswerListeners[$actionId] as $listener) { - call_user_func(array($listener[0], $listener[1]), $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); + } + } } } /** * Send the given manialink to players - * + * * @param string $manialinkText - * @param mixed $logins - * @param int $timeout - * @param bool $hideOnClick + * @param mixed $logins + * @param int $timeout + * @param bool $hideOnClick * @return bool */ public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) { - $manialinkText = (string)$manialinkText; - + $manialinkText = (string) $manialinkText; + if (!$manialinkText) { return true; } - + try { if (!$logins) { return $this->maniaControl->client->sendDisplayManialinkPage(null, $manialinkText, $timeout, $hideOnClick); @@ -151,32 +188,34 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener } if (is_array($logins)) { $success = true; - foreach($logins as $login) { + foreach ($logins as $login) { $subSuccess = $this->maniaControl->client->sendDisplayManialinkPage($login, $manialinkText, $timeout, $hideOnClick); if (!$subSuccess) { $success = false; } } - + return $success; } - } catch(LoginUnknownException $e) { + } + catch (LoginUnknownException $e) { return false; } - + return true; } /** * Enable the alt menu for the player - * + * * @param Player $player * @return bool */ public function enableAltMenu(Player $player) { try { $success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_EnableAltMenu', $player->login); - } catch(NotInScriptModeException $e) { + } + catch (NotInScriptModeException $e) { return false; } return $success; @@ -184,14 +223,15 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener /** * Disable the alt menu for the player - * + * * @param Player $player * @return bool */ public function disableAltMenu(Player $player) { try { $success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_DisableAltMenu', $player->login); - } catch(NotInScriptModeException $e) { + } + catch (NotInScriptModeException $e) { return false; } return $success; @@ -199,17 +239,17 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener /** * Displays a ManiaLink Widget to a certain Player (Should only be used on Main Widgets) - * - * @param mixed $maniaLink + * + * @param mixed $maniaLink * @param Player $player * @param string $widgetName */ public function displayWidget($maniaLink, Player $player, $widgetName = '') { // render and display xml $this->sendManialink($maniaLink, $player->login); - + if ($widgetName != '') { - //TODO make check by manialinkId, getter is needed to avoid uses on non main widgets + // TODO make check by manialinkId, getter is needed to avoid uses on non main widgets $this->disableAltMenu($player); // Trigger callback $this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_OPENED, $player, $widgetName); @@ -218,8 +258,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener /** * Closes a widget via the callback - * - * @param array $callback + * + * @param array $callback * @param Player $player */ public function closeWidgetCallback(array $callback, Player $player) { @@ -228,19 +268,20 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener /** * Closes a Manialink Widget - * + * * @param Player $player - * @param bool $widgetId + * @param bool $widgetId */ public function closeWidget(Player $player, $widgetId = false) { if (!$widgetId) { $emptyManialink = new ManiaLink(self::MAIN_MLID); $this->sendManialink($emptyManialink, $player->login); $this->enableAltMenu($player); - + // Trigger callback $this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_CLOSED, $player); - } else { + } + else { $emptyManialink = new ManiaLink($widgetId); $this->sendManialink($emptyManialink, $player->login); } @@ -248,7 +289,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener /** * Adds a line of labels - * + * * @param Frame $frame * @param array $labelStrings * @param array $properties @@ -256,14 +297,14 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener */ public function labelLine(Frame $frame, array $labelStrings, array $properties = array()) { // define standard properties - $hAlign = (isset($properties['hAlign']) ? $properties['hAlign'] : Control::LEFT); - $style = (isset($properties['style']) ? $properties['style'] : Label_Text::STYLE_TextCardSmall); - $textSize = (isset($properties['textSize']) ? $properties['textSize'] : 1.5); + $hAlign = (isset($properties['hAlign']) ? $properties['hAlign'] : Control::LEFT); + $style = (isset($properties['style']) ? $properties['style'] : Label_Text::STYLE_TextCardSmall); + $textSize = (isset($properties['textSize']) ? $properties['textSize'] : 1.5); $textColor = (isset($properties['textColor']) ? $properties['textColor'] : 'FFF'); - $profile = (isset($properties['profile']) ? $properties['profile'] : false); - + $profile = (isset($properties['profile']) ? $properties['profile'] : false); + $labels = array(); - foreach($labelStrings as $text => $x) { + foreach ($labelStrings as $text => $x) { $label = new Label_Text(); $frame->add($label); $label->setHAlign($hAlign); @@ -272,14 +313,14 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener $label->setTextSize($textSize); $label->setText($text); $label->setTextColor($textColor); - + if ($profile) { - $label->addPlayerProfileFeature($profile); + $label->addPlayerProfileFeature($profile); } - + $labels[] = $label; // add Label to the labels array } - + return $labels; } }