From 6ce66e1640dcd05a2f5fa8be32f041f712b76c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Tue, 12 Nov 2013 19:33:25 +0100 Subject: [PATCH] - fixed join/leave messages - improved time formatter - bugfix setting manager - donation plugin --- application/core/Commands/CommandManager.php | 185 +------------- application/core/Formatter.php | 10 +- application/core/ManiaControl.php | 1 + application/core/Players/PlayerManager.php | 17 +- application/core/Plugins/Plugin.php | 9 + application/core/Plugins/PluginManager.php | 5 +- application/core/Settings/SettingManager.php | 3 + application/core/authentication.php | 3 +- application/plugins/DonationPlugin.php | 239 +++++++++++++++++++ 9 files changed, 277 insertions(+), 195 deletions(-) create mode 100644 application/plugins/DonationPlugin.php diff --git a/application/core/Commands/CommandManager.php b/application/core/Commands/CommandManager.php index 6884a2d5..a326c193 100644 --- a/application/core/Commands/CommandManager.php +++ b/application/core/Commands/CommandManager.php @@ -23,12 +23,11 @@ class CommandManager implements CallbackListener, CommandListener { */ private $maniaControl = null; private $commandListeners = array(); - private $openBills = array(); private $serverShutdownTime = -1; private $serverShutdownEmpty = false; /** - * Construct commands handler + * Construct commands manager * * @param \ManiaControl\ManiaControl $maniaControl */ @@ -36,12 +35,10 @@ class CommandManager implements CallbackListener, CommandListener { $this->maniaControl = $maniaControl; $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_5_SECOND, $this, 'each5Seconds'); - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback'); // Register basic commands - $commands = array('help', 'version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'getplanets', 'donate', - 'pay', 'kick'); + $commands = array('version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'kick'); foreach ($commands as $command) { $this->registerCommandListener($command, $this, 'command_' . $command); } @@ -90,56 +87,16 @@ class CommandManager implements CallbackListener, CommandListener { $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 handler registered + // No command listener registered return true; } - // Inform command handlers + // Inform command listeners foreach ($this->commandListeners[$command] as $listener) { call_user_func(array($listener[0], $listener[1]), $callback, $player); } return true; } - /** - * Handle bill updated callback - * - * @param array $callback - * @return bool - */ - public function handleBillUpdated(array $callback) { - $bill = $callback[1]; - if (!array_key_exists($bill[0], $this->openBills)) { - return false; - } - $login = $this->openBills[$bill[0]]; - switch ($bill[1]) { - case 4: - { - // Payed - $message = 'Success! Thanks.'; - $this->maniaControl->chat->sendSuccess($message, $login); - unset($this->openBills[$bill[0]]); - break; - } - case 5: - { - // Refused - $message = 'Transaction cancelled.'; - $this->maniaControl->chat->sendError($message, $login); - unset($this->openBills[$bill[0]]); - break; - } - case 6: - { - // Error - $this->maniaControl->chat->sendError($bill[2], $login); - unset($this->openBills[$bill[0]]); - break; - } - } - return true; - } - /** * Send ManiaControl version * @@ -152,138 +109,6 @@ class CommandManager implements CallbackListener, CommandListener { return $this->maniaControl->chat->sendInformation($message, $login); } - /** - * Send help list - * - * @param array $chat - * @return bool - */ - private function command_help(array $chat) { - $login = $chat[1][1]; - // TODO: improve help command - // TODO: enable help for specific commands - $list = 'Available commands: '; - $commands = array_keys($this->commandHandlers); - $count = count($commands); - for ($index = 0; $index < $count; $index++) { - if (!$this->maniaControl->authentication->checkRight($login, $this->getRightsLevel($commands[$index], 'superadmin'))) { - unset($commands[$index]); - } - } - $count = count($commands); - $index = 0; - foreach ($commands as $command) { - $list .= $command; - if ($index < $count - 1) { - $list .= ', '; - } - $index++; - } - return $this->maniaControl->chat->sendInformation($list, $login); - } - - /** - * Handle getplanets command - * - * @param array $chat - * @param Player $player - * @return bool - */ - private function command_getplanets(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_ADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); - return false; - } - if (!$this->maniaControl->client->query('GetServerPlanets')) { - trigger_error("Couldn't retrieve server planets. " . $this->maniaControl->getClientErrorText()); - return false; - } - $planets = $this->maniaControl->client->getResponse(); - $message = "This Server has {$planets} Planets!"; - return $this->maniaControl->chat->sendInformation($message, $player->login); - } - - /** - * Handle donate command - * - * @param array $chat - * @param Player $player - * @return bool - */ - private function command_donate(array $chat, Player $player) { - $params = explode(' ', $chat[1][2]); - if (count($params) < 2) { - // TODO: send usage information - return false; - } - $amount = (int) $params[1]; - if (!$amount || $amount <= 0) { - // TODO: send usage information - return false; - } - if (count($params) >= 3) { - $receiver = $params[2]; - $receiverPlayer = $this->maniaControl->playerHandler->getPlayer($receiver); - $receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver); - } - else { - $receiver = ''; - $receiverName = $this->maniaControl->server->getName(); - } - $message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?'; - if (!$this->maniaControl->client->query('SendBill', $pl, $amount, $message, $receiver)) { - trigger_error( - "Couldn't create donation of {$amount} planets from '{$player->login}' for '{$receiver}'. " . - $this->maniaControl->getClientErrorText()); - $this->maniaControl->chat->sendError("Creating donation failed.", $player->login); - return false; - } - $bill = $this->maniaControl->client->getResponse(); - $this->openBills[$bill] = $player->login; - return true; - } - - /** - * Handle pay command - * - * @param array $chat - * @param Player $player - * @return bool - */ - private function command_pay(array $chat, Player $player) { - if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) { - $this->maniaControl->authentication->sendNotAllowed($player); - return false; - } - $params = explode(' ', $chat[1][2]); - if (count($params) < 2) { - // TODO: send usage information - return false; - } - $amount = (int) $params[1]; - if (!$amount || $amount <= 0) { - // TODO: send usage information - return false; - } - if (count($params) >= 3) { - $receiver = $params[2]; - } - else { - $receiver = $player->login; - } - $message = 'Payout from $<' . $this->maniaControl->server->getName() . '$>.'; - if (!$this->maniaControl->client->query('Pay', $receiver, $amount, $message)) { - trigger_error( - "Couldn't create payout of {$amount} planets by '{$player->login}' for '{$receiver}'. " . - $this->maniaControl->getClientErrorText()); - $this->maniaControl->chat->sendError("Creating payout failed.", $player->login); - return false; - } - $bill = $this->maniaControl->client->getResponse(); - $this->openBills[$bill] = $player->login; - return true; - } - /** * Handle systeminfo command * @@ -375,7 +200,7 @@ class CommandManager implements CallbackListener, CommandListener { return false; } $target = $params[1]; - $target = $this->maniaControl->playerHandler->getPlayer($target); + $target = $this->maniaControl->playerManager->getPlayer($target); if (!$target) { $this->maniaControl->chat->sendError("Invalid player login.", $player->login); return false; diff --git a/application/core/Formatter.php b/application/core/Formatter.php index f24c4afb..0428f472 100644 --- a/application/core/Formatter.php +++ b/application/core/Formatter.php @@ -35,21 +35,21 @@ abstract class Formatter { /** * Formats the given time (seconds) to hh:mm:ss * - * @param int $time + * @param int $seconds * @return string */ - public static function formatTimeH($time) { + public static function formatTimeH($seconds) { return gmdate("H:i:s", $seconds); } /** * Convert the given time (seconds) to mysql timestamp * - * @param int $time + * @param int $seconds * @return string */ - public static function formatTimestamp($time) { - return date("Y-m-d H:i:s", $time); + public static function formatTimestamp($seconds) { + return date("Y-m-d H:i:s", $seconds); } /** diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index d306bf6a..524938fb 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -15,6 +15,7 @@ require_once __DIR__ . '/Chat.php'; require_once __DIR__ . '/Commands/CommandManager.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__ . '/Manialinks/ManialinkUtil.php'; require_once __DIR__ . '/Maps/Map.php'; diff --git a/application/core/Players/PlayerManager.php b/application/core/Players/PlayerManager.php index ee9ba7c7..a94677ff 100644 --- a/application/core/Players/PlayerManager.php +++ b/application/core/Players/PlayerManager.php @@ -4,6 +4,7 @@ namespace ManiaControl\Players; require_once __DIR__ . '/Player.php'; +use ManiaControl\Formatter; use ManiaControl\ManiaControl; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; @@ -18,7 +19,7 @@ class PlayerManager implements CallbackListener { * Constants */ const TABLE_PLAYERS = 'mc_players'; - const SETTING_LEAVE_JOIN_MESSAGES = 'EnableLeaveJoinMessages'; + const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages'; /** * Private properties @@ -27,7 +28,7 @@ class PlayerManager implements CallbackListener { private $playerList = array(); /** - * Construct player handler + * Construct player manager * * @param \ManiaControl\ManiaControl $maniaControl */ @@ -82,8 +83,9 @@ class PlayerManager implements CallbackListener { */ public function onInit(array $callback) { // register settings - $this->maniaControl->settingManager->initSetting($this, "Leave Join Messages", true); + $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(); foreach ($playerList as $playerItem) { @@ -109,7 +111,7 @@ class PlayerManager implements CallbackListener { $player = new Player($playerInfo); $this->addPlayer($player); - if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_LEAVE_JOIN_MESSAGES)) { + if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) { return; } $string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); @@ -131,10 +133,11 @@ class PlayerManager implements CallbackListener { $login = $callback[1][0]; $player = $this->removePlayer($login); - if ($this->maniaControl->settingManager->getSetting($this, "Leave Join Messages")) { - $played = TimeFormatter::formatTime(time() - $player->joinTime); - $this->maniaControl->chat->sendChat($player->nickname . '$z $ff0has left the game. Played:$fff ' . $played); + if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) { + return; } + $played = Formatter::formatTimeH(time() - $player->joinTime); + $this->maniaControl->chat->sendChat('$<'.$player->nickname . '$> $ff0has left the game. Played:$fff ' . $played); } /** diff --git a/application/core/Plugins/Plugin.php b/application/core/Plugins/Plugin.php index 814a41b3..52d3a006 100644 --- a/application/core/Plugins/Plugin.php +++ b/application/core/Plugins/Plugin.php @@ -27,6 +27,15 @@ abstract class Plugin { */ public abstract function __construct(ManiaControl $maniaControl); + /** + * Get class name as string + * + * @return string + */ + public static function getClass() { + return __CLASS__; + } + /** * Get plugin author * diff --git a/application/core/Plugins/PluginManager.php b/application/core/Plugins/PluginManager.php index b7447e03..2b1c17f8 100644 --- a/application/core/Plugins/PluginManager.php +++ b/application/core/Plugins/PluginManager.php @@ -25,7 +25,7 @@ class PluginManager { private $pluginClasses = array(); /** - * Construct plugin handler + * Construct plugin manager * * @param \ManiaControl\ManiaControl $maniaControl */ @@ -121,6 +121,7 @@ class PluginManager { if ($pluginStatement->num_rows <= 0) { $pluginStatement->free_result(); $pluginStatement->close(); + $this->savePluginStatus($className, false); return false; } $pluginStatement->bind_result($activeInt); @@ -149,7 +150,7 @@ class PluginManager { $classesAfter = get_declared_classes(); $newClasses = array_diff($classesAfter, $classesBefore); foreach ($newClasses as $className) { - if (!is_subclass_of($className, 'ManiaControl\Plugin')) { + if (!is_subclass_of($className, Plugin::getClass())) { continue; } array_push($this->pluginClasses, $className); diff --git a/application/core/Settings/SettingManager.php b/application/core/Settings/SettingManager.php index 82d81912..98b86ad2 100644 --- a/application/core/Settings/SettingManager.php +++ b/application/core/Settings/SettingManager.php @@ -164,6 +164,9 @@ class SettingManager { if ($type === self::TYPE_ARRAY) { return implode($this->arrayDelimiter, $value); } + if ($type === self::TYPE_BOOL) { + return ($value ? 1 : 0); + } return $value; } diff --git a/application/core/authentication.php b/application/core/authentication.php index 66601973..ae51bdaa 100644 --- a/application/core/authentication.php +++ b/application/core/authentication.php @@ -3,6 +3,7 @@ namespace ManiaControl; use ManiaControl\Players\Player; +use ManiaControl\Players\PlayerManager; /** * Class handling authentication levels @@ -101,7 +102,7 @@ class Authentication { return false; } $mysqli = $this->maniaControl->database->mysqli; - $authQuery = "INSERT INTO `" . PlayerHandler::TABLE_PLAYERS . "` ( + $authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` ( `login`, `authLevel` ) VALUES ( diff --git a/application/plugins/DonationPlugin.php b/application/plugins/DonationPlugin.php new file mode 100644 index 00000000..c73daefb --- /dev/null +++ b/application/plugins/DonationPlugin.php @@ -0,0 +1,239 @@ +maniaControl = $maniaControl; + + $this->author = 'steeffeen'; + $this->name = 'Donation Plugin'; + $this->version = self::VERSION; + $this->description = 'DonationPlugin commands like /donate, /pay and /getplanets and a donation widget.'; + + $this->maniaControl->commandManager->registerCommandListener('donate', $this, 'command_Donate'); + $this->maniaControl->commandManager->registerCommandListener('pay', $this, 'command_Pay'); + $this->maniaControl->commandManager->registerCommandListener('getplanets', $this, 'command_GetPlanets'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); + } + + /** + * Handle /donate command + * + * @param array $chatCallback + * @param Player $player + * @return bool + */ + public function command_Donate(array $chatCallback, Player $player) { + $text = $chatCallback[1][2]; + $params = explode(' ', $text); + if (count($params) < 2) { + $this->sendDonateUsageExample($player); + return false; + } + $amount = (int) $params[1]; + if (!$amount || $amount <= 0) { + $this->sendDonateUsageExample($player); + return false; + } + if (count($params) >= 3) { + $receiver = $params[2]; + $receiverPlayer = $this->maniaControl->playerManager->getPlayer($receiver); + $receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver); + } + else { + $receiver = ''; + $receiverName = $this->maniaControl->server->getName(); + } + $message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?'; + if (!$this->maniaControl->client->query('SendBill', $player->login, $amount, $message, $receiver)) { + trigger_error( + "Couldn't create donation of {$amount} planets from '{$player->login}' for '{$receiver}'. " . + $this->maniaControl->getClientErrorText()); + $this->maniaControl->chat->sendError("Creating donation failed.", $player->login); + return false; + } + $bill = $this->maniaControl->client->getResponse(); + $this->openBills[$bill] = array(true, $player->login, $receiver, $amount, time()); + return true; + } + + /** + * Handle /pay command + * + * @param array $chatCallback + * @param Player $player + * @return bool + */ + public function command_Pay(array $chatCallback, Player $player) { + if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) { + $this->maniaControl->authentication->sendNotAllowed($player); + return false; + } + $text = $chatCallback[1][2]; + $params = explode(' ', $text); + if (count($params) < 2) { + $this->sendPayUsageExample($player); + return false; + } + $amount = (int) $params[1]; + if (!$amount || $amount <= 0) { + $this->sendPayUsageExample($player); + return false; + } + if (count($params) >= 3) { + $receiver = $params[2]; + } + else { + $receiver = $player->login; + } + $message = 'Payout from $<' . $this->maniaControl->server->getName() . '$>.'; + if (!$this->maniaControl->client->query('Pay', $receiver, $amount, $message)) { + trigger_error( + "Couldn't create payout of {$amount} planets by '{$player->login}' for '{$receiver}'. " . + $this->maniaControl->getClientErrorText()); + $this->maniaControl->chat->sendError("Creating payout failed.", $player->login); + return false; + } + $bill = $this->maniaControl->client->getResponse(); + $this->openBills[$bill] = array(false, $player->login, $receiver, $amount, time()); + return true; + } + + /** + * Handle /getplanets command + * + * @param array $chat + * @param Player $player + * @return bool + */ + public function command_GetPlanets(array $chatCallback, Player $player) { + if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_ADMIN)) { + $this->maniaControl->authentication->sendNotAllowed($player); + return false; + } + if (!$this->maniaControl->client->query('GetServerPlanets')) { + trigger_error("Couldn't retrieve server planets. " . $this->maniaControl->getClientErrorText()); + return false; + } + $planets = $this->maniaControl->client->getResponse(); + $message = "This Server has {$planets} Planets!"; + return $this->maniaControl->chat->sendInformation($message, $player->login); + } + + /** + * Handle bill updated callback + * + * @param array $callback + * @return bool + */ + public function handleBillUpdated(array $callback) { + $billId = $callback[1][0]; + if (!array_key_exists($billId, $this->openBills)) { + return false; + } + $billData = $this->openBills[$billId]; + $login = $billData[1]; + $receiver = $billData[2]; + switch ($callback[1][1]) { + case 4: + { + // Payed + $donation = $billData[0]; + $amount = $billData[3]; + if ($donation) { + // Donation + if (strlen($receiver) > 0) { + // To player + $message = "Successfully donated {$amount} to '{$receiver}'!"; + $this->maniaControl->chat->sendSuccess($message, $login); + } + else { + // To server + if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_ANNOUNCE_SERVERDONATION, true)) { + $player = $this->maniaControl->playerManager->getPlayer($login); + $message = '$<' . ($player ? $player->nickname : $login) . '$> donated ' . $amount . ' Planets! Thanks.'; + } + else { + $message = 'Donation successful! Thanks.'; + } + $this->maniaControl->chat->sendSuccess($message, $login); + } + } + else { + // Payout + $message = "Successfully payed out {$amount} to '{$receiver}'!"; + $this->maniaControl->chat->sendSuccess($message, $login); + } + unset($this->openBills[$billId]); + break; + } + case 5: + { + // Refused + $message = 'Transaction cancelled.'; + $this->maniaControl->chat->sendError($message, $login); + unset($this->openBills[$billId]); + break; + } + case 6: + { + // Error + $this->maniaControl->chat->sendError($callback[1][2], $login); + unset($this->openBills[$billId]); + break; + } + } + return true; + } + + /** + * Send an usage example for /donate to the player + * + * @param Player $player + * @return boolean + */ + private function sendDonateUsageExample(Player $player) { + $colorCode = '$f80'; + return $this->maniaControl->chat->sendChat("{$colorCode}Usage Example: '/donate 100'", $player->login); + } + + /** + * Send an usage example for /pay to the player + * + * @param Player $player + * @return boolean + */ + private function sendPayUsageExample(Player $player) { + $colorCode = '$f80'; + return $this->maniaControl->chat->sendChat("{$colorCode}Usage Example: '/pay 100 login'", $player->login); + } +} + +?>