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 — master (#1088)
by Dan
06:55
created

SmrShipType::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 12
rs 9.9666
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * Defines the base ship types
5
 */
6
class SmrShipType {
7
	use Traits\RaceID;
8
9
	private static array $CACHE_SHIP_TYPES = [];
10
11
	private string $name;
12
	private int $typeID;
13
	private int $classID;
14
	private int $hardpoints;
15
	private int $speed;
16
	private int $cost;
17
	private int $restriction;
18
	private int $levelNeeded;
19
20
	private int $maxPower = 0;
21
	private array $maxHardware = [];
22
	private int $baseManeuverability;
23
24
	public static function get(int $shipTypeID, Smr\Database $db = null) : self {
25
		if (!isset(self::$CACHE_SHIP_TYPES[$shipTypeID])) {
26
			if ($db === null) {
27
				$db = Smr\Database::getInstance();
28
				$db->query('SELECT * FROM ship_type WHERE ship_type_id = ' . $db->escapeNumber($shipTypeID));
29
				$db->requireRecord();
30
			} elseif ($shipTypeID !== $db->getInt('ship_type_id')) {
31
				throw new Exception('Database result mismatch');
32
			}
33
			self::$CACHE_SHIP_TYPES[$shipTypeID] = new self($db);
34
		}
35
		return self::$CACHE_SHIP_TYPES[$shipTypeID];
36
	}
37
38
	public static function getAll() : array {
39
		$db = Smr\Database::getInstance();
40
		$db->query('SELECT * FROM ship_type ORDER BY ship_type_id ASC');
41
		while ($db->nextRecord()) {
42
			// populate the cache
43
			self::get($db->getInt('ship_type_id'), $db);
44
		}
45
		return self::$CACHE_SHIP_TYPES;
46
	}
47
48
	protected function __construct(Smr\Database $db) {
49
		$this->name = $db->getField('ship_name');
50
		$this->typeID = $db->getInt('ship_type_id');
51
		$this->classID = $db->getInt('ship_class_id');
52
		$this->raceID = $db->getInt('race_id');
53
		$this->hardpoints = $db->getInt('hardpoint');
54
		$this->speed = $db->getInt('speed');
55
		$this->cost = $db->getInt('cost');
56
		$this->restriction = $db->getInt('buyer_restriction');
57
		$this->levelNeeded = $db->getInt('lvl_needed');
58
59
		$maxPower = 0;
60
		switch ($this->hardpoints) {
61
			default:
62
				$maxPower += 1 * $this->hardpoints - 10;
63
			case 10:
64
				$maxPower += 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
65
			case 9:
66
				$maxPower += 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
67
			case 8:
68
				$maxPower += 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
69
			case 7:
70
				$maxPower += 2;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
71
			case 6:
72
				$maxPower += 3;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
73
			case 5:
74
				$maxPower += 3;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
75
			case 4:
76
				$maxPower += 3;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
77
			case 3:
78
				$maxPower += 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
79
			case 2:
80
				$maxPower += 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
81
			case 1:
82
				$maxPower += 5;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
83
			case 0:
84
				$maxPower += 0;
85
		}
86
		$this->maxPower = $maxPower;
87
88
89
		// get supported hardware from db
90
		$db2 = Smr\Database::getInstance();
91
		$db2->query('SELECT hardware_type_id, max_amount FROM ship_type_support_hardware ' .
92
			'WHERE ship_type_id = ' . $db2->escapeNumber($this->typeID) . ' ORDER BY hardware_type_id');
93
94
		while ($db2->nextRecord()) {
95
			// adding hardware to array
96
			$this->maxHardware[$db2->getInt('hardware_type_id')] = $db2->getInt('max_amount');
97
		}
98
99
		$this->baseManeuverability = IRound(
100
								700 -
101
								(
102
									(
103
										$this->maxHardware[HARDWARE_SHIELDS]
104
										+ $this->maxHardware[HARDWARE_ARMOUR]
105
										+ $this->maxHardware[HARDWARE_COMBAT] * 3
106
									) / 25
107
									+ $this->maxHardware[HARDWARE_CARGO] / 100
108
									- $this->speed * 5
109
									+ $this->hardpoints * 5
110
									+ $this->maxHardware[HARDWARE_COMBAT] / 5
111
								)
112
							);
113
	}
114
115
	public function getTypeID() : int {
116
		return $this->typeID;
117
	}
118
119
	public function getClassID() : int {
120
		return $this->classID;
121
	}
122
123
	public function getName() : string {
124
		return $this->name;
125
	}
126
127
	public function getCost() : int {
128
		return $this->cost;
129
	}
130
131
	public function getRestriction() : int {
132
		return $this->restriction;
133
	}
134
135
	/**
136
	 * Return the base ship speed (unmodified by the game speed)
137
	 */
138
	public function getSpeed() : int {
139
		return $this->speed;
140
	}
141
142
	public function getHardpoints() : int {
143
		return $this->hardpoints;
144
	}
145
146
	/**
147
	 * Return the maximum weapon power
148
	 */
149
	public function getMaxPower() : int {
150
		return $this->maxPower;
151
	}
152
153
	public function getMaxHardware(int $hardwareTypeID) : int {
154
		return $this->maxHardware[$hardwareTypeID];
155
	}
156
157
	public function getAllMaxHardware() : array {
158
		return $this->maxHardware;
159
	}
160
161
	public function canHaveJump() : bool {
162
		return $this->getMaxHardware(HARDWARE_JUMP) > 0;
163
	}
164
165
	public function canHaveDCS() : bool {
166
		return $this->getMaxHardware(HARDWARE_DCS) > 0;
167
	}
168
169
	public function canHaveScanner() : bool {
170
		return $this->getMaxHardware(HARDWARE_SCANNER) > 0;
171
	}
172
173
	public function canHaveCloak() : bool {
174
		return $this->getMaxHardware(HARDWARE_CLOAK) > 0;
175
	}
176
177
	public function canHaveIllusion() : bool {
178
		return $this->getMaxHardware(HARDWARE_ILLUSION) > 0;
179
	}
180
181
}
182