Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Pull Request — main (#1494)
by Dan
08:06 queued 03:15
created

CurrentSector::build()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 152
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 78
nc 307200
nop 2
dl 0
loc 152
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Player;
4
5
use AbstractSmrPlayer;
6
use Menu;
7
use Smr\Database;
8
use Smr\Epoch;
9
use Smr\Page\PlayerPage;
10
use Smr\Page\ReusableTrait;
11
use Smr\Pages\Player\Planet\Main as PlanetMain;
12
use Smr\Template;
13
use SmrGame;
14
15
class CurrentSector extends PlayerPage {
16
17
	use ReusableTrait;
18
19
	public string $file = 'current_sector.php';
20
21
	/** @var array<int> */
22
	private array $unreadMissions;
23
	private ?string $attackMessage = null;
24
25
	public function __construct(
26
		private readonly ?string $message = null,
27
		private readonly ?string $errorMessage = null,
28
		private readonly ?string $missionMessage = null,
29
		private readonly ?string $tradeMessage = null,
30
		private readonly bool $showForceRefreshMessage = false
31
	) {}
32
33
	public function build(AbstractSmrPlayer $player, Template $template): void {
34
		$sector = $player->getSector();
35
36
		// If on a planet, forward to planet_main.php
37
		if ($player->isLandedOnPlanet()) {
38
			(new PlanetMain($this->message, $this->errorMessage))->go();
39
		}
40
41
		$template->assign('SpaceView', true);
42
43
		$template->assign('PageTopic', 'Current Sector: ' . $player->getSectorID() . ' (' . $sector->getGalaxy()->getDisplayName() . ')');
44
45
		Menu::navigation($player);
46
47
		// *******************************************
48
		// *
49
		// * Sector List
50
		// *
51
		// *******************************************
52
53
		// Sector links
54
		$links = [];
55
		$links['Up'] = ['ID' => $sector->getLinkUp()];
56
		$links['Right'] = ['ID' => $sector->getLinkRight()];
57
		$links['Down'] = ['ID' => $sector->getLinkDown()];
58
		$links['Left'] = ['ID' => $sector->getLinkLeft()];
59
		$links['Warp'] = ['ID' => $sector->getWarp()];
60
61
		$unvisited = [];
62
63
		$db = Database::getInstance();
64
		$dbResult = $db->read('SELECT sector_id FROM player_visited_sector WHERE sector_id IN (' . $db->escapeArray($links) . ') AND ' . $player->getSQL());
65
		foreach ($dbResult->records() as $dbRecord) {
66
			$unvisited[$dbRecord->getInt('sector_id')] = true;
67
		}
68
69
		foreach ($links as $key => $linkArray) {
70
			if ($linkArray['ID'] > 0 && $linkArray['ID'] != $player->getSectorID()) {
71
				if ($player->getLastSectorID() == $linkArray['ID']) {
72
					$class = 'lastVisited';
73
				} elseif (isset($unvisited[$linkArray['ID']])) {
74
					$class = 'unvisited';
75
				} else {
76
					$class = 'visited';
77
				}
78
				$links[$key]['Class'] = $class;
79
			}
80
		}
81
82
		$template->assign('Sectors', $links);
83
84
		doTickerAssigns($template, $player, $db);
85
86
		$this->unreadMissions ??= $player->markMissionsRead();
87
		$template->assign('UnreadMissions', $this->unreadMissions);
88
89
		// *******************************************
90
		// *
91
		// * Force and other Results
92
		// *
93
		// *******************************************
94
		$game = SmrGame::getGame($player->getGameID());
95
		if (!$game->hasStarted()) {
96
			$turnsMessage = 'The game will start in ' . format_time($game->getStartTime() - Epoch::time()) . '!';
97
		} else {
98
			$turnsMessage = $player->getTurnsLevel()->message();
99
		}
100
		$template->assign('TurnsMessage', $turnsMessage);
101
102
		$protectionMessage = '';
103
		if ($player->getNewbieTurns()) {
104
			if ($player->getNewbieTurns() < 25) {
105
				$protectionMessage = '<span class="blue">PROTECTION</span>: You are almost out of <span class="green">NEWBIE</span> protection.';
106
			} else {
107
				$protectionMessage = '<span class="blue">PROTECTION</span>: You are under <span class="green">NEWBIE</span> protection.';
108
			}
109
		} elseif ($player->hasFederalProtection()) {
110
			$protectionMessage = '<span class="blue">PROTECTION</span>: You are under <span class="blue">FEDERAL</span> protection.';
111
		} elseif ($sector->offersFederalProtection()) {
112
			$protectionMessage = '<span class="blue">PROTECTION</span>: You are <span class="red">NOT</span> under protection.';
113
		}
114
115
		if (!empty($protectionMessage)) {
116
			$template->assign('ProtectionMessage', $protectionMessage);
117
		}
118
119
		//enableProtectionDependantRefresh($template,$player);
120
121
		// Do we have an unseen attack message to store in this var?
122
		$dbResult = $db->read('SELECT * FROM sector_message WHERE ' . $player->getSQL());
123
		if ($dbResult->hasRecord()) {
124
			$this->attackMessage = $dbResult->record()->getString('message');
125
			$db->write('DELETE FROM sector_message WHERE ' . $player->getSQL());
126
		}
127
128
		if ($this->attackMessage !== null) {
129
			checkForAttackMessage($this->attackMessage, $player);
130
		}
131
		if ($this->showForceRefreshMessage) {
132
			$template->assign('ForceRefreshMessage', getForceRefreshMessage($player));
133
		}
134
		if ($this->missionMessage !== null) {
135
			$template->assign('MissionMessage', $this->missionMessage);
136
		}
137
		if ($this->message !== null) {
138
			$template->assign('VarMessage', bbifyMessage($this->message));
139
		}
140
141
		//error msgs take precedence
142
		if ($this->errorMessage !== null) {
143
			$template->assign('ErrorMessage', $this->errorMessage);
144
		}
145
146
		// *******************************************
147
		// *
148
		// * Trade Result
149
		// *
150
		// *******************************************
151
152
		if ($this->tradeMessage !== null) {
153
			$template->assign('TradeMessage', $this->tradeMessage);
154
		}
155
156
		// *******************************************
157
		// *
158
		// * Ports
159
		// *
160
		// *******************************************
161
162
		if ($sector->hasPort()) {
163
			$port = $sector->getPort();
164
			$template->assign('PortIsAtWar', $player->getRelation($port->getRaceID()) < RELATIONS_WAR);
165
		}
166
167
		// *******************************************
168
		// *
169
		// * Ships
170
		// *
171
		// *******************************************
172
		$otherPlayers = $sector->getOtherTraders($player);
173
		$visiblePlayers = [];
174
		$cloakedPlayers = [];
175
		foreach ($otherPlayers as $accountID => $otherPlayer) {
176
			if ($player->canSee($otherPlayer)) {
177
				$visiblePlayers[$accountID] = $otherPlayer;
178
			} else {
179
				$cloakedPlayers[$accountID] = $otherPlayer;
180
			}
181
		}
182
		$template->assign('VisiblePlayers', $visiblePlayers);
183
		$template->assign('CloakedPlayers', $cloakedPlayers);
184
		$template->assign('SectorPlayersLabel', 'Ships');
185
	}
186
187
}
188
189
190
function getForceRefreshMessage(AbstractSmrPlayer $player): string {
191
	$db = Database::getInstance();
192
	$dbResult = $db->read('SELECT refresh_at FROM sector_has_forces WHERE refresh_at > ' . $db->escapeNumber(Epoch::time()) . ' AND sector_id = ' . $db->escapeNumber($player->getSectorID()) . ' AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND refresher = ' . $db->escapeNumber($player->getAccountID()) . ' ORDER BY refresh_at DESC LIMIT 1');
193
	if ($dbResult->hasRecord()) {
194
		$remainingTime = $dbResult->record()->getInt('refresh_at') - Epoch::time();
195
		$forceRefreshMessage = '<span class="green">REFRESH</span>: All forces will be refreshed in ' . $remainingTime . ' seconds.';
196
	} else {
197
		$forceRefreshMessage = '<span class="green">REFRESH</span>: All forces have finished refreshing.';
198
	}
199
	return $forceRefreshMessage;
200
}
201
202
function checkForAttackMessage(string $msg, AbstractSmrPlayer $player): void {
203
	$contains = 0;
204
	$msg = str_replace('[ATTACK_RESULTS]', '', $msg, $contains);
205
	if ($contains > 0) {
206
		// $msg now contains only the log_id, if there is one
207
		$logID = str2int($msg);
0 ignored issues
show
Bug introduced by
The function str2int was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
		$logID = /** @scrutinizer ignore-call */ str2int($msg);
Loading history...
208
209
		$template = Template::getInstance();
210
		$db = Database::getInstance();
211
		$dbResult = $db->read('SELECT sector_id,result,type FROM combat_logs WHERE log_id=' . $db->escapeNumber($logID) . ' LIMIT 1');
212
		if ($dbResult->hasRecord()) {
213
			$dbRecord = $dbResult->record();
214
			if ($player->getSectorID() == $dbRecord->getInt('sector_id')) {
215
				$results = $dbRecord->getObject('result', true);
216
				$template->assign('AttackResultsType', $dbRecord->getString('type'));
217
				$template->assign('AttackResults', $results);
218
				$template->assign('AttackLogLink', linkCombatLog($logID));
219
			}
220
		}
221
	}
222
}
223