TrackManiaControl/core/ManiaExchange/ManiaExchangeList.php

364 lines
12 KiB
PHP
Raw Normal View History

2014-01-28 16:54:23 +01:00
<?php
namespace ManiaControl\ManiaExchange;
use FML\Controls\Frame;
2014-04-13 18:15:38 +02:00
use FML\Controls\Gauge;
use FML\Controls\Label;
2014-01-28 16:54:23 +01:00
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quad;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\ManiaLink;
2014-04-27 16:22:12 +02:00
use FML\Script\Features\Paging;
2014-01-28 16:54:23 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\IconManager;
2017-03-29 23:26:03 +02:00
use ManiaControl\Manialinks\LabelLine;
2014-01-28 16:54:23 +01:00
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Maps\MapCommands;
2014-04-13 18:15:38 +02:00
use ManiaControl\Maps\MapManager;
2014-01-28 16:54:23 +01:00
use ManiaControl\Players\Player;
2014-05-15 17:45:08 +02:00
use ManiaControl\Utils\ColorUtil;
use ManiaControl\Utils\Formatter;
2014-01-28 16:54:23 +01:00
/**
* ManiaExchange List Widget Class
*
2014-05-02 17:50:30 +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:50:30 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-28 16:54:23 +01:00
*/
class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener {
/*
2014-01-28 16:54:23 +01:00
* Constants
*/
const ACTION_ADD_MAP = 'ManiaExchangeList.AddMap';
const ACTION_SEARCH_MAPNAME = 'ManiaExchangeList.SearchMapName';
const ACTION_SEARCH_AUTHOR = 'ManiaExchangeList.SearchAuthor';
const ACTION_GET_MAPS_FROM_AUTHOR = 'ManiaExchangeList.GetMapsFromAuthor';
const MAX_MX_MAPS_PER_PAGE = 14;
/*
* Private properties
2014-01-28 16:54:23 +01:00
*/
/** @var ManiaControl $maniaControl */
2014-01-28 16:54:23 +01:00
private $maniaControl = null;
private $mapListShown = array();
/**
* Construct a new MX List instance
2014-01-28 16:54:23 +01:00
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Callbacks
2014-08-13 11:05:52 +02:00
$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(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
2017-03-29 19:54:50 +02:00
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_SEARCH_MAPNAME, $this, 'showListCommand');
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_SEARCH_AUTHOR, $this, 'showListCommand');
2014-01-28 16:54:23 +01:00
}
2014-05-02 17:50:30 +02:00
/**
* Handle ManialinkPageAnswer Callback
*
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
2023-08-14 17:11:09 +02:00
if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return;
2014-05-02 17:50:30 +02:00
$actionId = $callback[1][2];
$actionArray = explode('.', $actionId);
if (count($actionArray) <= 2) {
return;
}
$action = $actionArray[0] . '.' . $actionArray[1];
$login = $callback[1][1];
2014-08-13 11:05:52 +02:00
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
2014-10-24 20:20:12 +02:00
$mapId = (int) $actionArray[2];
2014-05-02 17:50:30 +02:00
2017-03-29 19:54:50 +02:00
2014-05-02 17:50:30 +02:00
switch ($action) {
case self::ACTION_GET_MAPS_FROM_AUTHOR:
$callback[1][2] = 'auth:' . $actionArray[2];
2017-03-29 19:54:50 +02:00
$this->showListCommand($callback, $player);
2014-05-02 17:50:30 +02:00
break;
case self::ACTION_ADD_MAP:
2014-08-13 11:05:52 +02:00
$this->maniaControl->getMapManager()->addMapFromMx($mapId, $player->login);
2014-05-02 17:50:30 +02:00
break;
}
}
2014-01-28 16:54:23 +01:00
/**
2014-02-07 14:05:10 +01:00
* Shows the List
2014-01-28 16:54:23 +01:00
*
* @param array $chatCallback
* @param Player $player
*/
2017-03-29 19:54:50 +02:00
public function showListCommand(array $chatCallback, Player $player) {
2023-08-14 17:11:09 +02:00
if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return;
2014-01-28 16:54:23 +01:00
$this->mapListShown[$player->login] = true;
2014-02-07 14:05:10 +01:00
$params = explode(' ', $chatCallback[1][2]);
$searchString = '';
$author = '';
$environment = '';
2014-01-28 16:54:23 +01:00
if (count($params) >= 1) {
2014-05-02 17:50:30 +02:00
foreach ($params as $param) {
if ($param === '/xlist' || $param === MapCommands::ACTION_OPEN_XLIST) {
2014-01-28 16:54:23 +01:00
continue;
}
if ($param === self::ACTION_SEARCH_MAPNAME) {
2014-01-28 16:54:23 +01:00
$searchString = $chatCallback[1][3][0]['Value'];
} else if ($param === self::ACTION_SEARCH_AUTHOR) {
2014-01-28 16:54:23 +01:00
$author = $chatCallback[1][3][0]['Value'];
} else if (strtolower(substr($param, 0, 5)) === 'auth:') {
2014-01-28 16:54:23 +01:00
$author = substr($param, 5);
} else if (strtolower(substr($param, 0, 4)) === 'env:') {
2014-01-28 16:54:23 +01:00
$environment = substr($param, 4);
} else {
if (!$searchString) {
2014-01-28 16:54:23 +01:00
$searchString = $param;
} else {
// concatenate words in name
2014-01-28 16:54:23 +01:00
$searchString .= '%20' . $param;
}
}
}
}
2017-03-29 19:54:50 +02:00
$this->getMXMapsAndShowList($player, $author, $environment, $searchString);
}
/**
* Gets MX Maps and displays maplist
*
* @param \ManiaControl\Players\Player $player
* @param string $author
* @param string $environment
* @param string $searchString
*/
private function getMXMapsAndShowList(Player $player, $author = '', $environment = '', $searchString = '') {
2023-08-14 17:11:09 +02:00
if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return;
2017-05-19 18:35:33 +02:00
//TODO do more clean solution
if($environment == ""){
$titleId = $this->maniaControl->getServer()->titleId;
//Set Environments on Trackmania
$game = explode('@', $titleId);
$envNumber = ManiaExchangeMapSearch::getEnvironment($game[0]); //TODO enviroment as constant
if ($envNumber > -1) {
$environment = $envNumber;
}
}
2015-02-01 11:34:51 +01:00
//Search the Maps
$mxSearch = new ManiaExchangeMapSearch($this->maniaControl);
$mxSearch->setAuthorName($author);
$mxSearch->setEnvironments($environment);
$mxSearch->setMapName($searchString);
2017-05-12 21:16:54 +02:00
2015-02-01 11:34:51 +01:00
$mxSearch->fetchMapsAsync(function (array $maps) use (&$player) {
2014-08-25 15:33:22 +02:00
if (!$maps) {
$this->maniaControl->getChat()->sendError('No maps found, or MX is down!', $player);
2014-08-25 15:33:22 +02:00
return;
}
$this->showManiaExchangeList($maps, $player);
2015-02-01 11:34:51 +01:00
});
// show temporary list to wait for Async
$labelStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultLabelStyle();
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$frame = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultListFrame();
$maniaLink->addChild($frame);
$loadingLabel = new Label_Text();
$frame->addChild($loadingLabel);
$loadingLabel->setStyle($labelStyle);
$loadingLabel->setText('Loading maps, please wait ...');
$loadingLabel->setTextSize(2);
$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $player);
2014-02-07 14:05:10 +01:00
}
2014-01-28 16:54:23 +01:00
2017-03-29 19:54:50 +02:00
2014-02-07 14:05:10 +01:00
/**
* Display the Mania Exchange List
*
2014-05-27 22:25:55 +02:00
* @param MXMapInfo[] $maps
* @param Player $player
2014-02-07 14:17:03 +01:00
* @internal param array $chatCallback
2014-02-07 14:05:10 +01:00
*/
2014-05-27 22:25:55 +02:00
private function showManiaExchangeList(array $maps, Player $player) {
2023-08-14 17:11:09 +02:00
if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return;
2014-01-28 16:54:23 +01:00
// Start offsets
2014-08-13 11:05:52 +02:00
$width = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsWidth();
$height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
$posX = -$width / 2;
$posY = $height / 2;
2014-01-28 16:54:23 +01:00
2014-01-28 19:28:17 +01:00
//Create ManiaLink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$script = $maniaLink->getScript();
2014-05-02 17:50:30 +02:00
$paging = new Paging();
$script->addFeature($paging);
2014-01-28 19:28:17 +01:00
// 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);
2014-01-28 16:54:23 +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-28 16:54:23 +01:00
// Headline
$headFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($headFrame);
$headFrame->setY($posY - 12);
2014-01-28 16:54:23 +01:00
2017-03-29 23:26:03 +02:00
$labelLine = new LabelLine($headFrame);
$labelLine->addLabelEntryText('Id', $posX + 3.5, 8);
$labelLine->addLabelEntryText('Name', $posX + 12.5, 37.5);
$labelLine->addLabelEntryText('Author', $posX + 59, 43);
$labelLine->addLabelEntryText('Type', $posX + 103, 14);
$labelLine->addLabelEntryText('Mood', $posX + 118, 11);
$labelLine->addLabelEntryText('Last Update', $posX + 130, $width - ($posX + 131));
2017-03-29 23:26:03 +02:00
$labelLine->render();
$index = 0;
$posY = $height / 2 - 16;
2014-05-15 17:45:08 +02:00
$pageFrame = null;
foreach ($maps as $map) {
//TODO order possibilities
if ($index % self::MAX_MX_MAPS_PER_PAGE === 0) {
2014-01-28 16:54:23 +01:00
$pageFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($pageFrame);
$posY = $height / 2 - 16;
2017-03-25 18:40:15 +01:00
$paging->addPageControl($pageFrame);
2014-01-28 16:54:23 +01:00
}
// Map Frame
$mapFrame = new Frame();
2017-03-25 19:15:50 +01:00
$pageFrame->addChild($mapFrame);
2014-01-28 16:54:23 +01:00
if ($index % 2 === 0) {
2014-01-28 16:54:23 +01:00
$lineQuad = new Quad_BgsPlayerCard();
2017-03-25 19:15:50 +01:00
$mapFrame->addChild($lineQuad);
2014-01-28 16:54:23 +01:00
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
2017-04-11 12:28:04 +02:00
$lineQuad->setZ(-0.1);
2014-01-28 16:54:23 +01:00
}
2017-03-29 23:26:03 +02:00
$time = Formatter::timeElapsedString(strtotime($map->updated));
$labelLine = new LabelLine($mapFrame);
2017-03-30 16:25:54 +02:00
2017-03-29 23:26:03 +02:00
$labelLine->addLabelEntryText($map->id, $posX + 3.5, 9);
$labelLine->addLabelEntryText($map->name, $posX + 12.5, 38.5);
$labelLine->addLabelEntryText($map->author, $posX + 59, 20, self::ACTION_GET_MAPS_FROM_AUTHOR . '.' . $map->author);
2017-03-29 23:26:03 +02:00
$labelLine->addLabelEntryText(str_replace('Arena', '', $map->maptype), $posX + 103, 15);
$labelLine->addLabelEntryText($map->mood, $posX + 118, 12);
$labelLine->addLabelEntryText($time, $posX + 130, $width - ($posX + 130));
$labelLine->setPrefix('$s');
$labelLine->render();
2014-01-28 16:54:23 +01:00
$mapFrame->setY($posY);
2014-01-28 16:54:23 +01:00
$mxQuad = new Quad();
2017-03-25 19:15:50 +01:00
$mapFrame->addChild($mxQuad);
2014-01-28 16:54:23 +01:00
$mxQuad->setSize(3, 3);
2017-03-25 19:15:50 +01:00
$mxQuad->setImageUrl($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON));
$mxQuad->setImageFocusUrl($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON_MOVER));
$mxQuad->setX($posX + 56);
2014-01-28 16:54:23 +01:00
$mxQuad->setUrl($map->pageurl);
$mxQuad->setZ(0.01);
2014-05-02 17:50:30 +02:00
$description = 'View $<' . $map->name . '$> on Mania-Exchange';
$mxQuad->addTooltipLabelFeature($descriptionLabel, $description);
2014-01-28 16:54:23 +01:00
2017-03-29 19:54:50 +02:00
if ($this->maniaControl->getAuthenticationManager()->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)) {
2014-01-28 16:54:23 +01:00
$addQuad = new Quad_Icons64x64_1();
2017-03-25 19:15:50 +01:00
$mapFrame->addChild($addQuad);
$addQuad->setX($posX + 53);
2014-01-28 16:54:23 +01:00
$addQuad->setZ(-0.1);
$addQuad->setSubStyle($addQuad::SUBSTYLE_Add);
$addQuad->setSize(4, 4);
$addQuad->setAction(self::ACTION_ADD_MAP . '.' . $map->id);
$addQuad->setZ(0.01);
2014-05-02 17:50:30 +02:00
$description = 'Add-Map: $<' . $map->name . '$>';
$addQuad->addTooltipLabelFeature($descriptionLabel, $description);
2014-01-28 16:54:23 +01:00
}
//Award Quad
if ($map->awards > 0) {
$awardQuad = new Quad_Icons64x64_1();
2017-03-25 19:15:50 +01:00
$mapFrame->addChild($awardQuad);
2014-01-28 16:54:23 +01:00
$awardQuad->setSize(3, 3);
$awardQuad->setSubStyle($awardQuad::SUBSTYLE_OfficialRace);
$awardQuad->setX($posX + 97);
2014-01-28 16:54:23 +01:00
$awardQuad->setZ(0.01);
$awardLabel = new Label_Text();
2017-03-25 19:15:50 +01:00
$mapFrame->addChild($awardLabel);
$awardLabel->setX($posX + 98.5);
2017-03-25 19:15:50 +01:00
$awardLabel->setHorizontalAlign($awardLabel::LEFT);
2014-01-28 16:54:23 +01:00
$awardLabel->setText($map->awards);
$awardLabel->setTextSize(1);
2014-01-28 16:54:23 +01:00
}
2014-04-13 18:15:38 +02:00
//Map Karma
$karmaGauge = $this->maniaControl->getManialinkManager()->getElementBuilder()->buildKarmaGauge(
$map,
20,
10
);
2020-05-11 17:05:56 +02:00
if ($karmaGauge) {
$mapFrame->addChild($karmaGauge);
$karmaGauge->setX($posX + 87);
}
2014-04-13 18:15:38 +02:00
$posY -= 4;
$index++;
2014-01-28 16:54:23 +01:00
}
2017-05-12 21:16:54 +02:00
$searchFrame = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultMapSearch(self::ACTION_SEARCH_MAPNAME, self::ACTION_SEARCH_AUTHOR);
$searchFrame->setY($height / 2 - 5);
$frame->addChild($searchFrame);
2017-03-29 19:54:50 +02:00
2014-01-28 16:54:23 +01:00
// render and display xml
2014-08-13 11:05:52 +02:00
$this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $player, 'ManiaExchangeList');
2014-01-28 16:54:23 +01:00
}
/**
* Unset the player if he opened another Main Widget
*
2014-02-19 17:37:37 +01:00
* @param Player $player
* @param $openedWidget
2014-01-28 16:54:23 +01:00
*/
2014-02-19 17:37:37 +01:00
public function handleWidgetOpened(Player $player, $openedWidget) {
2023-08-14 17:11:09 +02:00
if (!$this->maniaControl->getMapManager()->getMXManager()->getStatus()) return;
2014-01-28 16:54:23 +01:00
//unset when another main widget got opened
if ($openedWidget !== 'ManiaExchangeList') {
2014-01-28 16:54:23 +01:00
unset($this->mapListShown[$player->login]);
}
}
/**
* Closes the widget
*
2014-02-19 17:37:37 +01:00
* @param \ManiaControl\Players\Player $player
* @internal param array $callback
2014-01-28 16:54:23 +01:00
*/
2014-02-19 17:37:37 +01:00
public function closeWidget(Player $player) {
2014-01-28 16:54:23 +01:00
unset($this->mapListShown[$player->login]);
}
2014-10-24 20:20:12 +02:00
}