From 9bed5b3d3f87805dbce9aeb3465f841b4136cf6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Thu, 28 Nov 2013 03:47:08 +0100 Subject: [PATCH] - Improved ManialinkManager - First Step for ingame Configurator - AdminMenu - StyleManager - Callbacks of PlayerManager --- application/core/Admin/AdminMenu.php | 147 ++++++++++++++++++ .../core/Admin/AuthenticationManager.php | 3 - .../core/Callbacks/CallbackManager.php | 2 - application/core/Commands/CommandManager.php | 10 +- .../core/Configurators/Configurator.php | 60 +++++++ application/core/ManiaControl.php | 27 ++-- .../core/Manialinks/ManialinkIdHandler.php | 31 ---- .../core/Manialinks/ManialinkManager.php | 48 +++++- application/core/Manialinks/StyleManager.php | 67 ++++++++ application/core/Players/PlayerManager.php | 31 ++-- .../core/Settings/SettingConfigurator.php | 41 ----- application/core/Settings/SettingManager.php | 5 - application/plugins/Karma.php | 9 +- application/plugins/LocalRecords.php | 71 +++++---- 14 files changed, 402 insertions(+), 150 deletions(-) create mode 100644 application/core/Configurators/Configurator.php delete mode 100644 application/core/Manialinks/ManialinkIdHandler.php create mode 100644 application/core/Manialinks/StyleManager.php delete mode 100644 application/core/Settings/SettingConfigurator.php diff --git a/application/core/Admin/AdminMenu.php b/application/core/Admin/AdminMenu.php index 64387dd7..044c45ce 100644 --- a/application/core/Admin/AdminMenu.php +++ b/application/core/Admin/AdminMenu.php @@ -1,5 +1,152 @@ maniaControl = $maniaControl; + + // Init settings + $this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_POSX, 130.); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_POSY, -70.); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_ITEMSIZE, 6.); + + // Register for callbacks + $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_ONINIT, $this, 'handleOnInit'); + $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerJoined'); + } + + /** + * Add a new menu item + * + * @param Control $control + * @param float $order + */ + public function addMenuItem(Control $control, $order = 0.) { + if (!isset($this->menuItems[$order])) { + $this->menuItems[$order] = array(); + } + array_push($this->menuItems[$order], $control); + } + + /** + * Handle ManiaControl OnInit callback + * + * @param array $callback + */ + public function handleOnInit(array $callback) { + $this->buildManialink(); + $manialinkText = $this->manialink->render()->saveXML(); + $players = $this->maniaControl->playerManager->getPlayers(); + foreach ($players as $player) { + if (!$this->checkPlayerRight($player)) continue; + $this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login); + } + } + + /** + * Handle PlayerConnect callback + * + * @param array $callback + */ + public function handlePlayerJoined(array $callback) { + $player = $callback[1]; + if (!$player || !$this->checkPlayerRight($player)) return; + $this->buildManialink(); + $manialinkText = $this->manialink->render()->saveXML(); + $this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login); + } + + /** + * Check if the player has access to the admin menu + * + * @param Player $player + * @return bool + */ + private function checkPlayerRight(Player $player) { + return AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_OPERATOR); + } + + /** + * Build the menu manialink if necessary + * + * @param bool $forceBuild + */ + private function buildManialink($forceBuild = false) { + if (is_object($this->manialink) && !$forceBuild) return; + + $posX = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MENU_POSX); + $posY = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MENU_POSY); + $itemSize = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MENU_ITEMSIZE); + $quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle(); + $quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle(); + + $itemCount = count($this->menuItems); + $itemMarginFactorX = 1.3; + $itemMarginFactorY = 1.2; + + $manialink = new ManiaLink(self::MLID_MENU); + + $frame = new Frame(); + $manialink->add($frame); + $frame->setPosition($posX, $posY); + + $backgroundQuad = new Quad(); + $frame->add($backgroundQuad); + $backgroundQuad->setSize($itemCount * $itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY); + $backgroundQuad->setStyles($quadStyle, $quadSubstyle); + + $itemsFrame = new Frame(); + $frame->add($itemsFrame); + + // Add items + $x = 0.5 * $itemSize * $itemMarginFactorX; + foreach ($this->menuItems as $itemOrder => $menuItems) { + foreach ($menuItems as $menuItem) { + $menuItem->setSize($itemSize, $itemSize); + $itemsFrame->add($menuItem); + + $x += $itemSize * $itemMarginFactorX; + } + } + + $this->manialink = $manialink; + } +} ?> diff --git a/application/core/Admin/AuthenticationManager.php b/application/core/Admin/AuthenticationManager.php index 5234474b..0843c0a6 100644 --- a/application/core/Admin/AuthenticationManager.php +++ b/application/core/Admin/AuthenticationManager.php @@ -185,9 +185,6 @@ class AuthenticationManager implements CommandListener { * @return bool */ public static function checkRight(Player $player, $neededAuthLevel) { - if (!$player) { - return false; - } return ($player->authLevel >= $neededAuthLevel); } diff --git a/application/core/Callbacks/CallbackManager.php b/application/core/Callbacks/CallbackManager.php index d7c46a6f..08e0154a 100644 --- a/application/core/Callbacks/CallbackManager.php +++ b/application/core/Callbacks/CallbackManager.php @@ -2,8 +2,6 @@ namespace ManiaControl\Callbacks; -require_once __DIR__ . '/CallbackListener.php'; - use ManiaControl\ManiaControl; /** diff --git a/application/core/Commands/CommandManager.php b/application/core/Commands/CommandManager.php index 17df15bb..63b4fcf2 100644 --- a/application/core/Commands/CommandManager.php +++ b/application/core/Commands/CommandManager.php @@ -2,8 +2,6 @@ namespace ManiaControl\Commands; -require_once __DIR__ . '/CommandListener.php'; - use ManiaControl\ManiaControl; use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackListener; @@ -60,30 +58,28 @@ class CommandManager implements CallbackListener { * Handle chat callback * * @param array $callback - * @return bool */ public function handleChatCallback(array $callback) { // Check for command if (!$callback[1][3]) { - return false; + return; } // Check for valid player $player = $this->maniaControl->playerManager->getPlayer($callback[1][1]); if (!$player) { - return false; + return; } // Handle command $command = explode(" ", substr($callback[1][2], 1)); $command = strtolower($command[0]); if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) { // No command listener registered - return true; + return; } // Inform command listeners foreach ($this->commandListeners[$command] as $listener) { call_user_func(array($listener[0], $listener[1]), $callback, $player); } - return true; } } diff --git a/application/core/Configurators/Configurator.php b/application/core/Configurators/Configurator.php new file mode 100644 index 00000000..3d517aaf --- /dev/null +++ b/application/core/Configurators/Configurator.php @@ -0,0 +1,60 @@ +maniaControl = $maniaControl; + $this->addAdminMenuItem(); + + // Register for page answer + $this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_CONFIG, $this, 'handlePageAnswer'); + } + + /** + * Handle PageAnswer callback + * + * @param array $callback + * @param Player $player + */ + public function handlePageAnswer(array $callback, Player $player) { + } + + /** + * Add menu item to the admin menu + */ + private function addAdminMenuItem() { + $itemQuad = new Quad(); + $itemQuad->setStyles('Icons128x32_1', 'Settings'); + $itemQuad->setAction(self::ACTION_CONFIG); + + $this->maniaControl->adminMenu->addMenuItem($itemQuad, 5); + } +} + +?> diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 38bbc338..e8052207 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -2,26 +2,34 @@ namespace ManiaControl; +use ManiaControl\Admin\AdminMenu; use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandManager; -use ManiaControl\Manialinks\ManialinkIdHandler; +use ManiaControl\Configurators\Configurator; +use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Maps\MapManager; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; use ManiaControl\Plugins\PluginManager; use ManiaControl\Server\Server; -use ManiaControl\Manialinks\ManialinkManager; -require_once __DIR__ . '/Callbacks/CallbackManager.php'; -require_once __DIR__ . '/Commands/CommandManager.php'; +require_once __DIR__ . '/Callbacks/CallbackListener.php'; +require_once __DIR__ . '/Commands/CommandListener.php'; +require_once __DIR__ . '/Manialinks/ManialinkPageAnswerListener.php'; +require_once __DIR__ . '/Admin/AdminMenu.php'; require_once __DIR__ . '/Admin/AuthenticationManager.php'; +require_once __DIR__ . '/Callbacks/CallbackManager.php'; require_once __DIR__ . '/Chat.php'; +require_once __DIR__ . '/ColorUtil.php'; +require_once __DIR__ . '/Commands/CommandManager.php'; +require_once __DIR__ . '/Configurators/Configurator.php'; require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/FileUtil.php'; require_once __DIR__ . '/Formatter.php'; -require_once __DIR__ . '/Manialinks/ManialinkIdHandler.php'; +require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php'; +require_once __DIR__ . '/ManiaExchange/mxinfosearcher.inc.php'; require_once __DIR__ . '/Manialinks/ManialinkManager.php'; require_once __DIR__ . '/Maps/Map.php'; require_once __DIR__ . '/Maps/MapManager.php'; @@ -30,9 +38,6 @@ require_once __DIR__ . '/Plugins/PluginManager.php'; require_once __DIR__ . '/Server/Server.php'; require_once __DIR__ . '/Settings/SettingManager.php'; require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php'; -require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php'; -require_once __DIR__ . '/ManiaExchange/mxinfosearcher.inc.php'; -require_once __DIR__ . '/ColorUtil.php'; list($endiantest) = array_values(unpack('L1L', pack('V', 1))); if ($endiantest == 1) { require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php'; @@ -57,9 +62,11 @@ class ManiaControl implements CommandListener { /** * Public properties */ + public $adminMenu = null; public $authenticationManager = null; public $callbackManager = null; public $chat = null; + public $configurator = null; public $client = null; public $commandManager = null; public $database = null; @@ -81,8 +88,9 @@ class ManiaControl implements CommandListener { public function __construct() { $this->database = new Database($this); $this->callbackManager = new CallbackManager($this); - $this->manialinkManager = new ManialinkManager($this); $this->settingManager = new SettingManager($this); + $this->manialinkManager = new ManialinkManager($this); + $this->adminMenu = new AdminMenu($this); $this->chat = new Chat($this); $this->commandManager = new CommandManager($this); $this->server = new Server($this); @@ -90,6 +98,7 @@ class ManiaControl implements CommandListener { $this->authenticationManager = new AuthenticationManager($this); $this->mapManager = new MapManager($this); $this->pluginManager = new PluginManager($this); + $this->configurator = new Configurator($this); $this->commandManager->registerCommandListener('version', $this, 'command_Version'); } diff --git a/application/core/Manialinks/ManialinkIdHandler.php b/application/core/Manialinks/ManialinkIdHandler.php deleted file mode 100644 index 55897fd4..00000000 --- a/application/core/Manialinks/ManialinkIdHandler.php +++ /dev/null @@ -1,31 +0,0 @@ -maniaLinkIdCount++); - } - return $manialinkIds; - } -} - -?> diff --git a/application/core/Manialinks/ManialinkManager.php b/application/core/Manialinks/ManialinkManager.php index aa4382f3..3d869465 100644 --- a/application/core/Manialinks/ManialinkManager.php +++ b/application/core/Manialinks/ManialinkManager.php @@ -6,7 +6,7 @@ use ManiaControl\ManiaControl; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; -require_once __DIR__ . '/ManialinkPageAnswerListener.php'; +require_once __DIR__ . '/StyleManager.php'; require_once __DIR__ . '/../FML/autoload.php'; /** @@ -15,11 +15,16 @@ require_once __DIR__ . '/../FML/autoload.php'; * @author steeffeen & kremsy */ class ManialinkManager implements CallbackListener { + /** + * Public properties + */ + public $styleManager = null; /** * Private properties */ private $maniaControl = null; private $pageAnswerListeners = array(); + private $maniaLinkIdCount = 0; /** * Create a new manialink manager @@ -28,6 +33,9 @@ class ManialinkManager implements CallbackListener { */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; + $this->styleManager = new StyleManager($maniaControl); + + // Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); } @@ -35,32 +43,56 @@ class ManialinkManager implements CallbackListener { /** * Register a new manialink page answer listener * - * @param string $manialinkId + * @param string $actionId * @param ManialinkPageAnswerListener $listener * @param string $method * @return bool */ - public function registerManialinkPageAnswerListener($manialinkId, ManialinkPageAnswerListener $listener, $method) { + public function registerManialinkPageAnswerListener($actionId, ManialinkPageAnswerListener $listener, $method) { if (!method_exists($listener, $method)) { - trigger_error("Given listener for manialinkId '{$manialinkId}' doesn't have callback method '{$method}'."); + trigger_error("Given listener for actionId '{$actionId}' doesn't have callback method '{$method}'!"); return false; } - if (!array_key_exists($manialinkId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$manialinkId])) { + if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) { // Init listeners array - $this->pageAnswerListeners[$manialinkId] = array(); + $this->pageAnswerListeners[$actionId] = array(); } // Register page answer listener - array_push($this->pageAnswerListeners[$manialinkId], array($listener, $method)); + array_push($this->pageAnswerListeners[$actionId], array($listener, $method)); return true; } + /** + * Reserve manialink ids + * + * @param int $count + * @return array + */ + public function reserveManiaLinkIds($count) { + $manialinkIds = array(); + for ($i = 0; $i < $count; $i++) { + array_push($manialinkIds, $this->maniaLinkIdCount++); + } + return $manialinkIds; + } + /** * Handle ManialinkPageAnswer callback * * @param array $callback */ public function handleManialinkPageAnswer(array $callback) { - var_dump($callback); + $actionId = $callback[1][2]; + $login = $callback[1][1]; + $player = $this->maniaControl->playerManager->getPlayer($login); + if (!array_key_exists($actionId, $this->pageAnswerListeners) || !is_array($this->pageAnswerListeners[$actionId])) { + // No page answer listener registered + return; + } + // Inform page answer listeners + foreach ($this->pageAnswerListeners[$actionId] as $listener) { + call_user_func(array($listener[0], $listener[1]), $callback, $player); + } } /** diff --git a/application/core/Manialinks/StyleManager.php b/application/core/Manialinks/StyleManager.php new file mode 100644 index 00000000..f540a2c1 --- /dev/null +++ b/application/core/Manialinks/StyleManager.php @@ -0,0 +1,67 @@ +maniaControl = $maniaControl; + + // Init settings + $this->maniaControl->settingManager->initSetting($this, self::SETTING_LABEL_DEFAULT_STYLE, 'TextTitle1'); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_QUAD_DEFAULT_STYLE, 'Bgs1InRace'); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_QUAD_DEFAULT_SUBSTYLE, 'BgTitleShadow'); + } + + /** + * Get the default label style + * + * @return string + */ + public function getDefaultLabelStyle() { + return $this->maniaControl->settingManager->getSetting($this, self::SETTING_LABEL_DEFAULT_STYLE); + } + + /** + * Get the default quad style + * + * @return string + */ + public function getDefaultQuadStyle() { + return $this->maniaControl->settingManager->getSetting($this, self::SETTING_QUAD_DEFAULT_STYLE); + } + + /** + * Get the default quad substyle + * + * @return string + */ + public function getDefaultQuadSubstyle() { + return $this->maniaControl->settingManager->getSetting($this, self::SETTING_QUAD_DEFAULT_SUBSTYLE); + } +} + +?> diff --git a/application/core/Players/PlayerManager.php b/application/core/Players/PlayerManager.php index 441e4d60..ce50ce48 100644 --- a/application/core/Players/PlayerManager.php +++ b/application/core/Players/PlayerManager.php @@ -18,6 +18,8 @@ class PlayerManager implements CallbackListener { /** * Constants */ + const CB_PLAYERJOINED = 'PlayerManagerCallback.PlayerJoined'; + const CB_ONINIT = 'PlayerManagerCallback.OnInit'; const TABLE_PLAYERS = 'mc_players'; const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages'; @@ -34,9 +36,12 @@ class PlayerManager implements CallbackListener { */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; - $this->initTables(); + // Init settings + $this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, false); + + // Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this, @@ -82,9 +87,6 @@ class PlayerManager implements CallbackListener { * @param array $callback */ public function onInit(array $callback) { - // register settings - $this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, false); - // Add all players $this->maniaControl->client->query('GetPlayerList', 300, 0, 2); $playerList = $this->maniaControl->client->getResponse(); @@ -97,6 +99,9 @@ class PlayerManager implements CallbackListener { $player = new Player($playerInfo); $this->addPlayer($player); } + + // Trigger own callback + $this->maniaControl->callbackManager->triggerCallback(self::CB_ONINIT, array(self::CB_ONINIT)); } /** @@ -111,15 +116,17 @@ class PlayerManager implements CallbackListener { $player = new Player($playerInfo); $this->addPlayer($player); - if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) { - return; + if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) { + // TODO: improve styling? + $string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); + $nickname = Formatter::stripCodes($player->nickname); + $this->maniaControl->chat->sendChat( + '$ff0' . $string[$player->authLevel] . ': ' . $nickname . '$z $ff0Nation:$fff ' . $player->getCountry() . + ' $ff0Ladder: $fff' . $player->ladderRank); } - // TODO: improve styling? - $string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); - $nickname = Formatter::stripCodes($player->nickname); - $this->maniaControl->chat->sendChat( - '$ff0' . $string[$player->authLevel] . ': ' . $nickname . '$z $ff0Nation:$fff ' . $player->getCountry() . - ' $ff0Ladder: $fff' . $player->ladderRank); + + // Trigger own callback + $this->maniaControl->callbackManager->triggerCallback(self::CB_PLAYERJOINED, array(self::CB_PLAYERJOINED, $player)); } /** diff --git a/application/core/Settings/SettingConfigurator.php b/application/core/Settings/SettingConfigurator.php deleted file mode 100644 index 4e880a5e..00000000 --- a/application/core/Settings/SettingConfigurator.php +++ /dev/null @@ -1,41 +0,0 @@ -maniaControl = $maniaControl; - - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit'); - } - - /** - * Handle OnInit callback - * - * @param array $callback - */ - public function onInit(array $callback) { - // TODO: handle callback - // $this->maniaControl->manialinkUtil-> - // $this->maniaControl->chat->sendChat("test"); - } -} \ No newline at end of file diff --git a/application/core/Settings/SettingManager.php b/application/core/Settings/SettingManager.php index 441c6bc3..f446fa82 100644 --- a/application/core/Settings/SettingManager.php +++ b/application/core/Settings/SettingManager.php @@ -2,10 +2,6 @@ namespace ManiaControl; -require_once __DIR__ . '/SettingConfigurator.php'; - -use ManiaControl\Settings\SettingConfigurator; - /** * Class managing settings and configurations * @@ -37,7 +33,6 @@ class SettingManager { public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; $this->initTables(); - $this->configurator = new SettingConfigurator($maniaControl); } /** diff --git a/application/plugins/Karma.php b/application/plugins/Karma.php index c7095b40..a8f34b7b 100644 --- a/application/plugins/Karma.php +++ b/application/plugins/Karma.php @@ -372,11 +372,14 @@ class KarmaPlugin extends Plugin implements CallbackListener { private function buildManialink($forceBuild = false) { if (is_object($this->manialink) && !$forceBuild) return; - $title = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_TITLE, 'Map-Karma'); + $title = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_TITLE); $pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSX); $pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSY); $width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_WIDTH); $height = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_HEIGHT); + $labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle(); + $quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle(); + $quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle(); $manialink = new ManiaLink(self::MLID_KARMA); @@ -388,13 +391,13 @@ class KarmaPlugin extends Plugin implements CallbackListener { $frame->add($backgroundQuad); $backgroundQuad->setY($height * 0.15); $backgroundQuad->setSize($width, $height); - $backgroundQuad->setStyles('Bgs1InRace', 'BgTitleShadow'); + $backgroundQuad->setStyles($quadStyle, $quadSubstyle); $titleLabel = new Label(); $frame->add($titleLabel); $titleLabel->setY($height * 0.36); $titleLabel->setWidth($width * 0.85); - $titleLabel->setStyle('TextTitle1'); + $titleLabel->setStyle($labelStyle); $titleLabel->setTranslate(true); $titleLabel->setTextSize(1); $titleLabel->setText($title); diff --git a/application/plugins/LocalRecords.php b/application/plugins/LocalRecords.php index e6b7b1f8..20f54cf8 100644 --- a/application/plugins/LocalRecords.php +++ b/application/plugins/LocalRecords.php @@ -27,6 +27,12 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { const VERSION = '1.0'; const MLID_RECORDS = 'ml_local_records'; const TABLE_RECORDS = 'mc_localrecords'; + const SETTING_WIDGET_TITLE = 'Widget Title'; + const SETTING_WIDGET_POSX = 'Widget Position: X'; + const SETTING_WIDGET_POSY = 'Widget Position: Y'; + const SETTING_WIDGET_WIDTH = 'Widget Width'; + const SETTING_WIDGET_LINESCOUNT = 'Widget Displayed Lines Count'; + const SETTING_WIDGET_LINEHEIGHT = 'Widget Line Height'; /** * Private properties @@ -38,10 +44,16 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; - - // Init tables $this->initTables(); + // Init settings + $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_TITLE, 'Local Records'); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSX, -139.); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSY, 65.); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_WIDTH, 40.); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINESCOUNT, 25); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINEHEIGHT, 4.); + // Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_SECOND, $this, 'handle1Second'); @@ -66,8 +78,9 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { PRIMARY KEY (`index`), UNIQUE KEY `player_map_record` (`mapIndex`,`playerIndex`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;"; - if (!$mysqli->query($query)) { - trigger_error("Couldn't create records table. " . $mysqli->error, E_USER_ERROR); + $mysqli->query($query); + if ($mysqli->error) { + trigger_error($mysqli->error, E_USER_ERROR); } } @@ -77,7 +90,6 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { * @param array $callback */ public function handleOnInit(array $callback) { - // Let manialinks update $this->updateManialink = true; } @@ -87,12 +99,10 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { * @param array $callback */ public function handle1Second(array $callback) { - // Send records manialinks if needed - if ($this->updateManialink) { - $manialink = $this->buildLocalManialink(); - $this->sendManialink($manialink); - $this->updateManialink = false; - } + if (!$this->updateManialink) return; + $this->updateManialink = false; + $manialink = $this->buildManialink(); + $this->sendManialink($manialink); } /** @@ -217,18 +227,21 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { * * @return string */ - private function buildLocalManialink() { + private function buildManialink() { $map = $this->maniaControl->mapManager->getCurrentMap(); if (!$map) { return null; } - $pos_x = $this->maniaControl->settingManager->getSetting($this, 'Widget_PosX', -139.); - $pos_y = $this->maniaControl->settingManager->getSetting($this, 'Widget_PosY', 65.); - $title = $this->maniaControl->settingManager->getSetting($this, 'Widget_Title', 'Local Records'); - $width = $this->maniaControl->settingManager->getSetting($this, 'Widget_Width', 40.); - $lines = $this->maniaControl->settingManager->getSetting($this, 'Widget_LinesCount', 25); - $line_height = $this->maniaControl->settingManager->getSetting($this, 'Widget_LineHeight', 4.); + $title = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_TITLE); + $pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSX); + $pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSY); + $width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_WIDTH); + $lines = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINESCOUNT); + $lineHeight = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINEHEIGHT); + $labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle(); + $quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle(); + $quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle(); $records = $this->getLocalRecords($map); if (!is_array($records)) { @@ -244,21 +257,21 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { $backgroundQuad = new Quad(); $frame->add($backgroundQuad); $backgroundQuad->setVAlign(Control::TOP); - $backgroundQuad->setSize($width * 1.05, 7. + $lines * $line_height); - $backgroundQuad->setStyles('Bgs1InRace', 'BgTitleShadow'); + $backgroundQuad->setSize($width * 1.05, 7. + $lines * $lineHeight); + $backgroundQuad->setStyles($quadStyle, $quadSubstyle); $titleLabel = new Label(); $frame->add($titleLabel); - // TODO: set translateable - $titleLabel->setPosition(0, $line_height * -0.9); + $titleLabel->setPosition(0, $lineHeight * -0.9); $titleLabel->setWidth($width); - $titleLabel->setStyle(Label_Text::STYLE_TextTitle1); + $titleLabel->setStyle($labelStyle); $titleLabel->setTextSize(2); $titleLabel->setText($title); + $titleLabel->setTranslate(true); // Times foreach ($records as $index => $record) { - $y = -8. - $index * $line_height; + $y = -8. - $index * $lineHeight; $recordFrame = new Frame(); $frame->add($recordFrame); @@ -266,14 +279,14 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { $backgroundQuad = new Quad(); $recordFrame->add($backgroundQuad); - $backgroundQuad->setSize($width, $line_height); - $backgroundQuad->setStyles('Bgs1InRace', 'BgTitleGlow'); + $backgroundQuad->setSize($width, $lineHeight); + $backgroundQuad->setStyles($quadStyle, $quadSubstyle); $rankLabel = new Label(); $recordFrame->add($rankLabel); $rankLabel->setHAlign(Control::LEFT); $rankLabel->setPosition($width * -0.47); - $rankLabel->setSize($width * 0.06, $line_height); + $rankLabel->setSize($width * 0.06, $lineHeight); $rankLabel->setTextSize(1); $rankLabel->setTextPrefix('$o'); $rankLabel->setText($record->rank); @@ -282,7 +295,7 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { $recordFrame->add($nameLabel); $nameLabel->setHAlign(Control::LEFT); $nameLabel->setPosition($width * -0.4); - $nameLabel->setSize($width * 0.6, $line_height); + $nameLabel->setSize($width * 0.6, $lineHeight); $nameLabel->setTextSize(1); $nameLabel->setText($record->nickname); @@ -290,7 +303,7 @@ class LocalRecordsPlugin extends Plugin implements CallbackListener { $recordFrame->add($timeLabel); $timeLabel->setHAlign(Control::RIGHT); $timeLabel->setPosition($width * 0.47); - $timeLabel->setSize($width * 0.25, $line_height); + $timeLabel->setSize($width * 0.25, $lineHeight); $timeLabel->setTextSize(1); $timeLabel->setText(Formatter::formatTime($record->time)); }