From 7005469973009a223311ac784479fbf82dc69ffd Mon Sep 17 00:00:00 2001 From: kremsy Date: Tue, 25 Feb 2014 18:34:35 +0100 Subject: [PATCH] bill manager --- application/core/Bills/BillData.php | 27 ++++ application/core/Bills/BillManager.php | 141 +++++++++++++++++ application/core/ManiaControl.php | 4 + application/plugins/Dedimania/Dedimania.php | 2 +- .../plugins/Dedimania/DedimaniaData.php | 8 +- .../plugins/Dedimania/DedimaniaPlayer.php | 8 +- application/plugins/Dedimania/RecordData.php | 12 +- application/plugins/Donations.php | 144 ++++++------------ 8 files changed, 235 insertions(+), 111 deletions(-) create mode 100644 application/core/Bills/BillData.php create mode 100644 application/core/Bills/BillManager.php diff --git a/application/core/Bills/BillData.php b/application/core/Bills/BillData.php new file mode 100644 index 00000000..298a97bb --- /dev/null +++ b/application/core/Bills/BillData.php @@ -0,0 +1,27 @@ +function = $function; + $this->player = $player; + $this->amount = $amount; + $this->pay = $pay; + $this->receiverLogin = $receiverLogin; + $this->creationTime = time(); + } + +} \ No newline at end of file diff --git a/application/core/Bills/BillManager.php b/application/core/Bills/BillManager.php new file mode 100644 index 00000000..f57d35de --- /dev/null +++ b/application/core/Bills/BillManager.php @@ -0,0 +1,141 @@ +maniaControl = $maniaControl; + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); + } + + /** + * send a Bill to a Player + * + * @param $function + * @param Player $player + * @param $amount + * @param $message + * @param bool $receiver + * @return bool + */ + public function sendBill($function, Player $player, $amount, $message, $receiver = false) { + if (!is_callable($function)) { + trigger_error("Function is not callable"); + return false; + } + + try { + if (!$receiver) { + $bill = $this->maniaControl->client->sendBill($player->login, $amount, $message); + } else { + $bill = $this->maniaControl->client->sendBill($player->login, $amount, $message, $receiver); + } + } catch(Exception $e) { + // TODO: handle errors like 'too few server planets' - throw other like connection errors + $this->maniaControl->errorHandler->triggerDebugNotice("Couldn't create donation of {$amount} planets from '{$player->login}' for '{$receiver}'. " . $e->getMessage()); + return false; + } + + $this->openBills[$bill] = new BillData($function, $player, $amount); + return true; + } + + /** + * Send Planets from the server to a Player + * + * @param $function + * @param $receiverLogin + * @param $amount + * @param $message + * @return bool + */ + public function sendPlanets($function, $receiverLogin, $amount, $message) { + if (!is_callable($function)) { + trigger_error("Function is not callable"); + return false; + } + + try { + $bill = $this->maniaControl->client->pay($receiverLogin, $amount, $message); + } catch(Exception $e) { + // TODO: handle errors like 'too few server planets' - throw other like connection errors + //trigger_error("Couldn't create payout of {$amount} planets by '{$player->login}' for '{$receiver}'. " . $e->getMessage()); + $this->maniaControl->errorHandler->triggerDebugNotice("Couldn't create payout of {$amount} planets to '{$receiverLogin}' " . $e->getMessage()); + return false; + } + + $this->openBills[$bill] = new BillData($function, $receiverLogin, $amount, true); + + return true; + } + + /** + * 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; + } + $billData = $this->openBills[$billId]; + + /** @var BillData $billData */ + switch($callback[1][1]) { + case Bill::STATE_PAYED: + if ($billData->pay) { + call_user_func($billData->function, $billData, self::PAYED_FROM_SERVER); + } else { + if ($billData->receiverLogin) { + call_user_func($billData->function, $billData, self::DONATED_TO_RECEIVER); + } else { + call_user_func($billData->function, $billData, self::DONATED_TO_SERVER); + } + } + unset($this->openBills[$billId]); + break; + case Bill::STATE_REFUSED: + call_user_func($billData->function, $billData, self::PLAYER_REFUSED_DONATION); + unset($this->openBills[$billId]); + break; + case Bill::STATE_ERROR: + call_user_func($billData->function, $callback[1][2], self::ERROR_WHILE_TRANSACTION); + unset($this->openBills[$billId]); + break; + } + } +} \ No newline at end of file diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index fa86464b..ba9e0dd1 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -4,6 +4,7 @@ namespace ManiaControl; use ManiaControl\Admin\ActionsMenu; use ManiaControl\Admin\AuthenticationManager; +use ManiaControl\Bills\BillManager; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerManager; @@ -77,6 +78,8 @@ class ManiaControl implements CommandListener, TimerListener { public $errorHandler = null; public $timerManager = null; public $fileReader = null; + public $billManager = null; + /** * Private properties */ @@ -99,6 +102,7 @@ class ManiaControl implements CommandListener, TimerListener { $this->callbackManager = new CallbackManager($this); $this->timerManager = new TimerManager($this); $this->fileReader = new AsynchronousFileReader($this); + $this->billManager = new BillManager($this); $this->settingManager = new SettingManager($this); $this->statisticManager = new StatisticManager($this); $this->manialinkManager = new ManialinkManager($this); diff --git a/application/plugins/Dedimania/Dedimania.php b/application/plugins/Dedimania/Dedimania.php index fdfea53a..e27c34fa 100644 --- a/application/plugins/Dedimania/Dedimania.php +++ b/application/plugins/Dedimania/Dedimania.php @@ -301,7 +301,7 @@ class Dedimania implements CallbackListener, TimerListener, Plugin { if ($error != '') { $this->maniaControl->log("Dedimania Error: " . $error); } - var_dump($data); + //var_dump($data); $data = $this->decode($data); //var_dump($data); if (is_array($data)) { diff --git a/application/plugins/Dedimania/DedimaniaData.php b/application/plugins/Dedimania/DedimaniaData.php index 2bf91e12..7d39b2c3 100644 --- a/application/plugins/Dedimania/DedimaniaData.php +++ b/application/plugins/Dedimania/DedimaniaData.php @@ -1,11 +1,9 @@ nullRecord = false; + $this->nullRecord = false; $this->login = $login; $this->nickName = $nickName; $this->best = $best; diff --git a/application/plugins/Donations.php b/application/plugins/Donations.php index b77c64be..11e495ce 100644 --- a/application/plugins/Donations.php +++ b/application/plugins/Donations.php @@ -9,6 +9,7 @@ use FML\Controls\Quads\Quad_Icons128x128_1; use FML\ManiaLink; use FML\Script\Script; use ManiaControl\Admin\AuthenticationManager; +use ManiaControl\Bills\BillManager; use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Commands\CommandListener; @@ -16,7 +17,6 @@ use ManiaControl\ManiaControl; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; use ManiaControl\Plugins\Plugin; -use Maniaplanet\DedicatedServer\Xmlrpc\Exception; /** * Donation plugin @@ -50,7 +50,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { * @var maniaControl $maniaControl */ private $maniaControl = null; - private $openBills = array(); /** * Prepares the Plugin @@ -75,7 +74,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { $this->maniaControl->commandManager->registerCommandListener('planets', $this, 'command_GetPlanets', true); // Register for callbacks - $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); @@ -312,7 +310,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { if (count($params) >= 3) { $receiver = $params[2]; $receiverPlayer = $this->maniaControl->playerManager->getPlayer($receiver); - $receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver); + $receiverName = ($receiverPlayer ? $receiverPlayer->nickname : $receiver); } else { $receiver = ''; $receiverName = $this->maniaControl->client->getServerName(); @@ -328,21 +326,41 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { * @param $value */ private function handleDonation(Player $player, $amount, $receiver = '', $receiverName = false) { + if (!$receiverName) { - $receiverName = $this->maniaControl->client->getServerName(); + $serverName = $this->maniaControl->client->getServerName(); + $message = 'Donate ' . $amount . ' Planets to $<' . $serverName . '$>?'; + } else { + $message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?'; } - $message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?'; - try { - $bill = $this->maniaControl->client->sendBill($player->login, $amount, $message, $receiver); - } catch(Exception $e) { - // TODO: handle errors like 'too few server planets' - throw other like connection errors - trigger_error("Couldn't create donation of {$amount} planets from '{$player->login}' for '{$receiver}'. " . $e->getMessage()); - $this->maniaControl->chat->sendError("Creating donation failed.", $player->login); - return false; - } - - $this->openBills[$bill] = array(true, $player->login, $receiver, $amount, time()); + //Send and Handle the Bill + $this->maniaControl->billManager->sendBill(function ($data, $status) use (&$player, $amount, $receiver) { + switch($status) { + case BillManager::DONATED_TO_SERVER: + if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_ANNOUNCE_SERVERDONATION, true) && $amount >= $this->maniaControl->settingManager->getSetting($this, self::SETTING_MIN_AMOUNT_SHOWN, true)) { + $login = null; + $message = '$<' . $player->nickname . '$> donated ' . $amount . ' Planets! Thanks.'; + } else { + $message = 'Donation successful! Thanks.'; + } + $this->maniaControl->chat->sendSuccess($message, $player->login); + $this->maniaControl->statisticManager->insertStat(self::STAT_PLAYER_DONATIONS, $player, $this->maniaControl->server->index, $amount); + break; + case BillManager::DONATED_TO_RECEIVER: + $message = "Successfully donated {$amount} to '{$receiver}'!"; + $this->maniaControl->chat->sendSuccess($message, $player->login); + break; + case BillManager::PLAYER_REFUSED_DONATION: + $message = 'Transaction cancelled.'; + $this->maniaControl->chat->sendError($message, $player->login); + break; + case BillManager::ERROR_WHILE_TRANSACTION: + $message = $data; + $this->maniaControl->chat->sendError($message, $player->login); + break; + } + }, $player, $amount, $message); return true; } @@ -377,16 +395,23 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { } $message = 'Payout from $<' . $this->maniaControl->client->getServerName() . '$>.'; - try { - $bill = $this->maniaControl->client->pay($receiver, $amount, $message); - } catch(Exception $e) { - // TODO: handle errors like 'too few server planets' - throw other like connection errors - trigger_error("Couldn't create payout of {$amount} planets by '{$player->login}' for '{$receiver}'. " . $e->getMessage()); - $this->maniaControl->chat->sendError("Creating payout failed.", $player->login); - return false; - } + $this->maniaControl->billManager->sendPlanets(function ($data, $status) use (&$player, $amount, $receiver) { + switch($status) { + case BillManager::PAYED_FROM_SERVER: + $message = "Successfully payed out {$amount} to '{$receiver}'!"; + $this->maniaControl->chat->sendSuccess($message, $player->login); + break; + case BillManager::PLAYER_REFUSED_DONATION: + $message = 'Transaction cancelled.'; + $this->maniaControl->chat->sendError($message, $player->login); + break; + case BillManager::ERROR_WHILE_TRANSACTION: + $message = $data; + $this->maniaControl->chat->sendError($message, $player->login); + break; + } + }, $receiver, $amount, $message); - $this->openBills[$bill] = array(false, $player->login, $receiver, $amount, time()); return true; } @@ -407,75 +432,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { 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) { - $player = $this->maniaControl->playerManager->getPlayer($login); - - // 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) && $amount >= $this->maniaControl->settingManager->getSetting($this, self::SETTING_MIN_AMOUNT_SHOWN, true) - ) { - $login = null; - $message = '$<' . $player->nickname . '$> donated ' . $amount . ' Planets! Thanks.'; - } else { - $message = 'Donation successful! Thanks.'; - } - - - $this->maniaControl->chat->sendSuccess($message, $login); - $this->maniaControl->statisticManager->insertStat(self::STAT_PLAYER_DONATIONS, $player, $this->maniaControl->server->index, $amount); - } - } 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 *