1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CommercialShippingManager |
5
|
|
|
* |
6
|
|
|
* @author Jacky Casas |
7
|
|
|
* @copyright Expansion - le jeu |
8
|
|
|
* |
9
|
|
|
* @package Athena |
10
|
|
|
* @version 19.11.13 |
11
|
|
|
**/ |
12
|
|
|
namespace Asylamba\Modules\Athena\Manager; |
13
|
|
|
|
14
|
|
|
use Asylamba\Modules\Athena\Manager\OrbitalBaseManager; |
15
|
|
|
use Asylamba\Modules\Hermes\Manager\NotificationManager; |
16
|
|
|
use Asylamba\Classes\Library\Utils; |
17
|
|
|
use Asylamba\Classes\Entity\EntityManager; |
18
|
|
|
use Asylamba\Modules\Athena\Model\CommercialShipping; |
19
|
|
|
use Asylamba\Modules\Athena\Model\Transaction; |
20
|
|
|
use Asylamba\Classes\Library\Format; |
21
|
|
|
use Asylamba\Classes\Library\Session\SessionWrapper; |
22
|
|
|
use Asylamba\Modules\Athena\Resource\ShipResource; |
23
|
|
|
use Asylamba\Modules\Hermes\Model\Notification; |
24
|
|
|
use Asylamba\Modules\Ares\Resource\CommanderResources; |
25
|
|
|
use Asylamba\Modules\Ares\Model\Commander; |
26
|
|
|
use Asylamba\Classes\Exception\ErrorException; |
27
|
|
|
use Asylamba\Classes\Scheduler\RealTimeActionScheduler; |
28
|
|
|
|
29
|
|
|
class CommercialShippingManager { |
30
|
|
|
/** @var EntityManager **/ |
31
|
|
|
protected $entityManager; |
32
|
|
|
/** @var OrbitalBaseManager **/ |
33
|
|
|
protected $orbitalBaseManager; |
34
|
|
|
/** @var NotificationManager **/ |
35
|
|
|
protected $notificationManager; |
36
|
|
|
/** @var RealTimeActionScheduler **/ |
37
|
|
|
protected $realtimeActionScheduler; |
38
|
|
|
/** @var SessionWrapper **/ |
39
|
|
|
protected $sessionWrapper; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param EntityManager $entityManager |
43
|
|
|
* @param OrbitalBaseManager $orbitalBaseManager |
44
|
|
|
* @param NotificationManager $notificationManager |
45
|
|
|
* @param RealTimeActionScheduler $realtimeActionScheduler |
46
|
|
|
* @param SessionWrapper $session |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
EntityManager $entityManager, |
50
|
|
|
OrbitalBaseManager $orbitalBaseManager, |
51
|
|
|
NotificationManager $notificationManager, |
52
|
|
|
RealTimeActionScheduler $realtimeActionScheduler, |
53
|
|
|
SessionWrapper $session |
54
|
|
|
) { |
55
|
|
|
$this->entityManager = $entityManager; |
56
|
|
|
$this->orbitalBaseManager = $orbitalBaseManager; |
57
|
|
|
$this->notificationManager = $notificationManager; |
58
|
|
|
$this->realtimeActionScheduler = $realtimeActionScheduler; |
59
|
|
|
$this->sessionWrapper = $session; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function scheduleShippings() |
63
|
|
|
{ |
64
|
|
|
$shippings = $this->entityManager->getRepository(CommercialShipping::class)->getAll(); |
|
|
|
|
65
|
|
|
|
66
|
|
|
foreach ($shippings as $commercialShipping) { |
67
|
|
|
$this->realtimeActionScheduler->schedule( |
68
|
|
|
'athena.orbital_base_manager', |
69
|
|
|
'uCommercialShipping', |
70
|
|
|
$commercialShipping, |
71
|
|
|
$commercialShipping->getArrivedAt() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $id |
78
|
|
|
* @return CommercialShipping |
79
|
|
|
*/ |
80
|
|
|
public function get($id) |
81
|
|
|
{ |
82
|
|
|
return $this->entityManager->getRepository(CommercialShipping::class)->get($id); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $id |
87
|
|
|
* @return CommercialShipping |
88
|
|
|
*/ |
89
|
|
|
public function getByTransactionId($id) |
90
|
|
|
{ |
91
|
|
|
return $this->entityManager->getRepository(CommercialShipping::class)->getByTransactionId($id); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param type $orbitalBaseId |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getByBase($orbitalBaseId) |
99
|
|
|
{ |
100
|
|
|
return $this->entityManager->getRepository(CommercialShipping::class)->getByBase($orbitalBaseId); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param CommercialShipping $commercialShipping |
105
|
|
|
*/ |
106
|
|
|
public function add(CommercialShipping $commercialShipping) { |
107
|
|
|
$this->entityManager->persist($commercialShipping); |
108
|
|
|
$this->entityManager->flush($commercialShipping); |
109
|
|
|
|
110
|
|
|
$this->realtimeActionScheduler->schedule( |
111
|
|
|
'athena.orbital_base_manager', |
112
|
|
|
'uCommercialShipping', |
113
|
|
|
$commercialShipping, |
|
|
|
|
114
|
|
|
$commercialShipping->getArrivedAt() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function deliver(CommercialShipping $commercialShipping, $transaction, $destOB, $commander) { |
119
|
|
|
if ($transaction !== NULL AND $transaction->statement == Transaction::ST_COMPLETED) { |
|
|
|
|
120
|
|
|
|
121
|
|
|
switch ($transaction->type) { |
122
|
|
|
case Transaction::TYP_RESOURCE: |
123
|
|
|
$this->orbitalBaseManager->increaseResources($destOB, $transaction->quantity, TRUE); |
124
|
|
|
|
125
|
|
|
# notif pour l'acheteur |
126
|
|
|
$n = new Notification(); |
127
|
|
|
$n->setRPlayer($destOB->getRPlayer()); |
128
|
|
|
$n->setTitle('Ressources reçues'); |
129
|
|
|
$n->addBeg()->addTxt('Vous avez reçu les ' . $transaction->quantity . ' ressources que vous avez achetées au marché.'); |
130
|
|
|
$n->addEnd(); |
131
|
|
|
$this->notificationManager->add($n); |
132
|
|
|
|
133
|
|
|
break; |
134
|
|
|
case Transaction::TYP_SHIP: |
135
|
|
|
$this->orbitalBaseManager->addShipToDock($destOB, $transaction->identifier, $transaction->quantity); |
136
|
|
|
|
137
|
|
|
# notif pour l'acheteur |
138
|
|
|
$n = new Notification(); |
139
|
|
|
$n->setRPlayer($destOB->getRPlayer()); |
140
|
|
|
if ($commercialShipping->resourceTransported == NULL) { |
|
|
|
|
141
|
|
|
# transaction |
142
|
|
|
if ($transaction->quantity == 1) { |
143
|
|
|
$n->setTitle('Vaisseau reçu'); |
144
|
|
|
$n->addBeg()->addTxt('Vous avez reçu le vaisseau de type ' . ShipResource::getInfo($transaction->identifier, 'codeName') . ' que vous avez acheté au marché.'); |
145
|
|
|
$n->addSep()->addTxt('Il a été ajouté à votre hangar.'); |
146
|
|
|
} else { |
147
|
|
|
$n->setTitle('Vaisseaux reçus'); |
148
|
|
|
$n->addBeg()->addTxt('Vous avez reçu les ' . $transaction->quantity . ' vaisseaux de type ' . ShipResource::getInfo($transaction->identifier, 'codeName') . ' que vous avez achetés au marché.'); |
149
|
|
|
$n->addSep()->addTxt('Ils ont été ajoutés à votre hangar.'); |
150
|
|
|
} |
151
|
|
|
} else { |
152
|
|
|
# ships sending |
153
|
|
|
if ($transaction->quantity == 1) { |
154
|
|
|
$n->setTitle('Vaisseau reçu'); |
155
|
|
|
$n->addBeg()->addTxt('Vous avez reçu le vaisseau de type ' . ShipResource::getInfo($transaction->identifier, 'codeName') . ' envoyé par un marchand galactique.'); |
156
|
|
|
$n->addSep()->addTxt('Il a été ajouté à votre hangar.'); |
157
|
|
|
} else { |
158
|
|
|
$n->setTitle('Vaisseaux reçus'); |
159
|
|
|
$n->addBeg()->addTxt('Vous avez reçu les ' . $transaction->quantity . ' vaisseaux de type ' . ShipResource::getInfo($transaction->identifier, 'codeName') . ' envoyés par un marchand galactique.'); |
160
|
|
|
$n->addSep()->addTxt('Ils ont été ajoutés à votre hangar.'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
$n->addEnd(); |
164
|
|
|
$this->notificationManager->add($n); |
165
|
|
|
break; |
166
|
|
|
case Transaction::TYP_COMMANDER: |
167
|
|
|
$commander->setStatement(Commander::RESERVE); |
168
|
|
|
$commander->setRPlayer($destOB->getRPlayer()); |
169
|
|
|
$commander->setRBase($commercialShipping->rBaseDestination); |
170
|
|
|
|
171
|
|
|
# notif pour l'acheteur |
172
|
|
|
$n = new Notification(); |
173
|
|
|
$n->setRPlayer($destOB->getRPlayer()); |
174
|
|
|
$n->setTitle('Commandant reçu'); |
175
|
|
|
$n->addBeg()->addTxt('Le commandant ' . $commander->getName() . ' que vous avez acheté au marché est bien arrivé.'); |
176
|
|
|
$n->addSep()->addTxt('Il se trouve pour le moment dans votre école de commandement'); |
177
|
|
|
$n->addEnd(); |
178
|
|
|
$this->notificationManager->add($n); |
179
|
|
|
break; |
180
|
|
|
default: |
181
|
|
|
throw new ErrorException('type de transaction inconnue dans deliver()'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$commercialShipping->statement = CommercialShipping::ST_MOVING_BACK; |
185
|
|
|
|
|
|
|
|
186
|
|
|
} elseif ($transaction === NULL AND $commercialShipping->rTransaction == NULL AND $commercialShipping->resourceTransported != NULL) { |
|
|
|
|
187
|
|
|
# resource sending |
188
|
|
|
|
189
|
|
|
$this->orbitalBaseManager->increaseResources($destOB, $commercialShipping->resourceTransported, TRUE); |
190
|
|
|
|
191
|
|
|
# notif for the player who receive the resources |
192
|
|
|
$n = new Notification(); |
193
|
|
|
$n->setRPlayer($destOB->getRPlayer()); |
194
|
|
|
$n->setTitle('Ressources reçues'); |
195
|
|
|
$n->addBeg()->addTxt('Vous avez bien reçu les ' . $commercialShipping->resourceTransported . ' ressources sur votre base orbitale ' . $destOB->name . '.'); |
196
|
|
|
$n->addEnd(); |
197
|
|
|
$this->notificationManager->add($n); |
198
|
|
|
|
199
|
|
|
$commercialShipping->statement = CommercialShipping::ST_MOVING_BACK; |
200
|
|
|
} else { |
201
|
|
|
throw new ErrorException('impossible de délivrer ce chargement'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function render(CommercialShipping $commercialShipping) { |
206
|
|
|
switch ($commercialShipping->typeOfTransaction) { |
207
|
|
|
case Transaction::TYP_RESOURCE: $class = 'resources'; break; |
|
|
|
|
208
|
|
|
case Transaction::TYP_COMMANDER: $class = 'commander'; break; |
|
|
|
|
209
|
|
|
case Transaction::TYP_SHIP: |
210
|
|
|
$class = 'ship'; |
211
|
|
|
break; |
212
|
|
|
default: break; |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
echo '<div class="transaction ' . $class . '">'; |
|
|
|
|
216
|
|
|
if ($commercialShipping->statement != CommercialShipping::ST_MOVING_BACK) { |
217
|
|
|
echo '<div class="product">'; |
218
|
|
|
if ($commercialShipping->statement == CommercialShipping::ST_WAITING) { |
219
|
|
|
echo '<a href="' . Format::actionBuilder('canceltransaction', $this->sessionWrapper->get('token'), ['rtransaction' => $commercialShipping->rTransaction]) . '" class="hb lt right-link" title="supprimer cette offre coûtera ' . Format::number(floor($commercialShipping->price * Transaction::PERCENTAGE_TO_CANCEL / 100)) . ' crédits">×</a>'; |
220
|
|
|
} |
221
|
|
|
if ($commercialShipping->typeOfTransaction == Transaction::TYP_RESOURCE) { |
222
|
|
|
echo '<img src="' . MEDIA . 'market/resources-pack-' . Transaction::getResourcesIcon($commercialShipping->quantity) . '.png" alt="" class="picto" />'; |
223
|
|
|
echo '<div class="offer">'; |
224
|
|
|
if ($commercialShipping->resourceTransported == NULL) { |
|
|
|
|
225
|
|
|
# transaction |
226
|
|
|
echo Format::numberFormat($commercialShipping->quantity) . ' <img src="' . MEDIA . 'resources/resource.png" alt="" class="icon-color" />'; |
227
|
|
|
} else { |
228
|
|
|
# resources sending |
229
|
|
|
echo Format::numberFormat($commercialShipping->resourceTransported) . ' <img src="' . MEDIA . 'resources/resource.png" alt="" class="icon-color" />'; |
230
|
|
|
} |
231
|
|
|
echo '</div>'; |
232
|
|
|
} elseif ($commercialShipping->typeOfTransaction == Transaction::TYP_COMMANDER) { |
233
|
|
|
echo '<img src="' . MEDIA . 'commander/small/' . $commercialShipping->commanderAvatar . '.png" alt="" class="picto" />'; |
234
|
|
|
echo '<div class="offer">'; |
235
|
|
|
echo '<strong>' . CommanderResources::getInfo($commercialShipping->commanderLevel, 'grade') . ' ' . $commercialShipping->commanderName . '</strong>'; |
236
|
|
|
echo '<em>' . Format::numberFormat($commercialShipping->commanderExperience) . ' xp | ' . $commercialShipping->commanderVictory . ' victoire' . Format::addPlural($commercialShipping->commanderVictory) . '</em>'; |
237
|
|
|
echo '</div>'; |
238
|
|
|
} elseif ($commercialShipping->typeOfTransaction == Transaction::TYP_SHIP) { |
239
|
|
|
echo '<img src="' . MEDIA . 'ship/picto/ship' . $commercialShipping->identifier . '.png" alt="" class="picto" />'; |
240
|
|
|
echo '<div class="offer">'; |
241
|
|
|
echo '<strong>' . $commercialShipping->quantity . ' ' . ShipResource::getInfo($commercialShipping->identifier, 'codeName') . Format::plural($commercialShipping->quantity) . '</strong>'; |
242
|
|
|
echo '<em>' . ShipResource::getInfo($commercialShipping->identifier, 'name') . ' / ' . ShipResource::getInfo($commercialShipping->identifier, 'pev') . ' pev</em>'; |
243
|
|
|
echo '</div>'; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if ($commercialShipping->resourceTransported === NULL) { |
247
|
|
|
# transaction |
248
|
|
|
echo '<div class="for">'; |
249
|
|
|
echo '<span>pour</span>'; |
250
|
|
|
echo '</div>'; |
251
|
|
|
echo '<div class="price">'; |
252
|
|
|
echo Format::numberFormat($commercialShipping->price) . ' <img src="' . MEDIA . 'resources/credit.png" alt="" class="icon-color" />'; |
253
|
|
|
echo '</div>'; |
254
|
|
|
} elseif ($commercialShipping->resourceTransported == 0) { |
255
|
|
|
# ships sending |
256
|
|
|
echo '<div class="for"><span></span></div>'; |
257
|
|
|
echo '<div class="price">'; |
258
|
|
|
echo 'envoi de vaisseaux'; |
259
|
|
|
echo '</div>'; |
260
|
|
|
} else { |
261
|
|
|
# resources sending |
262
|
|
|
echo '<div class="for"><span></span></div>'; |
263
|
|
|
echo '<div class="price">'; |
264
|
|
|
echo 'envoi de ressources'; |
265
|
|
|
echo '</div>'; |
266
|
|
|
} |
267
|
|
|
echo '</div>'; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$totalTime = Utils::interval($commercialShipping->dDeparture, $commercialShipping->dArrival, 's'); |
271
|
|
|
$currentTime = Utils::interval(Utils::now(), $commercialShipping->dDeparture, 's'); |
272
|
|
|
|
273
|
|
|
echo ($commercialShipping->statement != CommercialShipping::ST_WAITING) |
274
|
|
|
?'<div class="shipping progress" data-progress-total-time="' . $totalTime . '" data-progress-current-time="' . ($totalTime - $currentTime) . '" data-progress-output="lite">' |
275
|
|
|
: '<div class="shipping">'; |
276
|
|
|
echo '<span class="progress-container">'; |
277
|
|
|
echo '<span style="width: ' . Format::percent($currentTime, $totalTime) . '%;" class="progress-bar"></span>'; |
278
|
|
|
echo '</span>'; |
279
|
|
|
|
280
|
|
|
echo '<div class="ships">'; |
281
|
|
|
echo $commercialShipping->shipQuantity; |
282
|
|
|
echo '<img src="' . MEDIA . 'resources/transport.png" alt="" class="icon-color" />'; |
283
|
|
|
echo '</div>'; |
284
|
|
|
|
285
|
|
|
if ($commercialShipping->statement == CommercialShipping::ST_WAITING) { |
286
|
|
|
echo '<div class="time">à quai</div>'; |
287
|
|
|
} else { |
288
|
|
|
echo '<div class="time progress-text"></div>'; |
289
|
|
|
} |
290
|
|
|
echo '</div>'; |
291
|
|
|
echo '</div>'; |
292
|
|
|
} |
293
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: