2013-11-28 02:04:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Manialinks;
|
|
|
|
|
2013-12-30 14:52:48 +01:00
|
|
|
use FML\Controls\Control;
|
|
|
|
use FML\Controls\Frame;
|
|
|
|
use FML\Controls\Labels\Label_Text;
|
2013-12-31 17:11:53 +01:00
|
|
|
use FML\ManiaLink;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Players\Player;
|
2014-02-13 14:21:25 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
|
2013-11-28 02:04:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manialink manager class
|
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
|
|
|
*/
|
2013-12-30 14:52:48 +01:00
|
|
|
class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener {
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2013-12-30 14:52:48 +01:00
|
|
|
/**
|
2013-12-24 12:49:53 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2013-12-31 17:11:53 +01:00
|
|
|
const MAIN_MLID = 'Main.ManiaLinkId';
|
|
|
|
const ACTION_CLOSEWIDGET = 'ManiaLinkManager.CloseWidget';
|
2013-12-30 14:52:48 +01:00
|
|
|
const CB_MAIN_WINDOW_CLOSED = 'ManialinkManagerCallback.MainWindowClosed';
|
2014-01-11 17:24:03 +01:00
|
|
|
const CB_MAIN_WINDOW_OPENED = 'ManialinkManagerCallback.MainWindowOpened';
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
/**
|
|
|
|
* Public properties
|
|
|
|
*/
|
|
|
|
public $styleManager = null;
|
2013-12-30 14:52:48 +01:00
|
|
|
public $customUIManager = null;
|
2013-12-31 17:09:29 +01:00
|
|
|
public $iconManager = null;
|
|
|
|
|
2013-11-28 02:04:06 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
|
|
|
private $pageAnswerListeners = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new manialink manager
|
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-11-28 02:04:06 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
2013-12-31 17:11:53 +01:00
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->styleManager = new StyleManager($maniaControl);
|
2013-12-30 14:52:48 +01:00
|
|
|
$this->customUIManager = new CustomUIManager($maniaControl);
|
2013-12-31 17:11:53 +01:00
|
|
|
$this->iconManager = new IconManager($maniaControl);
|
2013-12-31 17:09:29 +01:00
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
// Register for callbacks
|
2013-12-30 14:52:48 +01:00
|
|
|
$this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback');
|
2013-12-31 17:11:53 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new manialink page answer listener
|
|
|
|
*
|
2013-12-31 17:11:53 +01:00
|
|
|
* @param string $actionId
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param ManialinkPageAnswerListener $listener
|
2013-12-31 17:11:53 +01:00
|
|
|
* @param string $method
|
2013-11-28 02:04:06 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-11-28 03:47:08 +01:00
|
|
|
public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) {
|
2014-01-28 15:56:50 +01:00
|
|
|
if (!method_exists($listener, $method)) {
|
2013-11-28 03:47:08 +01:00
|
|
|
trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!");
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2013-11-28 02:04:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-28 15:56:50 +01:00
|
|
|
if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) {
|
2013-11-28 02:04:06 +01:00
|
|
|
// Init listeners array
|
2013-11-28 03:47:08 +01:00
|
|
|
$this->pageAnswerListeners[$actionId] = array();
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|
|
|
|
// Register page answer listener
|
2013-11-28 03:47:08 +01:00
|
|
|
array_push($this->pageAnswerListeners[$actionId], array($listener, $method));
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2013-11-28 02:04:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-14 23:27:15 +01:00
|
|
|
/**
|
|
|
|
* Remove a Manialink Page Answer Listener
|
2013-12-30 14:52:48 +01:00
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param ManialinkPageAnswerListener $listener
|
2013-12-14 23:27:15 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function unregisterManialinkPageAnswerListener(ManialinkPageAnswerListener $listener) {
|
2014-01-19 00:09:15 +01:00
|
|
|
$removed = false;
|
|
|
|
foreach($this->pageAnswerListeners as &$listeners) {
|
|
|
|
foreach($listeners as $key => &$listenerCallback) {
|
2014-01-28 15:56:50 +01:00
|
|
|
if ($listenerCallback[0] != $listener) {
|
2014-01-19 00:09:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unset($listeners[$key]);
|
|
|
|
$removed = true;
|
|
|
|
}
|
2013-12-14 23:27:15 +01:00
|
|
|
}
|
2014-01-19 00:09:15 +01:00
|
|
|
return $removed;
|
2013-11-28 03:47:08 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 02:04:06 +01:00
|
|
|
/**
|
|
|
|
* Handle ManialinkPageAnswer callback
|
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param array $callback
|
2013-11-28 02:04:06 +01:00
|
|
|
*/
|
|
|
|
public function handleManialinkPageAnswer(array $callback) {
|
2013-11-28 03:47:08 +01:00
|
|
|
$actionId = $callback[1][2];
|
2013-12-31 17:11:53 +01:00
|
|
|
$login = $callback[1][1];
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2014-01-28 15:56:50 +01:00
|
|
|
if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) {
|
2013-11-28 03:47:08 +01:00
|
|
|
// No page answer listener registered
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Inform page answer listeners
|
2014-01-03 16:24:35 +01:00
|
|
|
foreach($this->pageAnswerListeners[$actionId] as $listener) {
|
2013-11-28 03:47:08 +01:00
|
|
|
call_user_func(array($listener[0], $listener[1]), $callback, $player);
|
|
|
|
}
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the given manialink to players
|
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param string $manialinkText
|
2013-12-31 17:11:53 +01:00
|
|
|
* @param mixed $logins
|
|
|
|
* @param int $timeout
|
|
|
|
* @param bool $hideOnClick
|
2013-11-28 02:04:06 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-01-28 16:05:18 +01:00
|
|
|
public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) {
|
2014-01-11 17:24:03 +01:00
|
|
|
$manialinkText = (string)$manialinkText;
|
2014-01-17 22:35:14 +01:00
|
|
|
|
|
|
|
try {
|
2014-01-28 15:56:50 +01:00
|
|
|
if (!$logins) {
|
2014-01-17 22:35:14 +01:00
|
|
|
return $this->maniaControl->client->sendDisplayManialinkPage(null, $manialinkText, $timeout, $hideOnClick);
|
|
|
|
}
|
2014-01-28 15:56:50 +01:00
|
|
|
if (is_string($logins)) {
|
2014-03-01 12:18:42 +01:00
|
|
|
$success = $this->maniaControl->client->sendDisplayManialinkPage($logins, $manialinkText, $timeout, $hideOnClick);
|
|
|
|
return $success;
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|
2014-02-19 15:44:00 +01:00
|
|
|
if ($logins instanceof Player) {
|
2014-03-01 12:18:42 +01:00
|
|
|
$success = $this->maniaControl->client->sendDisplayManialinkPage($logins->login, $manialinkText, $timeout, $hideOnClick);
|
|
|
|
return $success;
|
2014-02-19 15:44:00 +01:00
|
|
|
}
|
2014-01-28 15:56:50 +01:00
|
|
|
if (is_array($logins)) {
|
2014-01-17 22:35:14 +01:00
|
|
|
$success = true;
|
2014-02-19 15:44:00 +01:00
|
|
|
foreach ($logins as $login) {
|
2014-01-17 22:35:14 +01:00
|
|
|
$subSuccess = $this->maniaControl->client->sendDisplayManialinkPage($login, $manialinkText, $timeout, $hideOnClick);
|
2014-01-28 15:56:50 +01:00
|
|
|
if (!$subSuccess) {
|
2014-01-17 22:35:14 +01:00
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2014-01-17 22:35:14 +01:00
|
|
|
return $success;
|
|
|
|
}
|
2014-02-13 14:21:25 +01:00
|
|
|
} catch(Exception $e) {
|
2014-03-01 12:18:42 +01:00
|
|
|
if($e->getMessage() == "Login unknown."){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->maniaControl->errorHandler->triggerDebugNotice("Exception while sending Manialink: " . $e->getMessage());
|
2014-02-13 14:21:25 +01:00
|
|
|
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
|
2014-01-17 22:35:14 +01:00
|
|
|
return false;
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2014-01-17 22:35:14 +01:00
|
|
|
return true;
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|
2013-11-28 04:48:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the alt menu for the player
|
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param Player $player
|
2013-11-28 04:48:04 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function enableAltMenu(Player $player) {
|
2014-02-13 18:18:14 +01:00
|
|
|
try {
|
|
|
|
$success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_EnableAltMenu', $player->login);
|
2014-02-19 17:16:24 +01:00
|
|
|
} catch(Exception $e) {
|
|
|
|
if ($e->getMessage() == 'Not in script mode.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw $e;
|
2014-02-13 18:18:14 +01:00
|
|
|
}
|
|
|
|
return $success;
|
2013-11-28 04:48:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the alt menu for the player
|
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param Player $player
|
2013-11-28 04:48:04 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function disableAltMenu(Player $player) {
|
2014-02-13 18:18:14 +01:00
|
|
|
try {
|
|
|
|
$success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_DisableAltMenu', $player->login);
|
2014-02-19 17:16:24 +01:00
|
|
|
} catch(Exception $e) {
|
|
|
|
if ($e->getMessage() == 'Not in script mode.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw $e;
|
2014-02-13 18:18:14 +01:00
|
|
|
}
|
|
|
|
return $success;
|
2013-11-28 04:48:04 +01:00
|
|
|
}
|
2013-12-15 11:29:49 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-28 20:23:47 +01:00
|
|
|
* Displays a ManiaLink Widget to a certain Player (Should only be used on Main Widgets)
|
2013-12-30 14:52:48 +01:00
|
|
|
*
|
2014-01-11 17:24:03 +01:00
|
|
|
* @param mixed $maniaLink
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param Player $player
|
2014-01-11 17:36:46 +01:00
|
|
|
* @param string $widgetName
|
2013-12-15 11:29:49 +01:00
|
|
|
*/
|
2014-01-11 17:24:03 +01:00
|
|
|
public function displayWidget($maniaLink, Player $player, $widgetName = '') {
|
2013-12-30 14:52:48 +01:00
|
|
|
// render and display xml
|
2014-01-05 19:15:37 +01:00
|
|
|
$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player->login);
|
2014-01-11 17:24:03 +01:00
|
|
|
|
2014-01-28 20:23:47 +01:00
|
|
|
if ($widgetName != '') { //TODO make check by manialinkId, getter is needed to avoid uses on non main widgets
|
|
|
|
$this->disableAltMenu($player);
|
2014-01-11 17:24:03 +01:00
|
|
|
// Trigger callback
|
2014-02-19 15:44:00 +01:00
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_OPENED, $player, $widgetName);
|
2014-01-11 17:24:03 +01:00
|
|
|
}
|
2013-12-15 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
2013-12-24 12:49:53 +01:00
|
|
|
/**
|
|
|
|
* Closes a widget via the callback
|
2013-12-30 14:52:48 +01:00
|
|
|
*
|
2013-12-31 17:11:53 +01:00
|
|
|
* @param array $callback
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param Player $player
|
2013-12-24 12:49:53 +01:00
|
|
|
*/
|
|
|
|
public function closeWidgetCallback(array $callback, Player $player) {
|
|
|
|
$this->closeWidget($player);
|
|
|
|
}
|
|
|
|
|
2013-12-15 11:29:49 +01:00
|
|
|
/**
|
2014-01-16 20:58:39 +01:00
|
|
|
* Closes a Manialink Widget
|
2013-12-30 14:52:48 +01:00
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param Player $player
|
2014-01-16 20:58:39 +01:00
|
|
|
* @param bool $widgetId
|
2013-12-15 11:29:49 +01:00
|
|
|
*/
|
2014-01-16 20:58:39 +01:00
|
|
|
public function closeWidget(Player $player, $widgetId = false) {
|
2014-01-28 15:56:50 +01:00
|
|
|
if (!$widgetId) {
|
2014-01-16 20:58:39 +01:00
|
|
|
$emptyManialink = new ManiaLink(self::MAIN_MLID);
|
|
|
|
$manialinkText = $emptyManialink->render()->saveXML();
|
|
|
|
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login);
|
|
|
|
$this->enableAltMenu($player);
|
|
|
|
|
|
|
|
// Trigger callback
|
2014-02-19 15:44:00 +01:00
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_MAIN_WINDOW_CLOSED, $player);
|
2014-01-16 20:58:39 +01:00
|
|
|
} else {
|
|
|
|
$emptyManialink = new ManiaLink($widgetId);
|
|
|
|
$manialinkText = $emptyManialink->render()->saveXML();
|
|
|
|
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login);
|
|
|
|
}
|
2013-12-15 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
2013-12-15 11:55:44 +01:00
|
|
|
/**
|
|
|
|
* Adds a line of labels
|
2013-12-30 14:52:48 +01:00
|
|
|
*
|
2013-12-31 12:17:25 +01:00
|
|
|
* @param Frame $frame
|
|
|
|
* @param array $labelStrings
|
|
|
|
* @param array $properties
|
2014-01-03 16:24:35 +01:00
|
|
|
* @return array Returns the labels (to add special Properties later)
|
2013-12-15 11:55:44 +01:00
|
|
|
*/
|
2013-12-30 14:52:48 +01:00
|
|
|
public function labelLine(Frame $frame, array $labelStrings, array $properties = array()) {
|
|
|
|
// define standard properties
|
2014-01-28 16:05:18 +01:00
|
|
|
$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');
|
2013-12-31 17:11:53 +01:00
|
|
|
$profile = (isset($properties['profile']) ? $properties['profile'] : false);
|
|
|
|
$script = (isset($properties['script']) ? $properties['script'] : null);
|
|
|
|
|
2014-01-03 16:24:35 +01:00
|
|
|
$labels = array();
|
|
|
|
foreach($labelStrings as $text => $x) {
|
2013-12-15 11:55:44 +01:00
|
|
|
$label = new Label_Text();
|
|
|
|
$frame->add($label);
|
|
|
|
$label->setHAlign($hAlign);
|
|
|
|
$label->setX($x);
|
|
|
|
$label->setStyle($style);
|
|
|
|
$label->setTextSize($textSize);
|
|
|
|
$label->setText($text);
|
|
|
|
$label->setTextColor($textColor);
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2014-01-28 15:56:50 +01:00
|
|
|
if ($profile) {
|
2013-12-31 12:17:25 +01:00
|
|
|
$script->addProfileButton($label, $profile);
|
|
|
|
}
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2014-01-03 16:24:35 +01:00
|
|
|
$labels[] = $label; // add Label to the labels array
|
2013-12-15 11:55:44 +01:00
|
|
|
}
|
2013-12-31 17:11:53 +01:00
|
|
|
|
2014-01-03 16:24:35 +01:00
|
|
|
return $labels;
|
2013-12-15 11:55:44 +01:00
|
|
|
}
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|