2013-11-28 02:04:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Manialinks;
|
|
|
|
|
2013-12-15 11:55:44 +01:00
|
|
|
use FML\Controls\Control;
|
|
|
|
use FML\Controls\Frame;
|
|
|
|
use FML\Controls\Labels\Label_Text;
|
2013-12-15 11:29:49 +01:00
|
|
|
use FML\ManiaLink;
|
2013-11-28 02:04:06 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2013-11-28 04:48:04 +01:00
|
|
|
use ManiaControl\Players\Player;
|
2013-12-24 12:49:53 +01:00
|
|
|
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
2013-11-28 02:04:06 +01:00
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
require_once __DIR__ . '/StyleManager.php';
|
2013-11-28 02:04:06 +01:00
|
|
|
require_once __DIR__ . '/../FML/autoload.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manialink manager class
|
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
|
|
|
*/
|
2013-12-24 12:49:53 +01:00
|
|
|
class ManialinkManager implements ManialinkPageAnswerListener,CallbackListener {
|
2013-12-15 11:29:49 +01:00
|
|
|
|
2013-12-24 12:49:53 +01:00
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
2013-12-15 11:29:49 +01:00
|
|
|
const MAIN_MLID = 'Main.ManiaLinkId';
|
2013-12-24 12:49:53 +01:00
|
|
|
const ACTION_CLOSEWIDGET = 'ManiaLinkManager.CloseWidget';
|
2013-11-28 03:47:08 +01:00
|
|
|
/**
|
|
|
|
* Public properties
|
|
|
|
*/
|
|
|
|
public $styleManager = null;
|
2013-11-28 02:04:06 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
|
|
|
private $pageAnswerListeners = array();
|
2013-11-28 03:47:08 +01:00
|
|
|
private $maniaLinkIdCount = 0;
|
2013-11-28 02:04:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new manialink manager
|
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2013-11-28 03:47:08 +01:00
|
|
|
$this->styleManager = new StyleManager($maniaControl);
|
|
|
|
|
|
|
|
// Register for callbacks
|
2013-12-24 12:49:53 +01:00
|
|
|
$this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET , $this,
|
|
|
|
'closeWidgetCallback');
|
2013-11-28 02:04:06 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this,
|
|
|
|
'handleManialinkPageAnswer');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new manialink page answer listener
|
|
|
|
*
|
2013-11-28 03:47:08 +01:00
|
|
|
* @param string $actionId
|
2013-11-28 02:04:06 +01:00
|
|
|
* @param ManialinkPageAnswerListener $listener
|
|
|
|
* @param string $method
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-11-28 03:47:08 +01:00
|
|
|
public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) {
|
2013-11-28 02:04:06 +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-11-28 02:04:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-11-28 03:47:08 +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-11-28 02:04:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-14 23:27:15 +01:00
|
|
|
/**
|
|
|
|
* Remove a Manialink Page Answer Listener
|
|
|
|
*
|
|
|
|
* @param ManialinkPageAnswerListener $listener
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function unregisterManialinkPageAnswerListener(ManialinkPageAnswerListener $listener) {
|
|
|
|
$keys = array_keys($this->pageAnswerListeners, $listener);
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
unset($this->pageAnswerListeners[$key]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-28 03:47:08 +01:00
|
|
|
/**
|
|
|
|
* Reserve manialink ids
|
|
|
|
*
|
|
|
|
* @param int $count
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function reserveManiaLinkIds($count) {
|
|
|
|
$manialinkIds = array();
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
array_push($manialinkIds, $this->maniaLinkIdCount++);
|
|
|
|
}
|
|
|
|
return $manialinkIds;
|
|
|
|
}
|
|
|
|
|
2013-11-28 02:04:06 +01:00
|
|
|
/**
|
|
|
|
* Handle ManialinkPageAnswer callback
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handleManialinkPageAnswer(array $callback) {
|
2013-11-28 03:47:08 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
// Inform page answer listeners
|
|
|
|
foreach ($this->pageAnswerListeners[$actionId] as $listener) {
|
|
|
|
call_user_func(array($listener[0], $listener[1]), $callback, $player);
|
|
|
|
}
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the given manialink to players
|
|
|
|
*
|
|
|
|
* @param string $manialinkText
|
|
|
|
* @param mixed $logins
|
|
|
|
* @param int $timeout
|
|
|
|
* @param bool $hideOnClick
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) {
|
|
|
|
if (!$logins) {
|
|
|
|
return $this->maniaControl->client->query('SendDisplayManialinkPage', $manialinkText, $timeout, $hideOnClick);
|
|
|
|
}
|
|
|
|
if (is_string($logins)) {
|
|
|
|
return $this->maniaControl->client->query('SendDisplayManialinkPageToLogin', $logins, $manialinkText, $timeout,
|
|
|
|
$hideOnClick);
|
|
|
|
}
|
|
|
|
if (is_array($logins)) {
|
|
|
|
$success = true;
|
|
|
|
foreach ($logins as $login) {
|
|
|
|
$subSuccess = $this->maniaControl->client->query('SendDisplayManialinkPageToLogin', $login, $manialinkText, $timeout,
|
|
|
|
$hideOnClick);
|
|
|
|
if (!$subSuccess) {
|
|
|
|
$success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-28 04:48:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the alt menu for the player
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function enableAltMenu(Player $player) {
|
|
|
|
return $this->maniaControl->client->query('TriggerModeScriptEvent', 'LibXmlRpc_EnableAltMenu', $player->login);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the alt menu for the player
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function disableAltMenu(Player $player) {
|
|
|
|
return $this->maniaControl->client->query('TriggerModeScriptEvent', 'LibXmlRpc_DisableAltMenu', $player->login);
|
|
|
|
}
|
2013-12-15 11:29:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays a ManiaLink Widget to a certain Player
|
|
|
|
* @param String $maniaLink
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function displayWidget($maniaLink, Player $player) {
|
|
|
|
//render and display xml
|
|
|
|
$maniaLinkText = $maniaLink->render()->saveXML();
|
|
|
|
$this->maniaControl->manialinkManager->sendManialink($maniaLinkText, $player->login);
|
|
|
|
$this->maniaControl->manialinkManager->disableAltMenu($player);
|
|
|
|
}
|
|
|
|
|
2013-12-24 12:49:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes a widget via the callback
|
|
|
|
* @param array $callback
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function closeWidgetCallback(array $callback, Player $player) {
|
|
|
|
$this->closeWidget($player);
|
|
|
|
}
|
|
|
|
|
2013-12-15 11:29:49 +01:00
|
|
|
/**
|
|
|
|
* Closes the Manialink Widget and enables the Alt Menu
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function closeWidget(Player $player) {
|
|
|
|
$emptyManialink = new ManiaLink(self::MAIN_MLID);
|
|
|
|
$manialinkText = $emptyManialink->render()->saveXML();
|
|
|
|
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login);
|
|
|
|
$this->maniaControl->manialinkManager->enableAltMenu($player);
|
|
|
|
//unset($this->playersMenuShown[$player->login]);
|
|
|
|
}
|
|
|
|
|
2013-12-15 11:55:44 +01:00
|
|
|
/**
|
|
|
|
* Adds a line of labels
|
|
|
|
* @param Frame $frame
|
|
|
|
* @param array $labelStrings
|
|
|
|
* @param array $properties
|
2013-12-15 12:34:35 +01:00
|
|
|
* @return array Returns the frames (to add special Properties later)
|
2013-12-15 11:55:44 +01:00
|
|
|
*/
|
|
|
|
public function labelLine(Frame $frame, array $labelStrings, array $properties = array()){
|
|
|
|
//TODO overwrite standard properties with properties from array
|
2013-12-15 11:59:06 +01:00
|
|
|
|
2013-12-15 11:55:44 +01:00
|
|
|
//define standard properties
|
|
|
|
$hAlign = Control::LEFT;
|
|
|
|
$style = Label_Text::STYLE_TextCardSmall;
|
|
|
|
$textSize = 1.5;
|
|
|
|
$textColor = 'FFF';
|
|
|
|
|
2013-12-15 12:34:35 +01:00
|
|
|
$frames = array();
|
2013-12-15 11:55:44 +01:00
|
|
|
foreach($labelStrings as $text => $x){
|
|
|
|
$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-15 12:34:35 +01:00
|
|
|
|
|
|
|
$frames[] = $frame; //add Frame to the frames array
|
2013-12-15 11:55:44 +01:00
|
|
|
}
|
2013-12-15 12:34:35 +01:00
|
|
|
return $frames;
|
2013-12-15 11:55:44 +01:00
|
|
|
}
|
2013-11-28 02:04:06 +01:00
|
|
|
}
|