1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use Smr\Database; |
4
|
|
|
use Smr\ShipIllusion; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Adds a database layer to an AbstractSmrShip instance. |
8
|
|
|
* Loads and saves ship properties from/to the database. |
9
|
|
|
*/ |
10
|
|
|
class SmrShip extends AbstractSmrShip { |
11
|
|
|
|
12
|
|
|
/** @var array<int, array<int, self>> */ |
13
|
|
|
protected static array $CACHE_SHIPS = []; |
14
|
|
|
|
15
|
|
|
protected readonly string $SQL; |
16
|
|
|
|
17
|
|
|
public static function clearCache(): void { |
18
|
|
|
self::$CACHE_SHIPS = []; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function saveShips(): void { |
22
|
|
|
foreach (self::$CACHE_SHIPS as $gameShips) { |
23
|
|
|
foreach ($gameShips as $ship) { |
24
|
|
|
$ship->update(); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function getShip(AbstractSmrPlayer $player, bool $forceUpdate = false): self { |
30
|
|
|
if ($forceUpdate || !isset(self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()])) { |
31
|
|
|
$s = new self($player); |
32
|
|
|
self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()] = $s; |
33
|
|
|
} |
34
|
|
|
return self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function __construct(AbstractSmrPlayer $player) { |
38
|
|
|
parent::__construct($player); |
39
|
|
|
$db = Database::getInstance(); |
40
|
|
|
$this->SQL = 'account_id=' . $db->escapeNumber($this->getAccountID()) . ' AND game_id=' . $db->escapeNumber($this->getGameID()); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->loadHardware(); |
43
|
|
|
$this->loadWeapons(); |
44
|
|
|
$this->loadCargo(); |
45
|
|
|
$this->loadCloak(); |
46
|
|
|
$this->loadIllusion(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function update(): void { |
50
|
|
|
$this->updateHardware(); |
51
|
|
|
$this->updateWeapons(); |
52
|
|
|
$this->updateCargo(); |
53
|
|
|
$this->updateCloak(); |
54
|
|
|
$this->updateIllusion(); |
55
|
|
|
// note: SmrShip::setTypeID updates the SmrPlayer only |
56
|
|
|
$this->getPlayer()->update(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Initialize the weapons onboard this ship. |
61
|
|
|
*/ |
62
|
|
|
protected function loadWeapons(): void { |
63
|
|
|
// determine weapon |
64
|
|
|
$db = Database::getInstance(); |
65
|
|
|
$dbResult = $db->read('SELECT * FROM ship_has_weapon JOIN weapon_type USING (weapon_type_id) |
66
|
|
|
WHERE ' . $this->SQL . ' |
67
|
|
|
ORDER BY order_id LIMIT ' . $db->escapeNumber($this->getHardpoints())); |
68
|
|
|
|
69
|
|
|
$this->weapons = []; |
70
|
|
|
// generate list of weapon names the user transports |
71
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
72
|
|
|
$weaponTypeID = $dbRecord->getInt('weapon_type_id'); |
73
|
|
|
$orderID = $dbRecord->getInt('order_id'); |
74
|
|
|
$weapon = SmrWeapon::getWeapon($weaponTypeID, $dbRecord); |
75
|
|
|
$weapon->setBonusAccuracy($dbRecord->getBoolean('bonus_accuracy')); |
76
|
|
|
$weapon->setBonusDamage($dbRecord->getBoolean('bonus_damage')); |
77
|
|
|
$this->weapons[$orderID] = $weapon; |
78
|
|
|
} |
79
|
|
|
$this->checkForExcessWeapons(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function loadHardware(): void { |
83
|
|
|
$this->hardware = []; |
84
|
|
|
|
85
|
|
|
// get currently hardware from db |
86
|
|
|
$db = Database::getInstance(); |
87
|
|
|
$dbResult = $db->read('SELECT * |
88
|
|
|
FROM ship_has_hardware |
89
|
|
|
JOIN hardware_type USING(hardware_type_id) |
90
|
|
|
WHERE ' . $this->SQL); |
91
|
|
|
|
92
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
93
|
|
|
$hardwareTypeID = $dbRecord->getInt('hardware_type_id'); |
94
|
|
|
|
95
|
|
|
// adding hardware to array |
96
|
|
|
$this->hardware[$hardwareTypeID] = $dbRecord->getInt('amount'); |
97
|
|
|
} |
98
|
|
|
$this->checkForExcessHardware(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function loadCargo(): void { |
102
|
|
|
// initialize cargo array |
103
|
|
|
$this->cargo = []; |
104
|
|
|
|
105
|
|
|
// get cargo from db |
106
|
|
|
$db = Database::getInstance(); |
107
|
|
|
$dbResult = $db->read('SELECT * FROM ship_has_cargo WHERE ' . $this->SQL); |
108
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
109
|
|
|
// adding cargo and amount to array |
110
|
|
|
$this->cargo[$dbRecord->getInt('good_id')] = $dbRecord->getInt('amount'); |
111
|
|
|
} |
112
|
|
|
$this->checkForExcessCargo(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function updateCargo(): void { |
116
|
|
|
if ($this->hasChangedCargo === false) { |
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
// write cargo info |
120
|
|
|
$db = Database::getInstance(); |
121
|
|
|
foreach ($this->getCargo() as $id => $amount) { |
122
|
|
|
if ($amount > 0) { |
123
|
|
|
$db->replace('ship_has_cargo', [ |
124
|
|
|
'account_id' => $db->escapeNumber($this->getAccountID()), |
125
|
|
|
'game_id' => $db->escapeNumber($this->getGameID()), |
126
|
|
|
'good_id' => $db->escapeNumber($id), |
127
|
|
|
'amount' => $db->escapeNumber($amount), |
128
|
|
|
]); |
129
|
|
|
} else { |
130
|
|
|
$db->write('DELETE FROM ship_has_cargo WHERE ' . $this->SQL . ' AND good_id = ' . $db->escapeNumber($id)); |
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 = Database::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->replace('ship_has_hardware', [ |
149
|
|
|
'account_id' => $db->escapeNumber($this->getAccountID()), |
150
|
|
|
'game_id' => $db->escapeNumber($this->getGameID()), |
151
|
|
|
'hardware_type_id' => $db->escapeNumber($hardwareTypeID), |
152
|
|
|
'amount' => $db->escapeNumber($amount), |
153
|
|
|
]); |
154
|
|
|
} else { |
155
|
|
|
$db->write('DELETE FROM ship_has_hardware WHERE ' . $this->SQL . ' AND hardware_type_id = ' . $db->escapeNumber($hardwareTypeID)); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
$this->hasChangedHardware = []; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function updateWeapons(): void { |
162
|
|
|
if ($this->hasChangedWeapons === false) { |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
// write weapon info |
166
|
|
|
$db = Database::getInstance(); |
167
|
|
|
$db->write('DELETE FROM ship_has_weapon WHERE ' . $this->SQL); |
168
|
|
|
foreach ($this->weapons as $orderID => $weapon) { |
169
|
|
|
$db->insert('ship_has_weapon', [ |
170
|
|
|
'account_id' => $db->escapeNumber($this->getAccountID()), |
171
|
|
|
'game_id' => $db->escapeNumber($this->getGameID()), |
172
|
|
|
'order_id' => $db->escapeNumber($orderID), |
173
|
|
|
'weapon_type_id' => $db->escapeNumber($weapon->getWeaponTypeID()), |
174
|
|
|
'bonus_accuracy' => $db->escapeBoolean($weapon->hasBonusAccuracy()), |
175
|
|
|
'bonus_damage' => $db->escapeBoolean($weapon->hasBonusDamage()), |
176
|
|
|
]); |
177
|
|
|
} |
178
|
|
|
$this->hasChangedWeapons = false; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function loadCloak(): void { |
182
|
|
|
$this->isCloaked = false; |
183
|
|
|
if ($this->hasCloak() === false) { |
184
|
|
|
return; |
185
|
|
|
} |
186
|
|
|
$db = Database::getInstance(); |
187
|
|
|
$dbResult = $db->read('SELECT 1 FROM ship_is_cloaked WHERE ' . $this->SQL); |
188
|
|
|
$this->isCloaked = $dbResult->hasRecord(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function updateCloak(): void { |
192
|
|
|
if ($this->hasChangedCloak === false) { |
193
|
|
|
return; |
194
|
|
|
} |
195
|
|
|
$db = Database::getInstance(); |
196
|
|
|
if ($this->isCloaked === false) { |
197
|
|
|
$db->write('DELETE FROM ship_is_cloaked WHERE ' . $this->SQL); |
198
|
|
|
} else { |
199
|
|
|
$db->insert('ship_is_cloaked', [ |
200
|
|
|
'account_id' => $db->escapeNumber($this->getAccountID()), |
201
|
|
|
'game_id' => $db->escapeNumber($this->getGameID()), |
202
|
|
|
]); |
203
|
|
|
} |
204
|
|
|
$this->hasChangedCloak = false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function loadIllusion(): void { |
208
|
|
|
$this->illusionShip = false; |
209
|
|
|
if ($this->hasIllusion() === false) { |
210
|
|
|
return; |
211
|
|
|
} |
212
|
|
|
$db = Database::getInstance(); |
213
|
|
|
$dbResult = $db->read('SELECT * FROM ship_has_illusion WHERE ' . $this->SQL); |
214
|
|
|
if ($dbResult->hasRecord()) { |
215
|
|
|
$dbRecord = $dbResult->record(); |
216
|
|
|
$this->illusionShip = new ShipIllusion( |
217
|
|
|
shipTypeID: $dbRecord->getInt('ship_type_id'), |
218
|
|
|
attackRating: $dbRecord->getInt('attack'), |
219
|
|
|
defenseRating: $dbRecord->getInt('defense'), |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function updateIllusion(): void { |
225
|
|
|
if ($this->hasChangedIllusion === false) { |
226
|
|
|
return; |
227
|
|
|
} |
228
|
|
|
$db = Database::getInstance(); |
229
|
|
|
if ($this->illusionShip === false) { |
230
|
|
|
$db->write('DELETE FROM ship_has_illusion WHERE ' . $this->SQL); |
231
|
|
|
} else { |
232
|
|
|
$db->replace('ship_has_illusion', [ |
233
|
|
|
'account_id' => $db->escapeNumber($this->getAccountID()), |
234
|
|
|
'game_id' => $db->escapeNumber($this->getGameID()), |
235
|
|
|
'ship_type_id' => $db->escapeNumber($this->illusionShip->shipTypeID), |
236
|
|
|
'attack' => $db->escapeNumber($this->illusionShip->attackRating), |
237
|
|
|
'defense' => $db->escapeNumber($this->illusionShip->defenseRating), |
238
|
|
|
]); |
239
|
|
|
} |
240
|
|
|
$this->hasChangedIllusion = false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|