Unified usages of Karma-Gauge in new ElementBuilder

This commit is contained in:
Alexander Nell 2020-04-24 18:52:18 +02:00
parent c2064c8d78
commit 66eb685688
8 changed files with 448 additions and 166 deletions

View File

@ -1,3 +1,8 @@
###v0.253###
#Changes
- //restart got replaced by //reboot
- Unified Creations of a Karma-Gauge in new Manialinks\ElementBuilder
###v0.252### ###v0.252###
#Additions #Additions
- Ingame-Changelog - Ingame-Changelog

View File

@ -294,31 +294,13 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener
} }
//Map Karma //Map Karma
$karma = $map->ratingVoteAverage / 100; $karmaGauge = $this->maniaControl->getManialinkManager()->getElementBuilder()->buildKarmaGauge(
$voteCount = $map->ratingVoteCount; $map,
if (is_numeric($karma) && $voteCount > 0) { 20,
$karmaGauge = new Gauge(); 10
);
$mapFrame->addChild($karmaGauge); $mapFrame->addChild($karmaGauge);
$karmaGauge->setZ(-0.05);
$karmaGauge->setX($posX + 87); $karmaGauge->setX($posX + 87);
$karmaGauge->setY(0.2);
$karmaGauge->setSize(20, 10);
$karmaGauge->setDrawBackground(false);
$karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '9');
$karmaLabel = new Label();
$mapFrame->addChild($karmaLabel);
$karmaLabel->setZ(1);
$karmaLabel->setX($posX + 87);
$karmaLabel->setSize(20 * 0.9, 5);
$karmaLabel->setY(-0.2);
$karmaLabel->setTextSize(0.9);
$karmaLabel->setTextColor('000');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')');
}
$posY -= 4; $posY -= 4;

View File

@ -0,0 +1,376 @@
<?php
namespace ManiaControl\Manialinks;
use FML\Controls\Frame;
use FML\Controls\Gauge;
use FML\Controls\Labels\Label_Button;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\ManiaLink;
use FML\Script\Features\Paging;
use ManiaControl\ManiaControl;
use ManiaControl\ManiaExchange\MXMapInfo;
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Maps\Map;
use ManiaControl\Players\Player;
use ManiaControl\Utils\ColorUtil;
/**
* ElementBuilder Class
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ElementBuilder {
/**
* Constants
*/
const COLUMN_ACTION_WIDTH = 4;
const COLUMN_ACTION_WIDTH_MIN = 20;
const COLUMN_ENTRY_WIDTH = 4;
const FOOTER_BUTTON_HEIGHT = 4;
const FOOTER_HEIGHT = 5;
const FOOTER_SPACING = 2;
const FOOTER_SPACING_BOTTOM = 10;
const HEADER_HEIGHT = 5;
const HEADER_SPACING = 2;
const HEADER_WIDTH_FACTOR = 0.9;
const ROW_LINE_HEIGHT = 4;
const ROW_SPACING_BOTTOM = 5;
const ROW_SPACING_SIDES = 2;
const ROW_SPACING_TOP = 5;
const DEFAULT_KARMA_PLUGIN = 'MCTeam\KarmaPlugin';
/**
* Private Properties
*/
/** @var array $actions */
private $actions = array();
/** @var array $columns */
private $columns = array();
/** @var callable $entryCurrent */
private $entryCurrent = null;
/** @var array $footerButtons */
private $footerButtons = array();
/** @var Frame $headerFrame */
private $headerFrame = null;
/** @var array $rowData */
private $rowData = array();
/**
* Construct a new ElementBuilder instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Adds actions to list entry
* @param string $icon
* @param bool $confirmation
* @param callable $tooltipFunction
*/
public function addAction($icon, $confirmation, $tooltipFunction) {
array_push($this->actions, array($icon, $confirmation, $tooltipFunction));
}
/**
* Adds a column to the list
* @param string $name
* @param float $widthFactor
* @param callable $dataFunction
*/
public function addColumn($name, $widthFactor, $dataFunction) {
array_push($this->columns, array($name, $widthFactor, $dataFunction));
}
/**
* Adds a arrow to point to the current/personal entry
* @param callable $function
*/
public function addEntryCurrent($function) {
$this->entryCurrent = $function;
}
/**
* Adds a footer button to the list
* @param string $description
* @param string $action
* @param callable $tooltipFunction
*/
public function addFooterButton($description, $action, $tooltipFunction = null) {
array_push($this->footerButtons, array($description, $action, $tooltipFunction));
}
/**
* Adds a header frame to the list
* @param Frame $headerFrame
*/
public function addHeaderFrame(Frame $headerFrame) {
$this->headerFrame = $headerFrame;
$this->headerFrame->setHeight(self::HEADER_HEIGHT);
$this->headerFrame->setWidth($this->getHeaderFrameWidth());
}
/**
* Adds rows to the list
* @param array $rows
*/
public function addRows(array $rows) {
$this->rowData = array_merge($this->rowData, $rows);
}
/**
* Build Karma Gauge
* @param Map|MXMapInfo $map
* @param float $width
* @param float $height
* @param float $textSize
*/
public function buildKarmaGauge($map, $width, $height, $textSize = 0.9) {
$karmaPlugin = $this->maniaControl->getPluginManager()->getPlugin(self::DEFAULT_KARMA_PLUGIN);
if (!$karmaPlugin) {
return null;
}
// default elements
$frame = new Frame();
$karmaGauge = new Gauge();
$frame->addChild($karmaGauge);
$karmaGauge->setDrawBackground(false);
$karmaGauge->setSize($width, $height);
$karmaGauge->setZ(-1);
$karmaLabel = new Label_Text();
$frame->addChild($karmaLabel);
$karmaLabel->setSize($width/2, $height * $textSize);
$karmaLabel->setStyle($this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultLabelStyle());
$karmaLabel->setTextColor('fff');
$karmaLabel->setTextSize($textSize);
$karmaLabel->setY(-$height/50);
$karmaLabel->setZ(1);
// fetch MX Karma
$displayMxKarma = $this->maniaControl->getSettingManager()->getSettingValue($karmaPlugin, $karmaPlugin::SETTING_WIDGET_DISPLAY_MX);
$karma = null;
$votes = null;
$mxInfo = null;
if ($map instanceof Map && isset($map->mx)) {
$mxInfo = $map->mx;
} elseif ($map instanceof MXMapInfo) {
$mxInfo = $map;
}
if ($displayMxKarma && $mxInfo) {
$karma = $mxInfo->ratingVoteAverage / 100;
$votes = array("count" => $mxInfo->ratingVoteCount);
} elseif ($map instanceof Map) {
$karma = $karmaPlugin->getMapKarma($map);
$votes = $karmaPlugin->getMapVotes($map);
}
if (!is_numeric($karma) || $votes['count'] <= 0) {
// No Karma
$karmaGauge->setColor('00fb');
$karmaGauge->setRatio(0.);
$karmaLabel->setText('-');
return $frame;
}
// Karma available
$karma = floatval($karma);
$karmaText = '';
if ($this->maniaControl->getSettingManager()->getSettingValue($karmaPlugin, $karmaPlugin::SETTING_NEWKARMA)) {
$karmaText = ' ' . round($karma * 100.) . '% (' . $votes['count'] . ')';
} else {
$minus = 0;
$plus = 0;
foreach ($votes as $vote) {
if (!isset($vote->vote) || $vote->vote === 0.5) {
continue;
}
if ($vote->vote < 0.5) {
$minus += $vote->count;
} else {
$plus += $vote->count;
}
}
$endKarma = $plus - $minus;
$karmaText = ' ' . $endKarma . ' (' . $votes['count'] . 'x / ' . round($karma * 100.) . '%)';
}
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '8');
$karmaGauge->setRatio(0.15 + 0.85*$karma);
$karmaLabel->setText($karmaText);
return $frame;
}
/**
* Returns the width of the optional header frame
*
* @return int
*/
public function getHeaderFrameWidth() {
return (int) (self::HEADER_WIDTH_FACTOR * $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsWidth());
}
public function renderList(Player $player) {
$nbActions = count($this->actions);
$nbFooterButtons = count($this->footerButtons);
$hasActions = $nbActions > 0;
$hasEntryCurrent = $this->entryCurrent !== null;
$hasFooter = $nbFooterButtons > 0;
$hasHeader = $this->headerFrame !== null;
$height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
$width = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsWidth();
$heightRows = $height - self::ROW_SPACING_TOP - self::ROW_SPACING_BOTTOM;
$posYRows = $height/2 - self::ROW_SPACING_TOP - self::ROW_LINE_HEIGHT/2;
// Create ManiaLink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$script = $maniaLink->getScript();
$paging = new Paging();
$script->addFeature($paging);
// Main frame
$frame = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultListFrame($script, $paging);
$maniaLink->addChild($frame);
// Header frame
if ($hasHeader) {
$frame->addChild($this->headerFrame);
$this->headerFrame->setY($height/2 - self::HEADER_SPACING - self::HEADER_HEIGHT/2);
$headerSize = 2*self::HEADER_SPACING + self::HEADER_HEIGHT;
$heightRows -= $headerSize;
if ($posYRows > $height/2 - $headerSize)
$posYRows = $height/2 - $headerSize;
}
// Footer
if ($hasFooter) {
$nbFooterSections = 3*$nbFooterButtons + 1;
$buttonSpacing = (1. / $nbFooterSections) * $width;
$buttonWidth = $buttonSpacing * 2;
for ($i = 0; $i < $nbFooterButtons; $i++) {
list($description, $action, $tooltipFunction) = $this->footerButtons[$i];
$label = new Label_Button();
$frame->addChild($label);
$label->setHeight(self::FOOTER_HEIGHT);
$label->setText($description);
$label->setTextSize(1);
$label->setWidth($buttonWidth);
$label->setX(-$width/2 + ($i+1)*$buttonSpacing + $i*$buttonWidth + $buttonWidth/2);
$label->setY(-$height/2 + self::FOOTER_SPACING_BOTTOM);
$label->setZ(0.1);
$quad = new Quad_BgsPlayerCard();
$frame->addChild($quad);
$quad->setAction($action);
$quad->setHeight(self::FOOTER_HEIGHT);
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setWidth($buttonWidth);
$quad->setX(-$width/2 + ($i+1)*$buttonSpacing + $i*$buttonWidth + $buttonWidth/2);
$quad->setY(-$height/2 + self::FOOTER_SPACING_BOTTOM);
$quad->setZ(0.01);
}
$footerSize = self::FOOTER_SPACING_BOTTOM + self::FOOTER_HEIGHT + self::FOOTER_SPACING;
$heightRows -= $footerSize;
}
$actionsWidth = max(self::COLUMN_ACTION_WIDTH_MIN, $nbActions * self::COLUMN_ACTION_WIDTH);
$baseColumnPosX = -$width / 2 + self::ROW_SPACING_SIDES + self::COLUMN_ENTRY_WIDTH;
$baseRowPosY = $posYRows;
$baseColumnWidth = $width - 2*self::ROW_SPACING_SIDES - self::COLUMN_ENTRY_WIDTH - $actionsWidth;
$nbRows = (int) ($heightRows / self::ROW_LINE_HEIGHT) - 1;
// Description Row
$descriptionFrame = new Frame();
$frame->addChild($descriptionFrame);
$descriptionFrame->setX($baseColumnPosX);
$descriptionFrame->setY($baseRowPosY);
$labelLine = new LabelLine($descriptionFrame);
$columnPosX = self::ROW_SPACING_SIDES;
if ($hasEntryCurrent) {
$columnPosX += self::COLUMN_ENTRY_WIDTH;
}
foreach ($this->columns as $column) {
list($name, $widthFactor, $dataFunction) = $column;
$labelLine->addLabelEntryText($name, $columnPosX);
$columnPosX += $widthFactor * $baseColumnWidth;
}
$labelLine->addLabelEntryText('Actions', $columnPosX);
$labelLine->render();
// Data Rows
$pageFrame = null;
$columnPosX = self::ROW_SPACING_SIDES;
$baseRowPosY -= self::ROW_LINE_HEIGHT;
$rowPosY = $baseRowPosY;
$pageNumber = 1;
for ($i = 0; $i < count($this->rowData); $i++) {
$data = $this->rowData[$i];
if ($i % $nbRows === 0) {
$pageFrame = new Frame();
$frame->addChild($pageFrame);
$paging->addPageControl($pageFrame, $pageNumber);
$pageNumber++;
$rowPosY = $baseRowPosY;
}
$playerFrame = new Frame();
$pageFrame->addChild($playerFrame);
$playerFrame->setX($baseColumnPosX);
$playerFrame->setY($rowPosY);
if ($i % 2 === 1) {
$lineQuad = new Quad_BgsPlayerCard();
$playerFrame->addChild($lineQuad);
$lineQuad->setSize($width, self::ROW_LINE_HEIGHT);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(-1);
}
if ($hasEntryCurrent) {
}
$rowPosY -= self::ROW_LINE_HEIGHT;
}
$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $player);
$this->reset();
}
/**
* Resets the internal builder data
*/
private function reset() {
$this->actions = array();
$this->columns = array();
$this->entryCurrent = null;
$this->footerButtons = array();
$this->headerFrame = null;
$this->rowData = array();
}
}

View File

@ -55,6 +55,9 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener,
/** @var SidebarMenuManager $sidebarMenuManager */ /** @var SidebarMenuManager $sidebarMenuManager */
private $sidebarMenuManager = null; private $sidebarMenuManager = null;
/** @var ElementBuilder $elementBuilder */
private $elementBuilder = null;
// TODO: use listening class // TODO: use listening class
private $pageAnswerListeners = array(); private $pageAnswerListeners = array();
private $pageAnswerRegexListener = array(); private $pageAnswerRegexListener = array();
@ -72,6 +75,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener,
$this->customUIManager = new CustomUIManager($maniaControl); $this->customUIManager = new CustomUIManager($maniaControl);
$this->iconManager = new IconManager($maniaControl); $this->iconManager = new IconManager($maniaControl);
$this->sidebarMenuManager = new SidebarMenuManager($maniaControl); $this->sidebarMenuManager = new SidebarMenuManager($maniaControl);
$this->elementBuilder = new ElementBuilder($maniaControl);
// Callbacks // Callbacks
$this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback'); $this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback');
@ -133,6 +137,15 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener,
return $this->iconManager; return $this->iconManager;
} }
/**
* Return the element builder
*
* @return ElementBuilder
*/
public function getElementBuilder() {
return $this->elementBuilder;
}
/** /**
* Register a new manialink page answer reg ex listener * Register a new manialink page answer reg ex listener
* *

View File

@ -142,7 +142,7 @@ class StyleManager implements UsageInformationAble {
$entry->setName('SearchString'); $entry->setName('SearchString');
if ($actionReset) { if ($actionReset) {
$quad = new Quad_Icons64x64_1();; $quad = new Quad_Icons64x64_1();
$frame->addChild($quad); $frame->addChild($quad);
$quad->setSubStyle($quad::SUBSTYLE_QuitRace); $quad->setSubStyle($quad::SUBSTYLE_QuitRace);
$quad->setColorize('aaa'); $quad->setColorize('aaa');
@ -150,7 +150,7 @@ class StyleManager implements UsageInformationAble {
$quad->setPosition(-$width / 2 + 15 + $width * 0.25 - 2, 0); $quad->setPosition(-$width / 2 + 15 + $width * 0.25 - 2, 0);
$quad->setZ(1); $quad->setZ(1);
$quad->setAction($actionReset); $quad->setAction($actionReset);
} }
//Search for Map-Name //Search for Map-Name
$label = new Label_Button(); $label = new Label_Button();

View File

@ -57,7 +57,6 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
const ACTION_RESET = 'MapList.ResetMapList'; const ACTION_RESET = 'MapList.ResetMapList';
const MAX_MAPS_PER_PAGE = 13; const MAX_MAPS_PER_PAGE = 13;
const MAX_PAGES_PER_CHUNK = 2; const MAX_PAGES_PER_CHUNK = 2;
const DEFAULT_KARMA_PLUGIN = 'MCTeam\KarmaPlugin';
const DEFAULT_CUSTOM_VOTE_PLUGIN = 'MCTeam\CustomVotesPlugin'; const DEFAULT_CUSTOM_VOTE_PLUGIN = 'MCTeam\CustomVotesPlugin';
const CACHE_CURRENT_PAGE = 'CurrentPage'; const CACHE_CURRENT_PAGE = 'CurrentPage';
const WIDGET_NAME = 'MapList'; const WIDGET_NAME = 'MapList';
@ -249,8 +248,6 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$frame->addChild($descriptionLabel); $frame->addChild($descriptionLabel);
$queuedMaps = $this->maniaControl->getMapManager()->getMapQueue()->getQueuedMapsRanking(); $queuedMaps = $this->maniaControl->getMapManager()->getMapQueue()->getQueuedMapsRanking();
/** @var KarmaPlugin $karmaPlugin */
$karmaPlugin = $this->maniaControl->getPluginManager()->getPlugin(self::DEFAULT_KARMA_PLUGIN);
$pageNumber = 1 + $chunkIndex * self::MAX_PAGES_PER_CHUNK; $pageNumber = 1 + $chunkIndex * self::MAX_PAGES_PER_CHUNK;
$paging->setStartPageNumber($pageIndex + 1); $paging->setStartPageNumber($pageIndex + 1);
@ -460,63 +457,14 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
} }
// Display Karma bar // Display Karma bar
if ($karmaPlugin) { $karmaGauge = $this->maniaControl->getManialinkManager()->getElementBuilder()->buildKarmaGauge(
$displayMxKarma = $this->maniaControl->getSettingManager()->getSettingValue($karmaPlugin, $karmaPlugin::SETTING_WIDGET_DISPLAY_MX); $map,
20,
//Display Mx Karma 10
if ($displayMxKarma && $map->mx) { );
$karma = $map->mx->ratingVoteAverage / 100; if ($karmaGauge) {
$votes = array("count" => $map->mx->ratingVoteCount);
//Display Local Karma
} else {
$karma = $karmaPlugin->getMapKarma($map);
$votes = $karmaPlugin->getMapVotes($map);
}
if (is_numeric($karma) && $votes['count'] > 0) {
if ($this->maniaControl->getSettingManager()->getSettingValue($karmaPlugin, $karmaPlugin::SETTING_NEWKARMA)) {
$karmaText = ' ' . round($karma * 100.) . '% (' . $votes['count'] . ')';
} else {
$min = 0;
$plus = 0;
foreach ($votes as $vote) {
if (isset($vote->vote)) {
if ($vote->vote !== 0.5) {
if ($vote->vote < 0.5) {
$min = $min + $vote->count;
} else {
$plus = $plus + $vote->count;
}
}
}
}
$endKarma = $plus - $min;
$karmaText = ' ' . $endKarma . ' (' . $votes['count'] . 'x / ' . round($karma * 100.) . '%)';
}
$karmaGauge = new Gauge();
$mapFrame->addChild($karmaGauge); $mapFrame->addChild($karmaGauge);
$karmaGauge->setZ(0.2);
$karmaGauge->setX($posX + 120); $karmaGauge->setX($posX + 120);
$karmaGauge->setY(0.2);
$karmaGauge->setSize(20, 10);
$karmaGauge->setDrawBackground(false);
$karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '9');
$karmaLabel = new Label();
$mapFrame->addChild($karmaLabel);
$karmaLabel->setZ(2);
$karmaLabel->setX($posX + 120);
$karmaLabel->setSize(20 * 0.9, 5);
$karmaLabel->setY(-0.2);
$karmaLabel->setTextSize(0.9);
$karmaLabel->setTextColor('000');
$karmaLabel->setText($karmaText);
}
} }
$posY -= 4; $posY -= 4;

View File

@ -21,7 +21,7 @@ abstract class ColorUtil implements UsageInformationAble {
* @param float $value * @param float $value
* @return string * @return string
*/ */
public static function floatToStatusColor($value) { public static function floatToStatusColor($value, $addBlue = true) {
$value = floatval($value); $value = floatval($value);
$red = 1.; $red = 1.;
$green = 1.; $green = 1.;
@ -33,7 +33,12 @@ abstract class ColorUtil implements UsageInformationAble {
} }
$red = ColorUtil::floatToCode($red); $red = ColorUtil::floatToCode($red);
$green = ColorUtil::floatToCode($green); $green = ColorUtil::floatToCode($green);
if ($addBlue) {
return $red . $green . '0'; return $red . $green . '0';
} else {
return $red . $green;
}
} }
/** /**
@ -55,7 +60,7 @@ abstract class ColorUtil implements UsageInformationAble {
if ($value < 10) { if ($value < 10) {
return (string) $value; return (string) $value;
} }
$codes = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f'); static $codes = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f');
return $codes[$value]; return $codes[$value];
} }
} }

View File

@ -3,7 +3,6 @@
namespace MCTeam; namespace MCTeam;
use FML\Controls\Frame; use FML\Controls\Frame;
use FML\Controls\Gauge;
use FML\Controls\Label; use FML\Controls\Label;
use FML\Controls\Quad; use FML\Controls\Quad;
use FML\ManiaLink; use FML\ManiaLink;
@ -71,8 +70,6 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
/** @var ManiaControl $maniaControl */ /** @var ManiaControl $maniaControl */
private $maniaControl = null; private $maniaControl = null;
private $updateManialink = false; private $updateManialink = false;
/** @var ManiaLink $manialink */
private $manialink = null;
// TODO: use some sort of model class instead of various array keys that you can't remember // TODO: use some sort of model class instead of various array keys that you can't remember
private $mxKarma = array(); private $mxKarma = array();
@ -150,9 +147,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_ENABLE, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_ENABLE, true);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_TITLE, 'Map-Karma'); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_TITLE, 'Map-Karma');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_POSX, 160 - 27.5); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_POSX, 160 - 27.5);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_POSY, 90 - 10 - 6); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_POSY, 90 - 9 - 5);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_WIDTH, 25.); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_WIDTH, 25.);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_HEIGHT, 12.); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_HEIGHT, 10.);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_NEWKARMA, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_NEWKARMA, true);
// Callbacks // Callbacks
@ -270,9 +267,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} else { } else {
Logger::logError("Error while authenticating on Mania-Exchange Karma"); Logger::logError("Error while authenticating on Mania-Exchange Karma");
if($data->data->message == "invalid server"){ if ($data->data->message == "invalid server") {
Logger::log("You need to get a Karma Key from MX with registering your server"); Logger::log("You need to get a Karma Key from MX with registering your server");
}else{ } else {
// TODO remove temp trigger // TODO remove temp trigger
$this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error ' . json_encode($data->data->message)); $this->maniaControl->getErrorHandler()->triggerDebugNotice('auth error ' . json_encode($data->data->message));
} }
@ -775,6 +772,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
return; return;
} }
$this->updateManialink = true;
$serverLogin = $this->maniaControl->getServer()->login; $serverLogin = $this->maniaControl->getServer()->login;
$karmaSettingName = self::buildKarmaSettingName($serverLogin); $karmaSettingName = self::buildKarmaSettingName($serverLogin);
@ -785,10 +783,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
case self::SETTING_WIDGET_ENABLE: { case self::SETTING_WIDGET_ENABLE: {
if ($setting->value) { if ($setting->value) {
$this->updateManialink = true;
$this->handle1Second(); $this->handle1Second();
} else { } else {
$this->updateManialink = false;
$this->maniaControl->getManialinkManager()->hideManialink(self::MLID_KARMA); $this->maniaControl->getManialinkManager()->hideManialink(self::MLID_KARMA);
} }
break; break;
@ -806,58 +802,21 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$displayMxKarma = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_DISPLAY_MX); $displayMxKarma = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_DISPLAY_MX);
// Get players
$players = $this->updateManialink;
if ($players === true) {
$players = $this->maniaControl->getPlayerManager()->getPlayers();
}
$this->updateManialink = false;
// Get map karma // Get map karma
$map = $this->maniaControl->getMapManager()->getCurrentMap(); $map = $this->maniaControl->getMapManager()->getCurrentMap();
// Display the mx Karma if the setting is chosen and the MX session is available // Display the mx Karma if the setting is chosen and the MX session is available
if ($displayMxKarma && isset($this->mxKarma['session']) && isset($this->mxKarma['voteCount'])) { if ($displayMxKarma && isset($this->mxKarma['session']) && isset($this->mxKarma['voteCount'])) {
$karma = $this->mxKarma['modeVoteAverage'] / 100; $map->mx->ratingVoteAverage = $this->mxKarma['modeVoteAverage'];
$voteCount = $this->mxKarma['modeVoteCount']; $map->mx->ratingVoteCount = $this->mxKarma['modeVoteCount'];
} else {
$karma = $this->getMapKarma($map);
$votes = $this->getMapVotes($map);
$voteCount = $votes['count'];
} }
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) { if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) {
// Build karma manialink // Build karma manialink
$this->buildManialink(); $this->buildManialink($map, $this->updateManialink);
// Update karma gauge & label
/**
* @var Gauge $karmaGauge
*/
$karmaGauge = $this->manialink->karmaGauge;
/**
* @var Label $karmaLabel
*/
$karmaLabel = $this->manialink->karmaLabel;
if (is_numeric($karma) && $voteCount > 0) {
$karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '7');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')');
} else {
$karmaGauge->setRatio(0.);
$karmaGauge->setColor('00fb');
$karmaLabel->setText('-');
} }
$this->updateManialink = false;
$this->maniaControl->getManialinkManager()->sendManialink($this->manialink, $players);
// TODO: show the player his own vote in some way
// $vote = $this->getPlayerVote($player, $map);
// $votesFrame = $this->manialink->votesFrame;
// $votesFrame->removeChildren();
}
} }
/** /**
@ -920,10 +879,11 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
/** /**
* Build Karma Voting Manialink if necessary * Build Karma Voting Manialink if necessary
* *
* @param Map $map
* @param bool $forceBuild * @param bool $forceBuild
*/ */
private function buildManialink($forceBuild = false) { private function buildManialink(Map $map = null, $forceBuild = false) {
if (is_object($this->manialink) && !$forceBuild) { if (!$forceBuild) {
return; return;
} }
@ -944,39 +904,32 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$backgroundQuad = new Quad(); $backgroundQuad = new Quad();
$frame->addChild($backgroundQuad); $frame->addChild($backgroundQuad);
$backgroundQuad->setY($height * 0.15);
$backgroundQuad->setSize($width, $height); $backgroundQuad->setSize($width, $height);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle); $backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$titleLabel = new Label(); $titleLabel = new Label();
$frame->addChild($titleLabel); $frame->addChild($titleLabel);
$titleLabel->setY($height * 0.36); $titleLabel->setScale(0.9);
$titleLabel->setWidth($width * 0.85);
$titleLabel->setStyle($labelStyle); $titleLabel->setStyle($labelStyle);
$titleLabel->setTranslate(true);
$titleLabel->setTextSize(1);
$titleLabel->setScale(0.90);
$titleLabel->setText($title); $titleLabel->setText($title);
$titleLabel->setTextSize(1);
$titleLabel->setTranslate(true);
$titleLabel->setWidth(0.85*$width);
$titleLabel->setY(0.2*$height);
$karmaGauge = new Gauge(); if ($map === null) {
$map = $this->maniaControl->getMapManager()->getCurrentMap();
}
$karmaGauge = $this->maniaControl->getManialinkManager()->getElementBuilder()->buildKarmaGauge(
$map,
0.95*$width,
0.95*$height
);
$karmaGauge->setY(-0.15*$height);
$frame->addChild($karmaGauge); $frame->addChild($karmaGauge);
$karmaGauge->setSize($width * 0.95, $height * 0.92);
$karmaGauge->setDrawBackground(false);
$manialink->karmaGauge = $karmaGauge;
$karmaLabel = new Label(); $this->maniaControl->getManialinkManager()->sendManialink($manialink);
$frame->addChild($karmaLabel);
$karmaLabel->setPosition(0, -0.4, 1);
$karmaLabel->setSize($width * 0.9, $height * 0.9);
$karmaLabel->setStyle($labelStyle);
$karmaLabel->setTextSize(1);
$manialink->karmaLabel = $karmaLabel;
$votesFrame = new Frame();
$frame->addChild($votesFrame);
$manialink->votesFrame = $votesFrame;
$this->manialink = $manialink;
} }
/** /**