|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Lib\Transfer\Strategy; |
|
6
|
|
|
|
|
7
|
|
|
use request; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Stu\Lib\Information\InformationInterface; |
|
10
|
|
|
use Stu\Lib\Transfer\Wrapper\StorageEntityWrapperInterface; |
|
11
|
|
|
use Stu\Module\Control\GameControllerInterface; |
|
12
|
|
|
use Stu\Orm\Entity\Colony; |
|
13
|
|
|
|
|
14
|
|
|
class TorpedoTransferStrategy implements TransferStrategyInterface |
|
15
|
|
|
{ |
|
16
|
|
|
#[\Override] |
|
17
|
|
|
public function setTemplateVariables( |
|
18
|
|
|
bool $isUnload, |
|
19
|
|
|
StorageEntityWrapperInterface $source, |
|
20
|
|
|
StorageEntityWrapperInterface $target, |
|
21
|
|
|
GameControllerInterface $game |
|
22
|
|
|
): void { |
|
23
|
|
|
|
|
24
|
|
|
if ($target instanceof Colony) { |
|
25
|
|
|
throw new RuntimeException('this should not happen'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if ($isUnload) { |
|
29
|
|
|
$max = min( |
|
30
|
|
|
$target->getMaxTorpedos(), |
|
31
|
|
|
$source->getTorpedoCount() |
|
32
|
|
|
); |
|
33
|
|
|
$commodityId = $source->getTorpedo() === null ? null : $source->getTorpedo()->getCommodityId(); |
|
34
|
|
|
} else { |
|
35
|
|
|
$max = $target->getTorpedoCount(); |
|
36
|
|
|
$commodityId = $target->getTorpedo() === null ? null : $target->getTorpedo()->getCommodityId(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$game->setTemplateVar('MAXIMUM', $max); |
|
40
|
|
|
$game->setTemplateVar('COMMODITY_ID', $commodityId); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
#[\Override] |
|
44
|
|
|
public function transfer( |
|
45
|
|
|
bool $isUnload, |
|
46
|
|
|
StorageEntityWrapperInterface $source, |
|
47
|
|
|
StorageEntityWrapperInterface $target, |
|
48
|
|
|
InformationInterface $information |
|
49
|
|
|
): void { |
|
50
|
|
|
|
|
51
|
|
|
$torpedoType = $isUnload ? $source->getTorpedo() : $target->getTorpedo(); |
|
52
|
|
|
if ($torpedoType === null) { |
|
53
|
|
|
throw new RuntimeException('this should not happen'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!$source->canTransferTorpedos($information)) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$destination = $isUnload ? $target : $source; |
|
61
|
|
|
if (!$destination->canStoreTorpedoType($torpedoType, $information)) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
//TODO use energy to transfer |
|
66
|
|
|
$requestedTransferCount = request::postInt('tcount'); |
|
67
|
|
|
|
|
68
|
|
|
$amount = min( |
|
69
|
|
|
$requestedTransferCount, |
|
70
|
|
|
$isUnload ? $source->getTorpedoCount() : $target->getTorpedoCount(), |
|
71
|
|
|
$isUnload ? $target->getMaxTorpedos() - $target->getTorpedoCount() : $source->getMaxTorpedos() - $source->getTorpedoCount() |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
if ($amount < 1) { |
|
75
|
|
|
$information->addInformation('Es konnten keine Torpedos transferiert werden'); |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$target->changeTorpedo($isUnload ? $amount : -$amount, $torpedoType); |
|
80
|
|
|
$source->changeTorpedo($isUnload ? -$amount : $amount, $torpedoType); |
|
81
|
|
|
|
|
82
|
|
|
$information->addInformation( |
|
83
|
|
|
sprintf( |
|
84
|
|
|
'Die %s hat in Sektor %s %d Torpedos %s %s transferiert', |
|
85
|
|
|
$source->getName(), |
|
86
|
|
|
$source->getLocation()->getSectorString(), |
|
87
|
|
|
$amount, |
|
88
|
|
|
$isUnload ? 'zur' : 'von der', |
|
89
|
|
|
$target->getName() |
|
90
|
|
|
), |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|