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 — master ( 4cbdc0...b86122 )
by Dan
27s queued 15s
created

SmrShip::updateIllusion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * Adds a database layer to an AbstractSmrShip instance.
5
 * Loads and saves ship properties from/to the database.
6
 */
7
class SmrShip extends AbstractSmrShip {
8
	protected static array $CACHE_SHIPS = [];
9
10
	protected string $SQL;
11
12
	public static function refreshCache() : void {
13
		foreach (self::$CACHE_SHIPS as &$gameShips) {
14
			foreach ($gameShips as &$ship) {
15
				$ship = self::getShip($ship->getPlayer(), true);
16
			}
17
		}
18
	}
19
20
	public static function clearCache() : void {
21
		self::$CACHE_SHIPS = array();
22
	}
23
24
	public static function saveShips() : void {
25
		foreach (self::$CACHE_SHIPS as $gameShips) {
26
			foreach ($gameShips as $ship) {
27
				$ship->update();
28
			}
29
		}
30
	}
31
32
	public static function getShip(AbstractSmrPlayer $player, bool $forceUpdate = false) : self {
33
		if ($forceUpdate || !isset(self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()])) {
34
			$s = new SmrShip($player);
35
			self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()] = $s;
36
		}
37
		return self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()];
38
	}
39
40
	protected function __construct(AbstractSmrPlayer $player) {
41
		parent::__construct($player);
42
		$db = MySqlDatabase::getInstance();
43
		$this->SQL = 'account_id=' . $db->escapeNumber($this->getAccountID()) . ' AND game_id=' . $db->escapeNumber($this->getGameID());
44
45
		$this->loadHardware();
46
		$this->loadWeapons();
47
		$this->loadCargo();
48
		$this->loadCloak();
49
		$this->loadIllusion();
50
	}
51
52
	public function update() : void {
53
		$this->updateHardware();
54
		$this->updateWeapons();
55
		$this->updateCargo();
56
		$this->updateCloak();
57
		$this->updateIllusion();
58
		// note: SmrShip::setShipTypeID updates the SmrPlayer only
59
		$this->getPlayer()->update();
60
	}
61
62
	/**
63
	 * Initialize the weapons onboard this ship.
64
	 */
65
	protected function loadWeapons() : void {
66
		// determine weapon
67
		$db = MySqlDatabase::getInstance();
68
		$db->query('SELECT * FROM ship_has_weapon JOIN weapon_type USING (weapon_type_id)
69
							WHERE ' . $this->SQL . '
70
							ORDER BY order_id LIMIT ' . $db->escapeNumber($this->getHardpoints()));
71
72
		$this->weapons = array();
73
		// generate list of weapon names the user transports
74
		while ($db->nextRecord()) {
75
			$weaponTypeID = $db->getInt('weapon_type_id');
76
			$orderID = $db->getInt('order_id');
77
			$weapon = SmrWeapon::getWeapon($weaponTypeID, $db);
78
			$weapon->setBonusAccuracy($db->getBoolean('bonus_accuracy'));
79
			$weapon->setBonusDamage($db->getBoolean('bonus_damage'));
80
			$this->weapons[$orderID] = $weapon;
81
		}
82
		$this->checkForExcessWeapons();
83
	}
84
85
	protected function loadHardware() : void {
86
		$this->hardware = array();
87
		$this->oldHardware = array();
88
89
		// get currently hardware from db
90
		$db = MySqlDatabase::getInstance();
91
		$db->query('SELECT *
92
							FROM ship_has_hardware
93
							JOIN hardware_type USING(hardware_type_id)
94
							WHERE ' . $this->SQL);
95
96
		while ($db->nextRecord()) {
97
			$hardwareTypeID = $db->getInt('hardware_type_id');
98
99
			// adding hardware to array
100
			$this->hardware[$hardwareTypeID] = $db->getInt('amount');
101
			$this->oldHardware[$hardwareTypeID] = $db->getInt('old_amount');
102
		}
103
		$this->checkForExcessHardware();
104
	}
105
106
	protected function loadCargo() : void {
107
		// initialize cargo array
108
		$this->cargo = array();
109
110
		// get cargo from db
111
		$db = MySqlDatabase::getInstance();
112
		$db->query('SELECT * FROM ship_has_cargo WHERE ' . $this->SQL);
113
		while ($db->nextRecord()) {
114
			// adding cargo and amount to array
115
			$this->cargo[$db->getInt('good_id')] = $db->getInt('amount');
116
		}
117
		$this->checkForExcessCargo();
118
	}
119
120
	public function updateCargo() : void {
121
		if ($this->hasChangedCargo === false) {
122
			return;
123
		}
124
		// write cargo info
125
		$db = MySqlDatabase::getInstance();
126
		foreach ($this->getCargo() as $id => $amount) {
127
			if ($amount > 0) {
128
				$db->query('REPLACE INTO ship_has_cargo (account_id, game_id, good_id, amount) VALUES(' . $db->escapeNumber($this->getAccountID()) . ', ' . $db->escapeNumber($this->getGameID()) . ', ' . $db->escapeNumber($id) . ', ' . $db->escapeNumber($amount) . ')');
129
			} else {
130
				$db->query('DELETE FROM ship_has_cargo WHERE ' . $this->SQL . ' AND good_id = ' . $db->escapeNumber($id) . ' LIMIT 1');
131
				// Unset now to omit displaying this good with 0 amount
132
				// before the next page is loaded.
133
				unset($this->cargo[$id]);
134
			}
135
		}
136
		$this->hasChangedCargo = false;
137
	}
138
139
	public function updateHardware() : void {
140
		// write hardware info only for hardware that has changed
141
		$db = MySqlDatabase::getInstance();
142
		foreach ($this->hasChangedHardware as $hardwareTypeID => $hasChanged) {
143
			if ($hasChanged === false) {
144
				continue;
145
			}
146
			$amount = $this->getHardware($hardwareTypeID);
147
			if ($amount > 0) {
148
				$db->query('REPLACE INTO ship_has_hardware (account_id, game_id, hardware_type_id, amount, old_amount) VALUES(' . $db->escapeNumber($this->getAccountID()) . ', ' . $db->escapeNumber($this->getGameID()) . ', ' . $db->escapeNumber($hardwareTypeID) . ', ' . $db->escapeNumber($amount) . ', ' . $db->escapeNumber($this->getOldHardware($hardwareTypeID)) . ')');
149
			} else {
150
				$db->query('DELETE FROM ship_has_hardware WHERE ' . $this->SQL . ' AND hardware_type_id = ' . $db->escapeNumber($hardwareTypeID));
151
			}
152
		}
153
		$this->hasChangedHardware = array();
154
	}
155
156
	private function updateWeapons() : void {
157
		if ($this->hasChangedWeapons === false) {
158
			return;
159
		}
160
		// write weapon info
161
		$db = MySqlDatabase::getInstance();
162
		$db->query('DELETE FROM ship_has_weapon WHERE ' . $this->SQL);
163
		foreach ($this->weapons as $orderID => $weapon) {
164
			$db->query('INSERT INTO ship_has_weapon (account_id, game_id, order_id, weapon_type_id, bonus_accuracy, bonus_damage)
165
							VALUES(' . $db->escapeNumber($this->getAccountID()) . ', ' . $db->escapeNumber($this->getGameID()) . ', ' . $db->escapeNumber($orderID) . ', ' . $db->escapeNumber($weapon->getWeaponTypeID()) . ', ' . $db->escapeBoolean($weapon->hasBonusAccuracy()) . ', ' . $db->escapeBoolean($weapon->hasBonusDamage()) . ')');
166
		}
167
		$this->hasChangedWeapons = false;
168
	}
169
170
	public function loadCloak() : void {
171
		$this->isCloaked = false;
172
		if ($this->hasCloak() === false) {
173
			return;
174
		}
175
		$db = MySqlDatabase::getInstance();
176
		$db->query('SELECT 1 FROM ship_is_cloaked WHERE ' . $this->SQL . ' LIMIT 1');
177
		$this->isCloaked = $db->getNumRows() > 0;
178
	}
179
180
	public function updateCloak() : void {
181
		if ($this->hasChangedCloak === false) {
182
			return;
183
		}
184
		$db = MySqlDatabase::getInstance();
185
		if ($this->isCloaked === false) {
186
			$db->query('DELETE FROM ship_is_cloaked WHERE ' . $this->SQL . ' LIMIT 1');
187
		} else {
188
			$db->query('INSERT INTO ship_is_cloaked VALUES(' . $db->escapeNumber($this->getAccountID()) . ', ' . $db->escapeNumber($this->getGameID()) . ')');
189
		}
190
		$this->hasChangedCloak = false;
191
	}
192
193
	public function loadIllusion() : void {
194
		$this->illusionShip = false;
195
		if ($this->hasIllusion() === false) {
196
			return;
197
		}
198
		$db = MySqlDatabase::getInstance();
199
		$db->query('SELECT * FROM ship_has_illusion WHERE ' . $this->SQL . ' LIMIT 1');
200
		if ($db->nextRecord()) {
201
			$this->illusionShip = [
202
				'ID' => $db->getInt('ship_type_id'),
203
				'Attack' => $db->getInt('attack'),
204
				'Defense' => $db->getInt('defense'),
205
			];
206
		}
207
	}
208
209
	public function updateIllusion() : void {
210
		if ($this->hasChangedIllusion === false) {
211
			return;
212
		}
213
		$db = MySqlDatabase::getInstance();
214
		if ($this->illusionShip === false) {
215
			$db->query('DELETE FROM ship_has_illusion WHERE ' . $this->SQL . ' LIMIT 1');
216
		} else {
217
			$db->query('REPLACE INTO ship_has_illusion VALUES(' . $db->escapeNumber($this->getAccountID()) . ', ' . $db->escapeNumber($this->getGameID()) . ', ' . $db->escapeNumber($this->illusionShip['ID']) . ', ' . $db->escapeNumber($this->illusionShip['Attack']) . ', ' . $db->escapeNumber($this->illusionShip['Defense']) . ')');
218
		}
219
		$this->hasChangedIllusion = false;
220
	}
221
222
}
223