bill manager

This commit is contained in:
kremsy
2014-02-25 18:34:35 +01:00
committed by Steffen Schröder
parent e6c7a025ee
commit 7005469973
8 changed files with 235 additions and 111 deletions

View File

@ -0,0 +1,27 @@
<?php
/**
* ManiaControl BillData Structure
*
* @author kremsy and steeffeen
*/
namespace ManiaControl\Bills;
class BillData {
public $function = null;
public $pay = false;
public $player = null;
public $receiverLogin = false;
public $amount = 0;
public $creationTime = -1;
public function __construct($function, $player, $amount, $pay = false, $receiverLogin = false) {
$this->function = $function;
$this->player = $player;
$this->amount = $amount;
$this->pay = $pay;
$this->receiverLogin = $receiverLogin;
$this->creationTime = time();
}
}

View File

@ -0,0 +1,141 @@
<?php
/**
* ManiaControl Bill-Manager
*
* @author kremsy and steeffeen
*/
namespace ManiaControl\Bills;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Structures\Bill;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
class BillManager implements CallbackListener {
/**
* 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;
/**
* 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');
}
/**
* send a Bill to a Player
*
* @param $function
* @param Player $player
* @param $amount
* @param $message
* @param bool $receiver
* @return bool
*/
public function sendBill($function, Player $player, $amount, $message, $receiver = false) {
if (!is_callable($function)) {
trigger_error("Function is not callable");
return false;
}
try {
if (!$receiver) {
$bill = $this->maniaControl->client->sendBill($player->login, $amount, $message);
} else {
$bill = $this->maniaControl->client->sendBill($player->login, $amount, $message, $receiver);
}
} catch(Exception $e) {
// TODO: handle errors like 'too few server planets' - throw other like connection errors
$this->maniaControl->errorHandler->triggerDebugNotice("Couldn't create donation of {$amount} planets from '{$player->login}' for '{$receiver}'. " . $e->getMessage());
return false;
}
$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;
}
try {
$bill = $this->maniaControl->client->pay($receiverLogin, $amount, $message);
} catch(Exception $e) {
// TODO: handle errors like 'too few server planets' - throw other like connection errors
//trigger_error("Couldn't create payout of {$amount} planets by '{$player->login}' for '{$receiver}'. " . $e->getMessage());
$this->maniaControl->errorHandler->triggerDebugNotice("Couldn't create payout of {$amount} planets to '{$receiverLogin}' " . $e->getMessage());
return false;
}
$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 */
switch($callback[1][1]) {
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;
}
}
}

View File

@ -4,6 +4,7 @@ namespace ManiaControl;
use ManiaControl\Admin\ActionsMenu;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Bills\BillManager;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Callbacks\TimerManager;
@ -77,6 +78,8 @@ class ManiaControl implements CommandListener, TimerListener {
public $errorHandler = null;
public $timerManager = null;
public $fileReader = null;
public $billManager = null;
/**
* Private properties
*/
@ -99,6 +102,7 @@ class ManiaControl implements CommandListener, TimerListener {
$this->callbackManager = new CallbackManager($this);
$this->timerManager = new TimerManager($this);
$this->fileReader = new AsynchronousFileReader($this);
$this->billManager = new BillManager($this);
$this->settingManager = new SettingManager($this);
$this->statisticManager = new StatisticManager($this);
$this->manialinkManager = new ManialinkManager($this);