2014-02-25 18:34:35 +01:00
|
|
|
<?php
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
namespace ManiaControl\Bills;
|
2014-02-25 18:34:35 +01:00
|
|
|
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
2014-02-25 18:34:35 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Players\Player;
|
2015-06-05 10:07:34 +02:00
|
|
|
use Maniaplanet\DedicatedServer\InvalidArgumentException;
|
2014-02-25 18:34:35 +01:00
|
|
|
use Maniaplanet\DedicatedServer\Structures\Bill;
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* ManiaControl Bill Manager Class
|
2014-04-12 12:14:37 +02:00
|
|
|
*
|
2017-04-07 23:38:18 +02:00
|
|
|
* @api
|
2014-05-02 17:40:47 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2018-03-27 20:11:40 +02:00
|
|
|
* @copyright 2014-2018 ManiaControl Team
|
2014-04-19 22:59:11 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-04-12 12:14:37 +02:00
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class BillManager implements CallbackListener, UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
2017-04-07 23:38:18 +02:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-02-25 18:34:35 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const DONATED_TO_SERVER = 1;
|
|
|
|
const DONATED_TO_RECEIVER = 2;
|
|
|
|
const PAYED_FROM_SERVER = 3;
|
|
|
|
const PLAYER_REFUSED_DONATION = 4;
|
|
|
|
const ERROR_WHILE_TRANSACTION = 5;
|
2015-06-03 22:22:07 +02:00
|
|
|
const CB_BILL_PAID = 'Billmanager.BillPaid';
|
2014-02-25 18:34:35 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-07-25 16:28:47 +02:00
|
|
|
* Private properties
|
2014-02-25 18:34:35 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2014-02-25 18:34:35 +01:00
|
|
|
private $maniaControl = null;
|
2014-09-07 23:17:02 +02:00
|
|
|
/** @var BillData[] $openBills */
|
2014-02-25 18:34:35 +01:00
|
|
|
private $openBills = array();
|
|
|
|
|
|
|
|
/**
|
2014-09-07 23:17:02 +02:00
|
|
|
* Construct a new Bill Manager instance
|
2014-02-25 18:34:35 +01:00
|
|
|
*
|
2014-07-25 16:28:47 +02:00
|
|
|
* @param ManiaControl $maniaControl
|
2014-02-25 18:34:35 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2014-08-03 01:34:18 +02:00
|
|
|
|
|
|
|
// Callbacks
|
2014-08-13 11:05:52 +02:00
|
|
|
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated');
|
2014-02-25 18:34:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-07 23:17:02 +02:00
|
|
|
* Send a bill to a player
|
2014-02-25 18:34:35 +01:00
|
|
|
*
|
2017-04-07 23:38:18 +02:00
|
|
|
* @api
|
2014-05-09 17:30:31 +02:00
|
|
|
* @param callable $function
|
|
|
|
* @param Player $player
|
|
|
|
* @param int $amount
|
|
|
|
* @param string $message
|
|
|
|
* @param string $receiver
|
2014-02-25 18:34:35 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-08-03 13:30:07 +02:00
|
|
|
public function sendBill(callable $function, Player $player, $amount, $message, $receiver = '') {
|
2015-07-10 22:56:27 +02:00
|
|
|
//Get the Caller Class
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$class = $backTrace[1]['class'];
|
|
|
|
|
2015-06-05 10:07:34 +02:00
|
|
|
try {
|
|
|
|
$billId = $this->maniaControl->getClient()->sendBill($player->login, intval($amount), $message, $receiver);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
//TODO better error handling, maybe call the user func with ERROR_WHILE_TRANSACTION
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-10 22:56:27 +02:00
|
|
|
$this->openBills[$billId] = new BillData($class, $function, $player, $amount, false, $receiver, $message);
|
2014-02-25 18:34:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-10 22:56:27 +02:00
|
|
|
|
2014-02-25 18:34:35 +01:00
|
|
|
/**
|
2014-09-07 23:17:02 +02:00
|
|
|
* Send planets from the server to a player
|
2014-02-25 18:34:35 +01:00
|
|
|
*
|
2017-04-07 23:38:18 +02:00
|
|
|
* @api
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param callable $function
|
2014-07-25 16:28:47 +02:00
|
|
|
* @param string $receiverLogin
|
|
|
|
* @param int $amount
|
|
|
|
* @param string $message
|
2014-02-25 18:34:35 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-13 16:40:05 +02:00
|
|
|
public function sendPlanets(callable $function, $receiverLogin, $amount, $message) {
|
2015-07-10 22:56:27 +02:00
|
|
|
//Get the Caller Class
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$class = $backTrace[1]['class'];
|
|
|
|
|
2015-06-05 10:07:34 +02:00
|
|
|
try {
|
|
|
|
$billId = $this->maniaControl->getClient()->pay($receiverLogin, intval($amount), $message);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-10 22:56:27 +02:00
|
|
|
|
|
|
|
$this->openBills[$billId] = new BillData($class, $function, $receiverLogin, $amount, true, $receiverLogin, $message);
|
2014-02-25 18:34:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle bill updated callback
|
|
|
|
*
|
2017-04-07 23:38:18 +02:00
|
|
|
* @internal
|
2014-02-25 18:34:35 +01:00
|
|
|
* @param array $callback
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function handleBillUpdated(array $callback) {
|
|
|
|
$billId = $callback[1][0];
|
2014-09-07 23:17:02 +02:00
|
|
|
if (!isset($this->openBills[$billId])) {
|
2017-04-07 23:38:18 +02:00
|
|
|
return null;
|
2014-02-25 18:34:35 +01:00
|
|
|
}
|
|
|
|
$billData = $this->openBills[$billId];
|
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
switch ($callback[1][1]) {
|
2014-02-25 18:34:35 +01:00
|
|
|
case Bill::STATE_PAYED:
|
|
|
|
if ($billData->pay) {
|
|
|
|
call_user_func($billData->function, $billData, self::PAYED_FROM_SERVER);
|
2015-01-11 16:12:57 +01:00
|
|
|
//Trigger a Callback for external Plugins
|
2015-01-15 11:06:47 +01:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_BILL_PAID, self::PAYED_FROM_SERVER, $billData);
|
2014-02-25 18:34:35 +01:00
|
|
|
} else {
|
|
|
|
if ($billData->receiverLogin) {
|
|
|
|
call_user_func($billData->function, $billData, self::DONATED_TO_RECEIVER);
|
2015-01-11 16:12:57 +01:00
|
|
|
//Trigger a Callback for external Plugins
|
2015-01-15 11:06:47 +01:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_BILL_PAID, self::DONATED_TO_RECEIVER, $billData);
|
2014-02-25 18:34:35 +01:00
|
|
|
} else {
|
|
|
|
call_user_func($billData->function, $billData, self::DONATED_TO_SERVER);
|
2015-01-11 16:12:57 +01:00
|
|
|
//Trigger a Callback for external Plugins
|
2015-01-11 20:37:52 +01:00
|
|
|
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_BILL_PAID, self::DONATED_TO_SERVER, $billData);
|
2014-02-25 18:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2017-04-07 23:38:18 +02:00
|
|
|
return null;
|
2014-02-25 18:34:35 +01:00
|
|
|
}
|
2014-04-12 12:14:37 +02:00
|
|
|
}
|