Added new functionality to LocalRecordsPlugin

This commit is contained in:
Jocy 2017-05-19 01:16:57 +02:00
parent 7efff0fa84
commit f8363441bb
2 changed files with 171 additions and 84 deletions

View File

@ -122,11 +122,11 @@ class SidebarMenuManager implements UsageInformationAble, CallbackListener {
* Registers an Entry to the SidebarMenu * Registers an Entry to the SidebarMenu
* Get the associated position with getEntryPosition($id) * Get the associated position with getEntryPosition($id)
* *
* @api
* @param SidebarMenuEntryRenderable $render * @param SidebarMenuEntryRenderable $render
* @param $order * @param $order
* @param $id * @param $id
* @return bool * @return bool
* @api
*/ */
public function addMenuEntry(SidebarMenuEntryRenderable $render, $order, $id) { public function addMenuEntry(SidebarMenuEntryRenderable $render, $order, $id) {
if (isset($this->menuEntries[$order])) { if (isset($this->menuEntries[$order])) {
@ -157,10 +157,10 @@ class SidebarMenuManager implements UsageInformationAble, CallbackListener {
/** /**
* @api
* @param SidebarMenuEntryRenderable $render * @param SidebarMenuEntryRenderable $render
* @param $id * @param $id
* @param bool $unregisterClass * @param bool $unregisterClass
* @api
*/ */
public function deleteMenuEntry(SidebarMenuEntryRenderable $render, $id, $unregisterClass = false) { public function deleteMenuEntry(SidebarMenuEntryRenderable $render, $id, $unregisterClass = false) {
foreach ($this->menuEntries as $k => $entry) { foreach ($this->menuEntries as $k => $entry) {

View File

@ -5,7 +5,9 @@ namespace MCTeam;
use FML\Controls\Frame; use FML\Controls\Frame;
use FML\Controls\Label; use FML\Controls\Label;
use FML\Controls\Quad; use FML\Controls\Quad;
use FML\Controls\Quads\Quad_Bgs1InRace;
use FML\Controls\Quads\Quad_BgsPlayerCard; use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\ManiaLink; use FML\ManiaLink;
use FML\Script\Features\Paging; use FML\Script\Features\Paging;
use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Admin\AuthenticationManager;
@ -54,9 +56,11 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
const SETTING_NOTIFY_ONLY_DRIVER = 'Notify only the Driver on New Records'; const SETTING_NOTIFY_ONLY_DRIVER = 'Notify only the Driver on New Records';
const SETTING_NOTIFY_BEST_RECORDS = 'Notify Publicly only for the X Best Records'; const SETTING_NOTIFY_BEST_RECORDS = 'Notify Publicly only for the X Best Records';
const SETTING_ADJUST_OUTER_BORDER = 'Adjust outer Border to Number of actual Records'; const SETTING_ADJUST_OUTER_BORDER = 'Adjust outer Border to Number of actual Records';
const SETTING_RECORDS_BEFORE_AFTER = 'Number of Records displayed before and after a player';
const CB_LOCALRECORDS_CHANGED = 'LocalRecords.Changed'; const CB_LOCALRECORDS_CHANGED = 'LocalRecords.Changed';
const ACTION_SHOW_RECORDSLIST = 'LocalRecords.ShowRecordsList'; const ACTION_SHOW_RECORDSLIST = 'LocalRecords.ShowRecordsList';
/* /*
* Private properties * Private properties
*/ */
@ -126,6 +130,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_NOTIFY_ONLY_DRIVER, false); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_NOTIFY_ONLY_DRIVER, false);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_NOTIFY_BEST_RECORDS, 10); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_NOTIFY_BEST_RECORDS, 10);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_ADJUST_OUTER_BORDER, false); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_ADJUST_OUTER_BORDER, false);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_RECORDS_BEFORE_AFTER, 2);
// Callbacks // Callbacks
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'handle1Second', 1000); $this->maniaControl->getTimerManager()->registerTimerListening($this, 'handle1Second', 1000);
@ -200,17 +205,16 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$this->updateManialink = false; $this->updateManialink = false;
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) { if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) {
$manialink = $this->buildManialink(); $this->sendWidgetManiaLink();
$this->maniaControl->getManialinkManager()->sendManialink($manialink);
} }
} }
/** /**
* Build the local records manialink * Build the local records widget ManiaLink and send it to the players
* *
* @return string * @return string
*/ */
private function buildManialink() { private function sendWidgetManiaLink() {
$map = $this->maniaControl->getMapManager()->getCurrentMap(); $map = $this->maniaControl->getMapManager()->getCurrentMap();
if (!$map) { if (!$map) {
return null; return null;
@ -222,19 +226,25 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$width = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH); $width = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH);
$lines = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINESCOUNT); $lines = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINESCOUNT);
$lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT); $lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT);
$recordsBeforeAfter = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_RECORDS_BEFORE_AFTER);
$labelStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultLabelStyle(); $labelStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultLabelStyle();
$quadStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadStyle(); $quadStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadStyle();
$quadSubstyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadSubstyle(); $quadSubstyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadSubstyle();
$records = $this->getLocalRecords($map); $records = $this->getLocalRecords($map, 1000);
if (!is_array($records)) { if (!is_array($records)) {
Logger::logError("Couldn't fetch player records."); Logger::logError("Couldn't fetch player records.");
return null; return null;
} }
$manialink = new ManiaLink(self::MLID_RECORDS); $playerRecords = array();
foreach ($records as $index => $record) {
$playerRecords[$record->playerIndex] = $index;
}
$maniaLink = new ManiaLink(self::MLID_RECORDS);
$frame = new Frame(); $frame = new Frame();
$manialink->addChild($frame); $maniaLink->addChild($frame);
$frame->setPosition($posX, $posY); $frame->setPosition($posX, $posY);
$backgroundQuad = new Quad(); $backgroundQuad = new Quad();
@ -255,21 +265,54 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$titleLabel->setText($title); $titleLabel->setText($title);
$titleLabel->setTranslate(true); $titleLabel->setTranslate(true);
// Times $preGeneratedRecordsFrame = $this->generateRecordsFrame($records, $lines - 2 * $recordsBeforeAfter - 1);
foreach ($records as $index => $record) {
if ($index >= $lines) { $players = $this->maniaControl->getPlayerManager()->getPlayers();
break; $topRecordsCount = $lines - $recordsBeforeAfter * 2 - 1;
foreach ($players as $player) {
if (isset($playerRecords[$player->index]) && $playerRecords[$player->index] >= $topRecordsCount) {
$frame->addChild($preGeneratedRecordsFrame);
$y = -8 - $topRecordsCount * $lineHeight;
$playerIndex = $playerRecords[$player->index];
//Line separator
$quad = new Quad();
$frame->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++) {
$recordFrame = $this->generateRecordLineFrame($records[$i],$player);
$recordFrame->setY($y);
$frame->addChild($recordFrame);
$y -= $lineHeight;
} }
$y = -8. - $index * $lineHeight; } else {
$frame->addChild($this->generateRecordsFrame($records, $lines, $player));
}
$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(); $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);
*/
$rankLabel = new Label(); $rankLabel = new Label();
$recordFrame->addChild($rankLabel); $recordFrame->addChild($rankLabel);
@ -298,17 +341,53 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$timeLabel->setTextSize(1); $timeLabel->setTextSize(1);
$timeLabel->setText(Formatter::formatTime($record->time)); $timeLabel->setText(Formatter::formatTime($record->time));
$timeLabel->setTextEmboss(true); $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 $manialink; 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;
}
$y = -8. - $index * $lineHeight;
$recordFrame = $this->generateRecordLineFrame($record, $player);
$frame->addChild($recordFrame);
$recordFrame->setPosition(0, $y);
}
return $frame;
}
/** /**
* Fetch local records for the given map * Fetch local records for the given map
* *
* @param Map $map * @param \ManiaControl\Maps\Map $map
* @param int $limit * @param int $limit
* @return array * @return array|null
*/ */
public function getLocalRecords(Map $map, $limit = -1) { public function getLocalRecords(Map $map, $limit = -1) {
$mysqli = $this->maniaControl->getDatabase()->getMysqli(); $mysqli = $this->maniaControl->getDatabase()->getMysqli();
@ -607,6 +686,14 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$lineQuad->setZ(-0.001); $lineQuad->setZ(-0.001);
} }
if ($listRecord->login === $player->login) {
$currentQuad = new Quad_Icons64x64_1();
$recordFrame->addChild($currentQuad);
$currentQuad->setX($posX + 3.5);
$currentQuad->setSize(4, 4);
$currentQuad->setSubStyle($currentQuad::SUBSTYLE_ArrowBlue);
}
if (strlen($listRecord->nickname) < 2) { if (strlen($listRecord->nickname) < 2) {
$listRecord->nickname = $listRecord->login; $listRecord->nickname = $listRecord->login;
} }