1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Ship\View\ShowScan; |
6
|
|
|
|
7
|
|
|
use request; |
8
|
|
|
use Stu\Module\Control\GameControllerInterface; |
9
|
|
|
use Stu\Module\Control\ViewControllerInterface; |
10
|
|
|
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum; |
11
|
|
|
use Stu\Module\Message\Lib\PrivateMessageSenderInterface; |
12
|
|
|
use Stu\Module\Ship\Lib\Interaction\InteractionCheckerInterface; |
13
|
|
|
use Stu\Module\Ship\Lib\ShipLoaderInterface; |
14
|
|
|
use Stu\Module\Ship\Lib\ShipWrapperInterface; |
15
|
|
|
use Stu\Module\Ship\View\ShowShip\ShowShip; |
16
|
|
|
use Stu\Orm\Entity\ShipInterface; |
17
|
|
|
|
18
|
|
|
final class ShowScan implements ViewControllerInterface |
19
|
|
|
{ |
20
|
|
|
public const VIEW_IDENTIFIER = 'SHOW_SCAN'; |
21
|
|
|
|
22
|
|
|
private ShipLoaderInterface $shipLoader; |
23
|
|
|
|
24
|
|
|
private InteractionCheckerInterface $interactionChecker; |
25
|
|
|
|
26
|
|
|
private PrivateMessageSenderInterface $privateMessageSender; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
ShipLoaderInterface $shipLoader, |
30
|
|
|
InteractionCheckerInterface $interactionChecker, |
31
|
|
|
PrivateMessageSenderInterface $privateMessageSender |
32
|
|
|
) { |
33
|
|
|
$this->shipLoader = $shipLoader; |
34
|
|
|
$this->interactionChecker = $interactionChecker; |
35
|
|
|
$this->privateMessageSender = $privateMessageSender; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function handle(GameControllerInterface $game): void |
39
|
|
|
{ |
40
|
|
|
$user = $game->getUser(); |
41
|
|
|
|
42
|
|
|
$shipId = request::indInt('id'); |
43
|
|
|
$targetId = request::getIntFatal('target'); |
44
|
|
|
|
45
|
|
|
$wrappers = $this->shipLoader->getWrappersBySourceAndUserAndTarget( |
46
|
|
|
$shipId, |
47
|
|
|
$user->getId(), |
48
|
|
|
$targetId |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$wrapper = $wrappers->getSource(); |
52
|
|
|
$ship = $wrapper->get(); |
53
|
|
|
|
54
|
|
|
$game->setMacroInAjaxWindow('html/shipmacros.xhtml/entity_not_available'); |
55
|
|
|
|
56
|
|
|
$targetWrapper = $wrappers->getTarget(); |
57
|
|
|
if ($targetWrapper === null) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$target = $targetWrapper->get(); |
62
|
|
|
if ($target->getCloakState()) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$game->setPageTitle(_('Scan')); |
67
|
|
|
$game->setMacroInAjaxWindow('html/shipmacros.xhtml/show_ship_scan'); |
68
|
|
|
if (!$this->interactionChecker->checkPosition($ship, $target)) { |
69
|
|
|
$game->addInformation(_('Das Schiff befindet sich nicht in diesem Sektor')); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$epsSystem = $wrapper->getEpsSystemData(); |
74
|
|
|
if ($epsSystem === null || $epsSystem->getEps() < 1) { |
75
|
|
|
$game->addInformation("Nicht genügend Energie vorhanden (1 benötigt)"); |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$epsSystem->lowerEps(1)->update(); |
80
|
|
|
|
81
|
|
|
if ($target->getDatabaseId() !== 0) { |
82
|
|
|
$game->checkDatabaseItem($target->getDatabaseId()); |
83
|
|
|
} |
84
|
|
|
if ($target->getRump()->getDatabaseId()) { |
|
|
|
|
85
|
|
|
$game->checkDatabaseItem($target->getRump()->getDatabaseId()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$href = sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $target->getId()); |
89
|
|
|
|
90
|
|
|
$this->privateMessageSender->send( |
91
|
|
|
$game->getUser()->getId(), |
92
|
|
|
$target->getUser()->getId(), |
93
|
|
|
sprintf( |
94
|
|
|
_('Die %s von Spieler %s hat %s %s bei %s gescannt.'), |
95
|
|
|
$ship->getName(), |
96
|
|
|
$game->getUser()->getName(), |
97
|
|
|
$target->isBase() ? 'deine Station' : 'dein Schiff', |
98
|
|
|
$target->getName(), |
99
|
|
|
$target->getSectorString() |
100
|
|
|
), |
101
|
|
|
$target->isBase() ? PrivateMessageFolderSpecialEnum::PM_SPECIAL_STATION : PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP, |
102
|
|
|
$href |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$game->setTemplateVar('TARGETWRAPPER', $targetWrapper); |
106
|
|
|
$game->setTemplateVar('SHIELD_PERCENTAGE', $this->calculateShieldPercentage($target)); |
107
|
|
|
$game->setTemplateVar('REACTOR_PERCENTAGE', $this->calculateReactorPercentage($targetWrapper)); |
108
|
|
|
$game->setTemplateVar('SHIP', $ship); |
109
|
|
|
|
110
|
|
|
$tradePostCrewCount = null; |
111
|
|
|
$targetTradePost = $target->getTradePost(); |
112
|
|
|
|
113
|
|
|
if ($targetTradePost !== null) { |
114
|
|
|
$tradePostCrewCount = $targetTradePost->getCrewCountOfUser($user); |
115
|
|
|
} |
116
|
|
|
$game->setTemplateVar('TRADE_POST_CREW_COUNT', $tradePostCrewCount); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function calculateShieldPercentage(ShipInterface $target): int |
120
|
|
|
{ |
121
|
|
|
return $target->getMaxShield() === 0 |
122
|
|
|
? 0 |
123
|
|
|
: (int)ceil($target->getShield() / $target->getMaxShield() * 100); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function calculateReactorPercentage(ShipWrapperInterface $wrapper): ?int |
127
|
|
|
{ |
128
|
|
|
$reactor = $wrapper->getReactorWrapper(); |
129
|
|
|
if ($reactor === null) { |
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $reactor->getCapacity() === 0 |
134
|
|
|
? 0 |
135
|
|
|
: (int)ceil($reactor->getLoad() / $reactor->getCapacity() * 100); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: