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

BuildDataStatic::eco_get_resource_on_location()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 4
dl 0
loc 12
ccs 0
cts 11
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 24.03.2018 21:54
4
 */
5
6
namespace Meta\Economic;
7
8
9
use SN;
10
11
class BuildDataStatic {
12
13
  /**
14
   * @param float $prevDivisor - because this is Hooker's client so prev result variable should be declared
15
   * @param array $user
16
   * @param array $planet
17
   * @param int   $unit_id
18
   * @param array $unit_data
19
   *
20
   * @return float
21
   */
22
  public static function getStructuresTimeDivisor($prevDivisor, $user, $planet, $unit_id, $unit_data) {
23
    $result = 1;
24
    if (in_array($unit_id, sn_get_groups('structures'))) {
25
      $result = pow(2, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) * (mrc_get_level($user, $planet, STRUC_FACTORY_ROBOT) + 1);
0 ignored issues
show
Bug introduced by
It seems like mrc_get_level($user, $pl...mic\STRUC_FACTORY_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
      $result = pow(2, /** @scrutinizer ignore-type */ mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) * (mrc_get_level($user, $planet, STRUC_FACTORY_ROBOT) + 1);
Loading history...
26
    } elseif (in_array($unit_id, sn_get_groups('defense'))) {
27
      $result = pow(2, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) * (mrc_get_level($user, $planet, STRUC_FACTORY_HANGAR) + 1);
28
    } elseif (in_array($unit_id, sn_get_groups('fleet'))) {
29
      $result = pow(2, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) * (mrc_get_level($user, $planet, STRUC_FACTORY_HANGAR) + 1);
30
    } elseif (in_array($unit_id, sn_get_groups('tech'))) {
31
      $result = eco_get_lab_max_effective_level($user, intval($unit_data[P_REQUIRE][STRUC_LABORATORY]));
32
    }
33
34
    return $result;
35
  }
36
37
  /**
38
   * @param int $unit_id
39
   *
40
   * @return float
41
   */
42
  public static function getMercenaryTimeDivisor($user, $planet, $unit_id) {
43
    if (in_array($unit_id, sn_get_groups('structures'))) {
44
      $mercenary = MRC_ENGINEER;
45
    } elseif (in_array($unit_id, sn_get_groups('tech'))) {
46
      $mercenary = MRC_ACADEMIC;
47
    } elseif (in_array($unit_id, sn_get_groups('defense'))) {
48
      $mercenary = MRC_FORTIFIER;
49
    } elseif (in_array($unit_id, sn_get_groups('fleet'))) {
50
      $mercenary = MRC_ENGINEER;
51
    } else {
52
      $mercenary = 0;
53
    }
54
55
    return $mercenary ? mrc_modify_value($user, $planet, $mercenary, 1) : 1;
56
  }
57
58
  /**
59
   * @param array $user
60
   * @param array $planet
61
   * @param int   $unit_id
62
   *
63
   * @return float|int
64
   */
65
  public static function getCapitalTimeDivisor($user, $planet, $unit_id) {
66
    static $capitalUnitGroups;
67
    empty($capitalUnitGroups) ? $capitalUnitGroups = sn_get_groups(sn_get_groups(GROUP_CAPITAL_BUILDING_BONUS_GROUPS)) : false;
0 ignored issues
show
Bug introduced by
It seems like sn_get_groups(Meta\Econo..._BUILDING_BONUS_GROUPS) can also be of type array<mixed,array>; however, parameter $groups of sn_get_groups() does only seem to accept string|string[], 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

67
    empty($capitalUnitGroups) ? $capitalUnitGroups = sn_get_groups(/** @scrutinizer ignore-type */ sn_get_groups(GROUP_CAPITAL_BUILDING_BONUS_GROUPS)) : false;
Loading history...
68
69
    if (
70
      // If planet is capital
71
      $user['id_planet'] == $planet['id']
72
      &&
73
      // There is capital building rate set
74
      SN::$gc->config->planet_capital_building_rate > 0
75
      &&
76
      // Unit is subject to Capital bonus
77
      in_array($unit_id, $capitalUnitGroups)
78
    ) {
79
      $capitalTimeDivisor = SN::$gc->config->planet_capital_building_rate;
80
    } else {
81
      $capitalTimeDivisor = 1;
82
    }
83
84
    return $capitalTimeDivisor;
85
  }
86
87
  /**
88
   * @param array $user
89
   * @param array $planet
90
   * @param int   $unit_id
91
   * @param array $cost
92
   *
93
   * @return float - Time need to destroy current level of unit in seconds
94
   */
95
  public static function getDestroyStatus($user, $planet, $unit_id, $cost) {
96
    static $groupStructures;
97
    empty($groupStructures) ? $groupStructures = sn_get_groups('structures') : false;
98
99
    $result = !in_array($unit_id, $groupStructures) ? BUILD_INDESTRUCTABLE :
100
      (!mrc_get_level($user, $planet, $unit_id, false, true) ? BUILD_NO_UNITS :
101
        (
102
        !($cost['CAN'][BUILD_DESTROY]) ? BUILD_NO_RESOURCES :
103
          ($cost['RESULT'][BUILD_CREATE] == BUILD_UNIT_BUSY ? BUILD_UNIT_BUSY : BUILD_ALLOWED)
104
        )
105
      );
106
107
    return $result;
108
  }
109
110
  /**
111
   * @param array $user
112
   * @param array $planet
113
   *
114
   * @return float
115
   */
116
  public static function getAutoconvertCount($user, $planet) {
117
    static $groupResourcesLoot;
118
    empty($groupResourcesLoot) ? $groupResourcesLoot = sn_get_groups('resources_loot') : false;
119
120
    $cost_in_metal = 0;
121
    $planetResourcesInMetal = 0;
122
    foreach ($groupResourcesLoot as $resource_id) {
123
      $planetResourcesInMetal += floor(get_unit_cost_in([$resource_id => mrc_get_level($user, $planet, $resource_id)]));
124
      !empty($cost[BUILD_CREATE][$resource_id])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cost seems to never exist and therefore empty should always be true.
Loading history...
125
        ? $cost_in_metal += get_unit_cost_in([$resource_id => $cost[BUILD_CREATE][$resource_id]])
126
        : false;
127
    }
128
129
    return floor($planetResourcesInMetal / $cost_in_metal);
130
  }
131
132
  /**
133
   * @param $user
134
   * @param $planet
135
   * @param $unit_data
136
   * @param $unit_level
137
   *
138
   * @return array
139
   */
140
  public static function getBasicData(&$user, $planet, $unit_data, $unit_level) {
141
    static $groupResourcesLoot;
142
    empty($groupResourcesLoot) ? $groupResourcesLoot = sn_get_groups('resources_loot') : false;
143
144
    $cost = [
145
      P_OPTIONS => [
146
        P_TIME_RAW => 0,
147
      ],
148
    ];
149
150
    $unit_factor = !empty($unit_data[P_COST][P_FACTOR]) ? $unit_data[P_COST][P_FACTOR] : 1;
151
    $levelPriceFactor = pow($unit_factor, $unit_level);
152
153
    $only_dark_matter = 0;
154
    $canDestroyAmount = 1000000000000;
155
    $canBuildAmount = !empty($unit_data[P_MAX_STACK]) ? $unit_data[P_MAX_STACK] : 1000000000000;
156
    foreach ($unit_data[P_COST] as $resource_id => $resource_amount) {
157
      if ($resource_id === P_FACTOR || !($resource_cost = ceil($resource_amount * $levelPriceFactor))) {
158
        continue;
159
      }
160
161
      $only_dark_matter = $only_dark_matter ? $only_dark_matter : $resource_id;
162
163
      $cost[BUILD_CREATE][$resource_id] = $resource_cost;
164
      $cost[BUILD_DESTROY][$resource_id] = ceil($resource_cost / 2);
165
166
      if (in_array($resource_id, $groupResourcesLoot)) {
167
        $cost[P_OPTIONS][P_TIME_RAW] += get_unit_cost_in([$resource_id => $resource_cost], RES_DEUTERIUM);
168
      }
169
170
      $resource_got = BuildDataStatic::eco_get_resource_on_location($user, $planet, $resource_id, $groupResourcesLoot);
171
172
      $canBuildAmount = min($canBuildAmount, floor($resource_got / $cost[BUILD_CREATE][$resource_id]));
173
      $canDestroyAmount = min($canDestroyAmount, floor($resource_got / $cost[BUILD_DESTROY][$resource_id]));
174
    }
175
    $cost['CAN'][BUILD_CREATE] = $canBuildAmount > 0 ? floor($canBuildAmount) : 0;
176
    $cost['CAN'][BUILD_DESTROY] = $canDestroyAmount > 0 ? floor($canDestroyAmount) : 0;
177
    $cost[P_OPTIONS][P_ONLY_DARK_MATTER] = $only_dark_matter == RES_DARK_MATTER;
178
179
    return $cost;
180
  }
181
182
  /**
183
   * Special function to get resource on location
184
   *
185
   * @param array $user
186
   * @param array $planet
187
   * @param int   $resource_id
188
   * @param array $groupResourcesLoot
189
   *
190
   * @return float
191
   */
192
  public static function eco_get_resource_on_location($user, $planet, $resource_id, $groupResourcesLoot) {
193
    if (in_array($resource_id, $groupResourcesLoot)) {
194
      $resource_got = mrc_get_level($user, $planet, $resource_id);
195
    } elseif ($resource_id == RES_DARK_MATTER) {
196
      $resource_got = mrc_get_level($user, [], $resource_id);
197
    } elseif ($resource_id == RES_ENERGY) {
198
      $resource_got = max(0, $planet['energy_max'] - $planet['energy_used']);
199
    } else {
200
      $resource_got = 0;
201
    }
202
203
    return $resource_got;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $resource_got also could return the type boolean which is incompatible with the documented return type double.
Loading history...
204
  }
205
206
  /**
207
   * @param array $user
208
   * @param array $planet
209
   * @param int   $unit_id
210
   * @param float $costCanBuildCreate
211
   *
212
   * @return int|mixed
213
   */
214
  public static function getBuildStatus(&$user, $planet, $unit_id, $costCanBuildCreate) {
215
    $result = eco_can_build_unit($user, $planet, $unit_id);
216
217
    // Additional check for resources. If no units can be built - it means that there are not enough resources
218
    return $result == BUILD_ALLOWED && $costCanBuildCreate < 1 ? BUILD_NO_RESOURCES : $result;
219
  }
220
221
222
}
223