Completed
Push — trunk ( dc0cf2...c3d2f5 )
by SuperNova.WS
03:52
created

eco_get_build_data()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
nc 24
nop 6
dl 0
loc 41
rs 5.3846
c 1
b 0
f 0
1
<?php
2
3
use Meta\Economic\BuildDataStatic;
4
use Unit\DBStaticUnit;
5
6
function eco_lab_sort_effectivness($a, $b) {
7
  return $a['laboratory_effective_level'] > $b['laboratory_effective_level'] ? -1 : ($a['laboratory_effective_level'] < $b['laboratory_effective_level'] ? 1 : 0);
8
}
9
10
/**
11
 * eco_get_build_data.php
12
 *
13
 * 1.0 - copyright (c) 2010 by Gorlum for http://supernova.ws
14
 * @version 1.0
15
 */
16
function eco_get_lab_max_effective_level(&$user, $lab_require) {
17
  if (!$user['user_as_ally'] && !isset($user['laboratories_active'])) {
18
    $user['laboratories_active'] = array();
19
    $query = DBStaticUnit::db_unit_list_laboratories($user['id']);
20
    while ($row = db_fetch($query)) {
21
      if (!eco_unit_busy($user, $row, UNIT_TECHNOLOGIES)) {
22
        $row += array(
23
          STRUC_LABORATORY             => $level_lab = mrc_get_level($user, $row, STRUC_LABORATORY),
24
          STRUC_LABORATORY_NANO        => $level_lab_nano = mrc_get_level($user, $row, STRUC_LABORATORY_NANO),
25
          'laboratory_effective_level' => $level_lab * pow(2, $level_lab_nano),
0 ignored issues
show
Bug introduced by
It seems like $level_lab_nano can also be of type boolean; however, parameter $exp of pow() does only seem to accept integer|double, maybe add an additional type check? ( Ignorable by Annotation )

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

25
          'laboratory_effective_level' => $level_lab * pow(2, /** @scrutinizer ignore-type */ $level_lab_nano),
Loading history...
26
        );
27
        $user['laboratories_active'][$row['id']] = $row;
28
      }
29
    }
30
31
    uasort($user['laboratories_active'], 'eco_lab_sort_effectivness');
32
  }
33
34
  if (!isset($user['research_effective_level'][$lab_require])) {
35
    if ($user['user_as_ally']) {
36
      $lab_level = doquery("SELECT ally_members AS effective_level FROM {{alliance}} WHERE id = {$user['user_as_ally']} LIMIT 1", true);
37
    } else {
38
      $tech_intergalactic = mrc_get_level($user, false, TECH_RESEARCH) + 1;
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

38
      $tech_intergalactic = mrc_get_level($user, /** @scrutinizer ignore-type */ false, TECH_RESEARCH) + 1;
Loading history...
39
      $lab_level['effective_level'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$lab_level was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lab_level = array(); before regardless.
Loading history...
40
41
      foreach ($user['laboratories_active'] as $data) {
42
        if ($tech_intergalactic <= 0) {
43
          break;
44
        }
45
        if ($data[STRUC_LABORATORY] >= $lab_require) {
46
          $lab_level['effective_level'] += $data['laboratory_effective_level'];
47
          $tech_intergalactic--;
48
        }
49
      }
50
    }
51
    $user['research_effective_level'][$lab_require] = $lab_level['effective_level'] ? $lab_level['effective_level'] : 1;
52
  }
53
54
  return $user['research_effective_level'][$lab_require];
55
}
56
57
/**
58
 * @param array      $user
59
 * @param array      $planet
60
 * @param int        $unit_id
61
 * @param int|string $unit_level
62
 * @param bool       $only_cost
63
 * @param array|null $info
64
 *
65
 * @return mixed
66
 */
67
function eco_get_build_data(&$user, $planet, $unit_id, $unit_level = 0, $only_cost = false, $info = null) {
68
  $unit_data = get_unit_param($unit_id);
69
70
  // Filling basic build data - unit cost
71
  $cost = BuildDataStatic::getBasicData($user, $planet, $unit_data, $unit_level);
72
73
  // Getting autoconvert unit amount
74
  $cost[BUILD_AUTOCONVERT] = $cost[P_OPTIONS][P_ONLY_DARK_MATTER] ? BuildDataStatic::getAutoconvertCount($user, $planet) : 0;
75
  // Limiting autoconvert amount to unit max stack - if is set
76
  !empty($unit_data[P_MAX_STACK]) ? $cost[BUILD_AUTOCONVERT] = min($unit_data[P_MAX_STACK], $cost[BUILD_AUTOCONVERT]) : false;
77
78
  $cost[P_OPTIONS][P_TIME_RAW] = $cost[P_OPTIONS][P_TIME_RAW] * 60 * 60 / get_game_speed() / 2500;
79
80
  // TODO - Вынести в отдельную процедуру расчёт стоимости
81
  if ($only_cost) {
82
    return $cost;
83
  }
84
85
  // Check if unit can be built
86
  $cost['RESULT'][BUILD_CREATE] = BuildDataStatic::getBuildStatus($user, $planet, $unit_id, $cost['CAN'][BUILD_CREATE]);
87
  // Setting destroy status
88
  $cost['RESULT'][BUILD_DESTROY] = BuildDataStatic::getDestroyStatus($user, $planet, $unit_id, $cost);
89
90
  // Time calculations
91
  if (in_array($unit_id, sn_get_groups('governors')) || $cost[P_OPTIONS][P_ONLY_DARK_MATTER]) {
92
    // Zero build time for Governors and other units with DM in price
93
    $cost[RES_TIME][BUILD_CREATE] = $cost[RES_TIME][BUILD_DESTROY] = 0;
94
  } else {
95
    $cost[RES_TIME][BUILD_CREATE] = $cost[P_OPTIONS][P_TIME_RAW];
96
97
    // Applying time modifiers
98
    $cost[RES_TIME][BUILD_CREATE] /= BuildDataStatic::getCapitalTimeDivisor($user, $planet, $unit_id);
99
    $cost[RES_TIME][BUILD_CREATE] /= BuildDataStatic::getMercenaryTimeDivisor($user, $planet, $unit_id);
100
    $cost[RES_TIME][BUILD_CREATE] /= SN::$gc->pimp->getStructuresTimeDivisor($user, $planet, $unit_id, $unit_data);
101
102
    // Final calculations
103
    $cost[RES_TIME][BUILD_CREATE] = $cost[RES_TIME][BUILD_CREATE] > 1 ? ceil($cost[RES_TIME][BUILD_CREATE]) : 1;
104
    $cost[RES_TIME][BUILD_DESTROY] = $cost[RES_TIME][BUILD_CREATE] / 2 > 1 ? ceil($cost[RES_TIME][BUILD_CREATE] / 2) : 1;
105
  }
106
107
  return $cost;
108
}
109
110
111
function eco_can_build_unit($user, $planet, $unit_id) {
112
  $result = null;
113
114
  return sn_function_call('eco_can_build_unit', [$user, $planet, $unit_id, &$result]);
115
}
116
117
function sn_eco_can_build_unit($user, $planet, $unit_id, &$result) {
118
  $result = isset($result) ? $result : BUILD_ALLOWED;
119
  $result = $result == BUILD_ALLOWED && eco_unit_busy($user, $planet, $unit_id) ? BUILD_UNIT_BUSY : $result;
120
121
  $unit_param = get_unit_param($unit_id);
122
  if ($unit_param[P_UNIT_TYPE] != UNIT_MERCENARIES || !SN::$config->empire_mercenary_temporary) {
123
    $requirement = &$unit_param[P_REQUIRE];
124
    if ($result == BUILD_ALLOWED && $requirement) {
125
      foreach ($requirement as $require_id => $require_level) {
126
        if (mrc_get_level($user, $planet, $require_id) < $require_level) {
127
          $result = BUILD_REQUIRE_NOT_MEET;
128
          break;
129
        }
130
      }
131
    }
132
  }
133
134
  return $result;
135
}
136
137
function eco_is_builds_in_que($planet_que, $unit_list) {
138
  $eco_is_builds_in_que = false;
139
140
  $unit_list = is_array($unit_list) ? $unit_list : array($unit_list => $unit_list);
141
  $planet_que = explode(';', $planet_que);
142
  foreach ($planet_que as $planet_que_item) {
143
    if ($planet_que_item) {
144
      list($planet_que_item) = explode(',', $planet_que_item);
145
      if (in_array($planet_que_item, $unit_list)) {
146
        $eco_is_builds_in_que = true;
147
        break;
148
      }
149
    }
150
  }
151
152
  return $eco_is_builds_in_que;
153
}
154
155
function eco_unit_busy(&$user, &$planet, $unit_id) {
156
  $result = null;
157
158
  return sn_function_call('eco_unit_busy', [&$user, &$planet, $unit_id, &$result]);
159
}
160
161
function sn_eco_unit_busy(&$user, &$planet, $unit_id, &$result) {
162
  global $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...
163
164
  $result = isset($result) ? $result : false;
165
  if (!$result) {
166
    if (($unit_id == STRUC_LABORATORY || $unit_id == STRUC_LABORATORY_NANO) && !$config->BuildLabWhileRun) {
167
      $global_que = que_get($user['id'], $planet['id'], QUE_RESEARCH, false);
168
      if (is_array($global_que['ques'][QUE_RESEARCH][$user['id']])) {
169
        $first_element = reset($global_que['ques'][QUE_RESEARCH][$user['id']]);
170
        if (is_array($first_element)) {
171
          $result = true;
172
        }
173
      }
174
      //if(!empty($global_que['ques'][QUE_RESEARCH][$user['id']][0]))
0 ignored issues
show
Unused Code Comprehensibility introduced by
96% 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...
175
      //{
176
      //  $result = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
177
      //}
178
    } elseif (($unit_id == UNIT_TECHNOLOGIES || in_array($unit_id, sn_get_groups('tech'))) && !$config->BuildLabWhileRun && $planet['que']) {
179
      $result = eco_is_builds_in_que($planet['que'], array(STRUC_LABORATORY, STRUC_LABORATORY_NANO));
180
    }
181
  }
182
183
//  switch($unit_id)
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
184
//  {
185
//    case STRUC_FACTORY_HANGAR:
186
//      $hangar_busy = $planet['b_hangar'] && $planet['b_hangar_id'];
187
//      $return = $hangar_busy;
188
//    break;
189
//  }
190
191
  return $result;
192
}
193