| Conditions | 61 |
| Paths | > 20000 |
| Total Lines | 277 |
| Code Lines | 163 |
| Lines | 4 |
| Ratio | 1.44 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 58 | function que_build($user, $planet, $build_mode = BUILD_CREATE, $redirect = true) { |
||
| 59 | $classLocale = classLocale::$lang; |
||
| 60 | |||
| 61 | $is_autoconvert = false; |
||
| 62 | if($build_mode == BUILD_AUTOCONVERT || sys_get_param_int('auto_convert')) { |
||
| 63 | $build_mode = BUILD_CREATE; |
||
| 64 | $is_autoconvert = true; |
||
| 65 | } |
||
| 66 | |||
| 67 | $unit_amount_qued = 0; |
||
| 68 | try { |
||
| 69 | if(!$user['id']) { |
||
| 70 | throw new exception('{Нет идентификатора пользователя - сообщите Администрации}', ERR_ERROR); // TODO EXCEPTION |
||
| 71 | } |
||
| 72 | |||
| 73 | $unit_id = sys_get_param_int('unit_id'); |
||
| 74 | /* |
||
| 75 | if(!$unit_id && is_array($unit_list = sys_get_param('fmenge'))) |
||
| 76 | { |
||
| 77 | foreach($unit_list as $unit_id => $unit_amount) if($unit_amount) break; |
||
| 78 | } |
||
| 79 | */ |
||
| 80 | if(!$unit_id) { |
||
| 81 | throw new exception('{Нет идентификатора юнита - сообщите Администрации}', ERR_ERROR); // TODO EXCEPTION |
||
| 82 | } |
||
| 83 | |||
| 84 | $que_id = que_get_unit_que($unit_id); |
||
| 85 | if(!$que_id) { |
||
| 86 | throw new exception('{Неправильный тип очереди - сообщите Администрации}', ERR_ERROR); // TODO EXCEPTION |
||
| 87 | } |
||
| 88 | |||
| 89 | if($build_mode == BUILD_DESTROY && $que_id != QUE_STRUCTURES) { |
||
| 90 | throw new exception('{Уничтожать можно только здания на планете}', ERR_ERROR); // TODO EXCEPTION |
||
| 91 | } |
||
| 92 | |||
| 93 | $que_data = sn_get_groups('ques'); |
||
| 94 | $que_data = $que_data[$que_id]; |
||
| 95 | |||
| 96 | // TODO Переделать под подочереди |
||
| 97 | if($que_id == QUE_STRUCTURES) { |
||
| 98 | $sn_groups_build_allow = sn_get_groups('build_allow'); |
||
| 99 | $que_data['unit_list'] = $sn_groups_build_allow[$planet['planet_type']]; |
||
| 100 | |||
| 101 | if(!isset($que_data['unit_list'][$unit_id])) { |
||
| 102 | throw new exception('{Это здание нельзя строить на ' . ($planet['planet_type'] == PT_PLANET ? 'планете' : 'луне'), ERR_ERROR); // TODO EXCEPTION |
||
| 103 | } |
||
| 104 | } |
||
| 105 | /* |
||
| 106 | // TODO Разделить очереди для Верфи и Обороны |
||
| 107 | elseif($que_id == QUE_HANGAR) |
||
| 108 | { |
||
| 109 | $que_data['mercenary'] = in_array($unit_id, sn_get_groups('defense')) ? MRC_FORTIFIER : MRC_ENGINEER; |
||
| 110 | } |
||
| 111 | elseif($que_id == QUE_HANGAR) |
||
| 112 | { |
||
| 113 | $que_data['mercenary'] = in_array($unit_id, sn_get_groups('defense')) ? MRC_FORTIFIER : MRC_ENGINEER; |
||
| 114 | } |
||
| 115 | */ |
||
| 116 | |||
| 117 | |||
| 118 | sn_db_transaction_start(); |
||
| 119 | // Это нужно, что бы заблокировать пользователя и работу с очередями |
||
| 120 | $user = db_user_by_id($user['id']); |
||
| 121 | // Это нужно, что бы заблокировать планету от списания ресурсов |
||
| 122 | if(isset($planet['id']) && $planet['id']) { |
||
| 123 | $planet = db_planet_by_id($planet['id'], true); |
||
| 124 | } else { |
||
| 125 | $planet['id'] = 0; |
||
| 126 | } |
||
| 127 | |||
| 128 | $planet_id = $que_id == QUE_RESEARCH ? 0 : intval($planet['id']); |
||
| 129 | |||
| 130 | $que = que_get($user['id'], $planet['id'], $que_id, true); |
||
| 131 | $in_que = &$que['in_que'][$que_id][$user['id']][$planet_id]; |
||
| 132 | $que_max_length = que_get_max_que_length($user, $planet, $que_id, $que_data); |
||
| 133 | // TODO Добавить вызовы функций проверок текущей и максимальной длин очередей |
||
| 134 | if(count($in_que) >= $que_max_length) { |
||
| 135 | throw new exception('{Все слоты очереди заняты}', ERR_ERROR); // TODO EXCEPTION |
||
| 136 | } |
||
| 137 | |||
| 138 | // TODO Отдельно посмотреть на уничтожение зданий - что бы можно было уничтожать их без планов |
||
| 139 | switch(eco_can_build_unit($user, $planet, $unit_id)) { |
||
| 140 | case BUILD_ALLOWED: |
||
| 141 | break; |
||
| 142 | case BUILD_UNIT_BUSY: |
||
| 143 | throw new exception('{Строение занято}', ERR_ERROR); |
||
| 144 | break; // TODO EXCEPTION eco_bld_msg_err_laboratory_upgrading |
||
| 145 | // case BUILD_REQUIRE_NOT_MEET: |
||
| 146 | default: |
||
| 147 | if($build_mode == BUILD_CREATE) { |
||
| 148 | throw new exception('{Требования не удовлетворены}', ERR_ERROR); |
||
| 149 | } |
||
| 150 | break; // TODO EXCEPTION eco_bld_msg_err_requirements_not_meet |
||
| 151 | } |
||
| 152 | |||
| 153 | $unit_amount = floor(sys_get_param_float('unit_amount', 1)); |
||
| 154 | $unit_amount_qued = $unit_amount; |
||
| 155 | $units_qued = isset($in_que[$unit_id]) ? $in_que[$unit_id] : 0; |
||
| 156 | $unit_level = mrc_get_level($user, $planet, $unit_id, true, true) + $units_qued; |
||
| 157 | if($unit_max = get_unit_param($unit_id, P_MAX_STACK)) { |
||
| 158 | if($unit_level >= $unit_max) { |
||
| 159 | throw new exception('{Максимальное количество юнитов данного типа уже достигнуто или будет достигнуто по окончанию очереди}', ERR_ERROR); // TODO EXCEPTION |
||
| 160 | } |
||
| 161 | $unit_amount = max(0, min($unit_amount, $unit_max - $unit_level)); |
||
| 162 | } |
||
| 163 | |||
| 164 | if($unit_amount < 1) { |
||
| 165 | throw new exception('{Неправильное количество юнитов - сообщите Администрации}', ERR_ERROR); // TODO EXCEPTION |
||
| 166 | } |
||
| 167 | |||
| 168 | // TODO Переделать eco_unit_busy для всех типов зданий |
||
| 169 | // if(eco_unit_busy($user, $planet, $que, $unit_id)) |
||
| 170 | // { |
||
| 171 | // die('Unit busy'); // TODO EXCEPTION |
||
| 172 | // } |
||
| 173 | if(get_unit_param($unit_id, P_STACKABLE)) { |
||
| 174 | // TODO Поле 'max_Lot_size' для ограничения размера стэка в очереди - то ли в юниты, то ли в очередь |
||
| 175 | if(in_array($unit_id, $group_missile = sn_get_groups('missile'))) { |
||
| 176 | // TODO Поле 'container' - указывает на родительску структуру, в которой хранится данный юнит и по вместительности которой нужно применять размер юнита |
||
| 177 | $used_silo = 0; |
||
| 178 | View Code Duplication | foreach($group_missile as $missile_id) { |
|
| 179 | $missile_qued = isset($in_que[$missile_id]) ? $in_que[$missile_id] : 0; |
||
| 180 | $used_silo += (mrc_get_level($user, $planet, $missile_id, true, true) + $missile_qued) * get_unit_param($missile_id, P_UNIT_SIZE); |
||
| 181 | } |
||
| 182 | $free_silo = mrc_get_level($user, $planet, STRUC_SILO) * get_unit_param(STRUC_SILO, P_CAPACITY) - $used_silo; |
||
| 183 | if($free_silo <= 0) { |
||
| 184 | throw new exception('{Ракетная шахта уже заполнена или будет заполнена по окончанию очереди}', ERR_ERROR); // TODO EXCEPTION |
||
| 185 | } |
||
| 186 | $unit_size = get_unit_param($unit_id, P_UNIT_SIZE); |
||
| 187 | if($free_silo < $unit_size) { |
||
| 188 | throw new exception("{В ракетной шахте нет места для {$classLocale['tech'][$unit_id]}}", ERR_ERROR); // TODO EXCEPTION |
||
| 189 | } |
||
| 190 | $unit_amount = max(0, min($unit_amount, floor($free_silo / $unit_size))); |
||
| 191 | } |
||
| 192 | $unit_level = $new_unit_level = 0; |
||
| 193 | } else { |
||
| 194 | $unit_amount = 1; |
||
| 195 | if($que_id == QUE_STRUCTURES) { |
||
| 196 | // if($build_mode == BUILD_CREATE && eco_planet_fields_max($planet) - $planet['field_current'] - $que['sectors'][$planet['id']] <= 0) |
||
| 197 | $sectors_qued = is_array($in_que) ? array_sum($in_que) : 0; |
||
| 198 | if($build_mode == BUILD_CREATE && eco_planet_fields_max($planet) - $planet['field_current'] - $sectors_qued <= 0) { |
||
| 199 | throw new exception('{Не хватает секторов на планете}', ERR_ERROR); // TODO EXCEPTION |
||
| 200 | } |
||
| 201 | // И что это я такое написал? Зачем? |
||
| 202 | //if($build_mode == BUILD_DESTROY && $planet['field_current'] <= $que['amounts'][$que_id]) |
||
| 203 | //{ |
||
| 204 | // die('Too much buildings'); // TODO EXCEPTION |
||
| 205 | //} |
||
| 206 | } |
||
| 207 | $build_multiplier = $build_mode == BUILD_CREATE ? 1 : -1; |
||
| 208 | $new_unit_level = $unit_level + $unit_amount * $build_multiplier; |
||
| 209 | } |
||
| 210 | |||
| 211 | $build_data = eco_get_build_data($user, $planet, $unit_id, $unit_level); |
||
| 212 | |||
| 213 | $exchange = array(); |
||
| 214 | $market_get_autoconvert_cost = market_get_autoconvert_cost(); |
||
| 215 | if($is_autoconvert && $build_data[BUILD_AUTOCONVERT]) { |
||
| 216 | $dark_matter = mrc_get_level($user, null, RES_DARK_MATTER); |
||
| 217 | if(mrc_get_level($user, null, RES_DARK_MATTER) < $market_get_autoconvert_cost) { |
||
| 218 | throw new exception("{Нет хватает " . ($market_get_autoconvert_cost - $dark_matter) . "ТМ на постройки с автоконвертацией ресурсов}", ERR_ERROR); // TODO EXCEPTION |
||
| 219 | } |
||
| 220 | |||
| 221 | !get_unit_param($unit_id, P_STACKABLE) ? $unit_amount = 1 : false; |
||
| 222 | $resources_loot = sn_get_groups('resources_loot'); |
||
| 223 | $resource_got = array(); |
||
| 224 | $resource_exchange_rates = array(); |
||
| 225 | $resource_diff = array(); |
||
| 226 | $all_positive = true; |
||
| 227 | foreach($resources_loot as $resource_id) { |
||
| 228 | $resource_db_name = pname_resource_name($resource_id); |
||
| 229 | $resource_got[$resource_id] = floor(mrc_get_level($user, $planet, $resource_id)); |
||
| 230 | $resource_exchange_rates[$resource_id] = classSupernova::$config->__get("rpg_exchange_{$resource_db_name}"); |
||
| 231 | $resource_diff[$resource_id] = $resource_got[$resource_id] - $build_data[BUILD_CREATE][$resource_id] * $unit_amount; |
||
| 232 | $all_positive = $all_positive && ($resource_diff[$resource_id] > 0); |
||
| 233 | } |
||
| 234 | // Нужна автоконвертация |
||
| 235 | if($all_positive) { |
||
| 236 | $is_autoconvert = false; |
||
| 237 | } else { |
||
| 238 | foreach($resource_diff as $resource_diff_id => &$resource_diff_amount) { |
||
| 239 | if($resource_diff_amount >= 0) { |
||
| 240 | continue; |
||
| 241 | } |
||
| 242 | foreach($resource_diff as $resource_got_id => &$resource_got_amount) { |
||
| 243 | if($resource_got_amount <= 0) { |
||
| 244 | continue; |
||
| 245 | } |
||
| 246 | $current_exchange = $resource_exchange_rates[$resource_got_id] / $resource_exchange_rates[$resource_diff_id]; |
||
| 247 | |||
| 248 | $will_exchage_to = min(-$resource_diff_amount, floor($resource_got_amount * $current_exchange)); |
||
| 249 | $will_exchage_from = $will_exchage_to / $current_exchange; |
||
| 250 | |||
| 251 | $resource_diff_amount += $will_exchage_to; |
||
| 252 | $resource_got_amount -= $will_exchage_from; |
||
| 253 | $exchange[$resource_diff_id] += $will_exchage_to; |
||
| 254 | $exchange[$resource_got_id] -= $will_exchage_from; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | $is_autoconvert_ok = true; |
||
| 259 | foreach($resource_diff as $resource_diff_amount2) { |
||
| 260 | if($resource_diff_amount2 < 0) { |
||
| 261 | $is_autoconvert_ok = false; |
||
| 262 | break; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | if($is_autoconvert_ok) { |
||
| 267 | $build_data['RESULT'][$build_mode] = BUILD_ALLOWED; |
||
| 268 | $build_data['CAN'][$build_mode] = $unit_amount; |
||
| 269 | } else { |
||
| 270 | $unit_amount = 0; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | $unit_amount = min($build_data['CAN'][$build_mode], $unit_amount); |
||
| 275 | if($unit_amount <= 0) { |
||
| 276 | throw new exception('{Не хватает ресурсов}', ERR_ERROR); // TODO EXCEPTION |
||
| 277 | } |
||
| 278 | |||
| 279 | if($new_unit_level < 0) { |
||
| 280 | throw new exception('{Нельзя уничтожить больше юнитов, чем есть}', ERR_ERROR); // TODO EXCEPTION |
||
| 281 | } |
||
| 282 | |||
| 283 | if($build_data['RESULT'][$build_mode] != BUILD_ALLOWED) { |
||
| 284 | throw new exception('{Строительство блокировано}', ERR_ERROR); // TODO EXCEPTION |
||
| 285 | } |
||
| 286 | |||
| 287 | if($is_autoconvert) { |
||
| 288 | ksort($exchange); |
||
| 289 | ksort($resource_got); |
||
| 290 | db_change_units($user, $planet, array( |
||
| 291 | RES_METAL => !empty($exchange[RES_METAL]) ? $exchange[RES_METAL] : 0, |
||
| 292 | RES_CRYSTAL => !empty($exchange[RES_CRYSTAL]) ? $exchange[RES_CRYSTAL] : 0, |
||
| 293 | RES_DEUTERIUM => !empty($exchange[RES_DEUTERIUM]) ? $exchange[RES_DEUTERIUM] : 0, |
||
| 294 | )); |
||
| 295 | rpg_points_change($user['id'], RPG_BUILD_AUTOCONVERT, -$market_get_autoconvert_cost, sprintf( |
||
| 296 | classLocale::$lang['bld_autoconvert'], $unit_id, $unit_amount, uni_render_planet_full($planet, '', false, true), classLocale::$lang['tech'][$unit_id], |
||
| 297 | sys_unit_arr2str($build_data[BUILD_CREATE]), sys_unit_arr2str($resource_got), sys_unit_arr2str($exchange) |
||
| 298 | )); |
||
| 299 | } |
||
| 300 | |||
| 301 | $unit_amount_qued = 0; |
||
| 302 | while($unit_amount > 0 && count($que['ques'][$que_id][$user['id']][$planet_id]) < $que_max_length) { |
||
| 303 | $place = min($unit_amount, MAX_FLEET_OR_DEFS_PER_ROW); |
||
| 304 | que_add_unit($unit_id, $user, $planet, $build_data, $new_unit_level, $place, $build_mode); |
||
| 305 | $unit_amount -= $place; |
||
| 306 | $que = que_get($user['id'], $planet['id'], $que_id, true); |
||
| 307 | $unit_amount_qued += $place; |
||
| 308 | } |
||
| 309 | |||
| 310 | sn_db_transaction_commit(); |
||
| 311 | |||
| 312 | if($redirect) { |
||
| 313 | sys_redirect("{$_SERVER['PHP_SELF']}?mode=" . sys_get_param_str('mode') . "&ally_id=" . sys_get_param_id('ally_id')); |
||
| 314 | die(); |
||
| 315 | } |
||
| 316 | |||
| 317 | $operation_result = array( |
||
| 318 | 'STATUS' => ERR_NONE, |
||
| 319 | 'MESSAGE' => '{Строительство начато}', |
||
| 320 | ); |
||
| 321 | } catch(exception $e) { |
||
| 322 | sn_db_transaction_rollback(); |
||
| 323 | $operation_result = array( |
||
| 324 | 'STATUS' => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR, |
||
| 325 | 'MESSAGE' => $e->getMessage() |
||
| 326 | ); |
||
| 327 | } |
||
| 328 | |||
| 329 | if(!empty($operation_result['MESSAGE'])) { |
||
| 330 | $operation_result['MESSAGE'] .= ' ' . ($unit_amount_qued ? $unit_amount_qued : $unit_amount) . 'x[' . classLocale::$lang['tech'][$unit_id] . ']'; |
||
| 331 | } |
||
| 332 | |||
| 333 | return $operation_result; |
||
| 334 | } |
||
| 335 | |||
| 724 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.