|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: php_r |
|
5
|
|
|
* Date: 25.2.2018 |
|
6
|
|
|
* Time: 17.31 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace eXpansion\Framework\GameCurrencyBundle\Services; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
13
|
|
|
use eXpansion\Framework\GameCurrencyBundle\Model\Gamecurrency; |
|
14
|
|
|
use eXpansion\Framework\GameCurrencyBundle\Model\GameCurrencyQueryBuilder; |
|
15
|
|
|
use eXpansion\Framework\GameCurrencyBundle\Plugins\GameCurrencyPlugin; |
|
16
|
|
|
use eXpansion\Framework\GameCurrencyBundle\Structures\CurrencyEntry; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
|
|
19
|
|
|
class GameCurrencyService |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var GameCurrencyPlugin |
|
24
|
|
|
*/ |
|
25
|
|
|
private $currencyPlugin; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var GameCurrencyQueryBuilder |
|
28
|
|
|
*/ |
|
29
|
|
|
private $queryBuilder; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Console |
|
32
|
|
|
*/ |
|
33
|
|
|
private $console; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var LoggerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* GameCurrency constructor. |
|
41
|
|
|
* @param GameCurrencyPlugin $currencyPlugin |
|
42
|
|
|
* @param GameCurrencyQueryBuilder $queryBuilder |
|
43
|
|
|
* @param Console $console |
|
44
|
|
|
* @param LoggerInterface $logger |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
GameCurrencyPlugin $currencyPlugin, |
|
48
|
|
|
GameCurrencyQueryBuilder $queryBuilder, |
|
49
|
|
|
Console $console, |
|
50
|
|
|
LoggerInterface $logger |
|
51
|
|
|
) { |
|
52
|
|
|
|
|
53
|
|
|
$this->currencyPlugin = $currencyPlugin; |
|
54
|
|
|
$this->queryBuilder = $queryBuilder; |
|
55
|
|
|
$this->console = $console; |
|
56
|
|
|
$this->logger = $logger; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Send bill to login |
|
61
|
|
|
* Server needs to have in-game currency for sending a bill. |
|
62
|
|
|
* |
|
63
|
|
|
* @param mixed $bill |
|
64
|
|
|
* @param callable $onSuccess |
|
65
|
|
|
* @param callable $onFailure |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public function sendBill($bill, callable $onSuccess, callable $onFailure) |
|
69
|
|
|
{ |
|
70
|
|
|
// return if the bill is not defined by creating one. |
|
71
|
|
|
if ($bill === false) { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$currencyEntry = new CurrencyEntry(); |
|
76
|
|
|
$currencyEntry->setBill($bill); |
|
77
|
|
|
$currencyEntry->setSuccessCallback($onSuccess); |
|
78
|
|
|
$currencyEntry->setFailureCallback($onFailure); |
|
79
|
|
|
$this->currencyPlugin->sendBill($currencyEntry); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create a bill for sending it forward |
|
86
|
|
|
* @param $login |
|
87
|
|
|
* @param $amount |
|
88
|
|
|
* @param $receiver |
|
89
|
|
|
* @param $message |
|
90
|
|
|
* @return Gamecurrency|bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function createBill($login, $amount, $receiver, $message) |
|
93
|
|
|
{ |
|
94
|
|
|
$currencyEntry = new Gamecurrency(); |
|
95
|
|
|
$currencyEntry->setAmount($amount); |
|
96
|
|
|
$currencyEntry->setSenderlogin($login); |
|
97
|
|
|
$currencyEntry->setReceiverlogin($receiver); |
|
98
|
|
|
$currencyEntry->setMessage($message); |
|
99
|
|
|
$currencyEntry->setDatetime(new \DateTime()); |
|
100
|
|
|
try { |
|
101
|
|
|
$currencyEntry->save(); |
|
102
|
|
|
|
|
103
|
|
|
return $currencyEntry; |
|
104
|
|
|
|
|
105
|
|
|
} catch (\Exception $e) { |
|
106
|
|
|
$this->logger->error("Error while creating bill", ["exception" => $e]); |
|
107
|
|
|
$this->console->write('$f00 Fail.', true); |
|
108
|
|
|
|
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
} |