| Conditions | 43 |
| Paths | > 20000 |
| Total Lines | 138 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 218 | function sn_ube_combat_result_apply(&$combat_data) { |
||
| 219 | // TODO: Поменять все отладки на запросы |
||
| 220 | $destination_user_id = $combat_data[UBE_FLEETS][0][UBE_OWNER]; |
||
| 221 | |||
| 222 | $outcome = &$combat_data[UBE_OUTCOME]; |
||
| 223 | $planet_info = &$outcome[UBE_PLANET]; |
||
| 224 | $planet_id = $planet_info[PLANET_ID]; |
||
| 225 | // Обновляем поле обломков на планете |
||
| 226 | if (!$combat_data[UBE_OPTIONS][UBE_COMBAT_ADMIN] && !empty($outcome[UBE_DEBRIS])) { |
||
| 227 | DBStaticPlanet::db_planet_set_by_gspt($planet_info[PLANET_GALAXY], $planet_info[PLANET_SYSTEM], $planet_info[PLANET_PLANET], PT_PLANET, |
||
| 228 | "`debris_metal` = `debris_metal` + " . floor($outcome[UBE_DEBRIS][RES_METAL]) . ", `debris_crystal` = `debris_crystal` + " . floor($outcome[UBE_DEBRIS][RES_CRYSTAL]) |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | |||
| 232 | $db_save = array( |
||
| 233 | UBE_FLEET_GROUP => array(), // Для САБов |
||
| 234 | ); |
||
| 235 | |||
| 236 | $fleets_outcome = &$outcome[UBE_FLEETS]; |
||
| 237 | foreach ($combat_data[UBE_FLEETS] as $fleet_id => &$fleet_info) { |
||
| 238 | if ($fleet_info[UBE_FLEET_GROUP]) { |
||
| 239 | $db_save[UBE_FLEET_GROUP][$fleet_info[UBE_FLEET_GROUP]] = $fleet_info[UBE_FLEET_GROUP]; |
||
| 240 | } |
||
| 241 | |||
| 242 | $fleet_info[UBE_COUNT] = $fleet_info[UBE_COUNT] ? $fleet_info[UBE_COUNT] : array(); |
||
| 243 | $fleets_outcome[$fleet_id][UBE_UNITS_LOST] = $fleets_outcome[$fleet_id][UBE_UNITS_LOST] ? $fleets_outcome[$fleet_id][UBE_UNITS_LOST] : array(); |
||
| 244 | |||
| 245 | $fleet_query = array(); |
||
| 246 | $db_changeset = array(); |
||
| 247 | $old_fleet_count = array_sum($fleet_info[UBE_COUNT]); |
||
| 248 | $new_fleet_count = $old_fleet_count - array_sum($fleets_outcome[$fleet_id][UBE_UNITS_LOST]); |
||
| 249 | // Перебираем юниты если во время боя количество юнитов изменилось и при этом во флоту остались юниты или это планета |
||
| 250 | if ($new_fleet_count != $old_fleet_count && (!$fleet_id || $new_fleet_count)) { |
||
| 251 | // Просматриваем результаты изменения флотов |
||
| 252 | foreach ($fleet_info[UBE_COUNT] as $unit_id => $unit_count) { |
||
| 253 | // Перебираем аутком на случай восстановления юнитов |
||
| 254 | $units_lost = (float)$fleets_outcome[$fleet_id][UBE_UNITS_LOST][$unit_id]; |
||
| 255 | |||
| 256 | $units_left = $unit_count - $units_lost; |
||
| 257 | if ($fleet_id) { |
||
| 258 | // Не планета - всегда сразу записываем строку итогов флота |
||
| 259 | $fleet_query[$unit_id] = "{$unit_id},{$units_left}"; |
||
| 260 | } elseif ($units_lost) { |
||
| 261 | // Планета - записываем в ИД юнита его потери только если есть потери |
||
| 262 | $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit($unit_id, -$units_lost, $combat_data[UBE_PLAYERS][$destination_user_id][UBE_PLAYER_DATA], $planet_id); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | if ($fleet_id) { |
||
| 267 | // Для флотов перегенерируем массив как одно вхождение в SET SQL-запроса |
||
| 268 | $fleet_query = array( |
||
| 269 | 'fleet_array' => implode(';', $fleet_query), |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | $fleet_delta = array(); |
||
| 275 | // Если во флоте остались юниты или это планета - генерируем изменение ресурсов |
||
| 276 | if ($new_fleet_count || !$fleet_id) { |
||
| 277 | foreach (sn_get_groups('resources_loot') as $resource_id) { |
||
| 278 | $resource_change = (float)$fleets_outcome[$fleet_id][UBE_RESOURCES_LOOTED][$resource_id] + (float)$fleets_outcome[$fleet_id][UBE_CARGO_DROPPED][$resource_id]; |
||
| 279 | if ($resource_change) { |
||
| 280 | $resource_db_name = ($fleet_id ? 'fleet_resource_' : '') . pname_resource_name($resource_id); |
||
| 281 | $fleet_delta[$resource_db_name] = -($resource_change); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | if ($fleet_id && $new_fleet_count) { |
||
| 287 | // Если защитник и не РМФ - отправляем флот назад |
||
| 288 | if (($fleet_info[UBE_FLEET_TYPE] == UBE_DEFENDERS && !$outcome[UBE_SFR]) || $fleet_info[UBE_FLEET_TYPE] == UBE_ATTACKERS) { |
||
| 289 | $fleet_query['fleet_mess'] = 1; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | if ($fleet_id) // Не планета |
||
| 294 | { |
||
| 295 | if ($fleet_info[UBE_FLEET_TYPE] == UBE_ATTACKERS && $outcome[UBE_MOON_REAPERS] == UBE_MOON_REAPERS_DIED) { |
||
| 296 | $new_fleet_count = 0; |
||
| 297 | } |
||
| 298 | |||
| 299 | if ($new_fleet_count) { |
||
| 300 | if (!empty($fleet_query) || !empty($fleet_delta)) { |
||
| 301 | $fleet_query['fleet_amount'] = $new_fleet_count; |
||
| 302 | fleet_update_set($fleet_id, $fleet_query, $fleet_delta); |
||
| 303 | } |
||
| 304 | } else { |
||
| 305 | // Удаляем пустые флоты |
||
| 306 | db_fleet_delete($fleet_id); |
||
| 307 | DBStaticUnit::db_unit_list_delete(0, LOC_FLEET, $fleet_id, 0); |
||
| 308 | } |
||
| 309 | } else // Планета |
||
| 310 | { |
||
| 311 | // Сохраняем изменения ресурсов - если они есть |
||
| 312 | if (!empty($fleet_delta)) { |
||
| 313 | $temp = array(); |
||
| 314 | foreach ($fleet_delta as $resource_db_name => $resource_amount) { |
||
| 315 | $temp[] = "`{$resource_db_name}` = `{$resource_db_name}` + ({$resource_amount})"; |
||
| 316 | } |
||
| 317 | DBStaticPlanet::db_planet_set_by_id($planet_id, implode(',', $temp)); |
||
| 318 | } |
||
| 319 | if (!empty($db_changeset)) // Сохраняем изменения юнитов на планете - если они есть |
||
| 320 | { |
||
| 321 | OldDbChangeSet::db_changeset_apply($db_changeset); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | // TODO: Связать сабы с флотами констраинтами ON DELETE SET NULL |
||
| 327 | if (!empty($db_save[UBE_FLEET_GROUP])) { |
||
| 328 | $db_save[UBE_FLEET_GROUP] = implode(',', $db_save[UBE_FLEET_GROUP]); |
||
| 329 | doquery("DELETE FROM {{aks}} WHERE `id` IN ({$db_save[UBE_FLEET_GROUP]})"); |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($outcome[UBE_MOON] == UBE_MOON_CREATE_SUCCESS) { |
||
| 333 | $moon_row = uni_create_moon($planet_info[PLANET_GALAXY], $planet_info[PLANET_SYSTEM], $planet_info[PLANET_PLANET], $destination_user_id, $outcome[UBE_MOON_SIZE], '', false); |
||
| 334 | $outcome[UBE_MOON_NAME] = $moon_row['name']; |
||
| 335 | unset($moon_row); |
||
| 336 | } elseif ($outcome[UBE_MOON] == UBE_MOON_DESTROY_SUCCESS) { |
||
| 337 | DBStaticPlanet::db_planet_delete_by_id($planet_id); |
||
| 338 | } |
||
| 339 | |||
| 340 | { |
||
| 341 | $bashing_list = array(); |
||
| 342 | foreach ($combat_data[UBE_PLAYERS] as $player_id => $player_info) { |
||
| 343 | if ($player_info[UBE_ATTACKER]) { |
||
| 344 | if ($outcome[UBE_MOON] != UBE_MOON_DESTROY_SUCCESS) { |
||
| 345 | $bashing_list[] = "({$player_id}, {$planet_id}, {$combat_data[UBE_TIME]})"; |
||
| 346 | } |
||
| 347 | if ($combat_data[UBE_OPTIONS][UBE_MISSION_TYPE] == MT_ATTACK && $combat_data[UBE_OPTIONS][UBE_DEFENDER_ACTIVE]) { |
||
| 348 | $str_loose_or_win = $outcome[UBE_COMBAT_RESULT] == UBE_COMBAT_RESULT_WIN ? 'raidswin' : 'raidsloose'; |
||
| 349 | db_user_set_by_id($player_id, "`xpraid` = `xpraid` + 1, `raids` = `raids` + 1, `{$str_loose_or_win}` = `{$str_loose_or_win}` + 1"); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | $bashing_list = implode(',', $bashing_list); |
||
| 354 | if ($bashing_list) { |
||
| 355 | doquery("INSERT INTO {{bashing}} (bashing_user_id, bashing_planet_id, bashing_time) VALUES {$bashing_list};"); |
||
| 356 | } |
||
| 359 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.