TrackManiaControl/core/ManiaExchange/ManiaExchangeList.php

357 lines
12 KiB
PHP
Raw Normal View History

2014-01-28 16:54:23 +01:00
<?php
namespace ManiaControl\ManiaExchange;
use FML\Controls\Entry;
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_Button;
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;
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>
* @copyright 2014 ManiaControl Team
* @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');
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_SEARCH_MAPNAME, $this, 'showList');
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_SEARCH_AUTHOR, $this, 'showList');
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) {
$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
switch ($action) {
case self::ACTION_GET_MAPS_FROM_AUTHOR:
$callback[1][2] = 'auth:' . $actionArray[2];
$this->showList($callback, $player);
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
*/
2014-02-07 14:05:10 +01:00
public function showList(array $chatCallback, Player $player) {
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;
}
}
}
}
2014-04-20 14:52:26 +02:00
// search for matching maps
2014-08-13 11:05:52 +02:00
$this->maniaControl->getMapManager()->getMXManager()->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->login);
return;
}
$this->showManiaExchangeList($maps, $player);
}, $searchString, $author, $environment);
2014-02-07 14:05:10 +01:00
}
2014-01-28 16:54:23 +01: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) {
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);
2014-01-28 19:28:17 +01:00
$maniaLink->add($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();
2014-01-28 16:54:23 +01:00
$frame->add($descriptionLabel);
// Headline
$headFrame = new Frame();
$frame->add($headFrame);
$headFrame->setY($posY - 12);
$array = array('$oId' => $posX + 3.5, '$oName' => $posX + 12.5, '$oAuthor' => $posX + 59, '$oKarma' => $posX + 85, '$oType' => $posX + 103, '$oMood' => $posX + 118, '$oLast Update' => $posX + 130);
2014-08-13 11:05:52 +02:00
$this->maniaControl->getManialinkManager()->labelLine($headFrame, $array);
2014-01-28 16:54:23 +01:00
$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();
$frame->add($pageFrame);
$posY = $height / 2 - 16;
2014-05-02 17:50:30 +02:00
$paging->addPage($pageFrame);
2014-01-28 16:54:23 +01:00
}
// Map Frame
$mapFrame = new Frame();
$pageFrame->add($mapFrame);
if ($index % 2 === 0) {
2014-01-28 16:54:23 +01:00
$lineQuad = new Quad_BgsPlayerCard();
$mapFrame->add($lineQuad);
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(0.001);
}
2014-05-27 22:25:55 +02:00
$time = Formatter::time_elapsed_string(strtotime($map->updated));
$array = array('$s' . $map->id => $posX + 3.5, '$s' . $map->name => $posX + 12.5, '$s' . $map->author => $posX + 59, '$s' . str_replace('Arena', '', $map->maptype) => $posX + 103, '$s' . $map->mood => $posX + 118, '$s' . $time => $posX + 130);
2014-08-13 11:05:52 +02:00
$labels = $this->maniaControl->getManialinkManager()->labelLine($mapFrame, $array);
2014-01-28 16:54:23 +01:00
$authorLabel = $labels[2];
$authorLabel->setAction(self::ACTION_GET_MAPS_FROM_AUTHOR . '.' . $map->author);
$mapFrame->setY($posY);
2014-01-28 16:54:23 +01:00
$mxQuad = new Quad();
$mapFrame->add($mxQuad);
$mxQuad->setSize(3, 3);
2014-08-13 11:05:52 +02:00
$mxQuad->setImage($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON));
$mxQuad->setImageFocus($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
2014-08-13 11:05:52 +02:00
if ($this->maniaControl->getAuthenticationManager()->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)
2014-08-05 02:17:41 +02:00
) {
2014-01-28 16:54:23 +01:00
$addQuad = new Quad_Icons64x64_1();
$mapFrame->add($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();
$mapFrame->add($awardQuad);
$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();
$mapFrame->add($awardLabel);
$awardLabel->setX($posX + 98.5);
2014-06-22 19:02:18 +02:00
$awardLabel->setHAlign($awardLabel::LEFT);
2014-01-28 16:54:23 +01:00
$awardLabel->setText($map->awards);
$awardLabel->setTextSize(1.3);
}
2014-04-13 18:15:38 +02:00
//Map Karma
2014-05-02 17:50:30 +02:00
$karma = $map->ratingVoteAverage / 100;
2014-04-13 18:15:38 +02:00
$voteCount = $map->ratingVoteCount;
if (is_numeric($karma) && $voteCount > 0) {
$karmaGauge = new Gauge();
$mapFrame->add($karmaGauge);
$karmaGauge->setZ(2);
$karmaGauge->setX($posX + 89);
2014-04-14 19:27:56 +02:00
$karmaGauge->setSize(16.5, 9);
2014-04-13 18:15:38 +02:00
$karmaGauge->setDrawBg(false);
$karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '9');
$karmaLabel = new Label();
$mapFrame->add($karmaLabel);
$karmaLabel->setZ(2);
$karmaLabel->setX($posX + 89);
2014-04-14 19:27:56 +02:00
$karmaLabel->setSize(16.5 * 0.9, 5);
2014-04-13 18:15:38 +02:00
$karmaLabel->setTextSize(0.9);
$karmaLabel->setTextColor('000');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')');
}
$posY -= 4;
$index++;
2014-01-28 16:54:23 +01:00
}
$label = new Label_Text();
$frame->add($label);
$label->setPosition(-$width / 2 + 5, $height / 2 - 5);
2014-06-22 19:02:18 +02:00
$label->setHAlign($label::LEFT);
2014-01-28 16:54:23 +01:00
$label->setTextSize(1.3);
2014-06-22 19:02:18 +02:00
$label->setText('Search: ');
2014-01-28 16:54:23 +01:00
$entry = new Entry();
$frame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
2014-06-22 19:02:18 +02:00
$entry->setHAlign($entry::LEFT);
2014-01-28 16:54:23 +01:00
$entry->setPosition(-$width / 2 + 15, $height / 2 - 5);
$entry->setTextSize(1);
$entry->setSize($width * 0.25, 4);
$entry->setName('SearchString');
//Search for Map-Name
$label = new Label_Button();
$frame->add($label);
$label->setPosition(-$width / 2 + 63, $height / 2 - 5);
2014-06-22 19:02:18 +02:00
$label->setText('MapName');
2014-01-28 16:54:23 +01:00
$label->setTextSize(1.3);
$quad = new Quad_BgsPlayerCard();
$frame->add($quad);
$quad->setPosition(-$width / 2 + 63, $height / 2 - 5, 0.01);
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setSize(18, 5);
$quad->setAction(self::ACTION_SEARCH_MAPNAME);
//Search for Author
$label = new Label_Button();
$frame->add($label);
$label->setPosition(-$width / 2 + 82, $height / 2 - 5);
2014-06-22 19:02:18 +02:00
$label->setText('Author');
2014-01-28 16:54:23 +01:00
$label->setTextSize(1.3);
$quad = new Quad_BgsPlayerCard();
$frame->add($quad);
$quad->setPosition(-$width / 2 + 82, $height / 2 - 5, 0.01);
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setSize(18, 5);
$quad->setAction(self::ACTION_SEARCH_AUTHOR);
// 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) {
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
}