TrackManiaControl/core/Manialinks/ManialinkManager.php

400 lines
11 KiB
PHP
Raw Normal View History

<?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;
use Maniaplanet\DedicatedServer\Xmlrpc\FaultException;
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
/**
* Manialink Manager Class
2014-05-02 17:50:30 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
2015-01-19 11:06:20 +01:00
* @copyright 2014-2015 ManiaControl Team
2014-05-02 17:50:30 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2013-12-30 14:52:48 +01:00
class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener {
/*
2013-12-24 12:49:53 +01:00
* Constants
*/
2014-05-02 17:50:30 +02: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';
const CB_MAIN_WINDOW_OPENED = 'ManialinkManagerCallback.MainWindowOpened';
2014-05-02 17:50:30 +02:00
/*
* Public properties
*/
/** @var StyleManager $styleManager */
2014-10-24 20:08:21 +02:00
/** @deprecated
* @see getStyleManager()
*/
public $styleManager = null;
/** @var CustomUIManager $customUIManager */
2014-10-24 20:08:21 +02:00
/** @deprecated
* @see getCustomUIManager()
*/
2013-12-30 14:52:48 +01:00
public $customUIManager = null;
/** @var IconManager $iconManager */
2014-10-24 20:08:21 +02:00
/** @deprecated
* @see getIconManager()
*/
2013-12-31 17:09:29 +01:00
public $iconManager = null;
2014-05-02 17:50:30 +02:00
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
// TODO: use listening class
2014-10-24 20:08:21 +02:00
private $pageAnswerListeners = array();
2014-04-28 17:00:53 +02:00
private $pageAnswerRegexListener = array();
/**
* Construct a new manialink manager instance
2014-05-02 17:50:30 +02:00
*
2013-12-31 12:17:25 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Children
2014-05-02 17:50:30 +02:00
$this->styleManager = new StyleManager($maniaControl);
2013-12-30 14:52:48 +01:00
$this->customUIManager = new CustomUIManager($maniaControl);
2014-05-02 17:50:30 +02:00
$this->iconManager = new IconManager($maniaControl);
// Callbacks
2013-12-30 14:52:48 +01:00
$this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback');
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
}
/**
* Register a new manialink page answer listener
2014-05-02 17:50:30 +02:00
*
* @param string $actionId
2013-12-31 12:17:25 +01:00
* @param ManialinkPageAnswerListener $listener
2014-05-02 17:50:30 +02:00
* @param string $method
* @return bool
*/
public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) {
2014-01-28 15:56:50 +01:00
if (!method_exists($listener, $method)) {
trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!");
return false;
}
2014-05-02 17:50:30 +02:00
2014-01-28 15:56:50 +01:00
if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) {
// Init listeners array
$this->pageAnswerListeners[$actionId] = array();
}
2014-05-02 17:50:30 +02:00
// Register page answer listener
array_push($this->pageAnswerListeners[$actionId], array($listener, $method));
2014-05-02 17:50:30 +02:00
2014-04-28 17:00:53 +02:00
return true;
}
2013-12-31 17:11:53 +01:00
/**
* Return the style manager
*
* @return StyleManager
*/
public function getStyleManager() {
return $this->styleManager;
}
/**
* Return the custom UI manager
*
* @return CustomUIManager
*/
public function getCustomUIManager() {
return $this->customUIManager;
}
/**
* Return the icon manager
*
* @return IconManager
*/
public function getIconManager() {
return $this->iconManager;
}
2014-04-28 17:00:53 +02:00
/**
* Register a new manialink page answer reg ex listener
2014-05-02 17:50:30 +02:00
*
* @param string $actionIdRegex
2014-04-28 17:00:53 +02:00
* @param ManialinkPageAnswerListener $listener
2014-05-02 17:50:30 +02:00
* @param string $method
2014-04-28 17:00:53 +02:00
* @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;
}
2014-05-02 17:50:30 +02:00
2014-04-28 17:00:53 +02:00
if (!array_key_exists($actionIdRegex, $this->pageAnswerRegexListener) || !is_array($this->pageAnswerRegexListener[$actionIdRegex])) {
// Init regex listeners array
$this->pageAnswerRegexListener[$actionIdRegex] = array();
}
2014-05-02 17:50:30 +02:00
// Register page answer regex listener
2014-04-28 17:00:53 +02:00
array_push($this->pageAnswerRegexListener[$actionIdRegex], array($listener, $method));
2014-05-02 17:50:30 +02:00
return true;
}
2013-12-14 23:27:15 +01:00
/**
* Remove a Manialink Page Answer Listener
2014-05-02 17:50:30 +02: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-05-02 17:50:30 +02:00
$removed = false;
2014-04-28 17:00:53 +02:00
$allListeners = array_merge($this->pageAnswerListeners, $this->pageAnswerRegexListener);
foreach ($allListeners as &$listeners) {
foreach ($listeners as $key => &$listenerCallback) {
if ($listenerCallback[0] !== $listener) {
continue;
}
unset($listeners[$key]);
$removed = true;
}
2013-12-14 23:27:15 +01:00
}
return $removed;
}
/**
* Handle ManialinkPageAnswer callback
2014-05-02 17:50:30 +02:00
*
2013-12-31 12:17:25 +01:00
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2];
2014-05-02 17:50:30 +02:00
$login = $callback[1][1];
2014-08-13 11:05:52 +02:00
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
2014-05-02 17:50:30 +02:00
2014-04-28 17:00:53 +02:00
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);
}
}
2014-05-02 17:50:30 +02:00
2014-04-28 17:00:53 +02:00
// 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);
}
}
}
}
2014-04-28 18:42:50 +02:00
/**
* Displays a ManiaLink Widget to a certain Player (Should only be used on Main Widgets)
2014-05-02 17:50:30 +02:00
*
* @param mixed $maniaLink
* @param mixed $player
* @param string $widgetName
2014-04-28 18:42:50 +02:00
*/
public function displayWidget($maniaLink, $player, $widgetName = null) {
// render and display xml
$this->sendManialink($maniaLink, $player);
if ($widgetName) {
// TODO make check by manialinkId, getter is needed to avoid uses on non main widgets
$this->disableAltMenu($player);
// Trigger callback
2014-08-13 11:05:52 +02:00
$player = $this->maniaControl->getPlayerManager()->getPlayer($player);
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_MAIN_WINDOW_OPENED, $player, $widgetName);
2014-04-28 18:42:50 +02:00
}
}
2014-05-02 17:50:30 +02:00
/**
* Send the given manialink to players
2014-05-02 17:50:30 +02:00
*
2013-12-31 12:17:25 +01:00
* @param string $manialinkText
2014-05-02 17:50:30 +02:00
* @param mixed $logins
* @param int $timeout
* @param bool $hideOnClick
* @return bool
*/
2014-01-28 16:05:18 +01:00
public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) {
2014-10-24 20:08:21 +02:00
$manialinkText = (string) $manialinkText;
2014-05-02 17:50:30 +02:00
2014-04-19 23:14:37 +02:00
if (!$manialinkText) {
2014-03-02 14:18:10 +01:00
return true;
}
2014-05-02 17:50:30 +02:00
2014-01-17 22:35:14 +01:00
try {
2014-01-28 15:56:50 +01:00
if (!$logins) {
2014-08-13 11:05:52 +02:00
return $this->maniaControl->getClient()->sendDisplayManialinkPage(null, $manialinkText, $timeout, $hideOnClick);
2014-01-17 22:35:14 +01:00
}
2014-01-28 15:56:50 +01:00
if (is_string($logins)) {
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getClient()->sendDisplayManialinkPage($logins, $manialinkText, $timeout, $hideOnClick);
2014-03-01 12:18:42 +01:00
return $success;
}
if ($logins instanceof Player) {
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getClient()->sendDisplayManialinkPage($logins->login, $manialinkText, $timeout, $hideOnClick);
2014-03-01 12:18:42 +01:00
return $success;
}
2014-01-28 15:56:50 +01:00
if (is_array($logins)) {
2014-01-17 22:35:14 +01:00
$success = true;
2014-04-28 17:00:53 +02:00
foreach ($logins as $login) {
$subSuccess = $this->sendManialink($manialinkText, $login, $timeout, $hideOnClick);
2014-01-28 15:56:50 +01:00
if (!$subSuccess) {
2014-01-17 22:35:14 +01:00
$success = false;
}
}
return $success;
}
} catch (UnknownPlayerException $e) {
2014-04-19 23:14:37 +02:00
return false;
} catch (FaultException $e) {
//TODO added 17.01.2015, remove later:
$this->maniaControl->getErrorHandler()->triggerDebugNotice("Fault Exception: ManiaLink Manager, Message: " . $e->getMessage());
return false;
}
2014-05-02 17:50:30 +02:00
2014-01-17 22:35:14 +01:00
return true;
}
/**
* Disable the alt menu for the player
2014-05-02 17:50:30 +02:00
*
* @param mixed $player
* @return bool
*/
public function disableAltMenu($player) {
$login = Player::parseLogin($player);
2014-02-13 18:18:14 +01:00
try {
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getClient()->triggerModeScriptEvent('LibXmlRpc_DisableAltMenu', $login);
} catch (GameModeException $e) {
2014-04-19 23:14:37 +02:00
return false;
2014-02-13 18:18:14 +01:00
}
return $success;
}
2013-12-24 12:49:53 +01:00
/**
* Closes a widget via the callback
2014-05-02 17:50:30 +02: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);
}
/**
2014-01-16 20:58:39 +01:00
* Closes a Manialink Widget
2014-05-02 17:50:30 +02:00
*
* @param mixed $player
* @param bool $widgetId
*/
public function closeWidget($player, $widgetId = false) {
2014-01-28 15:56:50 +01:00
if (!$widgetId) {
2014-05-18 18:37:18 +02:00
$this->hideManialink(self::MAIN_MLID, $player);
2014-01-16 20:58:39 +01:00
$this->enableAltMenu($player);
2014-05-02 17:50:30 +02:00
2014-01-16 20:58:39 +01:00
// Trigger callback
2014-08-13 11:05:52 +02:00
$player = $this->maniaControl->getPlayerManager()->getPlayer($player);
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_MAIN_WINDOW_CLOSED, $player);
2014-05-02 17:50:30 +02:00
} else {
$this->hideManialink($widgetId, $player);
}
}
/**
* Hide the Manialink with the given Id
*
* @param mixed $manialinkId
* @param mixed $logins
*/
public function hideManialink($manialinkId, $logins = null) {
if (is_array($manialinkId)) {
foreach ($manialinkId as $mlId) {
$this->hideManialink($mlId, $logins);
}
} else {
$emptyManialink = new ManiaLink($manialinkId);
$this->sendManialink($emptyManialink, $logins);
2014-01-16 20:58:39 +01:00
}
}
2014-05-02 17:50:30 +02:00
/**
* Enable the alt menu for the player
*
* @param mixed $player
2014-05-02 17:50:30 +02:00
* @return bool
*/
public function enableAltMenu($player) {
$login = Player::parseLogin($player);
2014-05-02 17:50:30 +02:00
try {
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getClient()->triggerModeScriptEvent('LibXmlRpc_EnableAltMenu', $login);
} catch (GameModeException $e) {
2014-05-02 17:50:30 +02:00
return false;
}
return $success;
}
2013-12-15 11:55:44 +01:00
/**
* Adds a line of labels
* NOTE ALWAYS SET posIsKey Manually to true
2014-05-02 17:50:30 +02:00
*
* @param Frame $frame
* @param array $labelStrings if(posIsKey) array($pos => $value)
* @param array $properties
* @param boolean $posIsKey
2014-05-27 22:21:17 +02:00
* @return Label_Text[]
2013-12-15 11:55:44 +01:00
*/
public function labelLine(Frame $frame, array $labelStrings, array $properties = array(), $posIsKey = false) {
2013-12-30 14:52:48 +01:00
// define standard properties
2014-05-02 17:50:30 +02: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);
2014-01-28 16:05:18 +01:00
$textColor = (isset($properties['textColor']) ? $properties['textColor'] : 'FFF');
2014-05-02 17:50:30 +02:00
$profile = (isset($properties['profile']) ? $properties['profile'] : false);
2014-01-03 16:24:35 +01:00
$labels = array();
foreach ($labelStrings as $key => $value) {
if ($posIsKey) {
$x = $key;
$text = $value;
} else {
$x = $value;
$text = $key;
}
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);
2014-05-02 17:50:30 +02:00
2014-01-28 15:56:50 +01:00
if ($profile) {
2014-04-28 17:00:53 +02:00
$label->addPlayerProfileFeature($profile);
2013-12-31 12:17:25 +01:00
}
2014-05-02 17:50:30 +02:00
2014-05-27 22:21:17 +02:00
array_push($labels, $label);
2013-12-15 11:55:44 +01:00
}
2014-05-02 17:50:30 +02:00
2014-01-03 16:24:35 +01:00
return $labels;
2013-12-15 11:55:44 +01:00
}
}