Completed
Push — trunk ( 91a948...0e7a2e )
by SuperNova.WS
05:44
created

killer_add_planet()   D

Complexity

Conditions 13
Paths 168

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 18
nc 168
nop 1
dl 0
loc 35
rs 4.8178
c 0
b 0
f 0

How to fix   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
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 6 and the first side effect is on line 10.

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
use Planet\DBStaticPlanet;
4
use Unit\DBStaticUnit;
5
6
define('INSIDE', true);
7
define('INSTALL', false);
8
define('IN_ADMIN', true);
9
10
require('../common.' . substr(strrchr(__FILE__, '.'), 1));
11
12
global $lang, $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...
13
14
messageBoxAdminAccessDenied(AUTH_LEVEL_ADMINISTRATOR);
15
16
$template = gettemplate('admin/planet_compensate', 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

16
$template = gettemplate('admin/planet_compensate', /** @scrutinizer ignore-type */ true);
Loading history...
17
18
$galaxy_src = sys_get_param_int('galaxy_src');
19
$system_src = sys_get_param_int('system_src');
20
$planet_src = sys_get_param_int('planet_src');
21
22
$galaxy_dst = sys_get_param_int('galaxy_dst');
23
$system_dst = sys_get_param_int('system_dst');
24
$planet_dst = sys_get_param_int('planet_dst');
25
26
$bonus = sys_get_param_float('bonus', 1);
27
28
$username_unsafe = sys_get_param_str_unsafe('username');
29
$username = sys_get_param_escaped('username');
30
31
if ($galaxy_src) {
32
  sn_db_transaction_start();
33
  $errors = array();
34
35
  $owner = db_user_by_username($username_unsafe, true);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_username() 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

35
  $owner = /** @scrutinizer ignore-deprecated */ db_user_by_username($username_unsafe, true);
Loading history...
36
37
  $planet = sys_o_get_updated($owner, array('galaxy' => $galaxy_src, 'system' => $system_src, 'planet' => $planet_src, 'planet_type' => 1), SN_TIME_NOW);
38
  $que = $planet['que'];
39
  $planet = $planet['planet'];
40
  if (!$planet) {
41
    $errors[] = $lang['adm_pl_comp_err_0'];
42
  }
43
44
  if ($planet['destruyed']) {
45
    $errors[] = $lang['adm_pl_comp_err_1'];
46
  }
47
48
  if ($planet['id_owner'] != $owner['id'] || !$username) {
49
    $errors[] = $lang['adm_pl_comp_err_4'];
50
  }
51
52
  $destination = sys_o_get_updated($owner, array('galaxy' => $galaxy_dst, 'system' => $system_dst, 'planet' => $planet_dst, 'planet_type' => 1), SN_TIME_NOW);
53
  $destination = $destination['planet'];
54
  if (!$destination) {
55
    $errors[] = $lang['adm_pl_comp_err_2'];
56
  }
57
58
  if ($planet['id'] == $destination['id']) {
59
    $errors[] = $lang['adm_pl_comp_err_5'];
60
  }
61
62
  if ($planet['id_owner'] != $destination['id_owner']) {
63
    $errors[] = $lang['adm_pl_comp_err_3'];
64
  }
65
66
  if (!empty($errors)) {
67
    foreach ($errors as $error) {
68
      $template->assign_block_vars('error', array(
69
        'TEXT' => $error,
70
      ));
71
    }
72
  } else {
73
    $template->assign_var('CHECK', 1);
74
75
    killer_add_planet($planet);
76
77
    $moon = DBStaticPlanet::db_planet_by_gspt($galaxy_src, $system_src, $planet_src, PT_MOON, true);
78
    if ($moon) {
79
      $moon = sys_o_get_updated($owner, $moon, SN_TIME_NOW);
80
      $moon = $moon['planet'];
81
      killer_add_planet($moon);
82
    }
83
84
    foreach (sn_get_groups('resources_loot') as $resource_id) {
85
      $resource_name = pname_resource_name($resource_id);
86
      $template->assign_var("{$resource_name}_cost", $final_cost[$resource_id]);
87
      $final_cost[$resource_id] = floor($final_cost[$resource_id] * $bonus);
88
      $template->assign_var("{$resource_name}_bonus", $final_cost[$resource_id]);
89
    }
90
91
    if ($_GET['btn_confirm']) {
92
      $time = SN_TIME_NOW + PERIOD_DAY;
93
94
      DBStaticUnit::db_unit_list_delete($planet['id_owner'], LOC_PLANET, $planet['id']);
95
      DBStaticPlanet::db_planet_set_by_id($planet['id'], "id_owner = 0, destruyed = {$time}");
96
      if ($moon) {
97
        DBStaticUnit::db_unit_list_delete($planet['id_owner'], LOC_PLANET, $moon['id']);
98
        DBStaticPlanet::db_planet_set_by_id($moon['id'], "id_owner = 0, destruyed = {$time}");
99
      }
100
101
      DBStaticPlanet::db_planet_set_by_id($destination['id'], "metal = metal + '{$final_cost[RES_METAL]}', crystal = crystal + '{$final_cost[RES_CRYSTAL]}', deuterium = deuterium + '{$final_cost[RES_DEUTERIUM]}'");
102
      $template->assign_var('CHECK', 2);
103
    }
104
  }
105
  sn_db_transaction_commit();
106
}
107
108
$template->assign_vars(array(
109
  'galaxy_src' => $galaxy_src,
110
  'system_src' => $system_src,
111
  'planet_src' => $planet_src,
112
113
  'galaxy_dst' => $galaxy_dst,
114
  'system_dst' => $system_dst,
115
  'planet_dst' => $planet_dst,
116
117
  'bonus' => $bonus,
118
119
  'username' => $username,
120
));
121
122
display($template, $lang['adm_pl_comp_title']);
123
124
function killer_add_planet($planet) {
125
  global $final_cost;
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...
126
127
  $final_cost = array();
128
  $sn_group_resources_loot = sn_get_groups('resources_loot');
129
  /*
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...
130
  foreach($sn_group_resources_loot as &$value)
131
  {
132
    $value = get_unit_param($value, P_NAME);
133
  }
134
  */
135
136
  foreach (sn_get_groups('structures') as $unit_id) {
137
    $build_level = mrc_get_level($user, $planet, $unit_id, true, true);
138
    if ($build_level > 0) {
139
      $unit_cost = get_unit_param($unit_id, 'cost');
140
      $build_factor = $unit_cost['factor'] != 1 ? (1 - pow($unit_cost['factor'], $build_level)) / (1 - $unit_cost['factor']) : $unit_cost['factor'];
0 ignored issues
show
Bug introduced by
It seems like $build_level 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

140
      $build_factor = $unit_cost['factor'] != 1 ? (1 - pow($unit_cost['factor'], /** @scrutinizer ignore-type */ $build_level)) / (1 - $unit_cost['factor']) : $unit_cost['factor'];
Loading history...
141
      foreach ($sn_group_resources_loot as $resource_id) {
142
        $final_cost[$resource_id] += isset($unit_cost[$resource_id]) && $unit_cost[$resource_id] > 0 ? floor($unit_cost[$resource_id] * $build_factor) : 0;
143
      }
144
    }
145
  }
146
147
  foreach (sn_get_groups(array('defense', 'fleet')) as $unit_id) {
148
    $unit_count = mrc_get_level($user, $planet, $unit_id, true, true);
149
    if ($unit_count > 0) {
150
      $unit_cost = get_unit_param($unit_id, 'cost');
151
      foreach ($sn_group_resources_loot as $resource_id) {
152
        $final_cost[$resource_id] += isset($unit_cost[$resource_id]) && $unit_cost[$resource_id] > 0 ? floor($unit_cost[$resource_id] * $unit_count) : 0;
153
      }
154
    }
155
  }
156
157
  foreach ($sn_group_resources_loot as $resource_id) {
158
    $final_cost[$resource_id] += floor(mrc_get_level($user, $planet, $resource_id, true, true));
0 ignored issues
show
Bug introduced by
It seems like mrc_get_level($user, $pl...esource_id, true, true) can also be of type boolean; however, parameter $value of floor() does only seem to accept 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

158
    $final_cost[$resource_id] += floor(/** @scrutinizer ignore-type */ mrc_get_level($user, $planet, $resource_id, true, true));
Loading history...
159
  }
160
}
161