2013-11-12 19:33:25 +01:00
|
|
|
<?php
|
|
|
|
use ManiaControl\ManiaControl;
|
2013-11-13 01:43:12 +01:00
|
|
|
use ManiaControl\Admin\AuthenticationManager;
|
2013-11-12 19:33:25 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
|
|
|
use ManiaControl\Commands\CommandListener;
|
|
|
|
use ManiaControl\Players\Player;
|
2013-12-03 22:21:17 +01:00
|
|
|
use ManiaControl\Plugins\Plugin;
|
2013-11-12 19:33:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Donation plugin
|
|
|
|
*
|
|
|
|
* @author steeffeen
|
|
|
|
*/
|
2013-12-03 22:21:17 +01:00
|
|
|
class DonationPlugin implements CallbackListener, CommandListener, Plugin {
|
2013-11-12 19:33:25 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2013-12-09 10:04:22 +01:00
|
|
|
const ID = 3;
|
|
|
|
const VERSION = 0.1;
|
2013-11-12 19:33:25 +01:00
|
|
|
const SETTING_ANNOUNCE_SERVERDONATION = 'Enable Server-Donation Announcements';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $openBills = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct donation plugin
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
2013-12-03 18:50:30 +01:00
|
|
|
// Register for commands
|
2013-11-12 19:33:25 +01:00
|
|
|
$this->maniaControl->commandManager->registerCommandListener('donate', $this, 'command_Donate');
|
2013-11-13 01:43:12 +01:00
|
|
|
$this->maniaControl->commandManager->registerCommandListener('/pay', $this, 'command_Pay');
|
|
|
|
$this->maniaControl->commandManager->registerCommandListener('/getplanets', $this, 'command_GetPlanets');
|
2013-12-03 18:50:30 +01:00
|
|
|
|
|
|
|
// Register for callbacks
|
2013-11-12 19:33:25 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated');
|
|
|
|
}
|
|
|
|
|
2013-12-09 10:04:22 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getId()
|
|
|
|
*/
|
|
|
|
public static function getId() {
|
|
|
|
return self::ID;
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:21:17 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getName()
|
|
|
|
*/
|
|
|
|
public static function getName() {
|
2013-12-09 10:04:22 +01:00
|
|
|
return 'Donations Plugin';
|
2013-12-03 22:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getVersion()
|
|
|
|
*/
|
|
|
|
public static function getVersion() {
|
|
|
|
return self::VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getAuthor()
|
|
|
|
*/
|
|
|
|
public static function getAuthor() {
|
|
|
|
return 'steeffeen';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getDescription()
|
|
|
|
*/
|
|
|
|
public static function getDescription() {
|
2013-12-09 10:04:22 +01:00
|
|
|
return 'Plugin offering commands like /donate, /pay and /getplanets and a donation widget.';
|
2013-12-03 22:21:17 +01:00
|
|
|
}
|
|
|
|
|
2013-11-12 19:33:25 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-13 01:43:12 +01:00
|
|
|
* Handle //pay command
|
2013-11-12 19:33:25 +01:00
|
|
|
*
|
|
|
|
* @param array $chatCallback
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function command_Pay(array $chatCallback, Player $player) {
|
2013-11-13 01:43:12 +01:00
|
|
|
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
|
|
|
|
$this->maniaControl->authenticationManager->sendNotAllowed($player);
|
2013-11-12 19:33:25 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-13 01:43:12 +01:00
|
|
|
* Handle //getplanets command
|
2013-11-12 19:33:25 +01:00
|
|
|
*
|
|
|
|
* @param array $chat
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function command_GetPlanets(array $chatCallback, Player $player) {
|
2013-11-13 01:43:12 +01:00
|
|
|
if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
|
|
|
|
$this->maniaControl->authenticationManager->sendNotAllowed($player);
|
2013-11-12 19:33:25 +01:00
|
|
|
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) {
|
2013-11-13 01:43:12 +01:00
|
|
|
$message = "Usage Example: '/donate 100'";
|
|
|
|
return $this->maniaControl->chat->sendChat($message, $player->login);
|
2013-11-12 19:33:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an usage example for /pay to the player
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function sendPayUsageExample(Player $player) {
|
2013-11-13 01:43:12 +01:00
|
|
|
$message = "Usage Example: '/pay 100 login'";
|
|
|
|
return $this->maniaControl->chat->sendChat($message, $player->login);
|
2013-11-12 19:33:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|