TrackManiaControl/plugins/MCTeam/DonationPlugin.php

535 lines
18 KiB
PHP
Raw Normal View History

<?php
namespace MCTeam;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quad;
2014-07-04 18:29:30 +02:00
use FML\Controls\Quads\Quad_BgRaceScore2;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\ManiaLink;
use FML\Script\Features\Paging;
use ManiaControl\Admin\ActionsMenu;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Bills\BillManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Commands\CommandListener;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\LabelLine;
use ManiaControl\Manialinks\ManialinkManager;
2017-05-19 18:36:41 +02:00
use ManiaControl\Manialinks\SidebarMenuEntryListener;
use ManiaControl\Manialinks\SidebarMenuManager;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
/**
* ManiaControl Donation Plugin
*
2014-05-03 23:49:58 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
2014-05-03 23:49:58 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2017-05-19 18:36:41 +02:00
class DonationPlugin implements CallbackListener, CommandListener, Plugin, SidebarMenuEntryListener {
2014-05-03 23:49:58 +02:00
/*
* Constants
*/
2014-06-17 23:35:56 +02:00
const ID = 3;
const VERSION = 0.1;
const AUTHOR = 'MCTeam';
const NAME = 'Donation Plugin';
const SETTING_ANNOUNCE_SERVER_DONATION = 'Enable Server-Donation Announcements';
const STAT_PLAYER_DONATIONS = 'Donated Planets';
const ACTION_DONATE_VALUE = 'Donate.DonateValue';
const DONATIONPLUGIN_MENU_ID = 'DonationPlugin.MenuId';
// DonateWidget Properties
const MLID_DONATE_WIDGET = 'DonationPlugin.DonateWidget';
const SETTING_DONATE_WIDGET_ACTIVATED = 'Donate-Widget Activated';
const SETTING_DONATION_VALUES = 'Donation Values';
const SETTING_MIN_AMOUNT_SHOWN = 'Minimum Donation amount to get shown';
2014-05-03 23:49:58 +02:00
/*
* Private properties
*/
2014-05-07 22:59:09 +02:00
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
/**
2014-05-03 23:49:58 +02:00
* @see \ManiaControl\Plugins\Plugin::prepare()
*/
public static function prepare(ManiaControl $maniaControl) {
}
2014-05-03 23:49:58 +02:00
/**
* @see \ManiaControl\Plugins\Plugin::getId()
*/
public static function getId() {
return self::ID;
}
/**
* @see \ManiaControl\Plugins\Plugin::getName()
*/
public static function getName() {
return self::NAME;
}
/**
* @see \ManiaControl\Plugins\Plugin::getVersion()
*/
public static function getVersion() {
return self::VERSION;
}
/**
* @see \ManiaControl\Plugins\Plugin::getAuthor()
*/
public static function getAuthor() {
return self::AUTHOR;
}
/**
* @see \ManiaControl\Plugins\Plugin::getDescription()
*/
public static function getDescription() {
2014-05-07 22:59:09 +02:00
return 'Plugin offering Commands like /donate, /pay and /planets and a Donation Widget.';
2014-05-03 23:49:58 +02:00
}
/**
* @see \ManiaControl\Plugins\Plugin::load()
*/
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Register for commands
2014-08-13 11:14:29 +02:00
$this->maniaControl->getCommandManager()->registerCommandListener('donate', $this, 'command_Donate', false, 'Donate some planets to the server.');
$this->maniaControl->getCommandManager()->registerCommandListener('pay', $this, 'command_Pay', true, 'Pays planets from the server to a player.');
$this->maniaControl->getCommandManager()->registerCommandListener(array('getplanets', 'planets'), $this, 'command_GetPlanets', true, 'Checks the planets-balance of the server.');
$this->maniaControl->getCommandManager()->registerCommandListener('topdons', $this, 'command_TopDons', false, 'Provides an overview of who donated the most planets.');
// Register for callbacks
2014-08-13 11:14:29 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
// Define player stats
2014-08-13 11:14:29 +02:00
$this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_PLAYER_DONATIONS);
2017-05-19 18:36:41 +02:00
$this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry(SidebarMenuManager::ORDER_PLAYER_MENU + 5,self::DONATIONPLUGIN_MENU_ID,$this,'displayDonateWidget');
2014-08-13 11:14:29 +02:00
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED, true);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_DONATION_VALUES, "20,50,100,500,1000,2000");
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MIN_AMOUNT_SHOWN, 100);
// Register Stat in Simple StatsList
2014-08-13 11:14:29 +02:00
$this->maniaControl->getStatisticManager()->getSimpleStatsList()->registerStat(self::STAT_PLAYER_DONATIONS, 90, "DP", 15);
$this->displayWidget();
return true;
}
2017-05-19 18:36:41 +02:00
/**
2014-05-07 22:59:09 +02:00
* Display the widget
*/
public function displayWidget() {
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) {
$this->displayDonateWidget();
}
}
/**
2014-05-09 17:31:29 +02:00
* Display the Donation Widget
*
2014-05-07 22:59:09 +02:00
* @param string $login
*/
2014-05-09 17:31:29 +02:00
public function displayDonateWidget($login = null) {
$pos = $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->getEntryPosition(self::DONATIONPLUGIN_MENU_ID);
$itemSize = $this->maniaControl->getSettingManager()->getSettingValue($this->maniaControl->getManialinkManager()->getSidebarMenuManager(), SidebarMenuManager::SETTING_MENU_ITEMSIZE);
2014-08-13 11:14:29 +02:00
$values = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_DONATION_VALUES);
$quadStyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadStyle();
$quadSubstyle = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultQuadSubstyle();
$itemMarginFactorX = 1.3;
$itemMarginFactorY = 1.2;
$maniaLink = new ManiaLink(self::MLID_DONATE_WIDGET);
// Donate Menu Icon Frame
$frame = new Frame();
2017-03-25 19:15:50 +01:00
$maniaLink->addChild($frame);
$frame->setPosition($pos->getX(), $pos->getY());
2017-04-11 12:04:37 +02:00
$frame->setZ(ManialinkManager::MAIN_MANIALINK_Z_VALUE);
$backgroundQuad = new Quad();
2017-03-25 19:15:50 +01:00
$frame->addChild($backgroundQuad);
$backgroundQuad->setSize($itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$iconFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($iconFrame);
$iconFrame->setSize($itemSize, $itemSize);
2014-07-04 18:29:30 +02:00
$itemQuad = new Quad_BgRaceScore2();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Points);
$itemQuad->setSize($itemSize, $itemSize);
2017-03-25 19:15:50 +01:00
$iconFrame->addChild($itemQuad);
2014-06-22 19:02:18 +02:00
$valueArray = explode(',', $values);
// Values Menu
$popoutFrame = new Frame();
2017-04-11 12:04:37 +02:00
$frame->addChild($popoutFrame);
$popoutFrame->setPosition(-$itemSize * 0.5, 0);
2017-03-25 19:15:50 +01:00
$popoutFrame->setHorizontalAlign($popoutFrame::RIGHT);
$popoutFrame->setVisible(false);
2014-05-03 23:49:58 +02:00
$itemQuad->addToggleFeature($popoutFrame);
// Description Label
$descriptionFrame = new Frame();
2017-04-11 12:04:37 +02:00
$frame->addChild($descriptionFrame);
2017-03-25 19:15:50 +01:00
$descriptionFrame->setHorizontalAlign($descriptionFrame::RIGHT);
$descriptionLabel = new Label();
2017-03-25 19:15:50 +01:00
$descriptionFrame->addChild($descriptionLabel);
$descriptionLabel->setAlign($descriptionLabel::RIGHT, $descriptionLabel::TOP);
$descriptionLabel->setSize(40, 4);
$descriptionLabel->setTextSize(1);
$descriptionLabel->setVisible(true);
// Add items
$posX = -2;
2014-05-03 23:49:58 +02:00
foreach (array_reverse($valueArray) as $value) {
2014-06-22 19:02:18 +02:00
$label = new Label_Text();
2017-03-25 19:15:50 +01:00
$popoutFrame->addChild($label);
$label->setX($posX);
2017-03-25 19:15:50 +01:00
$label->setHorizontalAlign($label::RIGHT);
$label->setText('$s$FFF' . $value . '$09FP');
$label->setTextSize(1.2);
$label->setAction(self::ACTION_DONATE_VALUE . "." . $value);
2014-06-22 19:02:18 +02:00
$label->setStyle($label::STYLE_TextCardSmall);
2014-05-03 23:49:58 +02:00
$description = "Donate {$value} Planets";
$label->addTooltipLabelFeature($descriptionLabel, $description);
2017-04-03 22:06:06 +02:00
$posX -= strlen($value) * 1.6 + 2.5;
}
$descriptionFrame->setPosition($posX - $itemSize + $itemMarginFactorX, 0);
//Popout background
$quad = new Quad();
$popoutFrame->addChild($quad);
$quad->setHorizontalAlign($quad::RIGHT);
$quad->setStyles($quadStyle, $quadSubstyle);
$quad->setSize((2 - $posX), $itemSize * $itemMarginFactorY);
// Send manialink
2014-08-13 11:14:29 +02:00
$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $login);
}
/**
2014-05-03 23:49:58 +02:00
* @see \ManiaControl\Plugins\Plugin::unload()
*/
public function unload() {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getManialinkManager()->hideManialink(self::MLID_DONATE_WIDGET);
2014-05-03 23:49:58 +02:00
}
/**
* Handle ManialinkPageAnswer Callback
*
2014-05-03 23:49:58 +02:00
* @param array $callback
*/
2014-05-03 23:49:58 +02:00
public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2];
$boolSetting = (strpos($actionId, self::ACTION_DONATE_VALUE) === 0);
if (!$boolSetting) {
return;
}
2014-05-03 23:49:58 +02:00
$login = $callback[1][1];
2014-08-13 11:14:29 +02:00
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
2014-05-03 23:49:58 +02:00
$actionArray = explode(".", $callback[1][2]);
$this->handleDonation($player, intval($actionArray[2]));
}
/**
2014-05-09 17:31:29 +02:00
* Handle a Player Donation
*
* @param Player $player
2014-05-09 17:31:29 +02:00
* @param int $amount
* @param string $receiver
* @param string $receiverName
*/
2014-05-09 17:31:29 +02:00
private function handleDonation(Player $player, $amount, $receiver = '', $receiverName = null) {
2014-07-20 00:12:28 +02:00
if ($amount > 1000000) {
// Prevent too huge donation amounts that would cause xmlrpc parsing errors
$message = "You can only donate 1.000.000 Planets at a time!";
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendError($message, $player);
2014-07-20 00:12:28 +02:00
return;
}
2015-06-04 00:23:29 +02:00
//FIXME if you write "/donate 50 hallo" than comes Message: Donate to Hallo
if (!$receiverName) {
2014-08-13 11:14:29 +02:00
$serverName = $this->maniaControl->getClient()->getServerName();
$message = 'Donate ' . $amount . ' Planets to $<' . $serverName . '$>?';
} else {
$message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?';
}
//Send and Handle the Bill
2014-08-13 11:14:29 +02:00
$this->maniaControl->getBillManager()->sendBill(function ($data, $status) use (&$player, $amount, $receiver) {
2015-01-17 18:01:46 +01:00
switch ($status) {
case BillManager::DONATED_TO_SERVER:
if ($this->maniaControl->getSettingManager()->getSettingValue($this, DonationPlugin::SETTING_ANNOUNCE_SERVER_DONATION, true)
&& $amount >= $this->maniaControl->getSettingManager()->getSettingValue($this, DonationPlugin::SETTING_MIN_AMOUNT_SHOWN, true)
) {
$login = null;
$message = $player->getEscapedNickname() . ' donated ' . $amount . ' Planets! Thanks.';
} else {
$login = $player->login;
$message = 'Donation successful! Thanks.';
}
$this->maniaControl->getChat()->sendSuccess($message, $login);
$this->maniaControl->getStatisticManager()->insertStat(DonationPlugin::STAT_PLAYER_DONATIONS, $player, $this->maniaControl->getServer()->index, $amount);
break;
case BillManager::DONATED_TO_RECEIVER:
$message = "Successfully donated {$amount} to '{$receiver}'!";
$this->maniaControl->getChat()->sendSuccess($message, $player);
break;
case BillManager::PLAYER_REFUSED_DONATION:
$message = 'Transaction cancelled.';
$this->maniaControl->getChat()->sendError($message, $player);
break;
case BillManager::ERROR_WHILE_TRANSACTION:
$message = $data;
$this->maniaControl->getChat()->sendError($message, $player);
break;
}
}, $player, $amount, $message);
}
2014-05-03 23:49:58 +02:00
/**
* Handle PlayerConnect callback
*
* @param Player $player
*/
public function handlePlayerConnect(Player $player) {
// Display Map Widget
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) {
2014-05-03 23:49:58 +02:00
$this->displayDonateWidget($player->login);
}
}
/**
* Handle /donate command
*
* @param array $chatCallback
* @param Player $player
*/
public function command_Donate(array $chatCallback, Player $player) {
$text = $chatCallback[1][2];
$params = explode(' ', $text);
if (count($params) < 2) {
$this->sendDonateUsageExample($player);
2014-05-07 22:59:09 +02:00
return;
2014-05-03 23:49:58 +02:00
}
2015-01-17 18:01:46 +01:00
$amount = (int) $params[1];
2014-05-03 23:49:58 +02:00
if (!$amount || $amount <= 0) {
$this->sendDonateUsageExample($player);
2014-05-07 22:59:09 +02:00
return;
2014-05-03 23:49:58 +02:00
}
if (count($params) >= 3) {
$receiver = $params[2];
2014-08-13 11:14:29 +02:00
$receiverPlayer = $this->maniaControl->getPlayerManager()->getPlayer($receiver);
2014-05-03 23:49:58 +02:00
$receiverName = ($receiverPlayer ? $receiverPlayer->nickname : $receiver);
} else {
$receiver = '';
2014-08-13 11:14:29 +02:00
$receiverName = $this->maniaControl->getClient()->getServerName();
2014-05-03 23:49:58 +02:00
}
2014-05-07 22:59:09 +02:00
$this->handleDonation($player, $amount, $receiver, $receiverName);
2014-05-03 23:49:58 +02:00
}
/**
* Send an usage example for /donate to the player
*
* @param Player $player
*/
private function sendDonateUsageExample(Player $player) {
$message = "Usage Example: '/donate 100'";
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendChat($message, $player);
2014-05-03 23:49:58 +02:00
}
/**
* Handle //pay command
*
* @param array $chatCallback
* @param Player $player
*/
public function command_Pay(array $chatCallback, Player $player) {
if (!$this->maniaControl->getAuthenticationManager()->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
2014-05-07 22:59:09 +02:00
return;
}
$text = $chatCallback[1][2];
$params = explode(' ', $text);
if (count($params) < 2) {
$this->sendPayUsageExample($player);
2014-05-07 22:59:09 +02:00
return;
}
2015-01-17 18:01:46 +01:00
$amount = (int) $params[1];
if (!$amount || $amount <= 0) {
$this->sendPayUsageExample($player);
2014-05-07 22:59:09 +02:00
return;
}
if (count($params) >= 3) {
$receiver = $params[2];
} else {
$receiver = $player->login;
}
2014-08-13 11:14:29 +02:00
$message = 'Payout from $<' . $this->maniaControl->getClient()->getServerName() . '$>.';
$this->maniaControl->getBillManager()->sendPlanets(function ($data, $status) use (&$player, $amount, $receiver) {
2015-01-17 18:01:46 +01:00
switch ($status) {
case BillManager::PAYED_FROM_SERVER:
$message = "Successfully payed out {$amount} to '{$receiver}'!";
$this->maniaControl->getChat()->sendSuccess($message, $player);
break;
case BillManager::PLAYER_REFUSED_DONATION:
$message = 'Transaction cancelled.';
$this->maniaControl->getChat()->sendError($message, $player);
break;
case BillManager::ERROR_WHILE_TRANSACTION:
$message = $data;
$this->maniaControl->getChat()->sendError($message, $player);
break;
}
}, $receiver, $amount, $message);
}
2014-05-03 23:49:58 +02:00
/**
* Send an usage example for /pay to the player
*
* @param Player $player
*/
private function sendPayUsageExample(Player $player) {
2014-05-24 21:53:07 +02:00
$message = "Usage Example: '//pay 100 login'";
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendChat($message, $player);
2014-05-03 23:49:58 +02:00
}
/**
* Handle //getplanets command
*
* @param array $chatCallback
* @param Player $player
*/
public function command_GetPlanets(array $chatCallback, Player $player) {
if (!$this->maniaControl->getAuthenticationManager()->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
2014-05-07 22:59:09 +02:00
return;
}
2014-08-13 11:14:29 +02:00
$planets = $this->maniaControl->getClient()->getServerPlanets();
$message = "This Server has {$planets} Planets!";
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendInformation($message, $player);
}
/**
* Handles the /topdons command
*
* @param array $chatCallback
* @param Player $player
*/
public function command_TopDons(array $chatCallback, Player $player) {
$this->showTopDonsList($player);
}
/**
2014-05-13 18:26:38 +02:00
* Provide an Overview ManiaLink with Donators
*
* @param Player $player
*/
private function showTopDonsList(Player $player) {
2014-08-13 11:14:29 +02:00
$stats = $this->maniaControl->getStatisticManager()->getStatsRanking(self::STAT_PLAYER_DONATIONS);
2014-08-13 11:14:29 +02:00
$width = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsWidth();
$height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
// create manialink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
2014-05-03 23:49:58 +02:00
$script = $maniaLink->getScript();
$paging = new Paging();
$script->addFeature($paging);
// Main frame
2014-08-13 11:14:29 +02:00
$frame = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultListFrame($script, $paging);
2017-03-25 19:15:50 +01:00
$maniaLink->addChild($frame);
// Start offsets
$posX = -$width / 2;
$posY = $height / 2;
//Predefine description Label
2014-08-13 11:14:29 +02:00
$descriptionLabel = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultDescriptionLabel();
2017-03-25 19:15:50 +01:00
$frame->addChild($descriptionLabel);
// Headline
$headFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($headFrame);
$headFrame->setY($posY - 5);
$labelLine = new LabelLine($headFrame);
$labelLine->setPrefix('$o');
$labelLine->addLabelEntryText('Id', $posX + 5);
$labelLine->addLabelEntryText('Nickname', $posX + 18);
$labelLine->addLabelEntryText('Login', $posX + 70);
$labelLine->addLabelEntryText('Donated planets', $posX + 110);
$labelLine->render();
$index = 1;
$posY = $posY - 10;
2014-05-15 17:45:08 +02:00
$pageFrame = null;
2014-05-03 23:49:58 +02:00
foreach ($stats as $playerIndex => $donations) {
if ($index % 15 === 1) {
$pageFrame = new Frame();
2017-03-25 19:15:50 +01:00
$frame->addChild($pageFrame);
$posY = $height / 2 - 10;
$paging->addPageControl($pageFrame);
}
$playerFrame = new Frame();
2017-03-25 19:15:50 +01:00
$pageFrame->addChild($playerFrame);
$playerFrame->setY($posY);
if ($index % 2 !== 0) {
$lineQuad = new Quad_BgsPlayerCard();
2017-03-25 19:15:50 +01:00
$playerFrame->addChild($lineQuad);
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(-0.001);
}
2014-08-13 11:14:29 +02:00
$donatingPlayer = $this->maniaControl->getPlayerManager()->getPlayerByIndex($playerIndex);
2015-06-04 00:23:29 +02:00
$labelLine = new LabelLine($playerFrame);
$labelLine->addLabelEntryText($index, $posX + 5, 13);
$labelLine->addLabelEntryText($donatingPlayer->nickname, $posX + 18, 52);
$labelLine->addLabelEntryText($donatingPlayer->login, $posX + 70, 40);
$labelLine->addLabelEntryText($donations, $posX + 110, $width / 2 - ($posX + 110));
$labelLine->render();
$posY -= 4;
$index++;
if ($index > 100) {
break;
}
}
// Render and display xml
2014-08-13 11:14:29 +02:00
$this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $player, 'TopDons');
}
}