TrackManiaControl/application/core/Configurators/ServerSettings.php

398 lines
11 KiB
PHP
Raw Normal View History

2014-01-02 23:49:21 +01:00
<?php
namespace ManiaControl\Configurators;
use FML\Controls\Control;
use FML\Controls\Entry;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\Script\Script;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2014-01-02 23:49:21 +01:00
/**
* Class offering a Configurator for Server Settings
*
* @author steeffeen & kremsy
*/
class ServerSettings implements ConfiguratorMenu, CallbackListener {
/**
* Constants
*/
2014-01-05 20:34:48 +01:00
const ACTION_PREFIX_SETTING = 'ServerSettings';
const ACTION_SETTING_BOOL = 'ServerSettings.ActionBoolSetting.';
const CB_SERVERSETTING_CHANGED = 'ServerSettings.SettingChanged';
2014-01-05 19:15:58 +01:00
const CB_SERVERSETTINGS_CHANGED = 'ServerSettings.SettingsChanged';
2014-01-05 20:34:48 +01:00
const TABLE_SERVER_SETTINGS = 'mc_serversettings';
2014-01-02 23:49:21 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
/**
* Create a new Server Settings Instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-05 12:21:40 +01:00
$this->initTables();
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
// Register for callbacks
2014-01-05 20:34:48 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
2014-01-04 17:18:11 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
2014-01-05 20:34:48 +01:00
$mysqli = $this->maniaControl->database->mysqli;
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SERVER_SETTINGS . "` (
2014-01-05 14:41:19 +01:00
`serverIndex` int(11) NOT NULL AUTO_INCREMENT,
2014-01-04 17:18:11 +01:00
`settingName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`settingValue` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
2014-01-05 14:41:19 +01:00
UNIQUE KEY `setting` (`serverIndex`, `settingName`)
2014-01-04 17:18:11 +01:00
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Server Settings' AUTO_INCREMENT=1;";
$statement = $mysqli->prepare($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:18:11 +01:00
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$statement->execute();
2014-02-01 20:03:17 +01:00
if ($statement->error) {
2014-01-04 17:18:11 +01:00
trigger_error($statement->error, E_USER_ERROR);
return false;
}
$statement->close();
return true;
}
/**
* Handle OnInit callback
*
* @param array $callback
*/
public function onInit(array $callback) {
$this->loadSettingsFromDatabase();
}
/**
* Load Settings from Database
2014-01-04 17:30:29 +01:00
*
2014-01-04 17:18:11 +01:00
* @return bool
*/
public function loadSettingsFromDatabase() {
2014-01-12 22:24:30 +01:00
$serverId = $this->maniaControl->server->index;
$mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SERVER_SETTINGS . "` WHERE serverIndex = " . $serverId . ";";
$result = $mysqli->query($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:18:11 +01:00
trigger_error($mysqli->error);
2014-01-05 12:21:40 +01:00
return false;
2014-01-04 17:18:11 +01:00
}
2014-01-20 09:24:17 +01:00
$serverSettings = $this->maniaControl->client->getServerOptions()->toArray();
2014-01-05 20:34:48 +01:00
while($row = $result->fetch_object()) {
2014-02-01 20:03:17 +01:00
if (!isset($serverSettings[$row->settingName])) {
2014-01-05 20:34:48 +01:00
continue;
}
2014-01-24 13:11:27 +01:00
$oldType = gettype($serverSettings[$row->settingName]);
$serverSettings[$row->settingName] = $row->settingValue;
settype($serverSettings[$row->settingName], $oldType);
2014-01-04 17:18:11 +01:00
}
$result->close();
2014-02-01 20:03:17 +01:00
if (!$serverSettings) {
2014-01-05 20:34:48 +01:00
return true;
2014-01-12 22:24:30 +01:00
}
2014-01-05 20:34:48 +01:00
2014-01-20 20:51:03 +01:00
try {
2014-01-24 13:11:27 +01:00
$this->maniaControl->client->setServerOptions($serverSettings);
} catch(\Exception $e) {
2014-01-20 20:51:03 +01:00
trigger_error('Error occured: ' . $e->getMessage());
2014-01-04 17:18:11 +01:00
return false;
}
return true;
2014-01-02 23:49:21 +01:00
}
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
*/
public function getTitle() {
2014-01-05 12:21:40 +01:00
return 'Server Settings';
2014-01-02 23:49:21 +01:00
}
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
*/
public function getMenu($width, $height, Script $script) {
$pagesId = 'ServerSettingsPages';
2014-01-05 20:34:48 +01:00
$frame = new Frame();
2014-01-20 09:24:17 +01:00
$serverSettings = $this->maniaControl->client->getServerOptions()->toArray();
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
// Config
2014-01-05 20:34:48 +01:00
$pagerSize = 9.;
2014-01-02 23:49:21 +01:00
$settingHeight = 5.;
$labelTextSize = 2;
2014-01-05 20:34:48 +01:00
$pageMaxCount = 13;
2014-01-02 23:49:21 +01:00
// Pagers
$pagerPrev = new Quad_Icons64x64_1();
$frame->add($pagerPrev);
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2);
$pagerPrev->setSize($pagerSize, $pagerSize);
$pagerPrev->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowPrev);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$pagerNext = new Quad_Icons64x64_1();
$frame->add($pagerNext);
$pagerNext->setPosition($width * 0.45, $height * -0.44, 2);
$pagerNext->setSize($pagerSize, $pagerSize);
$pagerNext->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowNext);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$script->addPager($pagerPrev, -1, $pagesId);
$script->addPager($pagerNext, 1, $pagesId);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$pageCountLabel = new Label();
$frame->add($pageCountLabel);
$pageCountLabel->setHAlign(Control::RIGHT);
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
$pageCountLabel->setStyle('TextTitle1');
$pageCountLabel->setTextSize(2);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$script->addPageLabel($pageCountLabel, $pagesId);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
// Setting pages
$pageFrames = array();
2014-01-05 20:34:48 +01:00
$y = 0.;
$id = 0;
foreach($serverSettings as $name => $value) {
2014-01-05 12:21:40 +01:00
// Continue on CurrentMaxPlayers...
$pos = strpos($name, "Current"); // TODO maybe current irgentwo anzeigen
2014-02-01 20:03:17 +01:00
if ($pos !== false) {
2014-01-02 23:49:21 +01:00
continue;
}
2014-01-05 20:34:48 +01:00
2014-02-01 20:03:17 +01:00
if (!isset($pageFrame)) {
2014-01-02 23:49:21 +01:00
$pageFrame = new Frame();
$frame->add($pageFrame);
2014-02-01 20:03:17 +01:00
if (!empty($pageFrames)) {
2014-01-02 23:49:21 +01:00
$pageFrame->setVisible(false);
}
array_push($pageFrames, $pageFrame);
$y = $height * 0.41;
$script->addPage($pageFrame, count($pageFrames), $pagesId);
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$settingFrame = new Frame();
$pageFrame->add($settingFrame);
$settingFrame->setY($y);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$nameLabel = new Label_Text();
$settingFrame->add($nameLabel);
$nameLabel->setHAlign(Control::LEFT);
$nameLabel->setX($width * -0.46);
$nameLabel->setSize($width * 0.4, $settingHeight);
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setTextSize($labelTextSize);
$nameLabel->setText($name);
$nameLabel->setTextColor("FFF");
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$substyle = '';
2014-02-01 20:03:17 +01:00
if ($value === false) {
2014-01-02 23:49:21 +01:00
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlRed;
2014-02-01 20:03:17 +01:00
} else if ($value === true) {
2014-01-02 23:49:21 +01:00
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlGreen;
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$entry = new Entry();
$settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setHAlign(Control::CENTER);
$entry->setX($width / 2 * 0.46);
$entry->setTextSize(1);
$entry->setSize($width * 0.48, $settingHeight * 0.9);
2014-01-05 20:34:48 +01:00
$entry->setName(self::ACTION_PREFIX_SETTING . '.' . $name);
2014-01-02 23:49:21 +01:00
$entry->setDefault($value);
2014-01-05 20:34:48 +01:00
2014-02-01 20:03:17 +01:00
if ($name == "Comment") { //
2014-01-03 15:58:17 +01:00
$entry->setAutoNewLine(true);
$entry->setSize($width * 0.48, $settingHeight * 3 + $settingHeight * 0.9);
$settingFrame->setY($y - $settingHeight * 1.5);
2014-01-05 12:21:40 +01:00
// dummy:
2014-01-03 15:58:17 +01:00
$y -= $settingHeight * 3;
$id += 3;
}
2014-01-05 20:34:48 +01:00
2014-02-01 20:03:17 +01:00
if ($substyle != '') {
2014-01-02 23:49:21 +01:00
$quad = new Quad_Icons64x64_1();
$settingFrame->add($quad);
$quad->setX($width / 2 * 0.46);
$quad->setZ(-0.01);
$quad->setSubStyle($substyle);
$quad->setSize(4, 4);
$quad->setHAlign(Control::CENTER);
$quad->setAction(self::ACTION_SETTING_BOOL . $name);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$entry->setVisible(false);
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$y -= $settingHeight;
2014-02-01 20:03:17 +01:00
if ($id % $pageMaxCount == $pageMaxCount - 1) {
2014-01-02 23:49:21 +01:00
unset($pageFrame);
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$id++;
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
return $frame;
}
/**
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
*/
public function saveConfigData(array $configData, Player $player) {
2014-01-05 20:34:48 +01:00
$prefix = explode(".", $configData[3][0]['Name']);
2014-02-01 20:03:17 +01:00
if ($prefix[0] != self::ACTION_PREFIX_SETTING) {
2014-01-05 20:34:48 +01:00
return;
}
2014-01-20 09:24:17 +01:00
$serverSettings = $this->maniaControl->client->getServerOptions()->toArray();
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$actionArray = explode(".", $configData[2]);
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$boolSettingName = '';
2014-02-01 20:03:17 +01:00
if (isset($actionArray[2])) {
2014-01-05 20:34:48 +01:00
$boolSettingName = self::ACTION_PREFIX_SETTING . '.' . $actionArray[2];
2014-01-02 23:49:21 +01:00
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
$newSettings = array();
2014-01-05 20:34:48 +01:00
foreach($configData[3] as $setting) {
2014-01-05 12:21:40 +01:00
// Check if it was a boolean button
2014-02-01 20:03:17 +01:00
if ($setting['Name'] == $boolSettingName) {
2014-01-02 23:49:21 +01:00
$setting['Value'] = ($setting['Value'] ? false : true);
}
2014-01-05 20:34:48 +01:00
$settingName = substr($setting['Name'], $prefixLength + 1);
2014-01-02 23:49:21 +01:00
$newSettings[$settingName] = $setting['Value'];
settype($newSettings[$settingName], gettype($serverSettings[$settingName]));
}
2014-01-05 20:34:48 +01:00
2014-01-16 19:40:54 +01:00
$this->applyNewServerSettings($newSettings, $player);
2014-01-05 20:34:48 +01:00
//Reopen the Menu
$menuId = $this->maniaControl->configurator->getMenuId($this->getTitle());
2014-01-18 21:05:45 +01:00
$this->maniaControl->configurator->reopenMenu($player, $menuId);
2014-01-02 23:49:21 +01:00
}
/**
* Handle ManialinkPageAnswer Callback
*
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
2014-01-05 20:34:48 +01:00
$actionId = $callback[1][2];
2014-01-02 23:49:21 +01:00
$boolSetting = (strpos($actionId, self::ACTION_SETTING_BOOL) === 0);
2014-02-01 20:03:17 +01:00
if (!$boolSetting) {
2014-01-05 20:34:48 +01:00
return;
2014-01-12 22:24:30 +01:00
}
2014-01-05 20:34:48 +01:00
$login = $callback[1][1];
2014-01-02 23:49:21 +01:00
$player = $this->maniaControl->playerManager->getPlayer($login);
2014-01-05 20:34:48 +01:00
2014-01-05 12:21:40 +01:00
// Save all Changes
2014-01-02 23:49:21 +01:00
$this->saveConfigData($callback[1], $player);
}
/**
2014-01-16 19:40:54 +01:00
* Apply the Array of new Server Settings
2014-01-02 23:49:21 +01:00
*
2014-01-05 20:34:48 +01:00
* @param array $newSettings
2014-01-02 23:49:21 +01:00
* @param Player $player
2014-01-04 17:18:11 +01:00
* @return bool
2014-01-02 23:49:21 +01:00
*/
2014-01-16 19:40:54 +01:00
private function applyNewServerSettings(array $newSettings, Player $player) {
2014-02-01 20:03:17 +01:00
var_dump($newSettings);
if (!$newSettings) {
2014-01-05 20:34:48 +01:00
return true;
2014-01-12 22:24:30 +01:00
}
2014-01-16 22:15:05 +01:00
2014-01-20 20:51:03 +01:00
try {
$this->maniaControl->client->setServerOptions($newSettings);
} catch(\Exception $e) {
2014-01-20 20:51:03 +01:00
trigger_error('Error occured: ' . $e->getMessage());
2014-01-04 17:18:11 +01:00
return false;
2014-01-16 22:15:05 +01:00
}
2014-01-05 20:34:48 +01:00
2014-01-04 17:18:11 +01:00
// Save Settings into Database
$mysqli = $this->maniaControl->database->mysqli;
2014-01-05 20:34:48 +01:00
$query = "INSERT INTO `" . self::TABLE_SERVER_SETTINGS . "` (
2014-01-05 14:41:19 +01:00
`serverIndex`,
2014-01-04 17:18:11 +01:00
`settingName`,
`settingValue`
) VALUES (
?, ?, ?
) ON DUPLICATE KEY UPDATE
`settingValue` = VALUES(`settingValue`);";
$statement = $mysqli->prepare($query);
2014-02-01 20:03:17 +01:00
if ($mysqli->error) {
2014-01-04 17:18:11 +01:00
trigger_error($mysqli->error);
return false;
2014-01-02 23:49:21 +01:00
}
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
// Notifications
$settingsCount = count($newSettings);
2014-01-05 20:34:48 +01:00
$settingIndex = 0;
$title = $this->maniaControl->authenticationManager->getAuthLevelName($player->authLevel);
2014-01-05 12:21:40 +01:00
// $chatMessage = '$ff0' . $title . ' $<' . $player->nickname . '$> set ScriptSetting' . ($settingsCount > 1 ? 's' : '') . ' ';
2014-01-05 20:34:48 +01:00
foreach($newSettings as $setting => $value) {
2014-02-01 20:03:17 +01:00
if ($value === null) {
2014-01-20 09:24:17 +01:00
continue;
}
2014-02-01 20:03:17 +01:00
$statement->bind_param('iss', $this->maniaControl->server->index, $setting, $value);
2014-01-04 17:18:11 +01:00
$statement->execute();
2014-02-01 20:03:17 +01:00
if ($statement->error) {
2014-01-04 17:18:11 +01:00
trigger_error($statement->error);
$statement->close();
return false;
}
2014-01-05 20:34:48 +01:00
2014-01-05 12:21:40 +01:00
// $chatMessage .= '$<' . '$fff' . preg_replace('/^S_/', '', $setting) . '$z$s$ff0 ';
// $chatMessage .= 'to $fff' . $this->parseSettingValue($value) . '$>';
2014-01-05 20:34:48 +01:00
2014-01-05 12:21:40 +01:00
/*
* if ($settingIndex <= $settingsCount - 2) { $chatMessage .= ', '; }
*/
2014-01-05 20:34:48 +01:00
2014-01-02 23:49:21 +01:00
// Trigger own callback
2014-01-05 20:34:48 +01:00
$this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTING_CHANGED, array(self::CB_SERVERSETTING_CHANGED, $setting, $value));
2014-01-02 23:49:21 +01:00
$settingIndex++;
}
2014-01-05 20:34:48 +01:00
2014-01-04 17:18:11 +01:00
$statement->close();
2014-01-05 20:34:48 +01:00
2014-01-05 19:15:58 +01:00
$this->maniaControl->callbackManager->triggerCallback(self::CB_SERVERSETTINGS_CHANGED, array(self::CB_SERVERSETTINGS_CHANGED));
2014-01-05 20:34:48 +01:00
2014-01-05 12:21:40 +01:00
// $chatMessage .= '!';
// $this->maniaControl->chat->sendInformation($chatMessage);
// $this->maniaControl->log(Formatter::stripCodes($chatMessage));
2014-01-05 20:34:48 +01:00
2014-01-04 17:18:11 +01:00
return true;
2014-01-02 23:49:21 +01:00
}
}