Completed
Push — trunk ( 8dcff2...338765 )
by SuperNova.WS
04:11
created

updateProductionSpeeds()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 21
nc 14
nop 4
dl 0
loc 32
rs 4.909
c 1
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 25.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * resources.php
5
 *
6
 * Planet resource interface page
7
 *
8
 * 2.2 - copyright (ñ) 2010 by Gorlum for http://supernova.ws
9
 *   [~] - more optimization to utilize PTE
10
 *   [~] - code formatting according to PCG
11
 *   [~] - content of BuildRessourcePage.php moved to resource.php
12
 * 2.1 - copyright 2010 by Gorlum for http://supernova.ws
13
 *   [~] - Security checked for SQL-injection
14
 * 2.0 - copyright 2010 by Gorlum for http://supernova.ws
15
 *   [+] - almost fully rewrote and optimized
16
 * 1.0 [BuildRessourcePage.php] copyright 2008 by ShadoV for XNova
17
 *   [+] - Mise en module initiale (creation)
18
 * 1.1 - copyright 2010 by Gorlum for http://supernova.ws
19
 *   [%] - Security checks & tests by Gorlum for http://supernova.ws
20
 * 1.0 copyright (ñ) 2008 by Chlorel for XNova
21
 *   [!] - Passage en fonction pour utilisation XNova
22
 *
23
**/
24
25
include('common.' . substr(strrchr(__FILE__, '.'), 1));
26
27
use \Meta\Economic\ResourceCalculations;
28
use Planet\DBStaticPlanet;
29
use Planet\Planet;
30
31
/**
32
 * @param $resource_id
33
 * @param ResourceCalculations $capsObj
34
 */
35
function int_calc_storage_bar($resource_id, $capsObj)
36
{
37
  global $lang, $template, $planetrow, $user;
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...
38
39
  $totalProduction      = $capsObj->getProduction($resource_id);
40
  $storage_fill         = $capsObj->getStorage($resource_id) ? floor(mrc_get_level($user, $planetrow, $resource_id) / $capsObj->getStorage($resource_id) * 100) : 0;
41
42
  $template->assign_block_vars('resources', [
43
    'NAME'        => $lang["sys_" . pname_resource_name($resource_id)],
44
45
    'HOURLY'      => $totalProduction,
46
    'DAILY'       => $totalProduction * 24,
47
    'WEEKLY'      => $totalProduction * 24 * 7,
48
    'MONTHLY'     => $totalProduction * 24 * 30,
49
50
    'STORAGE'     => intval($storage_fill),
51
    'BAR'         => min($storage_fill, 100),
52
  ]);
53
};
54
55
$ValidList['percent'] = array (  0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100 );
56
$template = gettemplate('resources', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type null|template expected by parameter $template of gettemplate(). ( Ignorable by Annotation )

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

56
$template = gettemplate('resources', /** @scrutinizer ignore-type */ true);
Loading history...
57
58
/** @noinspection PhpUnhandledExceptionInspection */
59
$planet = SN::$gc->repoV2->getPlanet($planetrow['id']);
60
if(!empty($transmutation_result = $planet->sn_sys_planet_core_transmute($user))) {
0 ignored issues
show
Bug introduced by
The method sn_sys_planet_core_transmute() does not exist on Core\EntityDb. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

60
if(!empty($transmutation_result = $planet->/** @scrutinizer ignore-call */ sn_sys_planet_core_transmute($user))) {
Loading history...
61
  $template->assign_block_vars('result', $transmutation_result);
62
  $planet->dbLoadRecord($planetrow['id']);
63
}
64
65
$sn_group_factories = sn_get_groups('factories');
66
/**
67
 * @param debug  $debug
68
 * @param array  $sn_group_factories
69
 * @param array  $planetrow
70
 * @param Planet $planet
71
 *
72
 * @return mixed
73
 */
74
function updateProductionSpeeds($debug, $sn_group_factories, $planetrow, $planet) {
75
  $production = $_POST['production'];
76
  if (!is_array($production)) {
77
    return $planetrow;
78
  }
79
80
  $SubQry = [];
81
  foreach ($production as $prod_id => $percent) {
82
    if ($percent > 100 || $percent < 0) {
83
      $debug->warning('Supplying wrong production percent (less then 0 or greater then 100)', 'Hack attempt', 302, array('base_dump' => true));
84
      die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
85
    }
86
87
    $prod_id = intval($prod_id);
88
    if (in_array($prod_id, $sn_group_factories) && get_unit_param($prod_id, P_MINING_IS_MANAGED)) {
89
      $field_name = pname_factory_production_field_name($prod_id);
90
      $percent = floor($percent / 10);
91
      $planetrow[$field_name] = $percent;
92
      //$SubQry                 .= "`{$field_name}` = '{$percent}',";
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...
93
      $SubQry[] = "`{$field_name}` = '{$percent}'";
94
    } else {
95
      $debug->warning('Supplying wrong ID in production array - attempt to change some field - ID' . $prod_id, 'Resource Page', 301);
96
      continue;
97
    }
98
  }
99
100
  !empty($SubQry) ? DBStaticPlanet::db_planet_set_by_id($planetrow['id'], implode(',', $SubQry)) : false;
101
  if (!empty($SubQry)) {
102
    $planet->dbLoadRecord($planetrow['id']);
103
  }
104
105
  return $planetrow;
106
}
107
108
$planetrow = updateProductionSpeeds($debug, $sn_group_factories, $planetrow, $planet);
109
110
// -------------------------------------------------------------------------------------------------------
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
111
// $BuildTemp                   = $planetrow[ 'temp_max' ];
112
// $BuildEnergyTech             = $user['energy_tech'];
113
for ($Option = 10; $Option >= 0; $Option--)
114
{
115
 $template->assign_block_vars('option', array(
116
   'VALUE' => $Option * 10,
117
 ));
118
}
119
120
$capsObj = new ResourceCalculations();
121
$capsObj->eco_get_planet_caps($user, $planetrow, 3600);
122
123
$template->assign_block_vars('production', array(
124
  'TYPE'           => $lang['res_basic_income'],
125
126
  'METAL_TYPE'     => $capsObj->productionCurrentMatrix[RES_METAL][0],
127
  'CRYSTAL_TYPE'   => $capsObj->productionCurrentMatrix[RES_CRYSTAL][0],
128
  'DEUTERIUM_TYPE' => $capsObj->productionCurrentMatrix[RES_DEUTERIUM][0],
129
  'ENERGY_TYPE'    => $capsObj->productionCurrentMatrix[RES_ENERGY][0],
130
));
131
132
foreach($sn_group_factories as $unit_id)
133
{
134
  if(mrc_get_level($user, $planetrow, $unit_id) > 0 && get_unit_param($unit_id))
135
  {
136
    $level_plain = mrc_get_level($user, $planetrow, $unit_id, false, true);
137
    $template->assign_block_vars('production', array(
138
      'ID'             => $unit_id,
139
      'PERCENT'        => $planetrow[pname_factory_production_field_name($unit_id)] * 10,
140
      'TYPE'           => $lang['tech'][$unit_id],
141
      'LEVEL'          => $level_plain,
142
      'LEVEL_BONUS'    => mrc_get_level($user, $planetrow, $unit_id) - $level_plain,
143
      'LEVEL_TYPE'     => ($unit_id > 200) ? $lang['quantity'] : $lang['level'],
144
145
      'METAL_TYPE'     => $capsObj->productionCurrentMatrix[RES_METAL][$unit_id],
146
      'CRYSTAL_TYPE'   => $capsObj->productionCurrentMatrix[RES_CRYSTAL][$unit_id],
147
      'DEUTERIUM_TYPE' => $capsObj->productionCurrentMatrix[RES_DEUTERIUM][$unit_id],
148
      'ENERGY_TYPE'    => $capsObj->productionCurrentMatrix[RES_ENERGY][$unit_id],
149
150
      'METAL_FULL'     => $capsObj->productionFullMatrix[RES_METAL][$unit_id],
151
      'CRYSTAL_FULL'   => $capsObj->productionFullMatrix[RES_CRYSTAL][$unit_id],
152
      'DEUTERIUM_FULL' => $capsObj->productionFullMatrix[RES_DEUTERIUM][$unit_id],
153
      'ENERGY_FULL'    => $capsObj->productionFullMatrix[RES_ENERGY][$unit_id],
154
155
      'P_MINING_IS_MANAGED' => get_unit_param($unit_id, P_MINING_IS_MANAGED),
156
    ));
157
  }
158
}
159
160
161
$user_dark_matter = mrc_get_level($user, false, RES_DARK_MATTER);
162
$template->assign_recursive($planet->tpl_planet_density_info($user_dark_matter));
163
164
$template->assign_block_vars('production', array(
165
  'TYPE'           => $lang['res_total'],
166
167
  'METAL_TYPE'     => $capsObj->getProduction(RES_METAL),
168
  'CRYSTAL_TYPE'   => $capsObj->getProduction(RES_CRYSTAL),
169
  'DEUTERIUM_TYPE' => $capsObj->getProduction(RES_DEUTERIUM),
170
  'ENERGY_TYPE'    => $capsObj->getProduction(RES_ENERGY),
171
172
  'METAL_FULL'     => $capsObj->getProductionFull(RES_METAL),
173
  'CRYSTAL_FULL'   => $capsObj->getProductionFull(RES_CRYSTAL),
174
  'DEUTERIUM_FULL' => $capsObj->getProductionFull(RES_DEUTERIUM),
175
  'ENERGY_FULL'    => $capsObj->getProductionFull(RES_ENERGY),
176
));
177
178
int_calc_storage_bar(RES_METAL, $capsObj);
179
int_calc_storage_bar(RES_CRYSTAL, $capsObj);
180
int_calc_storage_bar(RES_DEUTERIUM, $capsObj);
181
182
$template->assign_vars(array(
183
 'PLANET_NAME'          => $planetrow['name'],
184
 'PLANET_TYPE'          => $planetrow['planet_type'],
185
186
 'PRODUCTION_LEVEL'     => floor($capsObj->efficiency * 100),
187
188
 'PAGE_HINT'            => $lang['res_hint'],
189
));
190
191
display($template, $lang['res_planet_production']);
192