Completed
Push — work-fleets ( fff2b6...e0e753 )
by SuperNova.WS
06:54
created

eco_get_planet_caps.php ➔ eco_get_planet_caps()   F

Complexity

Conditions 31
Paths 19449

Size

Total Lines 99
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 31
eloc 62
nc 19449
nop 3
dl 0
loc 99
rs 2
c 2
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
function eco_get_planet_caps_modify_production(&$item, $key, $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
4
  static $modifiers;
5
6
  if(!$modifiers) {
7
    $modifiers = sn_get_groups('modifiers');
8
  }
9
  $item = floor(mrc_modify_value($data['user'], $data['planet'], $modifiers[MODIFIER_RESOURCE_PRODUCTION], $item));
10
}
11
12
function eco_get_planet_caps(&$user, &$planet_row, $production_time = 0) {
0 ignored issues
show
Unused Code introduced by
The parameter $production_time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
  // TODO Считать $production_time для термоядерной электростанции
14
  static $sn_group_modifiers, $config_resource_multiplier, $config_resource_multiplier_plain, $config_eco_scale_storage;
15
16
  if(!$sn_group_modifiers) {
17
    $sn_group_modifiers = sn_get_groups('modifiers');
18
    $config_resource_multiplier = game_resource_multiplier();
19
    $config_resource_multiplier_plain = game_resource_multiplier(true);
20
    $config_eco_scale_storage = classSupernova::$config->eco_scale_storage ? $config_resource_multiplier_plain : 1;
21
  }
22
23
  $caps = array();
24
  $caps['storage'][RES_METAL][0] = classSupernova::$config->eco_planet_storage_metal;
0 ignored issues
show
Documentation introduced by
The property eco_planet_storage_metal does not exist on object<classConfig>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
25
  $caps['storage'][RES_CRYSTAL][0] = classSupernova::$config->eco_planet_storage_crystal;
0 ignored issues
show
Documentation introduced by
The property eco_planet_storage_crystal does not exist on object<classConfig>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
  $caps['storage'][RES_DEUTERIUM][0] = classSupernova::$config->eco_planet_storage_deuterium;
0 ignored issues
show
Documentation introduced by
The property eco_planet_storage_deuterium does not exist on object<classConfig>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
  foreach(sn_get_groups('storages') as $unit_id) {
28
    foreach(get_unit_param($unit_id, P_STORAGE) as $resource_id => $function) {
29
      $caps['storage'][$resource_id][$unit_id] = floor($config_eco_scale_storage *
30
        mrc_modify_value($user, $planet_row, $sn_group_modifiers[MODIFIER_RESOURCE_CAPACITY], $function(mrc_get_level($user, $planet_row, $unit_id)))
31
      );
32
    }
33
  }
34
35
  if($planet_row['planet_type'] == PT_MOON) {
36
    return $caps;
37
  }
38
39
  $sn_group_planet_density = sn_get_groups('planet_density');
40
  $planet_density = $sn_group_planet_density[$planet_row['density_index']][UNIT_RESOURCES];
41
42
  $caps['production_full'][RES_METAL][0] = floor(classSupernova::$config->metal_basic_income * $config_resource_multiplier * (isset($planet_density[RES_METAL]) ? $planet_density[RES_METAL] : 1));
43
  $caps['production_full'][RES_CRYSTAL][0] = floor(classSupernova::$config->crystal_basic_income * $config_resource_multiplier * (isset($planet_density[RES_CRYSTAL]) ? $planet_density[RES_CRYSTAL] : 1));
44
  $caps['production_full'][RES_DEUTERIUM][0] = floor(classSupernova::$config->deuterium_basic_income * $config_resource_multiplier * (isset($planet_density[RES_DEUTERIUM]) ? $planet_density[RES_DEUTERIUM] : 1));
45
  $caps['production_full'][RES_ENERGY][0] = floor(classSupernova::$config->energy_basic_income * $config_resource_multiplier_plain * (isset($planet_density[RES_ENERGY]) ? $planet_density[RES_ENERGY] : 1));
46
47
  foreach(sn_get_groups('factories') as $unit_id) {
48
    $unit_data = get_unit_param($unit_id);
49
    $unit_level = mrc_get_level($user, $planet_row, $unit_id);
50
    $unit_load = $planet_row[pname_factory_production_field_name($unit_id)];
51
52
    foreach($unit_data[P_UNIT_PRODUCTION] as $resource_id => $function) {
53
      $caps['production_full'][$resource_id][$unit_id] = $function($unit_level, $unit_load, $user, $planet_row)
54
        * ($resource_id == RES_ENERGY ? $config_resource_multiplier_plain : $config_resource_multiplier)
55
        * (isset($planet_density[$resource_id]) ? $planet_density[$resource_id] : 1);
56
    }
57
  }
58
59
  array_walk_recursive($caps['production_full'], 'eco_get_planet_caps_modify_production', array('user' => $user, 'planet' => $planet_row));
60
61
  foreach($caps['production_full'] as $resource_id => $resource_data) {
62
    $caps['total_production_full'][$resource_id] = array_sum($resource_data);
63
  }
64
65
  $caps['production'] = $caps['production_full'];
66
67
  if($caps['production'][RES_ENERGY][STRUC_MINE_FUSION]) {
68
    $deuterium_balance = array_sum($caps['production'][RES_DEUTERIUM]);
69
    $energy_balance = array_sum($caps['production'][RES_ENERGY]);
70
    if($deuterium_balance < 0 || $energy_balance < 0) {
71
      $caps['production'][RES_DEUTERIUM][STRUC_MINE_FUSION] = $caps['production'][RES_ENERGY][STRUC_MINE_FUSION] = 0;
72
    }
73
  }
74
75
  foreach($caps['production'][RES_ENERGY] as $energy) {
76
    $caps[RES_ENERGY][$energy >= 0 ? BUILD_CREATE : BUILD_DESTROY] += $energy;
77
  }
78
79
  $caps[RES_ENERGY][BUILD_DESTROY] = -$caps[RES_ENERGY][BUILD_DESTROY];
80
81
  $caps['efficiency'] = $caps[RES_ENERGY][BUILD_DESTROY] > $caps[RES_ENERGY][BUILD_CREATE]
82
    ? $caps[RES_ENERGY][BUILD_CREATE] / $caps[RES_ENERGY][BUILD_DESTROY]
83
    : 1;
84
85
  foreach($caps['production'] as $resource_id => &$resource_data) {
86
    if($caps['efficiency'] != 1) {
87
      foreach($resource_data as $unit_id => &$resource_production) {
88
        if(!($unit_id == STRUC_MINE_FUSION && $resource_id == RES_DEUTERIUM) && $unit_id != 0 && !($resource_id == RES_ENERGY && $resource_production >= 0)) {
89
          $resource_production = $resource_production * $caps['efficiency'];
90
        }
91
      }
92
    }
93
    $caps['total'][$resource_id] = array_sum($resource_data);
94
    $caps['total'][$resource_id] = $caps['total'][$resource_id] >= 0 ? floor($caps['total'][$resource_id]) : ceil($caps['total'][$resource_id]);
95
  }
96
97
  foreach($caps['storage'] as $resource_id => &$resource_data) {
98
    $caps['total_storage'][$resource_id] = array_sum($resource_data);
99
  }
100
101
  $planet_row['caps'] = $caps;
102
103
  $planet_row['metal_max'] = $caps['total_storage'][RES_METAL];
104
  $planet_row['crystal_max'] = $caps['total_storage'][RES_CRYSTAL];
105
  $planet_row['deuterium_max'] = $caps['total_storage'][RES_DEUTERIUM];
106
  $planet_row['energy_max'] = $caps[RES_ENERGY][BUILD_CREATE];
107
  $planet_row['energy_used'] = $caps[RES_ENERGY][BUILD_DESTROY];
108
109
  return $caps;
110
}
111