Completed
Push — trunk ( 153731...fde30a )
by SuperNova.WS
04:15
created

get_engine_data()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 16
nop 3
dl 0
loc 16
rs 8.8571
1
<?php
2
3
use Unit\DBStaticUnit;
4
5
/**
6
 * Created by Gorlum 04.12.2017 4:32
7
 */
8
9
function eco_get_total_cost($unit_id, $unit_level) {
10
  static $rate, $sn_group_resources_all, $sn_group_resources_loot;
11
  if (!$rate) {
12
    $sn_group_resources_all = sn_get_groups('resources_all');
13
    $sn_group_resources_loot = sn_get_groups('resources_loot');
14
15
    $rate[RES_METAL] = SN::$config->rpg_exchange_metal;
0 ignored issues
show
Bug Best Practice introduced by
The property rpg_exchange_metal does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
16
    $rate[RES_CRYSTAL] = SN::$config->rpg_exchange_crystal / SN::$config->rpg_exchange_metal;
0 ignored issues
show
Bug Best Practice introduced by
The property rpg_exchange_crystal does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
17
    $rate[RES_DEUTERIUM] = SN::$config->rpg_exchange_deuterium / SN::$config->rpg_exchange_metal;
0 ignored issues
show
Bug Best Practice introduced by
The property rpg_exchange_deuterium does not exist on classConfig. Since you implemented __get, consider adding a @property annotation.
Loading history...
18
  }
19
20
  $unit_cost_data = get_unit_param($unit_id, 'cost');
21
  if (!is_array($unit_cost_data)) {
22
    return array('total' => 0);
23
  }
24
  $factor = isset($unit_cost_data['factor']) ? $unit_cost_data['factor'] : 1;
25
  $cost_array = array(BUILD_CREATE => array(), 'total' => 0);
26
  $unit_level = $unit_level > 0 ? $unit_level : 0;
27
  foreach ($unit_cost_data as $resource_id => $resource_amount) {
28
    if (!in_array($resource_id, $sn_group_resources_all)) {
29
      continue;
30
    }
31
    $cost_array[BUILD_CREATE][$resource_id] = round($resource_amount * ($factor == 1 ? $unit_level : ((1 - pow($factor, $unit_level)) / (1 - $factor))));
32
    if (in_array($resource_id, $sn_group_resources_loot)) {
33
      $cost_array['total'] += $cost_array[BUILD_CREATE][$resource_id] * $rate[$resource_id];
34
    }
35
  }
36
37
  return $cost_array;
38
}
39
40
function sn_unit_purchase($unit_id) { }
41
42
function sn_unit_relocate($unit_id, $from, $to) { }
43
44
/**
45
 * @param array $user
46
 * @param array $planet ([])
47
 * @param int   $unit_id
48
 * @param bool  $for_update (false)
49
 * @param bool  $plain (false)
50
 *
51
 * @return int|float|bool
52
 */
53
function mrc_get_level(&$user, $planet = [], $unit_id, $for_update = false, $plain = false) { return sn_function_call(__FUNCTION__, [&$user, $planet, $unit_id, $for_update, $plain, &$result]); }
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be never defined.
Loading history...
54
55
function sn_mrc_get_level(&$user, $planet = [], $unit_id, $for_update = false, $plain = false, &$result) {
56
  $mercenary_level = 0;
57
  $unit_db_name = pname_resource_name($unit_id);
58
59
  if (in_array($unit_id, sn_get_groups(array('plans', 'mercenaries', 'tech', 'artifacts')))) {
60
    $unit = !empty($user['id']) ? DBStaticUnit::db_unit_by_location($user['id'], LOC_USER, $user['id'], $unit_id) : 0;
61
    $mercenary_level = !empty($unit['unit_level']) ? $unit['unit_level'] : 0;
62
  } elseif (in_array($unit_id, sn_get_groups(array('structures', 'fleet', 'defense')))) {
63
    $unit = DBStaticUnit::db_unit_by_location(is_array($user) ? $user['id'] : $planet['id_owner'], LOC_PLANET, $planet['id'], $unit_id);
64
    $mercenary_level = !empty($unit['unit_level']) ? $unit['unit_level'] : 0;
65
  } elseif (in_array($unit_id, sn_get_groups('governors'))) {
66
    $mercenary_level = $unit_id == $planet['PLANET_GOVERNOR_ID'] ? $planet['PLANET_GOVERNOR_LEVEL'] : 0;
67
  } elseif ($unit_id == RES_DARK_MATTER) {
68
    $mercenary_level = $user[$unit_db_name] + ($plain || $user['user_as_ally'] ? 0 : SN::$auth->account->account_metamatter);
69
  } elseif ($unit_id == RES_METAMATTER) {
70
    $mercenary_level = SN::$auth->account->account_metamatter; //$user[$unit_db_name];
71
  } elseif (in_array($unit_id, sn_get_groups(array('resources_loot'))) || $unit_id == UNIT_SECTOR) {
72
    $mercenary_level = !empty($planet[$unit_db_name]) ? $planet[$unit_db_name] : $user[$unit_db_name];
73
  }
74
75
  return $result = $mercenary_level;
76
}
77
78
function mrc_modify_value(&$user, $planet = array(), $mercenaries, $value) { return sn_function_call('mrc_modify_value', array(&$user, $planet, $mercenaries, $value)); }
79
80
function sn_mrc_modify_value(&$user, $planet = array(), $mercenaries, $value, $base_value = null) {
81
  if (!is_array($mercenaries)) {
82
    $mercenaries = array($mercenaries);
83
  }
84
85
  $base_value = isset($base_value) ? $base_value : $value;
86
87
  foreach ($mercenaries as $mercenary_id) {
88
    $mercenary_level = mrc_get_level($user, $planet, $mercenary_id);
89
90
    $mercenary = get_unit_param($mercenary_id);
91
    $mercenary_bonus = $mercenary[P_BONUS_VALUE];
92
93
    switch ($mercenary[P_BONUS_TYPE]) {
94
      case BONUS_PERCENT:
95
        $mercenary_level = $mercenary_bonus < 0 && $mercenary_level * $mercenary_bonus < -90 ? -90 / $mercenary_bonus : $mercenary_level;
96
        $value += $base_value * $mercenary_level * $mercenary_bonus / 100;
97
      break;
98
99
      case BONUS_ADD:
100
        $value += $mercenary_level * $mercenary_bonus;
101
      break;
102
103
      case BONUS_ABILITY:
104
        $value = $mercenary_level ? $mercenary_level : 0;
105
      break;
106
107
      default:
108
      break;
109
    }
110
  }
111
112
  return $value;
113
}
114
115
function sys_unit_str2arr($fleet_string) {
116
  $fleet_array = array();
117
  if (!empty($fleet_string)) {
118
    $arrTemp = explode(';', $fleet_string);
119
    foreach ($arrTemp as $temp) {
120
      if ($temp) {
121
        $temp = explode(',', $temp);
122
        if (!empty($temp[0]) && !empty($temp[1])) {
123
          $fleet_array[$temp[0]] += $temp[1];
124
        }
125
      }
126
    }
127
  }
128
129
  return $fleet_array;
130
}
131
132
function sys_unit_arr2str($unit_list) {
133
  $fleet_string = array();
134
  if (isset($unit_list)) {
135
    if (!is_array($unit_list)) {
136
      $unit_list = array($unit_list => 1);
137
    }
138
139
    foreach ($unit_list as $unit_id => $unit_count) {
140
      if ($unit_id && $unit_count) {
141
        $fleet_string[] = "{$unit_id},{$unit_count}";
142
      }
143
    }
144
  }
145
146
  return implode(';', $fleet_string);
147
}
148
149
// TODO Для полноценного функионирования апдейтера пакет функций, включая эту должен быть вынесен раньше - или грузить general.php до апдейтера
150
function sys_get_unit_location($user, $planet, $unit_id) { return sn_function_call('sys_get_unit_location', array($user, $planet, $unit_id)); }
151
152
function sn_sys_get_unit_location($user, $planet, $unit_id) {
153
  return get_unit_param($unit_id, 'location');
154
}
155
156
157
function get_engine_data($user, $engine_info, $user_tech_level = null) {
158
  $sn_data_tech_bonus = get_unit_param($engine_info['tech'], P_BONUS_VALUE);
159
160
  $user_tech_level = $user_tech_level === null ? intval(mrc_get_level($user, false, $engine_info['tech'])) : $user_tech_level;
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array expected by parameter $planet of mrc_get_level(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
  $user_tech_level = $user_tech_level === null ? intval(mrc_get_level($user, /** @scrutinizer ignore-type */ false, $engine_info['tech'])) : $user_tech_level;
Loading history...
161
162
  $engine_info['speed_base'] = $engine_info['speed'];
163
  $tech_bonus = ($user_tech_level - $engine_info['min_level']) * $sn_data_tech_bonus / 100;
164
  $tech_bonus = $tech_bonus < -0.9 ? -0.95 : $tech_bonus;
165
  $engine_info['speed'] = floor(mrc_modify_value($user, false, array(MRC_NAVIGATOR), $engine_info['speed']) * (1 + $tech_bonus));
166
167
  $engine_info['consumption_base'] = $engine_info['consumption'];
168
  $tech_bonus = ($user_tech_level - $engine_info['min_level']) * $sn_data_tech_bonus / 1000;
169
  $tech_bonus = $tech_bonus > 0.5 ? 0.5 : ($tech_bonus < 0 ? $tech_bonus * 2 : $tech_bonus);
170
  $engine_info['consumption'] = ceil($engine_info['consumption'] * (1 - $tech_bonus));
171
172
  return $engine_info;
173
}
174
175
function get_ship_data($ship_id, $user) {
176
  $ship_data = array();
177
  if (in_array($ship_id, sn_get_groups(array('fleet', 'missile')))) {
178
    foreach (get_unit_param($ship_id, 'engine') as $engine_info) {
179
      $tech_level = intval(mrc_get_level($user, false, $engine_info['tech']));
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array expected by parameter $planet of mrc_get_level(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

179
      $tech_level = intval(mrc_get_level($user, /** @scrutinizer ignore-type */ false, $engine_info['tech']));
Loading history...
180
      if (empty($ship_data) || $tech_level >= $engine_info['min_level']) {
181
        $ship_data = $engine_info;
182
        $ship_data['tech_level'] = $tech_level;
183
      }
184
    }
185
    $ship_data = get_engine_data($user, $ship_data);
186
    $ship_data['capacity'] = get_unit_param($ship_id, 'capacity');
187
  }
188
189
  return $ship_data;
190
}
191
192
/**
193
 * Get unit info by unit's SN ID
194
 *
195
 * @param int $unitSnId
196
 *
197
 * @return mixed
198
 */
199
function getUnitInfo($unitSnId) {
200
  return get_unit_param($unitSnId);
201
}
202
203
function get_unit_param($unit_id, $param_name = null, $user = null, $planet = null) { return sn_function_call('get_unit_param', array($unit_id, $param_name, $user, $planet, &$result)); }
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be never defined.
Loading history...
204
205
function sn_get_unit_param($unit_id, $param_name = null, $user = null, $planet = null, &$result) {
206
  global $sn_data;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
207
208
  $result = isset($sn_data[$unit_id])
209
    ? ($param_name === null
210
      ? $sn_data[$unit_id]
211
      : (isset($sn_data[$unit_id][$param_name]) ? $sn_data[$unit_id][$param_name] : $result)
212
    )
213
    : $result;
214
215
  return $result;
216
}
217
218
/**
219
 * @param string|string[] $groups
220
 *
221
 * @return array|array[]
222
 */
223
function sn_get_groups($groups) { return sn_function_call('sn_get_groups', array($groups, &$result)); }
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be never defined.
Loading history...
224
225
function sn_sn_get_groups($groups, &$result) {
226
  $result = is_array($result) ? $result : array();
227
  foreach ($groups = is_array($groups) ? $groups : array($groups) as $group_name) {
0 ignored issues
show
Unused Code introduced by
The assignment to $groups is dead and can be removed.
Loading history...
228
    $result += is_array($a_group = get_unit_param(UNIT_GROUP, $group_name)) ? $a_group : array();
229
  }
230
231
  return $result;
232
}
233
234
235
function unit_requirements_render($user, $planetrow, $unit_id, $field = P_REQUIRE) { return sn_function_call('unit_requirements_render', array($user, $planetrow, $unit_id, $field, &$result)); }
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be never defined.
Loading history...
236
237
function sn_unit_requirements_render($user, $planetrow, $unit_id, $field = P_REQUIRE, &$result) {
238
  global $lang, $config;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
239
240
  $sn_data_unit = get_unit_param($unit_id);
241
242
  $result = is_array($result) ? $result : array();
243
  if ($sn_data_unit[$field] && !($sn_data_unit[P_UNIT_TYPE] == UNIT_MERCENARIES && SN::$config->empire_mercenary_temporary)) {
244
    foreach ($sn_data_unit[$field] as $require_id => $require_level) {
245
      $level_got = mrc_get_level($user, $planetrow, $require_id);
246
      $level_basic = mrc_get_level($user, $planetrow, $require_id, false, true);
247
      $result[] = array(
248
        'NAME'             => $lang['tech'][$require_id],
249
        //'CLASS' => $require_level > $level_got ? 'negative' : ($require_level == $level_got ? 'zero' : 'positive'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
250
        'REQUEREMENTS_MET' => intval($require_level <= $level_got ? REQUIRE_MET : REQUIRE_MET_NOT),
251
        'LEVEL_REQUIRE'    => $require_level,
252
        'LEVEL'            => $level_got,
253
        'LEVEL_BASIC'      => $level_basic,
254
        'LEVEL_BONUS'      => max(0, $level_got - $level_basic),
255
        'ID'               => $require_id,
256
      );
257
    }
258
  }
259
260
  return $result;
261
}
262
263
/**
264
 * @param array $cost - [(int)resourceId => (float)unitAmount] => [RES_CRYSTAL => 100]
265
 * @param int   $in_resource - RES_METAL...
266
 *
267
 * @return float|int
268
 */
269
function get_unit_cost_in($cost, $in_resource = RES_METAL) {
270
  static $rates;
271
272
  if (!$rates) {
273
    $rates = SN::$gc->economicHelper->getResourcesExchange();
274
  }
275
276
  $mainResourceExchange = !empty($rates[$in_resource]) ? $rates[$in_resource] : 1;
277
  $metal_cost = 0;
278
  foreach ($cost as $resource_id => $resource_value) {
279
    $metal_cost += $rates[$resource_id] / $mainResourceExchange * $resource_value;
280
  }
281
282
  return $metal_cost;
283
}
284