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
Push — main ( d9cfb9...10f5c7 )
by Dan
32s queued 21s
created

src/pages/Player/SectorJumpProcessor.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Player;
4
5
use AbstractSmrPlayer;
6
use Globals;
7
use Plotter;
8
use Smr\MovementType;
9
use Smr\Page\PlayerPageProcessor;
10
use Smr\Request;
11
use Smr\SectorLock;
12
use SmrSector;
13
14
class SectorJumpProcessor extends PlayerPageProcessor {
15
16
	public function __construct(
17
		private readonly ?int $targetSectorID = null
18
	) {}
19
20
	public function build(AbstractSmrPlayer $player): never {
21
		$sector = $player->getSector();
22
23
		if (!$player->getGame()->hasStarted()) {
24
			create_error('You cannot move until the game has started!');
25
		}
26
27
		$target = $this->targetSectorID ?? Request::getInt('target');
28
29
		//allow hidden players (admins that don't play) to move without pinging, hitting mines, losing turns
30
		if (in_array($player->getAccountID(), Globals::getHiddenPlayers())) {
31
			$player->setSectorID($target);
32
			$player->update();
33
			$sector->markVisited($player);
34
			(new CurrentSector())->go();
35
		}
36
37
		// you can't move while on planet
38
		if ($player->isLandedOnPlanet()) {
39
			create_error('You are on a planet! You must launch first!');
40
		}
41
42
		// if no 'target' is given we forward to plot
43
		if (empty($target)) {
44
			create_error('Where do you want to go today?');
45
		}
46
47
		if ($player->getSectorID() == $target) {
48
			create_error('Hmmmm...if ' . $player->getSectorID() . '=' . $target . ' then that means...YOU\'RE ALREADY THERE! *cough*you\'re real smart*cough*');
49
		}
50
51
		if (!SmrSector::sectorExists($player->getGameID(), $target)) {
52
			create_error('The target sector doesn\'t exist!');
53
		}
54
55
		// If the Calculate Turn Cost button was pressed
56
		if (Request::get('action', '') == 'Calculate Turn Cost') {
57
			$container = new SectorJumpCalculate($target);
58
			$container->go();
59
		}
60
61
		if ($sector->hasForces()) {
62
			foreach ($sector->getForces() as $forces) {
63
				if ($forces->hasMines() && !$player->forceNAPAlliance($forces->getOwner())) {
64
					create_error('You cannot jump when there are hostile mines in the sector!');
65
				}
66
			}
67
		}
68
69
		// create sector object for target sector
70
		$targetSector = SmrSector::getSector($player->getGameID(), $target);
71
72
		$jumpInfo = $player->getJumpInfo($targetSector);
73
		$turnsToJump = $jumpInfo['turn_cost'];
74
		$maxMisjump = $jumpInfo['max_misjump'];
75
76
		// check for turns
77
		if ($player->getTurns() < $turnsToJump) {
78
			create_error('You don\'t have enough turns for that jump!');
79
		}
80
81
		// send scout msg
82
		$sector->leavingSector($player, MovementType::Jump);
83
84
		// Move the user around
85
		// TODO: (Must be done while holding both sector locks)
86
		$misjump = rand(0, $maxMisjump);
87
		if ($misjump > 0) { // we missed the sector
88
			$paths = Plotter::findDistanceToX('Distance', $targetSector, false, null, null, $misjump);
89
90
			// Group candidate sectors by distance from the target
91
			$distances = [0 => [$targetSector->getSectorID()]]; // fallback to target
92
			foreach ($paths as $sectorID => $path) {
93
				$distances[$path->getDistance()][] = $sectorID;
94
			}
95
96
			// Try to find a valid sector, reduce misjump if none
97
			while (!isset($distances[$misjump])) {
98
				$misjump--;
99
			}
100
101
			$misjumpSector = array_rand_value($distances[$misjump]);
0 ignored issues
show
The function array_rand_value 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

101
			$misjumpSector = /** @scrutinizer ignore-call */ array_rand_value($distances[$misjump]);
Loading history...
102
			$player->setSectorID($misjumpSector);
103
			unset($distances);
104
		} else { // we hit it. exactly
105
			$player->setSectorID($targetSector->getSectorID());
106
		}
107
		$player->takeTurns($turnsToJump, $turnsToJump);
108
109
		// log action
110
		$player->log(LOG_TYPE_MOVEMENT, 'Jumps to sector: ' . $target . ' but hits: ' . $player->getSectorID());
111
112
		$player->update();
113
114
		// We need to release the lock on our old sector
115
		$lock = SectorLock::getInstance();
116
		$lock->release();
117
118
		// We need a lock on the new sector so that more than one person isn't hitting the same mines
119
		$lock->acquireForPlayer($player);
120
121
		// get new sector object
122
		$sector = $player->getSector();
123
124
		// make current sector visible to him
125
		$sector->markVisited($player);
126
127
		// send scout msg
128
		$sector->enteringSector($player, MovementType::Jump);
129
130
		// If the new sector has mines...
131
		require_once(LIB . 'Default/sector_mines.inc.php');
132
		hit_sector_mines($player);
133
134
		(new CurrentSector())->go();
135
	}
136
137
}
138