Started to restructure Widgets of LocalRecords and Dedimania Plugins

This commit is contained in:
Jocy 2017-05-21 23:11:52 +02:00
parent 77c15c0a82
commit 6663d8baa6
4 changed files with 273 additions and 148 deletions

View File

@ -0,0 +1,167 @@
<?php
namespace MCTeam\Common;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Quad;
use FML\Controls\Quads\Quad_Bgs1InRace;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Utils\Formatter;
/**
* ManiaControl Local Records Plugin
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2017 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class RecordWidget {
private $maniaControl;
private $lineHeight = 4.;
private $width = 40.;
/**
* RecordWidget constructor.
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Returns a Frame with one line of the Record
*
* @param $record
* @param \ManiaControl\Players\Player|null $player
* @return \FML\Controls\Frame
*/
public function generateRecordLineFrame($record, Player $player = null) {
$width = $this->width;
$lineHeight = $this->lineHeight;
$recordFrame = new Frame();
$largeNumberDiff = 0;
if ($record->rank > 999) {
$largeNumberDiff = 0.03;
}
$rankLabel = new Label();
$recordFrame->addChild($rankLabel);
$rankLabel->setHorizontalAlign($rankLabel::LEFT);
$rankLabel->setX($width * -0.49);
$rankLabel->setSize($width * (0.09 + $largeNumberDiff), $lineHeight);
$rankLabel->setTextSize(1);
$rankLabel->setTextPrefix('$o');
$rankLabel->setText($record->rank);
$rankLabel->setTextEmboss(true);
$nameLabel = new Label();
$recordFrame->addChild($nameLabel);
$nameLabel->setHorizontalAlign($nameLabel::LEFT);
$nameLabel->setX($width * (-0.39 + $largeNumberDiff));
$nameLabel->setSize($width * (0.6 - $largeNumberDiff), $lineHeight);
$nameLabel->setTextSize(1);
$nameLabel->setText($record->nickname);
$nameLabel->setTextEmboss(true);
$timeLabel = new Label();
$recordFrame->addChild($timeLabel);
$timeLabel->setHorizontalAlign($timeLabel::RIGHT);
$timeLabel->setX($width * 0.49);
$timeLabel->setSize($width * 0.27, $lineHeight);
$timeLabel->setTextSize(1);
if (isset($record->time)) {
$timeLabel->setText(Formatter::formatTime($record->time));
} else {
$timeLabel->setText(Formatter::formatTime($record->best));
}
$timeLabel->setTextEmboss(true);
if ($player && $player->index == $record->playerIndex) {
$quad = new Quad();
$recordFrame->addChild($quad);
$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList);
$quad->setSize($width, $lineHeight);
}
return $recordFrame;
}
/**
* Returns a Frame with Records to a given limit
*
* @param $records
* @param $limit
* @param \ManiaControl\Players\Player|null $player
* @return \FML\Controls\Frame
*/
public function generateRecordsFrame($records, $limit, Player $player = null) {
$lineHeight = $this->lineHeight;
$frame = new Frame();
foreach ($records as $index => $record) {
if ($index >= $limit) {
break;
}
$y = -8. - $index * $lineHeight;
$recordFrame = $this->generateRecordLineFrame($record, $player);
$frame->addChild($recordFrame);
$recordFrame->setPosition(0, $y);
}
return $frame;
}
/**
* Returns the default separator Quad for the RecordWidget
*
* @param $width
* @return \FML\Controls\Quad
*/
public function getLineSeparatorQuad($width) {
$quad = new Quad();
$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList);
$quad->setSize($width, 0.4);
return $quad;
}
/**
* @return float
*/
public function getLineHeight() {
return $this->lineHeight;
}
/**
* @param float $lineHeight
*/
public function setLineHeight($lineHeight) {
$this->lineHeight = $lineHeight;
}
/**
* @return float
*/
public function getWidth() {
return $this->width;
}
/**
* @param float $width
*/
public function setWidth($width) {
$this->width = $width;
}
}

View File

@ -19,7 +19,10 @@ use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
use ManiaControl\Utils\Formatter;
use MCTeam\Common\RecordWidget;
/**
* ManiaControl Dedimania Plugin
@ -56,6 +59,9 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
/** @var \MCTeam\Common\RecordWidget $recordWidget */
private $recordWidget = null;
private $checkpoints = array();
/** @var \MCTeam\Dedimania\DedimaniaWebHandler $webHandler */
@ -100,6 +106,8 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONWAYPOINT, $this, 'handleCheckpointCallback');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONFINISHLINE, $this, 'handleFinishCallback');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONLAPFINISH, $this, 'handleFinishCallback');
$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChanged');
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'updateEverySecond', 1000);
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'handleEveryHalfMinute', 1000 * 30);
@ -129,6 +137,8 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$this->webHandler = new DedimaniaWebHandler($this->maniaControl);
$this->webHandler->setDedimaniaData($dedimaniaData);
$this->webHandler->openDedimaniaSession(true);
$this->recordWidget = new RecordWidget($this->maniaControl);
}
@ -174,6 +184,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$height = 7. + $lines * $lineHeight;
$backgroundQuad->setSize($width * 1.05, $height);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$backgroundQuad->setAction(self::ACTION_SHOW_DEDIRECORDSLIST);
$titleLabel = new Label();
$frame->addChild($titleLabel);
@ -184,54 +195,8 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$titleLabel->setText($title);
$titleLabel->setTranslate(true);
foreach ($records as $index => $record) {
/** @var RecordData $record */
if ($index >= $lines) {
break;
}
$y = -8. - $index * $lineHeight;
$recordFrame = new Frame();
$frame->addChild($recordFrame);
$recordFrame->setPosition(0, $y);
/*$backgroundQuad = new Quad();
$recordFrame->addChild($backgroundQuad);
$backgroundQuad->setSize($width * 1.04, $lineHeight * 1.4);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);*/
//Rank
$rankLabel = new Label();
$recordFrame->addChild($rankLabel);
$rankLabel->setHorizontalAlign($rankLabel::LEFT);
$rankLabel->setX($width * -0.47);
$rankLabel->setSize($width * 0.06, $lineHeight);
$rankLabel->setTextSize(1);
$rankLabel->setTextPrefix('$o');
$rankLabel->setText($record->rank);
$rankLabel->setTextEmboss(true);
//Name
$nameLabel = new Label();
$recordFrame->addChild($nameLabel);
$nameLabel->setHorizontalAlign($nameLabel::LEFT);
$nameLabel->setX($width * -0.4);
$nameLabel->setSize($width * 0.6, $lineHeight);
$nameLabel->setTextSize(1);
$nameLabel->setText($record->nickName);
$nameLabel->setTextEmboss(true);
//Time
$timeLabel = new Label();
$recordFrame->addChild($timeLabel);
$timeLabel->setHorizontalAlign($timeLabel::RIGHT);
$timeLabel->setX($width * 0.47);
$timeLabel->setSize($width * 0.25, $lineHeight);
$timeLabel->setTextSize(1);
$timeLabel->setText(Formatter::formatTime($record->best));
$timeLabel->setTextEmboss(true);
}
$recordsFrame = $this->recordWidget->generateRecordsFrame($records,$lines);
$frame->addChild($recordsFrame);
$this->maniaControl->getManialinkManager()->sendManialink($manialink);
}
@ -626,13 +591,13 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$recordFrame->addChild($lineQuad);
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(0.001);
$lineQuad->setZ(-0.001);
}
if (strlen($listRecord->nickName) < 2) {
$listRecord->nickName = $listRecord->login;
if (strlen($listRecord->nickname) < 2) {
$listRecord->nickname = $listRecord->login;
}
$array = array($listRecord->rank => $posX + 5, '$fff' . $listRecord->nickName => $posX + 18, $listRecord->login => $posX + 70, Formatter::formatTime($listRecord->best) => $posX + 101);
$array = array($listRecord->rank => $posX + 5, '$fff' . $listRecord->nickname => $posX + 18, $listRecord->login => $posX + 70, Formatter::formatTime($listRecord->best) => $posX + 101);
$this->maniaControl->getManialinkManager()->labelLine($recordFrame, $array);
$recordFrame->setY($posY);
@ -654,6 +619,26 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
return $this->webHandler->getDedimaniaData()->records;
}
/**
* Update the RecordWidget variables
*/
private function updateRecordWidget() {
$width = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH);
$lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINE_HEIGHT);
$this->recordWidget->setWidth($width);
$this->recordWidget->setLineHeight($lineHeight);
$this->webHandler->maniaLinkUpdateNeeded();
}
public function handleSettingChanged(Setting $setting){
if (!$setting->belongsToClass($this)) {
return;
}
$this->updateRecordWidget();
}
/**
* @see \ManiaControl\Plugins\Plugin::unload()
*/

View File

@ -17,7 +17,7 @@ class RecordData {
*/
public $nullRecord = false;
public $login = '';
public $nickName = '';
public $nickname = '';
public $best = -1;
public $rank = -1;
public $maxRank = -1;
@ -38,7 +38,7 @@ class RecordData {
}
$this->login = $record['Login'];
$this->nickName = Formatter::stripDirtyCodes($record['NickName']);
$this->nickname = Formatter::stripDirtyCodes($record['NickName']);
$this->best = $record['Best'];
$this->rank = $record['Rank'];
$this->maxRank = $record['MaxRank'];
@ -54,10 +54,10 @@ class RecordData {
* @param int $checkpoints
* @param bool $newRecord
*/
public function constructNewRecord($login, $nickName, $best, $checkpoints, $newRecord = false) {
public function constructNewRecord($login, $nickname, $best, $checkpoints, $newRecord = false) {
$this->nullRecord = false;
$this->login = $login;
$this->nickName = $nickName;
$this->nickname = $nickname;
$this->best = $best;
$this->checkpoints = $checkpoints;
$this->newRecord = $newRecord;

View File

@ -29,6 +29,7 @@ use ManiaControl\Plugins\Plugin;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
use ManiaControl\Utils\Formatter;
use MCTeam\Common\RecordWidget;
/**
* ManiaControl Local Records Plugin
@ -66,7 +67,9 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $maniaControl = null;
/** @var \MCTeam\Common\RecordWidget $recordWidget */
private $recordWidget = null;
private $updateManialink = false;
private $checkpoints = array();
@ -120,6 +123,8 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene
$this->maniaControl = $maniaControl;
$this->initTables();
$this->recordWidget = new RecordWidget($this->maniaControl);
// Settings
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_TITLE, 'Local Records');
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_POSX, -139.);
@ -266,12 +271,16 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene
$titleLabel->setText($title);
$titleLabel->setTranslate(true);
$topRecordsCount = $lines - $recordsBeforeAfter * 2 - 1;
$preGeneratedRecordsFrame = $this->generateRecordsFrame($records, $topRecordsCount);
$topRecordsCount = $lines - $recordsBeforeAfter * 2 - 1;
$preGeneratedTopRecordsFrame = $this->recordWidget->generateRecordsFrame($records, $topRecordsCount);
$preGeneratedRecordsFrameWithRecord = $this->recordWidget->generateRecordsFrame($records, $lines - 1);
$players = $this->maniaControl->getPlayerManager()->getPlayers();
$players = $this->maniaControl->getPlayerManager()->getPlayers();
$playersWithoutRecord = array();
foreach ($players as $player) {
$sendManiaLink = true;
$maniaLink = new ManiaLink(self::MLID_RECORDS);
$maniaLink->addChild($frame);
@ -280,116 +289,68 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene
$listFrame->setPosition($posX, $posY);
if (isset($playerRecords[$player->index]) && $playerRecords[$player->index] >= $topRecordsCount) {
$listFrame->addChild($preGeneratedRecordsFrame);
$listFrame->addChild($preGeneratedTopRecordsFrame);
$y = -8 - $topRecordsCount * $lineHeight;
$playerIndex = $playerRecords[$player->index];
//Line separator
$quad = new Quad();
$quad = $this->recordWidget->getLineSeparatorQuad($width);
$listFrame->addChild($quad);
$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList);
$quad->setSize($width, 0.4);
$quad->setY($y + $lineHeight / 2);
//Generate the Records around a player and display below topRecords
for ($i = $playerIndex - $recordsBeforeAfter; $i <= $playerIndex + $recordsBeforeAfter; $i++) {
if(array_key_exists($i, $records)){ //If there are no records behind you
$recordFrame = $this->generateRecordLineFrame($records[$i], $player);
if (array_key_exists($i, $records)) { //If there are no records behind you
$recordFrame = $this->recordWidget->generateRecordLineFrame($records[$i], $player);
$recordFrame->setY($y);
$listFrame->addChild($recordFrame);
$y -= $lineHeight;
}
}
} else if (isset($playerRecords[$player->index]) && $playerRecords[$player->index] < $topRecordsCount) {
$playersWithoutRecord[] = $player;
$sendManiaLink = false;
} else {
$listFrame->addChild($this->generateRecordsFrame($records, $lines, $player)); //TODO Collect all players with this case and send a common manialink to save resources
if ($record = $this->getLocalRecord($map, $player)) {
$record->nickname = $player->nickname;
$recordFrame = $preGeneratedRecordsFrameWithRecord;
$listFrame->addChild($recordFrame);
$y = -8 - ($lines - 1) * $lineHeight;
//Line separator
$quad = $this->recordWidget->getLineSeparatorQuad($width);
$listFrame->addChild($quad);
$quad->setY($y + $lineHeight / 2);
$frame = $this->recordWidget->generateRecordLineFrame($record, $player);
$listFrame->addChild($frame);
$frame->setY($y);
} else {
$playersWithoutRecord[] = $player;
$sendManiaLink = false;
}
}
$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $player);
}
}
/**
* Returns a Frame with one line of the Record
*
* @param $record
* @param \ManiaControl\Players\Player|null $player
* @return \FML\Controls\Frame
*/
private function generateRecordLineFrame($record, Player $player = null) {
$width = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH);
$lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT);
$recordFrame = new Frame();
$rankLabel = new Label();
$recordFrame->addChild($rankLabel);
$rankLabel->setHorizontalAlign($rankLabel::LEFT);
$rankLabel->setX($width * -0.49);
$rankLabel->setSize($width * 0.09, $lineHeight);
$rankLabel->setTextSize(1);
$rankLabel->setTextPrefix('$o');
$rankLabel->setText($record->rank);
$rankLabel->setTextEmboss(true);
$nameLabel = new Label();
$recordFrame->addChild($nameLabel);
$nameLabel->setHorizontalAlign($nameLabel::LEFT);
$nameLabel->setX($width * -0.39);
$nameLabel->setSize($width * 0.6, $lineHeight);
$nameLabel->setTextSize(1);
$nameLabel->setText($record->nickname);
$nameLabel->setTextEmboss(true);
$timeLabel = new Label();
$recordFrame->addChild($timeLabel);
$timeLabel->setHorizontalAlign($timeLabel::RIGHT);
$timeLabel->setX($width * 0.49);
$timeLabel->setSize($width * 0.27, $lineHeight);
$timeLabel->setTextSize(1);
$timeLabel->setText(Formatter::formatTime($record->time));
$timeLabel->setTextEmboss(true);
if ($player && $player->index == $record->playerIndex) {
$quad = new Quad();
$recordFrame->addChild($quad);
$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList);
$quad->setSize($width, $lineHeight);
}
return $recordFrame;
}
/**
* Returns a Frame with Records to a given limit
*
* @param $records
* @param $limit
* @param \ManiaControl\Players\Player|null $player
* @return \FML\Controls\Frame
*/
private function generateRecordsFrame($records, $limit, Player $player = null) {
$lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT);
$frame = new Frame();
foreach ($records as $index => $record) {
if ($index >= $limit) {
break;
if ($sendManiaLink) {
$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $player);
}
$y = -8. - $index * $lineHeight;
$recordFrame = $this->generateRecordLineFrame($record, $player);
$frame->addChild($recordFrame);
$recordFrame->setPosition(0, $y);
}
return $frame;
}
if ($playersWithoutRecord) {
$maniaLink = new ManiaLink(self::MLID_RECORDS);
$maniaLink->addChild($frame);
$listFrame = $this->recordWidget->generateRecordsFrame($records, $lines);
$maniaLink->addChild($listFrame);
$listFrame->setPosition($posX, $posY);
$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $playersWithoutRecord);
}
}
/**
* Fetch local records for the given map
@ -442,7 +403,7 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene
break;
}
default:
$this->updateManialink = true;
$this->updateRecordWidget();
break;
}
}
@ -765,4 +726,16 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_LOCALRECORDS_CHANGED, null);
$this->maniaControl->getChat()->sendInformation('Record no. $<$fff' . $recordId . '$> has been removed!');
}
/**
* Update the RecordWidget variables
*/
private function updateRecordWidget() {
$width = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH);
$lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT);
$this->recordWidget->setWidth($width);
$this->recordWidget->setLineHeight($lineHeight);
$this->updateManialink = true;
}
}