| Conditions | 11 |
| Paths | 13 |
| Total Lines | 89 |
| Code Lines | 57 |
| 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 |
||
| 102 | function coe_o_missile_calculate() { |
||
| 103 | // db_mysql::db_transaction_check(true); |
||
| 104 | |||
| 105 | global $lang; |
||
| 106 | |||
| 107 | $iraks = doquery("SELECT * FROM {{iraks}} WHERE `fleet_end_time` <= " . SN_TIME_NOW . " FOR UPDATE;"); |
||
| 108 | |||
| 109 | while ($fleetRow = db_fetch($iraks)) { |
||
| 110 | $sourcePlanet = DBStaticPlanet::db_planet_by_vector($fleetRow, 'fleet_start_'); |
||
| 111 | $target_planet_row = DBStaticPlanet::db_planet_by_gspt( |
||
| 112 | $fleetRow['fleet_end_galaxy'], |
||
| 113 | $fleetRow['fleet_end_system'], |
||
| 114 | $fleetRow['fleet_end_planet'], |
||
| 115 | PT_PLANET |
||
| 116 | ); |
||
| 117 | $targetUser = db_user_by_id($target_planet_row['id_owner'], true); |
||
| 118 | $rowAttacker = db_user_by_id($fleetRow['fleet_owner'], true); |
||
| 119 | |||
| 120 | db_mysql::db_transaction_start(); |
||
| 121 | set_time_limit(15); |
||
| 122 | SN::$gc->db->lockRecords([ |
||
| 123 | 'users' => [$targetUser['id'], $rowAttacker['id'],], |
||
| 124 | 'planets' => [$target_planet_row['id'], $sourcePlanet['id'],], |
||
| 125 | 'iraks' => [$fleetRow['id'],], |
||
| 126 | ]); |
||
| 127 | $fleetRow = doquery("SELECT * FROM `{{iraks}}` WHERE `id` = {$fleetRow['id']} FOR UPDATE;", '', true); |
||
| 128 | if (empty($fleetRow)) { |
||
| 129 | db_mysql::db_transaction_rollback(); |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | |||
| 133 | $db_changeset = array(); |
||
| 134 | |||
| 135 | $target_planet_row = sys_o_get_updated($targetUser['id'], $target_planet_row['id'], SN_TIME_NOW); |
||
| 136 | $target_planet_row = $target_planet_row['planet']; |
||
| 137 | |||
| 138 | |||
| 139 | if ($target_planet_row['id']) { |
||
| 140 | $planetDefense = array(); |
||
| 141 | foreach (sn_get_groups('defense_active') as $unit_id) { |
||
| 142 | $planetDefense[$unit_id] = array(mrc_get_level($targetUser, $target_planet_row, $unit_id, true, true)); |
||
| 143 | } |
||
| 144 | |||
| 145 | $message = ''; |
||
| 146 | $interceptors = mrc_get_level($targetUser, $target_planet_row, UNIT_DEF_MISSILE_INTERCEPTOR, true, true); //$target_planet_row[$interceptor_db_name]; // Number of interceptors |
||
| 147 | $missiles = $fleetRow['fleet_amount']; // Number of MIP |
||
| 148 | if ($interceptors >= $missiles) { |
||
| 149 | $message = $lang['mip_all_destroyed']; |
||
| 150 | $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit(UNIT_DEF_MISSILE_INTERCEPTOR, -$missiles, $targetUser, $target_planet_row['id']); |
||
| 151 | } else { |
||
| 152 | if ($interceptors) { |
||
| 153 | $message = sprintf($lang['mip_destroyed'], $interceptors); |
||
| 154 | $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit(UNIT_DEF_MISSILE_INTERCEPTOR, -$interceptors, $targetUser, $target_planet_row['id']); |
||
| 155 | } |
||
| 156 | |||
| 157 | $attackResult = COE_missileAttack($targetUser, $rowAttacker, $missiles - $interceptors, $planetDefense, $fleetRow['primaer']); |
||
| 158 | |||
| 159 | foreach ($attackResult['structures'] as $key => $structure) { |
||
| 160 | $destroyed = $planetDefense[$key][0] - $structure[0]; |
||
| 161 | if ($destroyed) { |
||
| 162 | $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit($key, -$destroyed, $targetUser, $target_planet_row['id']); |
||
| 163 | |||
| 164 | $message .= " {$lang['tech'][$key]} - {$destroyed} {$lang['quantity']}<br>"; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | if (!empty($message)) { |
||
| 169 | $message = $lang['mip_defense_destroyed'] . $message . "{$lang['mip_recycled']}{$lang['Metal']}: {$attackResult['metal']}, {$lang['Crystal']}: {$attackResult['crystal']}<br>"; |
||
| 170 | |||
| 171 | DBStaticPlanet::db_planet_set_by_id($target_planet_row['id'], "`metal` = `metal` + {$attackResult['metal']}, `crystal` = `crystal` + {$attackResult['crystal']}"); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | OldDbChangeSet::db_changeset_apply($db_changeset); |
||
| 175 | |||
| 176 | $fleetRow['fleet_start_type'] = PT_PLANET; |
||
| 177 | |||
| 178 | $message_vorlage = sprintf($lang['mip_body_attack'], $fleetRow['fleet_amount'], |
||
| 179 | addslashes($sourcePlanet['name']), $fleetRow['fleet_start_galaxy'], $fleetRow['fleet_start_system'], $fleetRow['fleet_start_planet'], |
||
| 180 | addslashes($target_planet_row['name']), $fleetRow['fleet_end_galaxy'], $fleetRow['fleet_end_system'], $fleetRow['fleet_end_planet']); |
||
| 181 | |||
| 182 | empty($message) ? $message = $lang['mip_no_defense'] : false; |
||
| 183 | |||
| 184 | msg_send_simple_message($fleetRow['fleet_owner'], '', SN_TIME_NOW, MSG_TYPE_SPY, $lang['mip_sender_amd'], $lang['mip_subject_amd'], $message_vorlage . $message); |
||
| 185 | msg_send_simple_message($fleetRow['fleet_target_owner'], '', SN_TIME_NOW, MSG_TYPE_SPY, $lang['mip_sender_amd'], $lang['mip_subject_amd'], $message_vorlage . $message); |
||
| 186 | } |
||
| 187 | |||
| 188 | doquery("DELETE FROM {{iraks}} WHERE id = '{$fleetRow['id']}';"); |
||
| 189 | |||
| 190 | db_mysql::db_transaction_commit(); |
||
| 191 | } |
||
| 194 |