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
* Get the associated position with getEntryPosition($id)
*
* @api
* @param SidebarMenuEntryRenderable $render
* @param $order
* @param $id
* @return bool
* @api
*/
public function addMenuEntry(SidebarMenuEntryRenderable $render, $order, $id) {
if (isset($this->menuEntries[$order])) {
@ -157,10 +157,10 @@ class SidebarMenuManager implements UsageInformationAble, CallbackListener {
/**
* @api
* @param SidebarMenuEntryRenderable $render
* @param $id
* @param bool $unregisterClass
* @api
*/
public function deleteMenuEntry(SidebarMenuEntryRenderable $render, $id, $unregisterClass = false) {
foreach ($this->menuEntries as $k => $entry) {

View File

@ -5,7 +5,9 @@ namespace MCTeam;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Quad;
use FML\Controls\Quads\Quad_Bgs1InRace;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\ManiaLink;
use FML\Script\Features\Paging;
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_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_RECORDS_BEFORE_AFTER = 'Number of Records displayed before and after a player';
const CB_LOCALRECORDS_CHANGED = 'LocalRecords.Changed';
const ACTION_SHOW_RECORDSLIST = 'LocalRecords.ShowRecordsList';
/*
* 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_BEST_RECORDS, 10);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_ADJUST_OUTER_BORDER, false);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_RECORDS_BEFORE_AFTER, 2);
// Callbacks
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'handle1Second', 1000);
@ -200,17 +205,16 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$this->updateManialink = false;
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_ENABLE)) {
$manialink = $this->buildManialink();
$this->maniaControl->getManialinkManager()->sendManialink($manialink);
$this->sendWidgetManiaLink();
}
}
/**
* Build the local records manialink
* Build the local records widget ManiaLink and send it to the players
*
* @return string
*/
private function buildManialink() {
private function sendWidgetManiaLink() {
$map = $this->maniaControl->getMapManager()->getCurrentMap();
if (!$map) {
return null;
@ -222,19 +226,25 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$width = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH);
$lines = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINESCOUNT);
$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();
$quadStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadStyle();
$quadSubstyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadSubstyle();
$records = $this->getLocalRecords($map);
$records = $this->getLocalRecords($map, 1000);
if (!is_array($records)) {
Logger::logError("Couldn't fetch player records.");
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();
$manialink->addChild($frame);
$maniaLink->addChild($frame);
$frame->setPosition($posX, $posY);
$backgroundQuad = new Quad();
@ -255,21 +265,54 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$titleLabel->setText($title);
$titleLabel->setTranslate(true);
// Times
foreach ($records as $index => $record) {
if ($index >= $lines) {
break;
$preGeneratedRecordsFrame = $this->generateRecordsFrame($records, $lines - 2 * $recordsBeforeAfter - 1);
$players = $this->maniaControl->getPlayerManager()->getPlayers();
$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();
$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();
$recordFrame->addChild($rankLabel);
@ -298,17 +341,53 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$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 $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
*
* @param Map $map
* @param \ManiaControl\Maps\Map $map
* @param int $limit
* @return array
* @return array|null
*/
public function getLocalRecords(Map $map, $limit = -1) {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
@ -386,7 +465,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$player = $structure->getPlayer();
if(!$player){ //TODO verify why this can happen
if (!$player) { //TODO verify why this can happen
return;
}
@ -552,7 +631,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
// get PlayerList
$records = $this->getLocalRecords($this->maniaControl->getMapManager()->getCurrentMap(),200);
$records = $this->getLocalRecords($this->maniaControl->getMapManager()->getCurrentMap(), 200);
// create manialink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
@ -578,9 +657,9 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$headFrame->setY($posY - 5);
$labelLine = new LabelLine($headFrame);
$labelLine->addLabelEntryText('Rank',$posX + 5);
$labelLine->addLabelEntryText('Nickname',$posX + 18);
$labelLine->addLabelEntryText('Login',$posX + 70);
$labelLine->addLabelEntryText('Rank', $posX + 5);
$labelLine->addLabelEntryText('Nickname', $posX + 18);
$labelLine->addLabelEntryText('Login', $posX + 70);
$labelLine->addLabelEntryText('Time', $posX + 101);
$labelLine->render();
@ -607,15 +686,23 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$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) {
$listRecord->nickname = $listRecord->login;
}
$labelLine = new LabelLine($recordFrame);
$labelLine->addLabelEntryText($listRecord->rank,$posX + 5, 13);
$labelLine->addLabelEntryText('$fff' . $listRecord->nickname,$posX + 18, 52);
$labelLine->addLabelEntryText($listRecord->login,$posX + 70,31);
$labelLine->addLabelEntryText(Formatter::formatTime($listRecord->time),$posX + 101, $width / 2 - ($posX + 110));
$labelLine->addLabelEntryText($listRecord->rank, $posX + 5, 13);
$labelLine->addLabelEntryText('$fff' . $listRecord->nickname, $posX + 18, 52);
$labelLine->addLabelEntryText($listRecord->login, $posX + 70, 31);
$labelLine->addLabelEntryText(Formatter::formatTime($listRecord->time), $posX + 101, $width / 2 - ($posX + 110));
$labelLine->render();
$recordFrame->setY($posY);