Total Complexity | 42 |
Total Lines | 243 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like FleetDispatchEvent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FleetDispatchEvent, and based on these observations, apply Extract Interface, too.
1 | <?php /** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection, PhpCastIsUnnecessaryInspection, PhpDeprecationInspection */ |
||
26 | class FleetDispatchEvent { |
||
27 | const IS_ATTACK = 'isAttack'; |
||
28 | const IS_TRANSPORT = 'isTransport'; |
||
29 | |||
30 | const F_FLEET_ID = 'fleet_id'; |
||
31 | const F_FLEET_OWNER_ID = 'fleet_owner'; |
||
32 | const F_PLANET_ID = 'id'; |
||
33 | const F_PLANET_OWNER_ID = 'id_owner'; |
||
34 | const IS_LOCK_SOURCE = 'isLockSource'; |
||
35 | |||
36 | /** @var array[] $sn_groups_mission */ |
||
37 | public static $sn_groups_mission = []; |
||
38 | /** @var int[] $userIdsToLock */ |
||
39 | public $userIdsToLock = []; |
||
40 | /** @var int[] $planetIdsToLock */ |
||
41 | public $planetIdsToLock = []; |
||
42 | /** @var int[] $fleetIdsToLock */ |
||
43 | public $fleetIdsToLock = []; |
||
44 | |||
45 | public static $MISSIONS = [ |
||
46 | MT_ATTACK => [self::IS_TRANSPORT => false, self::IS_ATTACK => true,], |
||
47 | MT_AKS => [self::IS_TRANSPORT => false, self::IS_ATTACK => true,], |
||
48 | MT_DESTROY => [self::IS_TRANSPORT => false, self::IS_ATTACK => true,], |
||
49 | MT_HOLD => [self::IS_TRANSPORT => false,], |
||
50 | MT_SPY => [self::IS_TRANSPORT => false, self::IS_LOCK_SOURCE => true, 'AJAX' => true,], |
||
51 | MT_TRANSPORT => [self::IS_TRANSPORT => true, self::IS_LOCK_SOURCE => true,], |
||
52 | MT_RELOCATE => [self::IS_TRANSPORT => true, self::IS_LOCK_SOURCE => true,], |
||
53 | MT_RECYCLE => [self::IS_TRANSPORT => false, 'AJAX' => true,], |
||
54 | MT_EXPLORE => [self::IS_TRANSPORT => false,], |
||
55 | MT_COLONIZE => [self::IS_TRANSPORT => true,], |
||
56 | MT_MISSILE => [self::IS_TRANSPORT => false, 'AJAX' => true,], |
||
57 | ]; |
||
58 | |||
59 | public function __construct($fleetRow, $eventType) { |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * LOCK - Lock all records which can be used with mission |
||
143 | * |
||
144 | * @return array|bool|mysqli_result|null |
||
145 | */ |
||
146 | public function lockEventRecords() { |
||
147 | // $query = []; |
||
148 | $locks = []; |
||
149 | |||
150 | if (!empty($this->userIdsToLock)) { |
||
151 | $locks['users'] = $this->userIdsToLock; |
||
152 | // /** @noinspection SqlResolve */ |
||
153 | // $query[] = "SELECT 1 FROM `{{users}}` WHERE `id` IN (" . implode(',', $this->userIdsToLock) . ") FOR UPDATE"; |
||
154 | } |
||
155 | if (!empty($this->planetIdsToLock)) { |
||
156 | $locks['planets'] = $this->planetIdsToLock; |
||
157 | // /** @noinspection SqlResolve */ |
||
158 | // $query[] = "SELECT 1 FROM `{{planets}}` WHERE `id` IN (" . implode(',', $this->planetIdsToLock) . ") FOR UPDATE"; |
||
159 | } |
||
160 | if (!empty($this->fleetIdsToLock)) { |
||
161 | $locks['fleets'] = $this->fleetIdsToLock; |
||
162 | // /** @noinspection SqlResolve */ |
||
163 | // $query[] = "SELECT 1 FROM `{{fleets}}` WHERE `fleet_id` IN (" . implode(',', $this->fleetIdsToLock) . ") FOR UPDATE"; |
||
164 | } |
||
165 | |||
166 | // Really - no checks here. We should lock at least flying fleet and fleet owner |
||
167 | |||
168 | // return doquery(implode(' UNION ', $query)); |
||
169 | return SN::$gc->db->lockRecords($locks); |
||
170 | } |
||
171 | |||
172 | public static function sortEvents(&$eventList) { |
||
173 | uasort($eventList, function (FleetDispatchEvent $a, FleetDispatchEvent $b) { |
||
174 | return |
||
175 | // Сравниваем время флотов - кто раньше, тот и первый обрабатывается |
||
176 | $a->eventTimeStamp > $b->eventTimeStamp ? 1 : ($a->eventTimeStamp < $b->eventTimeStamp ? -1 : |
||
177 | // Если время - одинаковое, сравниваем события флотов |
||
178 | // Если события - одинаковые, то флоты равны |
||
179 | ($a->event == $b->event ? 0 : |
||
180 | // Если события разные - первыми считаем прибывающие флоты |
||
181 | ($a->event == EVENT_FLT_ARRIVE ? 1 : ($b->event == EVENT_FLT_ARRIVE ? -1 : |
||
182 | // Если нет прибывающих флотов - дальше считаем флоты, которые закончили миссию |
||
183 | ($a->event == EVENT_FLT_ACCOMPLISH ? 1 : ($b->event == EVENT_FLT_ACCOMPLISH ? -1 : |
||
184 | // Если нет флотов, закончивших задание - остались возвращающиеся флоты, которые равны между собой |
||
185 | // TODO: Добавить еще проверку по ID флота и/или времени запуска - что бы обсчитывать их в порядке запуска |
||
186 | ( |
||
187 | 0 // Вообще сюда доходить не должно - будет отсекаться на равенстве событий |
||
188 | ) |
||
189 | )) |
||
190 | )) |
||
191 | ) |
||
192 | ); |
||
193 | }); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | */ |
||
198 | public function refreshMissionData() { |
||
199 | if (!empty($this->srcPlanetId) && !empty($this->srcPlanetOwnerId)) { |
||
200 | // $this->srcPlanetRow = DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_start_'); |
||
201 | $updateResult = sys_o_get_updated($this->srcPlanetOwnerId, $this->srcPlanetId, $this->eventTimeStamp); |
||
202 | |||
203 | $this->updateSrcPlanetRow($updateResult['planet']); |
||
204 | } |
||
205 | |||
206 | if (!empty($this->dstPlanetId) && !empty($this->dstPlanetOwnerId)) { |
||
207 | $updateResult = sys_o_get_updated($this->dstPlanetOwnerId, $this->dstPlanetId, $this->eventTimeStamp); |
||
208 | |||
209 | $this->updateDstPlanetRow($updateResult['planet']); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @return array|false |
||
215 | */ |
||
216 | public function refreshFleet() { |
||
217 | $this->fleet = DbFleetStatic::db_fleet_get($this->fleet[self::F_FLEET_ID]); |
||
218 | |||
219 | return $this->fleet; |
||
220 | } |
||
221 | |||
222 | public function getSrcPlanetRowFromFleet() { |
||
223 | $this->srcPlanetRow = DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_start_'); |
||
224 | |||
225 | $this->updateSrcPlanetRow($this->srcPlanetRow); |
||
226 | |||
227 | return $this->srcPlanetRow; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param array $srcPlanetRow |
||
232 | * |
||
233 | * @return array|bool |
||
234 | */ |
||
235 | public function updateSrcPlanetRow($srcPlanetRow) { |
||
236 | // $this->srcPlanetRow = is_array($srcPlanetRow) ? $srcPlanetRow : DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_start_'); |
||
237 | $this->srcPlanetRow = $srcPlanetRow; |
||
238 | |||
239 | $this->srcPlanetId = !empty($this->srcPlanetRow[self::F_PLANET_ID]) ? (int)$this->srcPlanetRow[self::F_PLANET_ID] : 0; |
||
240 | // Starting planet can change owner while fleet mission - and even change planet ID |
||
241 | // It can happen due to teleport shenanigans or because of planet capturing (in certain game modes) |
||
242 | $this->srcPlanetOwnerId = !empty($this->srcPlanetRow[self::F_PLANET_OWNER_ID]) ? (int)$this->srcPlanetRow[self::F_PLANET_OWNER_ID] : $this->srcPlanetOwnerId; |
||
243 | |||
244 | return $this->srcPlanetRow; |
||
245 | } |
||
246 | |||
247 | |||
248 | public function getDstPlanetRowFromFleet() { |
||
249 | $this->dstPlanetRow = DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_end_'); |
||
250 | |||
251 | $this->updateDstPlanetRow($this->dstPlanetRow); |
||
252 | |||
253 | return $this->dstPlanetRow; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param ?array $dstPlanetRow |
||
258 | * |
||
259 | * @return array|bool|null |
||
260 | */ |
||
261 | public function updateDstPlanetRow($dstPlanetRow = null) { |
||
269 | } |
||
270 | |||
271 | } |
||
272 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..