TrackManiaControl/application/core/Maps/MapList.php

680 lines
24 KiB
PHP
Raw Normal View History

2013-12-14 22:00:59 +01:00
<?php
namespace ManiaControl\Maps;
2014-01-02 10:38:46 +01:00
2013-12-14 22:00:59 +01:00
use FML\Controls\Control;
2014-01-02 10:38:46 +01:00
use FML\Controls\Frame;
2013-12-29 19:21:29 +01:00
use FML\Controls\Gauge;
2013-12-14 22:00:59 +01:00
use FML\Controls\Label;
2013-12-31 02:27:40 +01:00
use FML\Controls\Labels\Label_Button;
use FML\Controls\Labels\Label_Text;
2014-01-02 10:38:46 +01:00
use FML\Controls\Quad;
2013-12-31 02:27:40 +01:00
use FML\Controls\Quads\Quad_BgsPlayerCard;
2013-12-14 22:00:59 +01:00
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\Controls\Quads\Quad_UIConstruction_Buttons;
2014-01-02 10:38:46 +01:00
use FML\ManiaLink;
2014-04-27 16:22:12 +02:00
use FML\Script\Features\Paging;
2013-12-15 15:13:56 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
2014-04-28 20:50:38 +02:00
use ManiaControl\Callbacks\Callbacks;
2014-01-02 10:38:46 +01:00
use ManiaControl\ManiaControl;
2014-01-09 18:19:37 +01:00
use ManiaControl\Manialinks\IconManager;
use ManiaControl\Manialinks\ManialinkManager;
2013-12-14 22:00:59 +01:00
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Players\Player;
use ManiaControl\Utils\ColorUtil;
use ManiaControl\Utils\Formatter;
use Maniaplanet\DedicatedServer\Xmlrpc\NotInListException;
2014-05-02 17:50:30 +02:00
use MCTeam\CustomVotesPlugin;
2014-04-27 16:22:12 +02:00
use MCTeam\KarmaPlugin;
2013-12-14 22:00:59 +01:00
2013-12-24 13:09:39 +01:00
/**
* MapList 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
2013-12-24 13:09:39 +01:00
*/
2013-12-15 15:13:56 +01:00
class MapList implements ManialinkPageAnswerListener, CallbackListener {
/*
2013-12-18 15:48:33 +01:00
* Constants
*/
2014-05-02 17:50:30 +02:00
const ACTION_UPDATE_MAP = 'MapList.UpdateMap';
const ACTION_ERASE_MAP = 'MapList.EraseMap';
const ACTION_SWITCH_MAP = 'MapList.SwitchMap';
const ACTION_START_SWITCH_VOTE = 'MapList.StartMapSwitchVote';
const ACTION_QUEUED_MAP = 'MapList.QueueMap';
const ACTION_UNQUEUE_MAP = 'MapList.UnQueueMap';
const ACTION_CHECK_UPDATE = 'MapList.CheckUpdate';
const ACTION_CLEAR_MAPQUEUE = 'MapList.ClearMapQueue';
const ACTION_PAGING_CHUNKS = 'MapList.PagingChunk.';
const MAX_MAPS_PER_PAGE = 15;
const MAX_PAGES_PER_CHUNK = 2;
const DEFAULT_KARMA_PLUGIN = 'MCTeam\KarmaPlugin';
2014-04-29 22:06:58 +02:00
const DEFAULT_CUSTOM_VOTE_PLUGIN = 'MCTeam\CustomVotesPlugin';
2014-05-18 16:27:08 +02:00
const CACHE_CURRENT_PAGE = 'CurrentPage';
const WIDGET_NAME = 'MapList';
2014-05-02 17:50:30 +02:00
/*
2014-01-06 15:54:22 +01:00
* Private Properties
2013-12-14 22:00:59 +01:00
*/
2014-04-27 18:59:21 +02:00
private $maniaControl = null;
2013-12-14 22:00:59 +01:00
/**
2014-01-06 15:54:22 +01:00
* Create a new MapList Instance
2014-05-02 17:50:30 +02:00
*
2013-12-14 22:00:59 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:22 +01:00
// Register for Callbacks
2014-01-02 10:38:46 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_CLOSED, $this, 'closeWidget');
$this->maniaControl->callbackManager->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_OPENED, $this, 'handleWidgetOpened');
2014-01-08 18:07:09 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
2014-05-10 13:41:14 +02:00
$this->maniaControl->callbackManager->registerCallbackListener(MapQueue::CB_MAPQUEUE_CHANGED, $this, 'updateWidget');
2014-01-06 18:50:26 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_MAPS_UPDATED, $this, 'updateWidget');
2014-01-06 15:54:22 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_KARMA_UPDATED, $this, 'updateWidget');
2014-04-28 20:50:38 +02:00
$this->maniaControl->callbackManager->registerCallbackListener(Callbacks::BEGINMAP, $this, 'updateWidget');
2014-05-02 17:50:30 +02:00
2014-01-17 22:35:14 +01:00
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_CHECK_UPDATE, $this, 'checkUpdates');
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_CLEAR_MAPQUEUE, $this, 'clearMapQueue');
2013-12-14 22:00:59 +01:00
}
2014-01-17 22:35:14 +01:00
/**
* Clears the Map Queue
2014-05-02 17:50:30 +02:00
*
* @param array $chatCallback
2014-01-17 22:35:14 +01:00
* @param Player $player
*/
2014-04-27 18:59:21 +02:00
public function clearMapQueue(array $chatCallback, Player $player) {
// Clears the Map Queue
2014-01-17 22:35:14 +01:00
$this->maniaControl->mapManager->mapQueue->clearMapQueue($player);
}
/**
* Check for Map Updates
2014-05-02 17:50:30 +02:00
*
* @param array $chatCallback
2014-01-17 22:35:14 +01:00
* @param Player $player
*/
public function checkUpdates(array $chatCallback, Player $player) {
2014-04-27 18:59:21 +02:00
// Update Mx Infos
2014-05-18 16:27:08 +02:00
$this->maniaControl->mapManager->mxManager->fetchManiaExchangeMapInformation();
2014-05-02 17:50:30 +02:00
2014-04-27 18:59:21 +02:00
// Reshow the Maplist
2014-01-17 22:35:14 +01:00
$this->showMapList($player);
}
2014-01-09 16:41:17 +01:00
/**
2014-04-28 16:59:55 +02:00
* Display a MapList on the Screen
2014-05-02 17:50:30 +02:00
*
* @param Player $player
2014-05-27 22:32:54 +02:00
* @param Map[] $mapList
* @param int $pageIndex
*/
public function showMapList(Player $player, $mapList = null, $pageIndex = -1) {
2014-05-02 17:50:30 +02:00
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
2014-01-06 15:54:22 +01:00
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
2014-05-02 17:50:30 +02:00
if ($pageIndex < 0) {
2014-05-18 16:27:08 +02:00
$pageIndex = (int)$player->getCache($this, self::CACHE_CURRENT_PAGE);
}
2014-05-18 16:27:08 +02:00
$player->setCache($this, self::CACHE_CURRENT_PAGE, $pageIndex);
$queueBuffer = $this->maniaControl->mapManager->mapQueue->getQueueBuffer();
$chunkIndex = $this->getChunkIndexFromPageNumber($pageIndex);
$mapsBeginIndex = $this->getChunkMapsBeginIndex($chunkIndex);
2014-05-02 17:50:30 +02:00
2014-01-28 19:28:17 +01:00
// Get Maps
if (!is_array($mapList)) {
$mapList = $this->maniaControl->mapManager->getMaps();
2014-04-28 16:59:55 +02:00
}
$mapList = array_slice($mapList, $mapsBeginIndex, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
2014-05-02 17:50:30 +02:00
$totalMapsCount = $this->maniaControl->mapManager->getMapsCount();
$pagesCount = ceil($totalMapsCount / self::MAX_MAPS_PER_PAGE);
2014-04-27 18:59:21 +02:00
// Create ManiaLink
2014-01-28 19:28:17 +01:00
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
2014-05-02 17:50:30 +02:00
$script = $maniaLink->getScript();
$paging = new Paging();
2014-04-27 18:59:21 +02:00
$script->addFeature($paging);
$paging->setCustomMaxPageNumber($pagesCount);
2014-04-28 16:59:55 +02:00
$paging->setChunkActionAppendsPageNumber(true);
$paging->setChunkActions(self::ACTION_PAGING_CHUNKS);
2014-05-02 17:50:30 +02:00
2014-01-28 19:28:17 +01:00
// Main frame
2014-04-27 18:59:21 +02:00
$frame = $this->maniaControl->manialinkManager->styleManager->getDefaultListFrame($script, $paging);
2014-01-28 19:28:17 +01:00
$maniaLink->add($frame);
2014-05-02 17:50:30 +02:00
2014-04-27 18:59:21 +02:00
// Admin Buttons
2014-01-28 20:09:09 +01:00
if ($this->maniaControl->authenticationManager->checkPermission($player, MapQueue::SETTING_PERMISSION_CLEAR_MAPQUEUE)) {
2014-04-27 18:59:21 +02:00
// Clear Map-Queue
2014-01-17 22:35:14 +01:00
$label = new Label_Button();
$frame->add($label);
$label->setText("Clear Map-Queue");
$label->setTextSize(1);
$label->setPosition($width / 2 - 8, -$height / 2 + 9);
$label->setHAlign(Control::RIGHT);
2014-05-02 17:50:30 +02:00
2014-01-17 22:35:14 +01:00
$quad = new Quad_BgsPlayerCard();
$frame->add($quad);
$quad->setPosition($width / 2 - 5, -$height / 2 + 9, 0.01);
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setHAlign(Control::RIGHT);
$quad->setSize(29, 4);
$quad->setAction(self::ACTION_CLEAR_MAPQUEUE);
2014-01-28 20:14:21 +01:00
}
2014-05-02 17:50:30 +02:00
2014-01-28 20:14:21 +01:00
if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_CHECK_UPDATE)) {
2014-04-27 18:59:21 +02:00
// Check Update
2014-01-17 22:35:14 +01:00
$label = new Label_Button();
$frame->add($label);
$label->setText("Check MX Updates");
$label->setTextSize(1);
$label->setPosition($width / 2 - 41, -$height / 2 + 9, 0.01);
$label->setHAlign(Control::RIGHT);
2014-05-02 17:50:30 +02:00
2014-01-17 22:35:14 +01:00
$quad = new Quad_BgsPlayerCard();
$frame->add($quad);
$quad->setPosition($width / 2 - 37, -$height / 2 + 9, 0.01);
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setHAlign(Control::RIGHT);
$quad->setSize(35, 4);
$quad->setAction(self::ACTION_CHECK_UPDATE);
2014-05-02 17:50:30 +02:00
2014-01-17 22:35:14 +01:00
$mxQuad = new Quad();
$frame->add($mxQuad);
$mxQuad->setSize(3, 3);
$mxQuad->setImage($this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON_GREEN));
$mxQuad->setImageFocus($this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON_GREEN_MOVER));
$mxQuad->setPosition($width / 2 - 67, -$height / 2 + 9);
$mxQuad->setZ(0.01);
$mxQuad->setAction(self::ACTION_CHECK_UPDATE);
}
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:22 +01:00
// Headline
2013-12-15 11:55:44 +01:00
$headFrame = new Frame();
$frame->add($headFrame);
2014-01-06 15:54:22 +01:00
$headFrame->setY($height / 2 - 5);
2014-05-02 17:50:30 +02:00
$x = -$width / 2;
$array = array('Id' => $x + 5, 'Mx Id' => $x + 10, 'Map Name' => $x + 20, 'Author' => $x + 68, 'Karma' => $x + 115, 'Actions' => $width / 2 - 16);
2014-01-02 10:38:46 +01:00
$this->maniaControl->manialinkManager->labelLine($headFrame, $array);
2014-05-02 17:50:30 +02:00
2014-04-27 18:59:21 +02:00
// Predefine description Label
$descriptionLabel = $this->maniaControl->manialinkManager->styleManager->getDefaultDescriptionLabel();
2014-01-12 00:28:06 +01:00
$frame->add($descriptionLabel);
2014-05-02 17:50:30 +02:00
2013-12-31 12:36:49 +01:00
$queuedMaps = $this->maniaControl->mapManager->mapQueue->getQueuedMapsRanking();
2014-05-08 23:43:30 +02:00
/** @var KarmaPlugin $karmaPlugin */
2013-12-29 19:21:29 +01:00
$karmaPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_KARMA_PLUGIN);
2014-04-28 16:59:55 +02:00
$pageNumber = 1 + $chunkIndex * self::MAX_PAGES_PER_CHUNK;
$paging->setStartPageNumber($pageIndex + 1);
2014-05-02 17:50:30 +02:00
2014-05-18 16:27:08 +02:00
$index = 0;
2014-05-15 17:45:08 +02:00
$id = 1 + $mapsBeginIndex;
$y = $height / 2 - 10;
$pageFrame = null;
2014-05-08 23:43:30 +02:00
/** @var Map $map */
2014-05-02 17:50:30 +02:00
$currentMap = $this->maniaControl->mapManager->getCurrentMap();
$mxIcon = $this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON);
$mxIconHover = $this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON_MOVER);
$mxIconGreen = $this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON_GREEN);
$mxIconGreenHover = $this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON_GREEN_MOVER);
2014-05-02 17:50:30 +02:00
2014-04-27 18:59:21 +02:00
foreach ($mapList as $map) {
2014-05-16 20:31:50 +02:00
if ($index % self::MAX_MAPS_PER_PAGE === 0) {
2014-01-02 20:50:09 +01:00
$pageFrame = new Frame();
$frame->add($pageFrame);
2014-01-06 15:54:22 +01:00
$y = $height / 2 - 10;
2014-04-28 16:59:55 +02:00
$paging->addPage($pageFrame, $pageNumber);
$pageNumber++;
2014-01-02 20:50:09 +01:00
}
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:22 +01:00
// Map Frame
2013-12-14 22:00:59 +01:00
$mapFrame = new Frame();
2014-01-02 20:50:09 +01:00
$pageFrame->add($mapFrame);
2013-12-30 16:58:35 +01:00
$mapFrame->setZ(0.1);
2013-12-14 22:00:59 +01:00
$mapFrame->setY($y);
2014-05-02 17:50:30 +02:00
if ($id % 2 !== 0) {
2013-12-31 02:27:40 +01:00
$lineQuad = new Quad_BgsPlayerCard();
$mapFrame->add($lineQuad);
2014-01-06 15:54:22 +01:00
$lineQuad->setSize($width, 4);
2013-12-31 02:27:40 +01:00
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(0.001);
}
2014-05-02 17:50:30 +02:00
if ($currentMap === $map) {
2013-12-30 16:58:35 +01:00
$currentQuad = new Quad_Icons64x64_1();
2013-12-30 20:15:38 +01:00
$mapFrame->add($currentQuad);
2013-12-30 16:58:35 +01:00
$currentQuad->setX($x + 3.5);
$currentQuad->setZ(0.2);
$currentQuad->setSize(4, 4);
$currentQuad->setSubStyle($currentQuad::SUBSTYLE_ArrowBlue);
}
2014-05-02 17:50:30 +02:00
2013-12-30 16:58:35 +01:00
$mxId = '-';
2014-01-27 21:27:31 +01:00
if (isset($map->mx->id)) {
2013-12-30 16:58:35 +01:00
$mxId = $map->mx->id;
2014-05-02 17:50:30 +02:00
2014-01-12 00:28:06 +01:00
$mxQuad = new Quad();
$mapFrame->add($mxQuad);
$mxQuad->setSize(3, 3);
$mxQuad->setImage($mxIcon);
$mxQuad->setImageFocus($mxIconHover);
2014-01-12 00:28:06 +01:00
$mxQuad->setX($x + 65);
$mxQuad->setUrl($map->mx->pageurl);
$mxQuad->setZ(0.01);
2014-04-27 18:59:21 +02:00
$description = 'View $<' . $map->name . '$> on Mania-Exchange';
$mxQuad->addTooltipLabelFeature($descriptionLabel, $description);
2014-05-02 17:50:30 +02:00
2014-01-27 21:27:31 +01:00
if ($map->updateAvailable()) {
2014-01-12 00:28:06 +01:00
$mxQuad = new Quad();
$mapFrame->add($mxQuad);
$mxQuad->setSize(3, 3);
$mxQuad->setImage($mxIconGreen);
$mxQuad->setImageFocus($mxIconGreenHover);
2014-01-12 00:28:06 +01:00
$mxQuad->setX($x + 62);
$mxQuad->setUrl($map->mx->pageurl);
$mxQuad->setZ(0.01);
2014-04-27 18:59:21 +02:00
$description = 'Update for $<' . $map->name . '$> available on Mania-Exchange!';
$mxQuad->addTooltipLabelFeature($descriptionLabel, $description);
2014-05-02 17:50:30 +02:00
2014-04-27 18:59:21 +02:00
// Update Button
2014-01-27 21:27:31 +01:00
if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)) {
2014-01-15 20:49:01 +01:00
$mxQuad->setAction(self::ACTION_UPDATE_MAP . '.' . $map->uid);
}
2014-01-12 00:28:06 +01:00
}
2014-01-02 10:38:46 +01:00
}
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:22 +01:00
// Display Maps
2014-05-02 17:50:30 +02:00
$array = array($id => $x + 5, $mxId => $x + 10, Formatter::stripDirtyCodes($map->name) => $x + 20, $map->authorNick => $x + 68);
2014-01-27 21:27:31 +01:00
$labels = $this->maniaControl->manialinkManager->labelLine($mapFrame, $array);
if (isset($labels[3])) {
2014-05-08 23:43:30 +02:00
/** @var Label $label */
2014-05-02 17:50:30 +02:00
$label = $labels[3];
$description = 'Click to checkout all maps by $<' . $map->authorLogin . '$>!';
$label->setAction(MapCommands::ACTION_SHOW_AUTHOR . $map->authorLogin);
2014-04-27 18:59:21 +02:00
$label->addTooltipLabelFeature($descriptionLabel, $description);
2014-02-26 20:59:32 +01:00
}
2014-05-02 17:50:30 +02:00
2014-01-09 19:00:37 +01:00
// TODO action detailed map info including mx info
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:22 +01:00
// Map-Queue-Map-Label
2014-01-27 21:27:31 +01:00
if (isset($queuedMaps[$map->uid])) {
2013-12-31 12:36:49 +01:00
$label = new Label_Text();
$mapFrame->add($label);
2014-05-09 11:58:33 +02:00
$label->setX($width / 2 - 13);
2013-12-31 12:36:49 +01:00
$label->setZ(0.2);
$label->setTextSize(1.5);
$label->setText($queuedMaps[$map->uid]);
2014-01-06 15:54:22 +01:00
$label->setTextColor('fff');
2014-05-02 17:50:30 +02:00
2014-05-24 20:39:18 +02:00
// Checks if the Player who opened the Widget has queued the map
2014-01-28 20:09:09 +01:00
$queuer = $this->maniaControl->mapManager->mapQueue->getQueuer($map->uid);
if ($queuer->login == $player->login) {
2014-04-27 18:59:21 +02:00
$description = 'Remove $<' . $map->name . '$> from the Map Queue';
$label->addTooltipLabelFeature($descriptionLabel, $description);
2014-01-28 20:09:09 +01:00
$label->setAction(self::ACTION_UNQUEUE_MAP . '.' . $map->uid);
2014-05-02 17:50:30 +02:00
} else {
2014-04-27 18:59:21 +02:00
$description = '$<' . $map->name . '$> is on Map-Queue Position: ' . $queuedMaps[$map->uid];
$label->addTooltipLabelFeature($descriptionLabel, $description);
}
2014-05-02 17:50:30 +02:00
} else {
2014-01-06 15:54:22 +01:00
// Map-Queue-Map-Button
$queueLabel = new Label_Button();
$mapFrame->add($queueLabel);
$queueLabel->setX($width / 2 - 13);
2014-01-06 15:54:22 +01:00
$queueLabel->setZ(0.2);
$queueLabel->setSize(3, 3);
$queueLabel->setText('+');
2014-05-02 17:50:30 +02:00
if (in_array($map->uid, $queueBuffer)) {
if ($this->maniaControl->authenticationManager->checkPermission($player, MapQueue::SETTING_PERMISSION_CLEAR_MAPQUEUE)) {
$queueLabel->setAction(self::ACTION_QUEUED_MAP . '.' . $map->uid);
}
$queueLabel->setTextColor('f00');
$description = '$<' . $map->name . '$> has recently been played!';
$queueLabel->addTooltipLabelFeature($descriptionLabel, $description);
} else {
$queueLabel->setTextColor('09f');
$queueLabel->setAction(self::ACTION_QUEUED_MAP . '.' . $map->uid);
$description = 'Add $<' . $map->name . '$> to the Map Queue';
$queueLabel->addTooltipLabelFeature($descriptionLabel, $description);
}
2014-01-06 15:54:22 +01:00
}
2014-05-02 17:50:30 +02:00
2014-01-27 21:27:31 +01:00
if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_REMOVE_MAP)) {
2014-01-06 15:54:22 +01:00
// erase map quad
2014-01-06 14:43:26 +01:00
$eraseLabel = new Label_Button();
$mapFrame->add($eraseLabel);
2014-01-06 15:54:22 +01:00
$eraseLabel->setX($width / 2 - 5);
2014-01-06 14:43:26 +01:00
$eraseLabel->setZ(0.2);
$eraseLabel->setSize(3, 3);
$eraseLabel->setTextSize(1);
2014-01-06 15:54:22 +01:00
$eraseLabel->setText('x');
$eraseLabel->setTextColor('a00');
2014-05-02 17:50:30 +02:00
2014-05-08 23:43:30 +02:00
$confirmFrame = $this->buildConfirmFrame($maniaLink, $y, $map->uid, true);
2014-04-27 18:59:21 +02:00
$eraseLabel->addToggleFeature($confirmFrame);
$description = 'Remove Map: $<' . $map->name . '$>';
$eraseLabel->addTooltipLabelFeature($descriptionLabel, $description);
2013-12-16 12:41:14 +01:00
}
2014-05-02 17:50:30 +02:00
2014-02-19 22:24:56 +01:00
if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)) {
// Switch to map
2014-02-07 22:57:39 +01:00
$switchLabel = new Label_Button();
$mapFrame->add($switchLabel);
$switchLabel->setX($width / 2 - 9);
2014-02-07 22:57:39 +01:00
$switchLabel->setZ(0.2);
$switchLabel->setSize(3, 3);
$switchLabel->setTextSize(2);
$switchLabel->setText('»');
$switchLabel->setTextColor('0f0');
2014-05-02 17:50:30 +02:00
2014-05-08 23:43:30 +02:00
$confirmFrame = $this->buildConfirmFrame($maniaLink, $y, $map->uid);
2014-04-27 18:59:21 +02:00
$switchLabel->addToggleFeature($confirmFrame);
2014-05-02 17:50:30 +02:00
2014-04-27 18:59:21 +02:00
$description = 'Switch Directly to Map: $<' . $map->name . '$>';
$switchLabel->addTooltipLabelFeature($descriptionLabel, $description);
}
if ($this->maniaControl->pluginManager->isPluginActive(self::DEFAULT_CUSTOM_VOTE_PLUGIN)) {
if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)) {
// Switch Map Voting for Admins
$switchQuad = new Quad_UIConstruction_Buttons();
$mapFrame->add($switchQuad);
$switchQuad->setX($width / 2 - 17);
$switchQuad->setZ(0.2);
$switchQuad->setSubStyle($switchQuad::SUBSTYLE_Validate_Step2);
$switchQuad->setSize(3.8, 3.8);
2014-05-08 23:43:30 +02:00
$switchQuad->setAction(self::ACTION_START_SWITCH_VOTE . '.' . $map->uid);
$description = 'Start Map-Switch Vote: $<' . $map->name . '$>';
$switchQuad->addTooltipLabelFeature($descriptionLabel, $description);
} else {
// Switch Map Voting for Player
$switchLabel = new Label_Button();
$mapFrame->add($switchLabel);
$switchLabel->setX($width / 2 - 7);
$switchLabel->setZ(0.2);
$switchLabel->setSize(3, 3);
$switchLabel->setTextSize(2);
$switchLabel->setText('»');
$switchLabel->setTextColor('0f0');
2014-05-08 23:43:30 +02:00
$switchLabel->setAction(self::ACTION_START_SWITCH_VOTE . '.' . ($map->uid));
$description = 'Start Map-Switch Vote: $<' . $map->name . '$>';
$switchLabel->addTooltipLabelFeature($descriptionLabel, $description);
}
2013-12-16 12:41:14 +01:00
}
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:22 +01:00
// Display Karma bar
2014-01-27 21:27:31 +01:00
if ($karmaPlugin) {
2013-12-29 19:21:29 +01:00
$karma = $karmaPlugin->getMapKarma($map);
$votes = $karmaPlugin->getMapVotes($map);
2014-01-27 21:27:31 +01:00
if (is_numeric($karma)) {
2014-05-13 16:03:26 +02:00
if ($this->maniaControl->settingManager->getSettingValue($karmaPlugin, $karmaPlugin::SETTING_NEWKARMA)) {
2014-04-30 21:44:26 +02:00
$karmaText = ' ' . round($karma * 100.) . '% (' . $votes['count'] . ')';
} else {
2014-05-02 17:50:30 +02:00
$min = 0;
$plus = 0;
2014-05-02 17:50:30 +02:00
foreach ($votes as $vote) {
if (isset($vote->vote)) {
if ($vote->vote !== 0.5) {
2014-05-02 17:50:30 +02:00
if ($vote->vote < 0.5) {
$min = $min + $vote->count;
} else {
2014-05-02 17:50:30 +02:00
$plus = $plus + $vote->count;
}
}
}
}
2014-05-02 17:50:30 +02:00
$endKarma = $plus - $min;
$karmaText = ' ' . $endKarma . ' (' . $votes['count'] . 'x / ' . round($karma * 100.) . '%)';
}
2013-12-29 19:21:29 +01:00
$karmaGauge = new Gauge();
$mapFrame->add($karmaGauge);
2013-12-31 02:27:40 +01:00
$karmaGauge->setZ(2);
2013-12-30 23:53:42 +01:00
$karmaGauge->setX($x + 120);
2013-12-29 19:21:29 +01:00
$karmaGauge->setSize(20, 9);
$karmaGauge->setDrawBg(false);
$karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '9');
2014-05-02 17:50:30 +02:00
2013-12-29 19:21:29 +01:00
$karmaLabel = new Label();
$mapFrame->add($karmaLabel);
2013-12-31 02:27:40 +01:00
$karmaLabel->setZ(2);
2013-12-30 23:53:42 +01:00
$karmaLabel->setX($x + 120);
2013-12-29 19:21:29 +01:00
$karmaLabel->setSize(20 * 0.9, 5);
$karmaLabel->setTextSize(0.9);
2014-01-06 15:54:22 +01:00
$karmaLabel->setTextColor('000');
2014-04-30 21:44:26 +02:00
$karmaLabel->setText($karmaText);
2013-12-29 19:21:29 +01:00
}
}
2014-05-02 17:50:30 +02:00
2013-12-14 22:00:59 +01:00
$y -= 4;
2014-01-02 20:50:09 +01:00
$id++;
2014-05-16 20:31:50 +02:00
$index++;
2014-01-02 20:50:09 +01:00
}
2014-05-02 17:50:30 +02:00
2014-05-18 16:27:08 +02:00
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, self::WIDGET_NAME);
2013-12-14 22:00:59 +01:00
}
/**
* Get the Chunk Index with the given Page Index
*
* @param int $pageIndex
* @return int
*/
private function getChunkIndexFromPageNumber($pageIndex) {
$mapsCount = $this->maniaControl->mapManager->getMapsCount();
$pagesCount = ceil($mapsCount / self::MAX_MAPS_PER_PAGE);
if ($pageIndex > $pagesCount - 1) {
$pageIndex = $pagesCount - 1;
}
return floor($pageIndex / self::MAX_PAGES_PER_CHUNK);
}
/**
* Calculate the First Map Index to show for the given Chunk
*
* @param int $chunkIndex
* @return int
*/
private function getChunkMapsBeginIndex($chunkIndex) {
return $chunkIndex * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE;
}
2014-01-03 18:04:46 +01:00
/**
* Builds the confirmation frame
2014-05-02 17:50:30 +02:00
*
2014-01-03 18:04:46 +01:00
* @param ManiaLink $maniaLink
* @param float $y
2014-05-02 17:50:30 +02:00
* @param bool $mapUid
2014-05-08 23:43:30 +02:00
* @param bool $erase
2014-01-03 18:04:46 +01:00
* @return Frame
*/
2014-05-08 23:43:30 +02:00
public function buildConfirmFrame(Manialink $maniaLink, $y, $mapUid, $erase = false) {
// TODO: get rid of the confirm frame to decrease xml size & network usage
2014-05-02 17:50:30 +02:00
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle();
2014-01-06 15:54:22 +01:00
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle();
2014-05-02 17:50:30 +02:00
2014-01-03 18:04:46 +01:00
$confirmFrame = new Frame();
$maniaLink->add($confirmFrame);
2014-01-06 15:54:22 +01:00
$confirmFrame->setPosition($width / 2 + 6, $y);
2014-01-12 01:59:23 +01:00
$confirmFrame->setVisible(false);
2014-05-02 17:50:30 +02:00
2014-01-03 18:04:46 +01:00
$quad = new Quad();
$confirmFrame->add($quad);
2014-01-06 15:54:22 +01:00
$quad->setStyles($quadStyle, $quadSubstyle);
2014-01-03 18:04:46 +01:00
$quad->setSize(12, 4);
2014-05-02 17:50:30 +02:00
2014-01-03 18:04:46 +01:00
$quad = new Quad_BgsPlayerCard();
$confirmFrame->add($quad);
$quad->setSubStyle($quad::SUBSTYLE_BgCardSystem);
$quad->setSize(11, 3.5);
2014-05-02 17:50:30 +02:00
2014-01-03 18:04:46 +01:00
$label = new Label_Button();
$confirmFrame->add($label);
2014-01-06 15:54:22 +01:00
$label->setText('Sure?');
2014-01-03 18:04:46 +01:00
$label->setTextSize(1);
$label->setScale(0.90);
$label->setX(-1.3);
2014-05-02 17:50:30 +02:00
2014-01-03 18:04:46 +01:00
$buttLabel = new Label_Button();
$confirmFrame->add($buttLabel);
$buttLabel->setPosition(3.2, 0.4, 0.2);
$buttLabel->setSize(3, 3);
2014-05-02 17:50:30 +02:00
2014-05-08 23:43:30 +02:00
if (!$erase) {
$quad->setAction(self::ACTION_SWITCH_MAP . '.' . $mapUid);
2014-01-06 15:54:22 +01:00
$buttLabel->setText('»');
$buttLabel->setTextColor('0f0');
2014-01-03 18:04:46 +01:00
$buttLabel->setTextSize(2);
2014-05-02 17:50:30 +02:00
} else {
2014-01-03 18:04:46 +01:00
$buttLabel->setTextSize(1);
2014-01-06 15:54:22 +01:00
$buttLabel->setText('x');
$buttLabel->setTextColor('a00');
2014-05-08 23:43:30 +02:00
$quad->setAction(self::ACTION_ERASE_MAP . '.' . $mapUid);
2014-01-03 18:04:46 +01:00
}
return $confirmFrame;
}
/**
* Unset the player if he opened another Main Widget
2014-05-02 17:50:30 +02:00
*
2014-02-19 17:37:37 +01:00
* @param Player $player
* @param string $openedWidget
*/
2014-02-19 17:37:37 +01:00
public function handleWidgetOpened(Player $player, $openedWidget) {
2014-04-27 18:59:21 +02:00
// unset when another main widget got opened
2014-05-18 16:27:08 +02:00
if ($openedWidget !== self::WIDGET_NAME) {
$player->destroyCache($this, self::CACHE_CURRENT_PAGE);
}
}
2014-01-12 00:28:06 +01:00
/**
* Close the widget
2014-05-02 17:50:30 +02:00
*
* @param Player $player
*/
2014-02-19 17:37:37 +01:00
public function closeWidget(Player $player) {
// TODO: resolve duplicate with 'playerCloseWidget'
2014-05-18 16:27:08 +02:00
$player->destroyCache($this, self::CACHE_CURRENT_PAGE);
2014-01-03 18:04:46 +01:00
}
2013-12-15 15:13:56 +01:00
/**
2014-01-06 15:54:22 +01:00
* Handle ManialinkPageAnswer Callback
2014-05-02 17:50:30 +02:00
*
2013-12-15 15:13:56 +01:00
* @param array $callback
*/
2014-01-02 10:38:46 +01:00
public function handleManialinkPageAnswer(array $callback) {
2014-05-02 17:50:30 +02:00
$actionId = $callback[1][2];
2014-01-06 15:54:22 +01:00
$actionArray = explode('.', $actionId);
2014-05-02 17:50:30 +02:00
2014-01-27 21:27:31 +01:00
if (count($actionArray) <= 2) {
2014-01-08 18:07:09 +01:00
return;
}
2014-05-02 17:50:30 +02:00
2014-01-06 15:54:22 +01:00
$action = $actionArray[0] . '.' . $actionArray[1];
2014-05-02 17:50:30 +02:00
$login = $callback[1][1];
2014-01-06 15:54:22 +01:00
$player = $this->maniaControl->playerManager->getPlayer($login);
2014-05-08 23:43:30 +02:00
$mapUid = $actionArray[2];
2014-05-02 17:50:30 +02:00
2014-04-27 18:59:21 +02:00
switch ($action) {
2014-01-12 21:39:27 +01:00
case self::ACTION_UPDATE_MAP:
$this->maniaControl->mapManager->updateMap($player, $mapUid);
2014-05-08 22:33:01 +02:00
$this->showMapList($player);
2014-01-12 21:39:27 +01:00
break;
2014-01-02 10:38:46 +01:00
case self::ACTION_ERASE_MAP:
2014-01-10 15:58:27 +01:00
$this->maniaControl->mapManager->removeMap($player, $mapUid);
2014-01-02 10:38:46 +01:00
break;
case self::ACTION_SWITCH_MAP:
2014-05-09 11:58:33 +02:00
//Don't queue on Map-Change
$this->maniaControl->mapManager->mapQueue->dontQueueNextMapChange();
2014-01-25 23:33:39 +01:00
try {
2014-05-08 23:43:30 +02:00
$this->maniaControl->client->jumpToMapIdent($mapUid);
} catch (NotInListException $e) {
$this->maniaControl->chat->sendError("Error on Jumping to Map Ident!");
2014-01-25 23:33:39 +01:00
break;
}
2014-05-08 23:43:30 +02:00
$map = $this->maniaControl->mapManager->getMapByUid($mapUid);
2014-05-02 17:50:30 +02:00
$message = $player->getEscapedNickname() . ' skipped to Map $z' . $map->getEscapedName() . '!';
2014-01-06 15:54:22 +01:00
$this->maniaControl->chat->sendSuccess($message);
$this->maniaControl->log($message, true);
2014-05-02 17:50:30 +02:00
2014-01-03 18:04:46 +01:00
$this->playerCloseWidget($player);
2014-01-02 10:38:46 +01:00
break;
2014-02-07 22:57:39 +01:00
case self::ACTION_START_SWITCH_VOTE:
/** @var CustomVotesPlugin $votesPlugin */
2014-02-07 22:57:39 +01:00
$votesPlugin = $this->maniaControl->pluginManager->getPlugin(self::DEFAULT_CUSTOM_VOTE_PLUGIN);
2014-05-08 23:43:30 +02:00
$map = $this->maniaControl->mapManager->getMapByUid($mapUid);
2014-05-02 17:50:30 +02:00
$message = $player->getEscapedNickname() . '$s started a vote to switch to ' . $map->getEscapedName() . '!';
2014-05-02 17:50:30 +02:00
2014-02-08 00:00:00 +01:00
$votesPlugin->defineVote('switchmap', "Goto " . $map->name, true, $message);
2014-05-02 17:50:30 +02:00
2014-04-20 14:52:26 +02:00
$self = $this;
2014-05-02 17:50:30 +02:00
$votesPlugin->startVote($player, 'switchmap', function ($result) use (&$self, &$votesPlugin, &$map) {
$self->maniaControl->chat->sendInformation('$sVote Successful -> Map switched!');
2014-02-07 22:57:39 +01:00
$votesPlugin->undefineVote('switchmap');
2014-05-02 17:50:30 +02:00
2014-05-09 11:58:33 +02:00
//Don't queue on Map-Change
$this->maniaControl->mapManager->mapQueue->dontQueueNextMapChange();
2014-02-07 22:57:39 +01:00
try {
2014-05-08 23:43:30 +02:00
$self->maniaControl->client->JumpToMapIdent($map->uid);
} catch (NotInListException $e) {
2014-02-07 22:57:39 +01:00
}
});
break;
2014-01-02 10:38:46 +01:00
case self::ACTION_QUEUED_MAP:
2014-05-08 23:43:30 +02:00
$this->maniaControl->mapManager->mapQueue->addMapToMapQueue($callback[1][1], $mapUid);
2014-04-29 22:30:29 +02:00
$this->showMapList($player);
2014-01-02 10:38:46 +01:00
break;
2014-01-28 20:09:09 +01:00
case self::ACTION_UNQUEUE_MAP:
2014-05-08 23:43:30 +02:00
$this->maniaControl->mapManager->mapQueue->removeFromMapQueue($player, $mapUid);
2014-04-29 22:30:29 +02:00
$this->showMapList($player);
2014-01-28 20:09:09 +01:00
break;
2014-04-28 16:59:55 +02:00
default:
if (substr($actionId, 0, strlen(self::ACTION_PAGING_CHUNKS)) === self::ACTION_PAGING_CHUNKS) {
// Paging chunks
2014-05-02 17:50:30 +02:00
$neededPage = (int)substr($actionId, strlen(self::ACTION_PAGING_CHUNKS));
$this->showMapList($player, null, $neededPage - 1);
2014-04-28 16:59:55 +02:00
}
break;
2014-01-02 10:38:46 +01:00
}
2013-12-15 15:13:56 +01:00
}
2013-12-28 23:24:54 +01:00
2014-05-02 17:50:30 +02:00
/**
* Close the widget for
2014-05-02 17:50:30 +02:00
*
* @param Player $player
*/
public function playerCloseWidget(Player $player) {
2014-05-18 16:27:08 +02:00
$player->destroyCache($this, self::CACHE_CURRENT_PAGE);
2014-05-02 17:50:30 +02:00
$this->maniaControl->manialinkManager->closeWidget($player);
}
2013-12-28 23:24:54 +01:00
/**
2014-01-06 15:54:22 +01:00
* Reopen the widget on Map Begin, MapListChanged, etc.
2013-12-28 23:24:54 +01:00
*/
public function updateWidget() {
2014-05-18 16:27:08 +02:00
$players = $this->maniaControl->playerManager->getPlayers();
foreach ($players as $player) {
$currentPage = $player->getCache($this, self::CACHE_CURRENT_PAGE);
if ($currentPage !== null) {
$this->showMapList($player, null, $currentPage);
2013-12-28 23:24:54 +01:00
}
}
}
2013-12-14 22:00:59 +01:00
}