page answer action regex draft
This commit is contained in:
parent
8b83825344
commit
f0e7a96e0e
@ -16,16 +16,16 @@ use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException;
|
|||||||
/**
|
/**
|
||||||
* Manialink Manager Class
|
* Manialink Manager Class
|
||||||
*
|
*
|
||||||
* @author steeffeen & kremsy
|
* @author steeffeen & kremsy
|
||||||
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
* @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 {
|
class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener {
|
||||||
/*
|
/*
|
||||||
* Constants
|
* Constants
|
||||||
*/
|
*/
|
||||||
const MAIN_MLID = 'Main.ManiaLinkId';
|
const MAIN_MLID = 'Main.ManiaLinkId';
|
||||||
const ACTION_CLOSEWIDGET = 'ManiaLinkManager.CloseWidget';
|
const ACTION_CLOSEWIDGET = 'ManiaLinkManager.CloseWidget';
|
||||||
const CB_MAIN_WINDOW_CLOSED = 'ManialinkManagerCallback.MainWindowClosed';
|
const CB_MAIN_WINDOW_CLOSED = 'ManialinkManagerCallback.MainWindowClosed';
|
||||||
const CB_MAIN_WINDOW_OPENED = 'ManialinkManagerCallback.MainWindowOpened';
|
const CB_MAIN_WINDOW_OPENED = 'ManialinkManagerCallback.MainWindowOpened';
|
||||||
|
|
||||||
@ -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
|
||||||
@ -48,10 +49,10 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
* @param ManiaControl $maniaControl
|
* @param ManiaControl $maniaControl
|
||||||
*/
|
*/
|
||||||
public function __construct(ManiaControl $maniaControl) {
|
public function __construct(ManiaControl $maniaControl) {
|
||||||
$this->maniaControl = $maniaControl;
|
$this->maniaControl = $maniaControl;
|
||||||
$this->styleManager = new StyleManager($maniaControl);
|
$this->styleManager = new StyleManager($maniaControl);
|
||||||
$this->customUIManager = new CustomUIManager($maniaControl);
|
$this->customUIManager = new CustomUIManager($maniaControl);
|
||||||
$this->iconManager = new IconManager($maniaControl);
|
$this->iconManager = new IconManager($maniaControl);
|
||||||
|
|
||||||
// Register for callbacks
|
// Register for callbacks
|
||||||
$this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback');
|
$this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback');
|
||||||
@ -61,27 +62,53 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
/**
|
/**
|
||||||
* Register a new manialink page answer listener
|
* Register a new manialink page answer listener
|
||||||
*
|
*
|
||||||
* @param string $actionId
|
* @param string $actionId
|
||||||
* @param ManialinkPageAnswerListener $listener
|
* @param ManialinkPageAnswerListener $listener
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
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($listeners as $key => &$listenerCallback) {
|
foreach ($allListeners as &$listeners) {
|
||||||
if ($listenerCallback[0] != $listener) {
|
foreach ($listeners as $key => &$listenerCallback) {
|
||||||
|
if ($listenerCallback[0] !== $listener) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
unset($listeners[$key]);
|
unset($listeners[$key]);
|
||||||
@ -109,15 +137,24 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
*/
|
*/
|
||||||
public function handleManialinkPageAnswer(array $callback) {
|
public function handleManialinkPageAnswer(array $callback) {
|
||||||
$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
|
||||||
|
foreach ($this->pageAnswerListeners[$actionId] as $listener) {
|
||||||
|
call_user_func($listener, $callback, $player);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Inform page answer listeners
|
|
||||||
foreach($this->pageAnswerListeners[$actionId] as $listener) {
|
// Check regex listeners
|
||||||
call_user_func(array($listener[0], $listener[1]), $callback, $player);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,13 +162,13 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
* Send the given manialink to players
|
* Send the given manialink to players
|
||||||
*
|
*
|
||||||
* @param string $manialinkText
|
* @param string $manialinkText
|
||||||
* @param mixed $logins
|
* @param mixed $logins
|
||||||
* @param int $timeout
|
* @param int $timeout
|
||||||
* @param bool $hideOnClick
|
* @param bool $hideOnClick
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) {
|
public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) {
|
||||||
$manialinkText = (string)$manialinkText;
|
$manialinkText = (string) $manialinkText;
|
||||||
|
|
||||||
if (!$manialinkText) {
|
if (!$manialinkText) {
|
||||||
return true;
|
return true;
|
||||||
@ -151,7 +188,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
}
|
}
|
||||||
if (is_array($logins)) {
|
if (is_array($logins)) {
|
||||||
$success = true;
|
$success = true;
|
||||||
foreach($logins as $login) {
|
foreach ($logins as $login) {
|
||||||
$subSuccess = $this->maniaControl->client->sendDisplayManialinkPage($login, $manialinkText, $timeout, $hideOnClick);
|
$subSuccess = $this->maniaControl->client->sendDisplayManialinkPage($login, $manialinkText, $timeout, $hideOnClick);
|
||||||
if (!$subSuccess) {
|
if (!$subSuccess) {
|
||||||
$success = false;
|
$success = false;
|
||||||
@ -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;
|
||||||
@ -200,7 +240,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
/**
|
/**
|
||||||
* Displays a ManiaLink Widget to a certain Player (Should only be used on Main Widgets)
|
* 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 Player $player
|
||||||
* @param string $widgetName
|
* @param string $widgetName
|
||||||
*/
|
*/
|
||||||
@ -209,7 +249,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
$this->sendManialink($maniaLink, $player->login);
|
$this->sendManialink($maniaLink, $player->login);
|
||||||
|
|
||||||
if ($widgetName != '') {
|
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);
|
$this->disableAltMenu($player);
|
||||||
// Trigger callback
|
// Trigger callback
|
||||||
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_OPENED, $player, $widgetName);
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_OPENED, $player, $widgetName);
|
||||||
@ -219,7 +259,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
/**
|
/**
|
||||||
* Closes a widget via the callback
|
* Closes a widget via the callback
|
||||||
*
|
*
|
||||||
* @param array $callback
|
* @param array $callback
|
||||||
* @param Player $player
|
* @param Player $player
|
||||||
*/
|
*/
|
||||||
public function closeWidgetCallback(array $callback, Player $player) {
|
public function closeWidgetCallback(array $callback, Player $player) {
|
||||||
@ -230,7 +270,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
* Closes a Manialink Widget
|
* Closes a Manialink Widget
|
||||||
*
|
*
|
||||||
* @param Player $player
|
* @param Player $player
|
||||||
* @param bool $widgetId
|
* @param bool $widgetId
|
||||||
*/
|
*/
|
||||||
public function closeWidget(Player $player, $widgetId = false) {
|
public function closeWidget(Player $player, $widgetId = false) {
|
||||||
if (!$widgetId) {
|
if (!$widgetId) {
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
@ -256,14 +297,14 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
*/
|
*/
|
||||||
public function labelLine(Frame $frame, array $labelStrings, array $properties = array()) {
|
public function labelLine(Frame $frame, array $labelStrings, array $properties = array()) {
|
||||||
// define standard properties
|
// define standard properties
|
||||||
$hAlign = (isset($properties['hAlign']) ? $properties['hAlign'] : Control::LEFT);
|
$hAlign = (isset($properties['hAlign']) ? $properties['hAlign'] : Control::LEFT);
|
||||||
$style = (isset($properties['style']) ? $properties['style'] : Label_Text::STYLE_TextCardSmall);
|
$style = (isset($properties['style']) ? $properties['style'] : Label_Text::STYLE_TextCardSmall);
|
||||||
$textSize = (isset($properties['textSize']) ? $properties['textSize'] : 1.5);
|
$textSize = (isset($properties['textSize']) ? $properties['textSize'] : 1.5);
|
||||||
$textColor = (isset($properties['textColor']) ? $properties['textColor'] : 'FFF');
|
$textColor = (isset($properties['textColor']) ? $properties['textColor'] : 'FFF');
|
||||||
$profile = (isset($properties['profile']) ? $properties['profile'] : false);
|
$profile = (isset($properties['profile']) ? $properties['profile'] : false);
|
||||||
|
|
||||||
$labels = array();
|
$labels = array();
|
||||||
foreach($labelStrings as $text => $x) {
|
foreach ($labelStrings as $text => $x) {
|
||||||
$label = new Label_Text();
|
$label = new Label_Text();
|
||||||
$frame->add($label);
|
$frame->add($label);
|
||||||
$label->setHAlign($hAlign);
|
$label->setHAlign($hAlign);
|
||||||
@ -274,7 +315,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
|
|||||||
$label->setTextColor($textColor);
|
$label->setTextColor($textColor);
|
||||||
|
|
||||||
if ($profile) {
|
if ($profile) {
|
||||||
$label->addPlayerProfileFeature($profile);
|
$label->addPlayerProfileFeature($profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
$labels[] = $label; // add Label to the labels array
|
$labels[] = $label; // add Label to the labels array
|
||||||
|
Loading…
Reference in New Issue
Block a user