TrackManiaControl/application/core/Bills/BillManager.php

124 lines
3.3 KiB
PHP
Raw Normal View History

2014-02-25 18:34:35 +01:00
<?php
namespace ManiaControl\Bills;
2014-02-25 18:34:35 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Structures\Bill;
/**
* ManiaControl Bill-Manager
*
2014-05-02 17:40:47 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
2014-04-19 22:59:11 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2014-02-25 18:34:35 +01:00
class BillManager implements CallbackListener {
/*
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;
/*
2014-02-25 18:34:35 +01:00
* Private Properties
*/
private $maniaControl = null;
private $openBills = array();
/**
* Construct a new Bill Manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated');
}
/**
2014-05-09 17:30:31 +02:00
* Send a Bill to a Player
2014-02-25 18:34:35 +01:00
*
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-05-09 17:30:31 +02:00
public function sendBill($function, Player $player, $amount, $message, $receiver = '') {
2014-02-25 18:34:35 +01:00
if (!is_callable($function)) {
2014-05-09 17:30:31 +02:00
trigger_error("Function is not callable!");
2014-02-25 18:34:35 +01:00
return false;
}
2014-05-09 17:30:31 +02:00
$bill = $this->maniaControl->client->sendBill($player->login, $amount, $message, $receiver);
2014-02-25 18:34:35 +01:00
$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;
}
2014-04-19 22:59:11 +02:00
$bill = $this->maniaControl->client->pay($receiverLogin, $amount, $message);
2014-02-25 18:34:35 +01:00
$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 */
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);
} 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;
}
}
}