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

Passed
Push — master ( a6a1be...553b87 )
by Dan
09:17
created

getInputAmount()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 12
rs 10
1
<?php declare(strict_types=1);
2
3
if ($player->getNewbieTurns() > 0) {
4
	create_error('You can\'t take/drop forces under newbie protection!');
5
}
6
7
if ($player->isLandedOnPlanet()) {
8
	create_error('You must first launch to drop forces!');
9
}
10
11
if ($sector->hasLocation()) {
12
	create_error('You can\'t drop forces in a sector with a location!');
13
}
14
15
// take either from container or request, prefer container
16
$drop_mines = Request::getVarInt('drop_mines', 0);
17
$take_mines = Request::getVarInt('take_mines', 0);
18
$drop_combat_drones = Request::getVarInt('drop_combat_drones', 0);
19
$take_combat_drones = Request::getVarInt('take_combat_drones', 0);
20
$drop_scout_drones = Request::getVarInt('drop_scout_drones', 0);
21
$take_scout_drones = Request::getVarInt('take_scout_drones', 0);
22
23
// so how many forces do we take/add per type?
24
$change_mines = $drop_mines - $take_mines;
25
$change_combat_drones = $drop_combat_drones - $take_combat_drones;
26
$change_scout_drones = $drop_scout_drones - $take_scout_drones;
27
28
$forces = SmrForce::getForce($player->getGameID(), $player->getSectorID(), $var['owner_id']);
29
30
// check max on that stack
31
$at_max = false;
32
if ($forces->getMines() + $change_mines > 50) {
33
	$change_mines = 50 - $forces->getMines();
34
	$at_max = $change_mines == 0;
35
}
36
37
if ($forces->getCDs() + $change_combat_drones > 50) {
38
	$change_combat_drones = 50 - $forces->getCDs();
39
	$at_max = $change_combat_drones == 0;
40
}
41
42
if ($forces->getSDs() + $change_scout_drones > 5) {
43
	$change_scout_drones = 5 - $forces->getSDs();
44
	$at_max = $change_scout_drones == 0;
45
}
46
47
// Check if the delta is 0 after applying the caps, in case by applying the caps we actually changed it to 0.
48
if ($change_mines == 0 && $change_combat_drones == 0 && $change_scout_drones == 0) {
49
	if ($at_max) {
50
		// If no forces added only because the stack is full
51
		create_error('This stack can\'t hold any more of those forces!');
52
	} else {
53
		// If drop == take
54
		create_error('You want to add/remove 0 forces?');
55
	}
56
}
57
58
// NOTE: we do all error checking first, because any forces we remove from
59
// the ship will vanish if we hit an error afterwards. This is because we
60
// don't update the force stack expire time until the end of this script.
61
// Force stacks without an updated expire time are automatically removed.
62
//
63
// We don't make the expire time update part of every force change internally
64
// because those functions are used to remove forces via combat (which isn't
65
// supposed to update the expire time).
66
67
if ($change_combat_drones != 0) {
68
	// we can't take more forces than are in sector
69
	if ($forces->getCDs() + $change_combat_drones < 0) {
70
		create_error('You can\'t take more combat drones than are on this stack!');
71
	}
72
73
	if ($ship->getCDs() - $change_combat_drones > $ship->getMaxCDs()) {
74
		create_error('Your ships supports no more than ' . $ship->getMaxCDs() . ' combat drones!');
75
	}
76
77
	if ($ship->getCDs() - $change_combat_drones < 0) {
78
		create_error('You can\'t drop more combat drones than you carry!');
79
	}
80
}
81
82
if ($change_scout_drones != 0) {
83
	// we can't take more forces than are in sector
84
	if ($forces->getSDs() + $change_scout_drones < 0) {
85
		create_error('You can\'t take more scout drones than are on this stack!');
86
	}
87
88
	if ($ship->getSDs() - $change_scout_drones > $ship->getMaxSDs()) {
89
		create_error('Your ships supports no more than ' . $ship->getMaxSDs() . ' scout drones!');
90
	}
91
92
	if ($ship->getSDs() - $change_scout_drones < 0) {
93
		create_error('You can\'t drop more scout drones than you carry!');
94
	}
95
}
96
97
if ($change_mines != 0) {
98
	// we can't take more forces than are in sector
99
	if ($forces->getMines() + $change_mines < 0) {
100
		create_error('You can\'t take more mines than are on this stack!');
101
	}
102
103
	if ($ship->getMines() - $change_mines > $ship->getMaxMines()) {
104
		create_error('Your ships supports no more than ' . $ship->getMaxMines() . ' mines!');
105
	}
106
107
	if ($ship->getMines() - $change_mines < 0) {
108
		create_error('You can\'t drop more mines than you carry!');
109
	}
110
}
111
112
// All error checking is done, so now update the ship/force
113
114
if ($change_combat_drones != 0) {
115
	if ($change_combat_drones > 0) {
116
		$ship->decreaseCDs($change_combat_drones, true);
117
		$forces->addCDs($change_combat_drones);
118
	} else {
119
		$ship->increaseCDs(-$change_combat_drones, true);
120
		$forces->takeCDs(-$change_combat_drones);
121
	}
122
}
123
124
if ($change_scout_drones != 0) {
125
	if ($change_scout_drones > 0) {
126
		$ship->decreaseSDs($change_scout_drones);
127
		$forces->addSDs($change_scout_drones);
128
	} else {
129
		$ship->increaseSDs(-$change_scout_drones);
130
		$forces->takeSDs(-$change_scout_drones);
131
	}
132
}
133
134
if ($change_mines != 0) {
135
	if ($change_mines > 0) {
136
		$ship->decreaseMines($change_mines);
137
		$forces->addMines($change_mines);
138
		if ($ship->isCloaked()) {
139
			$ship->decloak();
140
			$player->giveTurns(1);
141
		}
142
	} else {
143
		$ship->increaseMines(-$change_mines);
144
		$forces->takeMines(-$change_mines);
145
	}
146
}
147
148
// message to send out
149
if ($forces->getOwnerID() != $player->getAccountID() && $forces->getOwner()->isForceDropMessages()) {
150
	$mines_message = '';
151
	if ($change_mines > 0) {
152
		$mines_message = 'added ' . $change_mines . ' mine';
153
	} elseif ($change_mines < 0) {
154
		$mines_message = 'removed ' . abs($change_mines) . ' mine';
155
	}
156
	//add s to mine if necesary
157
	if (abs($change_mines) > 1) {
158
		$mines_message .= 's';
159
	}
160
161
	if ($change_combat_drones > 0) {
162
		$combat_drones_message = ($change_mines <= 0 ? 'added ' : '') . $change_combat_drones . ' combat drone';
163
	} elseif ($change_combat_drones < 0) {
164
		$combat_drones_message = ($change_mines >= 0 ? 'removed ' : '') . abs($change_combat_drones) . ' combat drone';
165
	}
166
	//add s to drone if necesary
167
	if (abs($change_combat_drones) > 1) {
168
		$combat_drones_message .= 's';
169
	}
170
171
	if ($change_scout_drones > 0) {
172
		$scout_drones_message = '';
173
		if ((isset($combat_drones_message) && $change_combat_drones < 0) || (!isset($combat_drones_message) && $change_mines <= 0)) {
174
			$scout_drones_message = 'added ';
175
		}
176
		$scout_drones_message .= $change_scout_drones . ' scout drone';
177
	} elseif ($change_scout_drones < 0) {
178
		$scout_drones_message = '';
179
		if ((isset($combat_drones_message) && $change_combat_drones > 0) || (!isset($combat_drones_message) && $change_mines >= 0)) {
180
			$scout_drones_message = 'removed ';
181
		}
182
		$scout_drones_message .= abs($change_scout_drones) . ' scout drone';
183
	}
184
	//add s to drone if necesary
185
	if (abs($change_scout_drones) > 1) {
186
		$scout_drones_message .= 's';
187
	}
188
189
	// now compile it together
190
	$message = $player->getPlayerName() . ' has ' . $mines_message;
191
192
	if (!empty($mines_message) && isset($combat_drones_message) && !isset($scout_drones_message)) {
193
		$message .= ' and ' . $combat_drones_message;
194
	} elseif (!empty($mines_message) && isset($combat_drones_message)) {
195
		$message .= ', ' . $combat_drones_message;
196
	} elseif (empty($mines_message) && isset($combat_drones_message)) {
197
		$message .= $combat_drones_message;
198
	}
199
200
	if (!empty($mines_message) && isset($combat_drones_message) && isset($scout_drones_message)) {
201
		$message .= ', and ' . $scout_drones_message;
202
	} elseif ((!empty($mines_message) || isset($combat_drones_message)) && isset($scout_drones_message)) {
203
		$message .= ' and ' . $scout_drones_message;
204
	} elseif (empty($mines_message) && !isset($combat_drones_message) && isset($scout_drones_message)) {
205
		$message .= $scout_drones_message;
206
	}
207
208
	if ($change_mines >= 0 && $change_combat_drones >= 0 && $change_scout_drones >= 0) {
209
		$message .= ' to';
210
	} elseif ($change_mines <= 0 && $change_combat_drones <= 0 && $change_scout_drones <= 0) {
211
		$message .= ' from';
212
	} else {
213
		$message .= ' from/to';
214
	}
215
216
	$message .= ' your stack in sector ' . Globals::getSectorBBLink($forces->getSectorID());
217
218
	$player->sendMessage($forces->getOwnerID(), MSG_SCOUT, $message, false);
219
}
220
221
$account->log(LOG_TYPE_FORCES, $change_combat_drones . ' combat drones, ' . $change_scout_drones . ' scout drones, ' . $change_mines . ' mines', $player->getSectorID());
222
223
$forces->updateExpire();
224
$forces->update(); // Needs to be in db to show up on CS instantly when querying sector forces
225
226
// If we dropped forces from the Local Map, stay on that page
227
if (isset($var['referrer']) && $var['referrer'] == 'map_local.php') {
228
	$body = $var['referrer'];
229
} else {
230
	$body = 'current_sector.php';
231
}
232
forward(create_container('skeleton.php', $body));
233