TrackManiaControl/core/Server/VoteRatiosMenu.php

191 lines
5.8 KiB
PHP
Raw Permalink Normal View History

2014-07-25 17:47:46 +02:00
<?php
namespace ManiaControl\Server;
use FML\Controls\Entry;
use FML\Controls\Frame;
use FML\Controls\Labels\Label_Text;
use FML\Script\Script;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Configurator\ConfiguratorMenu;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Structures\VoteRatio;
/**
* Class offering a Configurator Menu for Vote Ratios
*
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
2014-07-25 17:47:46 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class VoteRatiosMenu implements CallbackListener, ConfiguratorMenu, TimerListener {
/*
* Constants
*/
const SETTING_PERMISSION_CHANGE_VOTE_RATIOS = 'Change Vote Ratios';
const ACTION_PREFIX_VOTE_RATIO = 'VoteRatiosMenu.VoteRatio.';
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
2014-07-25 17:47:46 +02:00
private $maniaControl = null;
/**
* Construct a new vote ratios menu instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Permissions
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHANGE_VOTE_RATIOS, AuthenticationManager::AUTH_LEVEL_ADMIN);
2014-07-25 17:47:46 +02:00
}
/**
2017-03-25 19:15:50 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::getTitle()
2014-07-25 17:47:46 +02:00
*/
public static function getTitle() {
return 'Vote Ratios';
}
/**
2017-03-25 19:15:50 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::getMenu()
2014-07-25 17:47:46 +02:00
*/
public function getMenu($width, $height, Script $script, Player $player) {
2014-07-27 21:07:42 +02:00
$frame = new Frame();
2014-07-25 17:47:46 +02:00
$posY = $height * 0.41;
$lineHeight = 5.;
2014-07-25 17:58:45 +02:00
$index = 0;
2014-07-25 17:47:46 +02:00
2014-07-27 21:07:42 +02:00
$voteRatioCommands = $this->getVoteCommands();
2014-08-13 11:05:52 +02:00
$voteRatios = $this->maniaControl->getClient()->getCallVoteRatios();
2014-07-25 17:58:45 +02:00
foreach ($voteRatioCommands as $voteRatioCommand => $voteRatioDescription) {
2014-07-25 17:47:46 +02:00
$voteRatioFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($voteRatioFrame);
2014-07-25 17:47:46 +02:00
$voteRatioFrame->setY($posY);
$nameLabel = new Label_Text();
2017-03-25 19:15:50 +01:00
$voteRatioFrame->addChild($nameLabel);
$nameLabel->setHorizontalAlign($nameLabel::LEFT);
$nameLabel->setX($width * -0.5 + 4);
$nameLabel->setSize($width * 0.7, $lineHeight);
$nameLabel->setTextSize(2);
$nameLabel->setTranslate(true);
$nameLabel->setText($voteRatioDescription);
2014-07-25 17:47:46 +02:00
$entry = new Entry();
2017-03-25 19:15:50 +01:00
$voteRatioFrame->addChild($entry);
$entry->setX($width * 0.5 - $width * 0.14 / 2 - 4);
$entry->setSize($width * 0.14, $lineHeight * 0.9);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setTextSize($index === 0 ? 2 : 1);
$entry->setName(self::ACTION_PREFIX_VOTE_RATIO . $voteRatioCommand);
2014-07-25 17:47:46 +02:00
$voteRatio = $this->getVoteRatioForCommand($voteRatios, $voteRatioCommand);
if ($voteRatio) {
$entry->setDefault($voteRatio->ratio);
}
$posY -= $lineHeight;
if ($index === 0) {
$posY -= $lineHeight;
}
2014-07-25 17:58:45 +02:00
$index++;
2014-07-25 17:47:46 +02:00
}
return $frame;
}
2014-07-27 21:07:42 +02:00
/**
* Get the list of available vote commands
*
* @return string[]
*/
private function getVoteCommands() {
return array(VoteRatio::COMMAND_DEFAULT => 'Default', VoteRatio::COMMAND_RESTART_MAP => 'Restart Map', VoteRatio::COMMAND_NEXT_MAP => 'Skip Map', VoteRatio::COMMAND_SET_NEXT_MAP => 'Set next Map', VoteRatio::COMMAND_JUMP_MAP => 'Jump to Map', VoteRatio::COMMAND_TEAM_BALANCE => 'Balance Teams', VoteRatio::COMMAND_SCRIPT_SETTINGS => 'Change Script Settings and Commands', VoteRatio::COMMAND_KICK => 'Kick Players', VoteRatio::COMMAND_BAN => 'Ban Players');
}
2014-07-25 17:47:46 +02:00
/**
* Return the vote ratio for the given command
*
* @param VoteRatio[] $voteRatios
* @param string $command
* @return VoteRatio
*/
private function getVoteRatioForCommand(array $voteRatios, $command) {
foreach ($voteRatios as $voteRatio) {
if ($voteRatio->command === $command) {
return $voteRatio;
}
}
return null;
}
/**
* @see \ManiaControl\Configurator\ConfiguratorMenu::saveConfigData()
2014-07-25 17:47:46 +02:00
*/
public function saveConfigData(array $configData, Player $player) {
2014-08-13 11:05:52 +02:00
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_VOTE_RATIOS)
2014-08-05 02:17:41 +02:00
) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
2014-07-25 17:47:46 +02:00
return;
}
if (!$configData[3] || strpos($configData[3][0]['Name'], self::ACTION_PREFIX_VOTE_RATIO) !== 0) {
return;
}
$prefixLength = strlen(self::ACTION_PREFIX_VOTE_RATIO);
$newVoteRatios = array();
foreach ($configData[3] as $voteRatioEntry) {
$voteRatioName = substr($voteRatioEntry['Name'], $prefixLength);
$voteRatioValue = $voteRatioEntry['Value'];
if ($voteRatioValue === '') {
continue;
}
if (!is_numeric($voteRatioValue)) {
$this->sendInvalidValueError($player, $voteRatioName);
continue;
}
$voteRatio = new VoteRatio();
$voteRatio->command = $voteRatioName;
$voteRatio->ratio = (float)$voteRatioValue;
if (!$voteRatio->isValid()) {
$this->sendInvalidValueError($player, $voteRatioName);
continue;
}
array_push($newVoteRatios, $voteRatio);
}
2014-08-13 11:05:52 +02:00
$success = $this->maniaControl->getClient()->setCallVoteRatios($newVoteRatios);
2014-07-25 17:47:46 +02:00
if ($success) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess('Vote Ratios saved!', $player);
2014-07-25 17:47:46 +02:00
} else {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Vote Ratios saving failed!', $player);
2014-07-25 17:47:46 +02:00
}
// Reopen the Menu
2014-08-13 11:05:52 +02:00
$this->maniaControl->getConfigurator()->showMenu($player, $this);
2014-07-25 17:47:46 +02:00
}
/**
* Inform the player that his entered value is invalid
*
* @param Player $player
* @param string $commandName
*/
private function sendInvalidValueError(Player $player, $commandName) {
2020-06-02 17:30:30 +02:00
$message = $this->maniaControl->getChat()->formatMessage(
'Invalid Value given for %s!',
$commandName
);
$this->maniaControl->getChat()->sendError($message, $player);
2014-07-25 17:47:46 +02:00
}
}