Completed
Push — trunk ( 0e7a2e...6a6c5e )
by SuperNova.WS
07:28
created

PageOverview::manage()   F

Complexity

Conditions 24
Paths 354

Size

Total Lines 153
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
cc 24
eloc 104
nc 354
nop 2
dl 0
loc 153
ccs 0
cts 124
cp 0
crap 600
rs 3.5633
c 0
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
 * Created by Gorlum 25.01.2018 7:50
4
 */
5
6
namespace Pages\Deprecated;
7
8
use Planet\DBStaticPlanet;
9
use SN;
10
use HelperString;
11
use Exception;
12
use Planet\Planet;
13
use template;
14
15
class PageOverview extends PageDeprecated {
16
  /**
17
   * @var \classConfig $config
18
   */
19
  protected $config;
20
  /**
21
   * @var \core_auth $auth
22
   */
23
  protected $auth;
24
  /**
25
   * @var \classLocale $lang
26
   */
27
  protected $lang;
28
29
  /**
30
   * @var Planet $planet
31
   */
32
  protected $planet;
33
34
  public function __construct() {
35
    parent::__construct();
36
37
    $this->lang = SN::$lang;
38
    $this->config = SN::$config;
39
    $this->auth = SN::$auth;
40
41
    lng_include('overview');
42
    lng_include('mrc_mercenary');
43
  }
44
45
  public function setPlanetById($planetId) {
46
    $this->planet = new Planet($planetId);
47
  }
48
49
  public function getPlanet() {
50
    if (empty($this->planet)) {
51
      throw new Exception('No planet in Overview');
52
    }
53
54
    return $this->planet;
55
  }
56
57
  public function route() {
58
    global $user, $planetrow, $que, $user_option_list;
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...
59
60
    $this->setPlanetById($planetrow['id']);
61
62
    switch ($mode = sys_get_param_str('mode')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $mode is dead and can be removed.
Loading history...
63
      case 'manage':
64
        $this->manage($user, $planetrow);
65
      break;
66
67
      default:
68
        $this->overview($user, $planetrow, $que, $user_option_list);
69
      break;
70
    }
71
  }
72
73
  /**
74
   * @param $user
75
   * @param $planetrow
76
   * @param $que
77
   * @param $user_option_list
78
   */
79
  public function overview($user, $planetrow, $que, $user_option_list) {
80
    $this->setPlanetById($planetrow['id']);
81
82
    $this->planet->sn_sys_sector_buy();
0 ignored issues
show
Deprecated Code introduced by
The function Planet\Planet::sn_sys_sector_buy() has been deprecated. ( Ignorable by Annotation )

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

82
    /** @scrutinizer ignore-deprecated */ $this->planet->sn_sys_sector_buy();
Loading history...
83
84
    rpg_level_up($user, RPG_STRUCTURE);
85
    rpg_level_up($user, RPG_RAID);
86
    rpg_level_up($user, RPG_TECH);
87
    rpg_level_up($user, RPG_EXPLORE);
88
89
    if (sys_get_param_str('rename') && $new_name = sys_get_param_str('new_name')) {
90
      $planetrow['name'] = $new_name;
91
      $new_name_safe = db_escape($new_name);
92
      DBStaticPlanet::db_planet_set_by_id($planetrow['id'], "`name` = '{$new_name_safe}'");
93
    }
94
95
    if (!empty($theResult = $this->planet->sn_sys_planet_core_transmute($user))) {
0 ignored issues
show
Deprecated Code introduced by
The function Planet\Planet::sn_sys_planet_core_transmute() has been deprecated. ( Ignorable by Annotation )

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

95
    if (!empty($theResult = /** @scrutinizer ignore-deprecated */ $this->planet->sn_sys_planet_core_transmute($user))) {
Loading history...
96
      $this->resultMessageList->add($theResult['MESSAGE'], $theResult['STATUS']);
97
    }
98
99
    $template = gettemplate('planet_overview', 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

99
    $template = gettemplate('planet_overview', /** @scrutinizer ignore-type */ true);
Loading history...
100
101
    $user_dark_matter = mrc_get_level($user, false, RES_DARK_MATTER);
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

101
    $user_dark_matter = mrc_get_level($user, /** @scrutinizer ignore-type */ false, RES_DARK_MATTER);
Loading history...
102
    $template->assign_recursive($this->planet->tpl_planet_density_info($user_dark_matter));
0 ignored issues
show
Bug introduced by
It seems like $user_dark_matter can also be of type boolean and double; however, parameter $user_dark_matter of Planet\Planet::tpl_planet_density_info() does only seem to accept integer, 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

102
    $template->assign_recursive($this->planet->tpl_planet_density_info(/** @scrutinizer ignore-type */ $user_dark_matter));
Loading history...
103
104
    $fleets_to_planet = [];
105
    $planet_count = 0;
106
    $planets_query = DBStaticPlanet::db_planet_list_sorted($user, false, '*');
107
    foreach ($planets_query as $an_id => $planetRecord) {
0 ignored issues
show
Bug introduced by
The expression $planets_query of type false is not traversable.
Loading history...
108
      $fleet_list = flt_get_fleets_to_planet($planetRecord);
109
      if (!empty($fleet_list['own']['count'])) {
110
        $fleets_to_planet[$an_id] = tpl_parse_fleet_sn($fleet_list['own']['total'], getUniqueFleetId($planetRecord));
111
      }
112
113
      if ($planetRecord['planet_type'] == PT_MOON) {
114
        continue;
115
      }
116
117
      $planet_count++;
118
119
      sn_db_transaction_start();
120
      $updatedData = sys_o_get_updated($user, $planetRecord['id'], SN_TIME_NOW, false, true);
121
      sn_db_transaction_commit();
122
123
      $templatizedPlanet = tpl_parse_planet($user, $updatedData['planet']);
124
      $templatizedPlanet += tpl_parse_planet_result_fleet($updatedData['planet'], $fleet_list);
125
      $templatizedPlanet += tpl_parse_planet_moon($planetRecord['id']);
126
127
      $template->assign_block_vars('planet', $templatizedPlanet);
128
    }
129
130
    $fleets = flt_parse_fleets_to_events(fleet_and_missiles_list_incoming($user['id']));
131
    tpl_assign_fleet($template, $fleets_to_planet);
0 ignored issues
show
Deprecated Code introduced by
The function tpl_assign_fleet() has been deprecated. ( Ignorable by Annotation )

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

131
    /** @scrutinizer ignore-deprecated */ tpl_assign_fleet($template, $fleets_to_planet);
Loading history...
132
    tpl_assign_fleet($template, $fleets);
0 ignored issues
show
Deprecated Code introduced by
The function tpl_assign_fleet() has been deprecated. ( Ignorable by Annotation )

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

132
    /** @scrutinizer ignore-deprecated */ tpl_assign_fleet($template, $fleets);
Loading history...
133
134
    $lune = $planetrow['planet_type'] == PT_PLANET ? DBStaticPlanet::db_planet_by_parent($planetrow['id']) : DBStaticPlanet::db_planet_by_id($planetrow['parent_planet']);
135
    if ($lune) {
136
      $template->assign_vars([
137
        'MOON_ID'   => $lune['id'],
138
        'MOON_IMG'  => $lune['image'],
139
        'MOON_NAME' => $lune['name'],
140
      ]);
141
    }
142
143
    $template->assign_recursive($this->planet->int_planet_pretemplate($user));
144
145
    if (!defined('GAME_STRUCTURES_DISABLED') || !GAME_STRUCTURES_DISABLED) {
0 ignored issues
show
Bug introduced by
The constant Pages\Deprecated\GAME_STRUCTURES_DISABLED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
146
      $this->templateQue($template, QUE_STRUCTURES, $que);
147
    }
148
149
    $this->templateQue($template, SUBQUE_FLEET, $que);
150
    if (!defined('GAME_DEFENSE_DISABLED') || !GAME_DEFENSE_DISABLED) {
0 ignored issues
show
Bug introduced by
The constant Pages\Deprecated\GAME_DEFENSE_DISABLED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
151
      $this->templateQue($template, SUBQUE_DEFENSE, $que);
152
    }
153
154
    $template->assign_vars($this->templateGridSizes($user, $user_option_list, $planet_count));
155
156
    $template->assign_vars($this->templateSector($user, $user_dark_matter));
157
158
    $planet_recyclers_orbiting = 0;
159
    foreach (sn_get_groups('flt_recyclers') as $recycler_id) {
160
      $planet_recyclers_orbiting += mrc_get_level($user, $planetrow, $recycler_id);
161
    }
162
    $governor_level = $planetrow['PLANET_GOVERNOR_ID'] ? mrc_get_level($user, $planetrow, $planetrow['PLANET_GOVERNOR_ID'], false, true) : 0;
163
    $template->assign_vars([
164
      'USER_ID'        => $user['id'],
165
      'user_username'  => $user['username'],
166
      'USER_AUTHLEVEL' => $user['authlevel'],
167
168
      'NEW_MESSAGES' => $user['new_message'],
169
      // TODO
170
      // 'NEW_LEVEL_MINER' => $level_miner,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
171
      // 'NEW_LEVEL_RAID'  => $level_raid,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
172
173
      'planet_diameter' => HelperString::numberFloorAndFormat($planetrow['diameter']),
174
175
      'metal_debris'         => HelperString::numberFloorAndFormat($planetrow['debris_metal']),
176
      'crystal_debris'       => HelperString::numberFloorAndFormat($planetrow['debris_crystal']),
177
      'PLANET_RECYCLERS'     => $planet_recyclers_orbiting,
178
      'planet_image'         => $planetrow['image'],
179
      'planet_temp_min'      => $planetrow['temp_min'],
180
      'planet_temp_avg'      => round(($planetrow['temp_min'] + $planetrow['temp_max']) / 2),
181
      'planet_temp_max'      => $planetrow['temp_max'],
182
      'planet_density'       => $planetrow['density'],
183
      'planet_density_index' => $planetrow['density_index'],
184
      'planet_density_text'  => $this->lang['uni_planet_density_types'][$planetrow['density_index']],
185
186
      'GATE_LEVEL'          => mrc_get_level($user, $planetrow, STRUC_MOON_GATE),
187
      'GATE_JUMP_REST_TIME' => uni_get_time_to_jump($planetrow),
188
189
      'ADMIN_EMAIL' => $this->config->game_adminEmail,
190
191
      'PLANET_GOVERNOR_ID'         => $planetrow['PLANET_GOVERNOR_ID'],
192
      'PLANET_GOVERNOR_LEVEL'      => $governor_level,
193
      'PLANET_GOVERNOR_LEVEL_PLUS' => mrc_get_level($user, $planetrow, $planetrow['PLANET_GOVERNOR_ID']) - $governor_level,
194
      'PLANET_GOVERNOR_NAME'       => $this->lang['tech'][$planetrow['PLANET_GOVERNOR_ID']],
195
196
      'IS_CAPITAL'  => $planetrow['planet_type'] == PT_PLANET && $planetrow['id'] == $user['id_planet'],
197
      'IS_MOON'     => $planetrow['planet_type'] == PT_MOON,
198
199
      'DARK_MATTER' => $user_dark_matter,
200
201
      'PAGE_HEADER' => $this->lang['ov_overview'] . " - " . $this->lang['sys_planet_type'][$planetrow['planet_type']] . " {$planetrow['name']} [{$planetrow['galaxy']}:{$planetrow['system']}:{$planetrow['planet']}]",
202
    ]);
203
    tpl_set_resource_info($template, $planetrow, $fleets_to_planet);
204
205
    $this->resultMessageList->templateAdd($template);
206
207
    display($template);
208
  }
209
210
  /**
211
   * @param $user
212
   * @param $planetrow
213
   */
214
  public function manage($user, $planetrow) {
215
    $this->setPlanetById($planetrow['id']);
216
217
    $this->planet->sn_sys_sector_buy('overview.php?mode=manage');
0 ignored issues
show
Deprecated Code introduced by
The function Planet\Planet::sn_sys_sector_buy() has been deprecated. ( Ignorable by Annotation )

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

217
    /** @scrutinizer ignore-deprecated */ $this->planet->sn_sys_sector_buy('overview.php?mode=manage');
Loading history...
218
219
    $user_dark_matter = mrc_get_level($user, false, RES_DARK_MATTER);
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

219
    $user_dark_matter = mrc_get_level($user, /** @scrutinizer ignore-type */ false, RES_DARK_MATTER);
Loading history...
220
    if (!empty($theResult = $this->planet->sn_sys_planet_core_transmute($user))) {
0 ignored issues
show
Deprecated Code introduced by
The function Planet\Planet::sn_sys_planet_core_transmute() has been deprecated. ( Ignorable by Annotation )

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

220
    if (!empty($theResult = /** @scrutinizer ignore-deprecated */ $this->planet->sn_sys_planet_core_transmute($user))) {
Loading history...
221
      $this->resultMessageList->add($theResult['MESSAGE'], $theResult['STATUS']);
222
    }
223
224
    $template = gettemplate('planet_manage', 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

224
    $template = gettemplate('planet_manage', /** @scrutinizer ignore-type */ true);
Loading history...
225
    $planet_id = sys_get_param_id('planet_id');
226
227
    if (sys_get_param_str('rename') && $new_name = sys_get_param_str('new_name')) {
228
      $planetrow['name'] = $new_name;
229
      DBStaticPlanet::db_planet_set_by_id($planetrow['id'], "`name` = '{$new_name}'");
230
    } elseif (sys_get_param_str('action') == 'make_capital') {
231
      try {
232
        sn_db_transaction_start();
233
        $user = db_user_by_id($user['id'], true, '*');
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

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

233
        $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true, '*');
Loading history...
234
        $planetrow = DBStaticPlanet::db_planet_by_id($planetrow['id'], true, '*');
235
236
        if ($planetrow['planet_type'] != PT_PLANET) {
237
          throw new exception($this->lang['ov_capital_err_not_a_planet'], ERR_ERROR);
238
        }
239
240
        if ($planetrow['id'] == $user['id_planet']) {
241
          throw new exception($this->lang['ov_capital_err_capital_already'], ERR_ERROR);
242
        }
243
244
        if ($user_dark_matter < $this->config->planet_capital_cost) {
245
          throw new exception($this->lang['ov_capital_err_no_dark_matter'], ERR_ERROR);
246
        }
247
248
        rpg_points_change($user['id'], RPG_CAPITAL, -$this->config->planet_capital_cost,
249
          array('Planet %s ID %d at coordinates %s now become Empire Capital', $planetrow['name'], $planetrow['id'], uni_render_coordinates($planetrow))
250
        );
251
252
        db_user_set_by_id($user['id'], "id_planet = {$planetrow['id']}, galaxy = {$planetrow['galaxy']}, system = {$planetrow['system']}, planet = {$planetrow['planet']}");
0 ignored issues
show
Deprecated Code introduced by
The function db_user_set_by_id() has been deprecated. ( Ignorable by Annotation )

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

252
        /** @scrutinizer ignore-deprecated */ db_user_set_by_id($user['id'], "id_planet = {$planetrow['id']}, galaxy = {$planetrow['galaxy']}, system = {$planetrow['system']}, planet = {$planetrow['planet']}");
Loading history...
253
254
        $user['id_planet'] = $planetrow['id'];
255
        $this->resultMessageList->add($this->lang['ov_capital_err_none'], ERR_NONE);
256
        sn_db_transaction_commit();
257
        sys_redirect('overview.php?mode=manage');
258
      } catch (exception $e) {
259
        sn_db_transaction_rollback();
260
        $this->resultMessageList->add($e->getMessage(), $e->getCode());
261
      }
262
    } elseif (sys_get_param_str('action') == 'planet_teleport') {
263
      try {
264
        if (!uni_coordinates_valid($new_coordinates = array(
265
          'galaxy' => sys_get_param_int('new_galaxy'),
266
          'system' => sys_get_param_int('new_system'),
267
          'planet' => sys_get_param_int('new_planet')))
268
        ) {
269
          throw new exception($this->lang['ov_teleport_err_wrong_coordinates'], ERR_ERROR);
270
        }
271
272
        sn_db_transaction_start();
273
        // При телепорте обновлять данные не надо - просто получить текущие данные и залочить их
274
        $user = db_user_by_id($user['id'], true, '*');
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

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

274
        $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true, '*');
Loading history...
275
        $planetrow = DBStaticPlanet::db_planet_by_id($planetrow['id'], true, '*');
276
277
        $can_teleport = uni_planet_teleport_check($user, $planetrow, $new_coordinates);
278
        if ($can_teleport['result'] != ERR_NONE) {
279
          throw new exception($can_teleport['message'], $can_teleport['result']);
280
        }
281
282
        rpg_points_change($user['id'], RPG_TELEPORT, -$this->config->planet_teleport_cost,
283
          array($this->lang['ov_teleport_log_record'], $planetrow['name'], $planetrow['id'], uni_render_coordinates($planetrow), uni_render_coordinates($new_coordinates))
284
        );
285
        $planet_teleport_next = SN_TIME_NOW + $this->config->planet_teleport_timeout;
286
        DBStaticPlanet::db_planet_set_by_gspt($planetrow['galaxy'], $planetrow['system'], $planetrow['planet'], PT_ALL,
287
          "galaxy = {$new_coordinates['galaxy']}, system = {$new_coordinates['system']}, planet = {$new_coordinates['planet']}, planet_teleport_next = {$planet_teleport_next}");
288
289
        if ($planetrow['id'] == $user['id_planet']) {
290
          db_user_set_by_id($user['id'], "galaxy = {$new_coordinates['galaxy']}, system = {$new_coordinates['system']}, planet = {$new_coordinates['planet']}");
0 ignored issues
show
Deprecated Code introduced by
The function db_user_set_by_id() has been deprecated. ( Ignorable by Annotation )

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

290
          /** @scrutinizer ignore-deprecated */ db_user_set_by_id($user['id'], "galaxy = {$new_coordinates['galaxy']}, system = {$new_coordinates['system']}, planet = {$new_coordinates['planet']}");
Loading history...
291
        }
292
        sn_db_transaction_commit();
293
294
        $user = db_user_by_id($user['id'], true, '*');
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

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

294
        $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true, '*');
Loading history...
295
        $planetrow = DBStaticPlanet::db_planet_by_id($planetrow['id'], true, '*');
296
        $this->resultMessageList->add($this->lang['ov_teleport_err_none'], ERR_NONE);
297
        sys_redirect('overview.php?mode=manage');
298
      } catch (exception $e) {
299
        sn_db_transaction_rollback();
300
        $this->resultMessageList->add($e->getMessage(), $e->getCode());
301
      }
302
    } elseif (sys_get_param_str('action') == 'planet_abandon') {
303
      if ($this->auth->password_check(sys_get_param('abandon_confirm'))) {
304
        if ($user['id_planet'] != $user['current_planet'] && $user['current_planet'] == $planet_id) {
305
          $destroyed = SN_TIME_NOW + 60 * 60 * 24;
306
          DBStaticPlanet::db_planet_set_by_id($user['current_planet'], "`destruyed`='{$destroyed}', `id_owner`=0");
307
          DBStaticPlanet::db_planet_set_by_parent($user['current_planet'], "`destruyed`='{$destroyed}', `id_owner`=0");
308
          db_user_set_by_id($user['id'], '`current_planet` = `id_planet`');
0 ignored issues
show
Deprecated Code introduced by
The function db_user_set_by_id() has been deprecated. ( Ignorable by Annotation )

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

308
          /** @scrutinizer ignore-deprecated */ db_user_set_by_id($user['id'], '`current_planet` = `id_planet`');
Loading history...
309
          messageBox($this->lang['ov_delete_ok'], $this->lang['colony_abandon'], 'overview.php?mode=manage');
310
        } else {
311
          messageBox($this->lang['ov_delete_wrong_planet'], $this->lang['colony_abandon'], 'overview.php?mode=manage');
312
        }
313
      } else {
314
        messageBox($this->lang['ov_delete_wrong_pass'], $this->lang['colony_abandon'], 'overview.php?mode=manage');
315
      }
316
    } elseif (($hire = sys_get_param_int('hire')) && in_array($hire, sn_get_groups('governors'))) {
317
      $this->planet->governorHire($hire);
318
319
      sys_redirect('overview.php?mode=manage');
320
      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...
321
    }
322
323
    // TODO - refresh planet by itself
324
    $this->setPlanetById($planetrow['id']);
325
    $template->assign_recursive($this->planet->int_planet_pretemplate($user));
326
327
    foreach (sn_get_groups('governors') as $governor_id) {
328
      if ($planetrow['planet_type'] == PT_MOON && $governor_id == MRC_TECHNOLOGIST) {
329
        continue;
330
      }
331
332
      $governor_level = $planetrow['PLANET_GOVERNOR_ID'] == $governor_id ? $planetrow['PLANET_GOVERNOR_LEVEL'] : 0;
333
      $build_data = eco_get_build_data($user, $planetrow, $governor_id, $governor_level);
334
      $template->assign_block_vars('governors', array(
335
        'ID'         => $governor_id,
336
        'NAME'       => $this->lang['tech'][$governor_id],
337
        'COST'       => $build_data[BUILD_CREATE][RES_DARK_MATTER],
338
        'MAX'        => get_unit_param($governor_id, P_MAX_STACK),
339
        'LEVEL'      => $governor_level,
340
        'LEVEL_PLUS' => mrc_get_level($user, $planetrow, $governor_id) - $governor_level,
341
      ));
342
    }
343
344
    $user_dark_matter = mrc_get_level($user, false, RES_DARK_MATTER);
345
    $template->assign_recursive($this->planet->tpl_planet_density_info($user_dark_matter));
0 ignored issues
show
Bug introduced by
It seems like $user_dark_matter can also be of type boolean and double; however, parameter $user_dark_matter of Planet\Planet::tpl_planet_density_info() does only seem to accept integer, 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

345
    $template->assign_recursive($this->planet->tpl_planet_density_info(/** @scrutinizer ignore-type */ $user_dark_matter));
Loading history...
346
347
    $template->assign_vars($this->templateSector($user, $user_dark_matter));
348
349
    $can_teleport = uni_planet_teleport_check($user, $planetrow);
350
    $template->assign_vars(array(
351
      'DARK_MATTER' => $user_dark_matter,
352
353
      'CAN_TELEPORT'         => $can_teleport['result'] == ERR_NONE,
354
      'CAN_NOT_TELEPORT_MSG' => $can_teleport['message'],
355
      'TELEPORT_COST_TEXT'   => prettyNumberStyledCompare($this->config->planet_teleport_cost, $user_dark_matter),
0 ignored issues
show
Bug introduced by
It seems like $user_dark_matter can also be of type boolean; however, parameter $compareTo of prettyNumberStyledCompare() 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

355
      'TELEPORT_COST_TEXT'   => prettyNumberStyledCompare($this->config->planet_teleport_cost, /** @scrutinizer ignore-type */ $user_dark_matter),
Loading history...
356
357
      'IS_CAPITAL'        => $planetrow['id'] == $user['id_planet'],
358
      'CAN_CAPITAL'       => $user_dark_matter >= $this->config->planet_capital_cost,
359
      'CAPITAL_COST_TEXT' => prettyNumberStyledCompare($this->config->planet_capital_cost, $user_dark_matter),
360
361
      'PAGE_HINT' => $this->lang['ov_manage_page_hint'],
362
    ));
363
364
    $this->resultMessageList->templateAdd($template);
365
366
    display($template, $this->lang['rename_and_abandon_planet']);
367
  }
368
369
  /**
370
   * @param $user
371
   * @param $user_dark_matter
372
   *
373
   * @return array
374
   */
375
  protected function templateSector($user, $user_dark_matter) {
376
    $sector_cost = eco_get_build_data($user, $this->planet->asArray(), UNIT_SECTOR, mrc_get_level($user, $this->planet->asArray(), UNIT_SECTOR), true);
377
    $sector_cost = $sector_cost[BUILD_CREATE][RES_DARK_MATTER];
378
    $planet_fill = floor($this->planet->field_current / eco_planet_fields_max($this->planet->asArray()) * 100);
0 ignored issues
show
Bug Best Practice introduced by
The property field_current does not exist on Planet\Planet. Since you implemented __get, consider adding a @property annotation.
Loading history...
379
    $planet_fill = $planet_fill > 100 ? 100 : $planet_fill;
380
    $vararray = [
381
      'planet_field_current' => $this->planet->field_current,
382
      'planet_field_max'     => eco_planet_fields_max($this->planet->asArray()),
383
      'PLANET_FILL'          => floor($this->planet->field_current / eco_planet_fields_max($this->planet->asArray()) * 100),
384
      'PLANET_FILL_BAR'      => $planet_fill,
385
      'SECTOR_CAN_BUY'       => $sector_cost <= $user_dark_matter,
386
      'SECTOR_COST'          => $sector_cost,
387
      'SECTOR_COST_TEXT'     => HelperString::numberFloorAndFormat($sector_cost),
388
    ];
389
390
    return $vararray;
391
  }
392
393
394
  /**
395
   * @param template $template
396
   * @param int      $que_type
397
   * @param          $que
398
   */
399
  protected function templateQue($template, $que_type, $que) {
400
//    $que = que_get($planet->id_owner, $planet->id, $que_type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
401
    $que = $que['ques'][$que_type][$this->planet->id_owner][$this->planet->id];
402
403
    $que_length = 0;
404
    if (!empty($que)) {
405
      foreach ($que as $que_item) {
406
        $template->assign_block_vars('que', que_tpl_parse_element($que_item));
407
      }
408
      $que_length = count($que);
409
    }
410
411
    $template->assign_block_vars('ques', [
412
      'ID'     => $que_type,
413
      'NAME'   => $this->lang['sys_ques'][$que_type],
414
      'LENGTH' => $que_length,
415
    ]);
416
  }
417
418
  /**
419
   * Calculates planet grid sizes (horizontal and vertical) for Planet Overview
420
   *
421
   * @param $user
422
   * @param $user_option_list
423
   * @param $planet_count
424
   *
425
   * @return array
426
   */
427
  protected function templateGridSizes($user, $user_option_list, $planet_count) {
428
    $overview_planet_rows = $user['opt_int_overview_planet_rows'];
429
    $overview_planet_columns = $user['opt_int_overview_planet_columns'];
430
431
    if ($overview_planet_rows <= 0 && $overview_planet_columns <= 0) {
432
      $overview_planet_rows = $user_option_list[OPT_INTERFACE]['opt_int_overview_planet_rows'];
433
      $overview_planet_columns = $user_option_list[OPT_INTERFACE]['opt_int_overview_planet_columns'];
434
    }
435
436
    if ($overview_planet_rows > 0 && $overview_planet_columns <= 0) {
437
      $overview_planet_columns = ceil($planet_count / $overview_planet_rows);
438
    }
439
    $vararray = [
440
      'LIST_ROW_COUNT'    => $overview_planet_rows,
441
      'LIST_COLUMN_COUNT' => $overview_planet_columns,
442
    ];
443
444
    return $vararray;
445
  }
446
447
}
448