TrackManiaControl/application/plugins/Donations.php

511 lines
16 KiB
PHP
Raw Normal View History

<?php
2014-01-04 20:20:55 +01:00
use FML\Controls\Control;
2014-01-02 18:06:27 +01:00
use FML\Controls\Frame;
2014-01-04 21:54:40 +01:00
use FML\Controls\Label;
2014-01-04 20:20:55 +01:00
use FML\Controls\Labels\Label_Button;
2014-01-04 22:58:57 +01:00
use FML\Controls\Labels\Label_Text;
2014-01-02 18:06:27 +01:00
use FML\Controls\Quad;
use FML\Controls\Quads\Quad_Icons128x128_1;
use FML\ManiaLink;
use FML\Script\Script;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Commands\CommandListener;
2014-01-02 17:31:58 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2014-01-02 18:06:54 +01:00
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
2014-01-20 20:51:03 +01:00
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
/**
* Donation plugin
*
2014-01-05 14:14:08 +01:00
* @author steeffeen & kremsy
*/
class DonationPlugin implements CallbackListener, CommandListener, Plugin {
2014-01-03 20:57:24 +01:00
/**
* Constants
*/
2014-01-08 20:20:18 +01:00
const ID = 3;
const VERSION = 0.1;
const SETTING_ANNOUNCE_SERVERDONATION = 'Enable Server-Donation Announcements';
2014-01-08 20:20:18 +01:00
const STAT_PLAYER_DONATIONS = 'Donated Planets';
const ACTION_DONATE_VALUE = 'Donate.DonateValue';
2014-01-02 18:06:27 +01:00
// DonateWidget Properties
2014-01-08 20:20:18 +01:00
const MLID_DONATE_WIDGET = 'DonationPlugin.DonateWidget';
2014-01-02 18:06:27 +01:00
const SETTING_DONATE_WIDGET_ACTIVATED = 'Donate-Widget Activated';
2014-01-08 20:20:18 +01:00
const SETTING_DONATE_WIDGET_POSX = 'Donate-Widget-Position: X';
const SETTING_DONATE_WIDGET_POSY = 'Donate-Widget-Position: Y';
const SETTING_DONATE_WIDGET_WIDTH = 'Donate-Widget-Size: Width';
const SETTING_DONATE_WIDGET_HEIGHT = 'Donate-Widget-Size: Height';
const SETTING_DONATION_VALUES = 'Donation Values';
const SETTING_MIN_AMOUNT_SHOWN = 'Minimum Donation amount to get shown';
2014-01-27 20:39:10 +01:00
/**
* Prepares the Plugin
*
* @param ManiaControl $maniaControl
* @return mixed
*/
public static function prepare(ManiaControl $maniaControl) {
// TODO: Implement prepare() method.
}
/**
* Private properties
*/
2014-01-05 14:14:08 +01:00
/**
*
* @var maniaControl $maniaControl
*/
2013-12-14 23:29:17 +01:00
private $maniaControl = null;
private $openBills = array();
/**
*
2013-12-14 23:29:17 +01:00
* @see \ManiaControl\Plugins\Plugin::load()
*/
2013-12-14 23:29:17 +01:00
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-08 20:20:18 +01:00
// Register for commands
$this->maniaControl->commandManager->registerCommandListener('donate', $this, 'command_Donate');
2013-12-14 23:29:17 +01:00
$this->maniaControl->commandManager->registerCommandListener('pay', $this, 'command_Pay', true);
$this->maniaControl->commandManager->registerCommandListener('getplanets', $this, 'command_GetPlanets', true);
2014-01-08 20:20:18 +01:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated');
2014-01-02 18:06:27 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerConnect');
2014-01-08 20:20:18 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
2014-01-02 17:31:58 +01:00
// Define player stats
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYER_DONATIONS);
2014-01-08 20:20:18 +01:00
2014-01-02 18:06:27 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED, true);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_DONATE_WIDGET_POSX, 156.);
2014-01-14 19:11:57 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_DONATE_WIDGET_POSY, -31.4);
2014-01-02 18:06:27 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_DONATE_WIDGET_WIDTH, 6);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_DONATE_WIDGET_HEIGHT, 6);
2014-01-04 20:20:55 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_DONATION_VALUES, "20,50,100,500,1000,2000");
2014-01-08 20:20:18 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MIN_AMOUNT_SHOWN, 100);
2014-01-18 10:26:32 +01:00
$this->displayWidget();
2013-12-14 23:29:17 +01:00
return true;
}
/**
*
* @see \ManiaControl\Plugins\Plugin::unload()
*/
public function unload() {
2014-01-17 16:16:12 +01:00
$emptyManialink = new ManiaLink(self::MLID_DONATE_WIDGET);
$manialinkText = $emptyManialink->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($manialinkText);
2013-12-14 23:29:17 +01:00
$this->maniaControl->callbackManager->unregisterCallbackListener($this);
$this->maniaControl->commandManager->unregisterCommandListener($this);
unset($this->maniaControl);
}
2013-12-09 10:04:22 +01:00
/**
*
* @see \ManiaControl\Plugins\Plugin::getId()
*/
public static function getId() {
return self::ID;
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getName()
*/
public static function getName() {
2013-12-09 10:04:22 +01:00
return 'Donations Plugin';
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getVersion()
*/
public static function getVersion() {
return self::VERSION;
}
/**
*
* @see \ManiaControl\Plugins\Plugin::getAuthor()
*/
public static function getAuthor() {
2014-01-04 20:20:55 +01:00
return 'steeffeen and kremsy';
}
/**
*
* @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.';
}
2014-01-02 18:06:27 +01:00
/**
2014-01-18 10:26:32 +01:00
* Handle ManiaControl OnStartup
2014-01-02 18:06:27 +01:00
*
* @param array $callback
*/
2014-01-18 10:26:32 +01:00
public function displayWidget() {
2014-01-08 20:20:18 +01:00
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) {
2014-01-02 18:06:27 +01:00
$this->displayDonateWidget();
}
}
2014-01-04 20:20:55 +01:00
/**
* Handle ManialinkPageAnswer Callback
*
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
2014-01-08 20:20:18 +01:00
$actionId = $callback[1][2];
2014-01-04 20:20:55 +01:00
$boolSetting = (strpos($actionId, self::ACTION_DONATE_VALUE) === 0);
2014-01-08 20:20:18 +01:00
if(!$boolSetting) {
return;
}
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
2014-01-04 20:20:55 +01:00
$actionArray = explode(".", $callback[1][2]);
$this->handleDonation($player, intval($actionArray[2]));
}
2014-01-02 18:06:27 +01:00
/**
* Handle PlayerConnect callback
*
* @param array $callback
*/
public function handlePlayerConnect(array $callback) {
$player = $callback[1];
// Display Map Widget
2014-01-08 20:20:18 +01:00
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) {
2014-01-02 18:06:27 +01:00
$this->displayDonateWidget($player->login);
}
}
/**
* Displays the Donate Widget
*
* @param bool $login
*/
public function displayDonateWidget($login = false) {
2014-01-08 20:20:18 +01:00
$posX = $this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_POSX);
$posY = $this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_POSY);
$width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_WIDTH);
$height = $this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_HEIGHT);
$values = $this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATION_VALUES);
2014-01-15 22:07:09 +01:00
$shootManiaOffset = $this->maniaControl->manialinkManager->styleManager->getDefaultIconOffsetSM();
2014-01-08 20:20:18 +01:00
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle();
2014-01-02 18:06:27 +01:00
$itemMarginFactorX = 1.3;
$itemMarginFactorY = 1.2;
2014-01-08 20:20:18 +01:00
2014-01-15 22:07:09 +01:00
// Get Title Id
$titleId = $this->maniaControl->server->titleId;
$titlePrefix = strtoupper(substr($titleId, 0, 2));
//If game is shootmania lower the icons position by 20
if($titlePrefix == 'SM') {
$posY -= $shootManiaOffset;
}
2014-01-02 18:06:27 +01:00
$itemSize = $width;
2014-01-08 20:20:18 +01:00
2014-01-02 18:06:27 +01:00
$maniaLink = new ManiaLink(self::MLID_DONATE_WIDGET);
2014-01-08 20:20:18 +01:00
$script = $maniaLink->getScript();
2014-01-05 14:14:08 +01:00
// Donate Menu Icon Frame
2014-01-02 18:06:27 +01:00
$frame = new Frame();
$maniaLink->add($frame);
$frame->setPosition($posX, $posY);
2014-01-08 20:20:18 +01:00
2014-01-02 18:06:27 +01:00
$backgroundQuad = new Quad();
$frame->add($backgroundQuad);
$backgroundQuad->setSize($width * $itemMarginFactorX, $height * $itemMarginFactorY);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
2014-01-08 20:20:18 +01:00
2014-01-02 18:06:27 +01:00
$iconFrame = new Frame();
$frame->add($iconFrame);
2014-01-08 20:20:18 +01:00
2014-01-02 18:06:27 +01:00
$iconFrame->setSize($itemSize, $itemSize);
$itemQuad = new Quad_Icons128x128_1();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Coppers);
$itemQuad->setSize($itemSize, $itemSize);
$iconFrame->add($itemQuad);
2014-01-08 20:20:18 +01:00
2014-01-04 20:20:55 +01:00
$valueArray = explode(",", $values);
2014-01-08 20:20:18 +01:00
2014-01-05 14:14:08 +01:00
// Values Menu
2014-01-04 20:20:55 +01:00
$popoutFrame = new Frame();
$maniaLink->add($popoutFrame);
$popoutFrame->setPosition($posX - $itemSize * 0.5, $posY);
$popoutFrame->setHAlign(Control::RIGHT);
$popoutFrame->setSize(4 * $itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
2014-01-12 01:59:23 +01:00
$popoutFrame->setVisible(false);
2014-01-08 20:20:18 +01:00
2014-01-04 20:20:55 +01:00
$quad = new Quad();
$popoutFrame->add($quad);
$quad->setHAlign(Control::RIGHT);
$quad->setStyles($quadStyle, $quadSubstyle);
$quad->setSize(strlen($values) * 2 + count($valueArray) * 1, $itemSize * $itemMarginFactorY);
2014-01-08 20:20:18 +01:00
2014-01-04 20:20:55 +01:00
$popoutFrame->add($quad);
2014-01-08 20:20:18 +01:00
2014-01-05 17:30:19 +01:00
$script->addToggle($itemQuad, $popoutFrame);
2014-01-08 20:20:18 +01:00
2014-01-05 14:14:08 +01:00
// Description Label
2014-01-04 21:54:40 +01:00
$descriptionFrame = new Frame();
$maniaLink->add($descriptionFrame);
2014-01-04 21:57:53 +01:00
$descriptionFrame->setPosition($posX - 50, $posY - 5);
2014-01-04 21:54:40 +01:00
$descriptionFrame->setHAlign(Control::RIGHT);
2014-01-08 20:20:18 +01:00
2014-01-04 21:54:40 +01:00
$descriptionLabel = new Label();
2014-01-04 21:57:53 +01:00
$descriptionFrame->add($descriptionLabel);
$descriptionLabel->setAlign(Control::LEFT, Control::TOP);
2014-01-04 21:54:40 +01:00
$descriptionLabel->setSize(40, 4);
$descriptionLabel->setTextSize(2);
$descriptionLabel->setVisible(true);
2014-01-05 00:02:13 +01:00
$descriptionLabel->setTextColor("0F0");
2014-01-08 20:20:18 +01:00
2014-01-04 20:20:55 +01:00
// Add items
$x = -2;
2014-01-08 20:20:18 +01:00
foreach(array_reverse($valueArray) as $value) {
2014-01-04 20:20:55 +01:00
$label = new Label_Button();
$popoutFrame->add($label);
$label->setX($x);
$label->setHAlign(Control::RIGHT);
2014-01-05 14:14:08 +01:00
$label->setText('$s$FFF' . $value . '$09FP');
2014-01-04 20:20:55 +01:00
$label->setTextSize(1.2);
$label->setAction(self::ACTION_DONATE_VALUE . "." . $value);
2014-01-04 22:58:57 +01:00
$label->setStyle(Label_Text::STYLE_TextCardSmall);
2014-01-04 21:54:40 +01:00
$script->addTooltip($label, $descriptionLabel, array(Script::OPTION_TOOLTIP_TEXT => "Donate " . $value . " Planets"));
2014-01-08 20:20:18 +01:00
2014-01-04 20:20:55 +01:00
$x -= strlen($value) * 2 + 1.7;
}
2014-01-08 20:20:18 +01:00
2014-01-02 18:06:27 +01:00
// Send manialink
$manialinkText = $maniaLink->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $login);
}
/**
* Handle /donate command
*
2014-01-08 20:20:18 +01:00
* @param array $chatCallback
2014-01-02 17:31:58 +01:00
* @param Player $player
* @return bool
*/
public function command_Donate(array $chatCallback, Player $player) {
2014-01-08 20:20:18 +01:00
$text = $chatCallback[1][2];
$params = explode(' ', $text);
2014-01-08 20:20:18 +01:00
if(count($params) < 2) {
$this->sendDonateUsageExample($player);
return false;
}
2014-01-08 20:20:18 +01:00
$amount = (int)$params[1];
if(!$amount || $amount <= 0) {
$this->sendDonateUsageExample($player);
return false;
}
2014-01-08 20:20:18 +01:00
if(count($params) >= 3) {
$receiver = $params[2];
$receiverPlayer = $this->maniaControl->playerManager->getPlayer($receiver);
2014-01-08 20:20:18 +01:00
$receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver);
} else {
$receiver = '';
$receiverName = $this->maniaControl->server->getName();
}
2014-01-08 20:20:18 +01:00
2014-01-04 20:20:55 +01:00
return $this->handleDonation($player, $amount, $receiver, $receiverName);
}
/**
* Handles a Player Donate
*
* @param Player $player
2014-01-08 20:20:18 +01:00
* @param $value
2014-01-04 20:20:55 +01:00
*/
private function handleDonation(Player $player, $amount, $receiver = '', $receiverName = false) {
2014-01-08 20:20:18 +01:00
if(!$receiverName) {
2014-01-04 20:20:55 +01:00
$receiverName = $this->maniaControl->server->getName();
}
2014-01-08 20:20:18 +01:00
$message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?';
2014-01-20 20:51:03 +01:00
try {
$bill = $this->maniaControl->client->sendBill($player->login, $amount, $message, $receiver);
} catch(Exception $e) {
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;
}
2014-01-20 20:51:03 +01:00
$this->openBills[$bill] = array(true, $player->login, $receiver, $amount, time());
2014-01-08 20:20:18 +01:00
return true;
}
/**
* Handle //pay command
*
2014-01-08 20:20:18 +01:00
* @param array $chatCallback
2014-01-02 17:31:58 +01:00
* @param Player $player
* @return bool
*/
public function command_Pay(array $chatCallback, Player $player) {
2014-01-08 20:20:18 +01:00
if(!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return false;
}
2014-01-08 20:20:18 +01:00
$text = $chatCallback[1][2];
$params = explode(' ', $text);
2014-01-08 20:20:18 +01:00
if(count($params) < 2) {
$this->sendPayUsageExample($player);
return false;
}
2014-01-08 20:20:18 +01:00
$amount = (int)$params[1];
if(!$amount || $amount <= 0) {
$this->sendPayUsageExample($player);
return false;
}
2014-01-08 20:20:18 +01:00
if(count($params) >= 3) {
$receiver = $params[2];
2014-01-08 20:20:18 +01:00
} else {
$receiver = $player->login;
}
$message = 'Payout from $<' . $this->maniaControl->server->getName() . '$>.';
2014-01-20 20:51:03 +01:00
try {
$bill = $this->maniaControl->client->pay($receiver, $amount, $message);
} catch(Exception $e) {
trigger_error("Couldn't retrieve server planets. " . $e->getMessage());
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;
}
2014-01-20 20:51:03 +01:00
$this->openBills[$bill] = array(false, $player->login, $receiver, $amount, time());
return true;
}
/**
* Handle //getplanets command
*
2014-01-08 20:20:18 +01:00
* @param array $chatCallback
2014-01-02 17:31:58 +01:00
* @param Player $player
* @return bool
*/
public function command_GetPlanets(array $chatCallback, Player $player) {
2014-01-08 20:20:18 +01:00
if(!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return false;
}
2014-01-20 20:51:03 +01:00
try {
$planets = $this->maniaControl->client->getServerPlanets();
} catch(Exception $e) {
trigger_error("Couldn't retrieve server planets. " . $e->getMessage());
return false;
}
2014-01-20 20:51:03 +01:00
$message = "This Server has {$planets} Planets!";
return $this->maniaControl->chat->sendInformation($message, $player->login);
}
/**
* Handle bill updated callback
*
2014-01-02 17:31:58 +01:00
* @param array $callback
* @return bool
*/
public function handleBillUpdated(array $callback) {
$billId = $callback[1][0];
2014-01-08 20:20:18 +01:00
if(!array_key_exists($billId, $this->openBills)) {
return false;
}
$billData = $this->openBills[$billId];
2014-01-08 20:20:18 +01:00
$login = $billData[1];
$receiver = $billData[2];
2014-01-08 20:20:18 +01:00
switch($callback[1][1]) {
case 4:
2014-01-08 20:20:18 +01:00
{
// 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.';
}
2014-01-08 20:20:18 +01:00
$this->maniaControl->chat->sendSuccess($message, $login);
2014-01-08 20:20:18 +01:00
$this->maniaControl->statisticManager->insertStat(self::STAT_PLAYER_DONATIONS, $player, $this->maniaControl->server->index, $amount);
}
2014-01-08 20:20:18 +01:00
} else {
// Payout
$message = "Successfully payed out {$amount} to '{$receiver}'!";
$this->maniaControl->chat->sendSuccess($message, $login);
}
2014-01-08 20:20:18 +01:00
unset($this->openBills[$billId]);
break;
}
case 5:
2014-01-08 20:20:18 +01:00
{
// Refused
$message = 'Transaction cancelled.';
$this->maniaControl->chat->sendError($message, $login);
unset($this->openBills[$billId]);
break;
}
case 6:
2014-01-08 20:20:18 +01:00
{
// 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
*
2014-01-02 17:31:58 +01:00
* @param Player $player
* @return boolean
*/
private function sendDonateUsageExample(Player $player) {
$message = "Usage Example: '/donate 100'";
return $this->maniaControl->chat->sendChat($message, $player->login);
}
/**
* Send an usage example for /pay to the player
*
2014-01-02 17:31:58 +01:00
* @param Player $player
* @return boolean
*/
private function sendPayUsageExample(Player $player) {
$message = "Usage Example: '/pay 100 login'";
return $this->maniaControl->chat->sendChat($message, $player->login);
}
}