TrackManiaControl/core/Admin/AdminLists.php

291 lines
8.5 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Admin;
use FML\Controls\Frame;
2014-01-31 14:19:39 +01:00
use FML\Controls\Labels\Label_Button;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quads\Quad_BgRaceScore2;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\Controls\Quads\Quad_UIConstruction_Buttons;
use FML\ManiaLink;
2014-04-27 16:22:12 +02:00
use FML\Script\Features\Paging;
2017-03-25 18:40:15 +01:00
use FML\Script\Script;
2014-01-31 14:19:39 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\ManiaControl;
2017-04-01 18:48:36 +02:00
use ManiaControl\Manialinks\LabelLine;
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Players\Player;
/**
* Widget Class listing Authorized Players
2014-05-02 17:31:10 +02:00
*
2017-04-07 23:38:18 +02:00
* @api
2014-05-02 17:31:10 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
2014-05-02 17:31:10 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class AdminLists implements ManialinkPageAnswerListener, CallbackListener, UsageInformationAble {
use UsageInformationTrait;
2017-04-01 18:48:36 +02:00
/*
* Constants
*/
2014-06-17 23:35:56 +02:00
const ACTION_OPEN_ADMIN_LIST = 'AdminList.OpenAdminList';
const ACTION_REVOKE_RIGHTS = 'AdminList.RevokeRights';
/*
2014-01-31 14:29:39 +01:00
* Private Properties
*/
private $adminListShown = array();
2017-02-04 11:41:31 +01:00
/** @var ManiaControl $maniaControl */
private $maniaControl;
/**
* Construct a new PlayerList instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
$this->maniaControl->getCallbackManager()->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_CLOSED, $this, 'closeWidget');
$this->maniaControl->getCallbackManager()->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_OPENED, $this, 'handleWidgetOpened');
$this->maniaControl->getCallbackManager()->registerCallbackListener(AuthenticationManager::CB_AUTH_LEVEL_CHANGED, $this, 'updateWidget');
2014-01-31 14:29:39 +01:00
// Menu Entry AdminList
2014-08-13 11:05:52 +02:00
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_OPEN_ADMIN_LIST, $this, 'openAdminList');
$itemQuad = new Quad_UIConstruction_Buttons();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Author);
2014-06-17 23:35:56 +02:00
$itemQuad->setAction(self::ACTION_OPEN_ADMIN_LIST);
2014-08-13 11:05:52 +02:00
$this->maniaControl->getActionsMenu()->addMenuItem($itemQuad, false, 50, 'Open AdminList');
}
/**
* Open Admin List Action
2014-05-02 17:31:10 +02:00
*
2017-04-07 23:38:18 +02:00
* @internal
2014-05-02 17:31:10 +02:00
* @param array $callback
* @param Player $player
*/
public function openAdminList(array $callback, Player $player) {
$this->showAdminLists($player);
}
/**
* Show the Admin List
2014-05-02 17:31:10 +02:00
*
2017-04-07 23:38:18 +02:00
* @api
* @param Player $player
*/
public function showAdminLists(Player $player) {
2014-01-31 14:29:39 +01:00
$this->adminListShown[$player->login] = true;
2014-08-13 11:05:52 +02:00
$width = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsWidth();
$height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
2014-01-31 14:19:39 +01:00
// get Admins
2014-08-13 11:05:52 +02:00
$admins = $this->maniaControl->getAuthenticationManager()->getAdmins();
2014-01-31 14:19:39 +01:00
//Create ManiaLink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
2014-05-02 17:31:10 +02:00
$paging = new Paging();
2017-04-01 18:48:36 +02:00
$script = new Script();
2014-05-02 17:31:10 +02:00
$script->addFeature($paging);
2017-03-25 18:40:15 +01:00
$maniaLink->setScript($script);
// Main frame
2014-08-13 11:05:52 +02:00
$frame = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultListFrame($script, $paging);
2017-03-25 19:15:50 +01:00
$maniaLink->addChild($frame);
// Start offsets
$posX = -$width / 2;
$posY = $height / 2;
2014-01-31 14:19:39 +01:00
//Predefine description Label
2014-08-13 11:05:52 +02:00
$descriptionLabel = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultDescriptionLabel();
2017-03-25 19:15:50 +01:00
$frame->addChild($descriptionLabel);
2014-01-31 14:19:39 +01:00
// Headline
$headFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($headFrame);
$headFrame->setY($posY - 5);
2017-04-01 18:48:36 +02:00
$labelLine = new LabelLine($headFrame);
$labelLine->addLabelEntryText('Id', $posX + 5);
$labelLine->addLabelEntryText('Nickname', $posX + 18);
$labelLine->addLabelEntryText('Login', $posX + 70);
$labelLine->addLabelEntryText('Actions', $posX + 120);
$labelLine->render();
$index = 1;
$posY -= 10;
2014-05-15 15:13:30 +02:00
$pageFrame = null;
2023-09-08 16:00:30 +02:00
$pageMaxCount = $this->getPlayersPerPage();
2014-05-15 15:13:30 +02:00
2014-05-02 17:31:10 +02:00
foreach ($admins as $admin) {
2023-09-08 16:00:30 +02:00
if ($index % $pageMaxCount === 1) {
$pageFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($pageFrame);
2014-05-15 15:13:30 +02:00
2017-03-25 18:40:15 +01:00
$paging->addPageControl($pageFrame);
$posY = $height / 2 - 10;
}
$playerFrame = new Frame();
2017-03-25 19:15:50 +01:00
$pageFrame->addChild($playerFrame);
$playerFrame->setY($posY);
if ($index % 2 !== 0) {
$lineQuad = new Quad_BgsPlayerCard();
2017-03-25 19:15:50 +01:00
$playerFrame->addChild($lineQuad);
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
2017-04-11 12:28:04 +02:00
$lineQuad->setZ(-0.1);
}
2017-04-01 18:48:36 +02:00
$labelLine = new LabelLine($playerFrame);
2017-04-07 23:38:18 +02:00
$labelLine->addLabelEntryText($index, $posX + 5, 13);
$labelLine->addLabelEntryText($admin->nickname, $posX + 18, 52);
$labelLine->addLabelEntryText($admin->login, $posX + 70, 48);
2017-04-01 18:48:36 +02:00
$labelLine->render();
// Level Quad
$rightQuad = new Quad_BgRaceScore2();
2017-03-25 19:15:50 +01:00
$playerFrame->addChild($rightQuad);
$rightQuad->setX($posX + 13);
$rightQuad->setZ(5);
$rightQuad->setSubStyle($rightQuad::SUBSTYLE_CupFinisher);
$rightQuad->setSize(7, 3.5);
$rightLabel = new Label_Text();
2017-03-25 19:15:50 +01:00
$playerFrame->addChild($rightLabel);
$rightLabel->setX($posX + 13.9);
$rightLabel->setTextSize(0);
$rightLabel->setZ(10);
2014-08-13 11:05:52 +02:00
$rightLabel->setText($this->maniaControl->getAuthenticationManager()->getAuthLevelAbbreviation($admin));
$description = $this->maniaControl->getAuthenticationManager()->getAuthLevelName($admin) . " " . $admin->nickname;
2014-05-02 17:31:10 +02:00
$rightLabel->addTooltipLabelFeature($descriptionLabel, $description);
2014-01-31 14:19:39 +01:00
//Revoke Button
2014-08-05 02:17:41 +02:00
if ($admin->authLevel > 0
2014-08-13 11:05:52 +02:00
&& $this->maniaControl->getAuthenticationManager()->checkRight($player, $admin->authLevel + 1)
2014-08-05 02:17:41 +02:00
) {
2014-01-31 14:19:39 +01:00
//Settings
$style = Label_Text::STYLE_TextCardSmall;
$textColor = 'FFF';
$quadWidth = 24;
$quadHeight = 3.4;
// Quad
$quad = new Quad_BgsPlayerCard();
2017-03-25 19:15:50 +01:00
$playerFrame->addChild($quad);
2014-01-31 14:19:39 +01:00
$quad->setZ(11);
$quad->setX($posX + 130);
2014-01-31 14:19:39 +01:00
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setSize($quadWidth, $quadHeight);
$quad->setAction(self::ACTION_REVOKE_RIGHTS . "." . $admin->login);
//Label
$label = new Label_Button();
2017-03-25 19:15:50 +01:00
$playerFrame->addChild($label);
$label->setX($posX + 130);
2014-01-31 14:19:39 +01:00
$quad->setZ(12);
$label->setStyle($style);
$label->setTextSize(1);
$label->setTextColor($textColor);
$label->setText("Revoke Rights");
}
$posY -= 4;
$index++;
}
// Render and display xml
2014-08-13 11:05:52 +02:00
$this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $player, 'AdminList');
}
2014-01-31 14:19:39 +01:00
/**
* Called on ManialinkPageAnswer
*
2017-04-07 23:38:18 +02:00
* @internal
2014-01-31 14:19:39 +01:00
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2];
$actionArray = explode('.', $actionId, 3);
if (count($actionArray) <= 2) {
return;
}
$action = $actionArray[0] . '.' . $actionArray[1];
2014-01-31 14:19:39 +01:00
$adminLogin = $callback[1][1];
$targetLogin = $actionArray[2];
2014-05-02 17:31:10 +02:00
switch ($action) {
2014-01-31 14:19:39 +01:00
case self::ACTION_REVOKE_RIGHTS:
2014-08-13 11:05:52 +02:00
$this->maniaControl->getPlayerManager()->getPlayerActions()->revokeAuthLevel($adminLogin, $targetLogin);
2014-01-31 14:19:39 +01:00
break;
}
}
2023-09-08 16:00:30 +02:00
/**
* Get number of players per page
*
* @return int
*/
public function getPlayersPerPage() {
$pageheight = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
return floor(($pageheight - 16) / 4);
2023-09-08 16:00:30 +02:00
}
2014-01-31 14:29:39 +01:00
/**
* Reopen the widget on Map Begin, MapListChanged, etc.
*
2017-04-07 23:38:18 +02:00
* @internal
* @param Player $player
2014-01-31 14:29:39 +01:00
*/
2014-02-27 13:16:34 +01:00
public function updateWidget(Player $player) {
2014-05-02 17:31:10 +02:00
foreach ($this->adminListShown as $login => $shown) {
2014-01-31 14:29:39 +01:00
if ($shown) {
2014-08-13 11:05:52 +02:00
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
if ($player) {
2014-01-31 14:29:39 +01:00
$this->showAdminLists($player);
} else {
unset($this->adminListShown[$login]);
}
}
}
}
/**
* Closes the widget
*
2017-04-07 23:38:18 +02:00
* @internal
2014-10-24 20:20:12 +02:00
* @param Player $player
2014-01-31 14:29:39 +01:00
*/
2014-02-19 17:37:37 +01:00
public function closeWidget(Player $player) {
2014-01-31 14:29:39 +01:00
unset($this->adminListShown[$player->login]);
}
/**
* Unset the player if he opened another Main Widget
*
2017-04-07 23:38:18 +02:00
* @internal
2014-02-19 17:37:37 +01:00
* @param Player $player
2014-10-24 20:20:12 +02:00
* @param string $openedWidget
2014-01-31 14:29:39 +01:00
*/
2014-02-19 17:37:37 +01:00
public function handleWidgetOpened(Player $player, $openedWidget) {
2014-01-31 14:29:39 +01:00
//unset when another main widget got opened
if ($openedWidget !== 'AdminList') {
2014-01-31 14:29:39 +01:00
unset($this->adminListShown[$player->login]);
}
}
}