Issues (1369)

admin/planet_compensate.php (5 issues)

1
<?php
2
3
use DBAL\db_mysql;
4
use Planet\DBStaticPlanet;
5
use Unit\DBStaticUnit;
6
7
define('INSIDE', true);
8
define('INSTALL', false);
9
define('IN_ADMIN', true);
10
11
require('../common.' . substr(strrchr(__FILE__, '.'), 1));
12
13
global $lang, $user;
14
15
SnTemplate::messageBoxAdminAccessDenied(AUTH_LEVEL_ADMINISTRATOR);
16
17
$template = SnTemplate::gettemplate('admin/planet_compensate', true);
0 ignored issues
show
true of type true is incompatible with the type null|template expected by parameter $template of SnTemplate::gettemplate(). ( Ignorable by Annotation )

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

17
$template = SnTemplate::gettemplate('admin/planet_compensate', /** @scrutinizer ignore-type */ true);
Loading history...
18
19
$galaxy_src = sys_get_param_int('galaxy_src');
20
$system_src = sys_get_param_int('system_src');
21
$planet_src = sys_get_param_int('planet_src');
22
23
$galaxy_dst = sys_get_param_int('galaxy_dst');
24
$system_dst = sys_get_param_int('system_dst');
25
$planet_dst = sys_get_param_int('planet_dst');
26
27
$bonus = sys_get_param_float('bonus', 1);
28
29
$username_unsafe = sys_get_param_str_unsafe('username');
30
$username = sys_get_param_escaped('username');
31
32
if ($galaxy_src) {
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...
Are you sure the assignment to $owner is correct as db_user_by_username($username_unsafe, true) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
  $planet = DBStaticPlanet::db_planet_by_gspt($galaxy_src, $system_src, $planet_src, PT_PLANET);
37
  if (empty($planet)) {
38
    $errors[] = $lang['adm_pl_comp_err_0'];
39
  }
40
  if ($planet['destruyed']) {
41
    $errors[] = $lang['adm_pl_comp_err_1'];
42
  }
43
  if (empty($username) || empty($owner) || $planet['id_owner'] != $owner['id']) {
44
    $errors[] = $lang['adm_pl_comp_err_4'];
45
  }
46
47
  $destination = DBStaticPlanet::db_planet_by_gspt($galaxy_dst, $system_dst, $planet_dst, PT_PLANET);
48
  if (empty($destination)) {
49
    $errors[] = $lang['adm_pl_comp_err_2'];
50
  }
51
  if ($planet['id'] == $destination['id']) {
52
    $errors[] = $lang['adm_pl_comp_err_5'];
53
  }
54
  if ($planet['id_owner'] != $destination['id_owner']) {
55
    $errors[] = $lang['adm_pl_comp_err_3'];
56
  }
57
58
  $moon = DBStaticPlanet::db_planet_by_gspt($galaxy_src, $system_src, $planet_src, PT_MOON);
59
  if (!empty($errors)) {
60
    foreach ($errors as $error) {
61
      $template->assign_block_vars('error', array(
62
        'TEXT' => $error,
63
      ));
64
    }
65
  } else {
66
  db_mysql::db_transaction_start();
67
  SN::$gc->db->lockRecords([
68
    'users'   => [$owner['id'],],
69
    'planets' => [$planet['id'], $destination['id'], !empty($moon['id']) ? $moon['id'] : 0],
70
  ]);
71
72
  $planet = sys_o_get_updated($owner['id'], $planet['id'], SN_TIME_NOW);
73
  $que = $planet['que'];
74
  $planet = $planet['planet'];
75
76
  $destination = sys_o_get_updated($owner['id'], $destination['id'], SN_TIME_NOW);
77
  $destination = $destination['planet'];
78
79
    $template->assign_var('CHECK', 1);
80
81
    $final_cost = killer_add_planet($planet);
82
83
    if (!empty($moon)) {
84
      $moon = sys_o_get_updated($owner['id'], $moon['id'], SN_TIME_NOW);
85
      $moon = $moon['planet'];
86
      $final_cost = killer_add_planet($moon, $final_cost);
87
    }
88
89
    foreach (sn_get_groups('resources_loot') as $resource_id) {
90
      $resource_name = pname_resource_name($resource_id);
91
      $template->assign_var("{$resource_name}_cost", $final_cost[$resource_id]);
92
      $final_cost[$resource_id] = floor($final_cost[$resource_id] * $bonus);
93
      $template->assign_var("{$resource_name}_bonus", $final_cost[$resource_id]);
94
    }
95
96
    if ($_GET['btn_confirm']) {
97
      $time = SN_TIME_NOW + PERIOD_DAY;
98
99
      DBStaticUnit::db_unit_list_delete($planet['id_owner'], LOC_PLANET, $planet['id']);
100
      DBStaticPlanet::db_planet_set_by_id($planet['id'], "id_owner = 0, destruyed = {$time}");
101
      if (!empty($moon)) {
102
        DBStaticUnit::db_unit_list_delete($planet['id_owner'], LOC_PLANET, $moon['id']);
103
        DBStaticPlanet::db_planet_set_by_id($moon['id'], "id_owner = 0, destruyed = {$time}");
104
      }
105
106
      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]}'");
107
      $template->assign_var('CHECK', 2);
108
    }
109
    db_mysql::db_transaction_commit();
110
  }
111
}
112
113
$template->assign_vars(array(
114
  'galaxy_src' => $galaxy_src,
115
  'system_src' => $system_src,
116
  'planet_src' => $planet_src,
117
118
  'galaxy_dst' => $galaxy_dst,
119
  'system_dst' => $system_dst,
120
  'planet_dst' => $planet_dst,
121
122
  'bonus' => $bonus,
123
124
  'username' => $username,
125
));
126
127
SnTemplate::display($template, $lang['adm_pl_comp_title']);
128
129
/**
130
 * @param array $planet
131
 * @param array $final_cost
132
 *
133
 * @return array|mixed
134
 */
135
function killer_add_planet($planet, $final_cost = []) {
136
  $sn_group_resources_loot = sn_get_groups('resources_loot');
137
138
  // Adding structures cost
139
  foreach (sn_get_groups('structures') as $unit_id) {
140
    $build_level = mrc_get_level($user, $planet, $unit_id, true, true);
141
    if ($build_level > 0) {
142
      $unit_cost = get_unit_param($unit_id, 'cost');
143
      $build_factor = $unit_cost['factor'] != 1 ? (1 - pow($unit_cost['factor'], $build_level)) / (1 - $unit_cost['factor']) : $unit_cost['factor'];
0 ignored issues
show
It seems like $build_level can also be of type boolean; however, parameter $exp of pow() does only seem to accept double|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

143
      $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...
144
      foreach ($sn_group_resources_loot as $resource_id) {
145
        $final_cost[$resource_id] += isset($unit_cost[$resource_id]) && $unit_cost[$resource_id] > 0 ? floor($unit_cost[$resource_id] * $build_factor) : 0;
146
      }
147
    }
148
  }
149
  // Adding fleet and defense cost
150
  foreach (sn_get_groups(array('defense', 'fleet')) as $unit_id) {
151
    $unit_count = mrc_get_level($user, $planet, $unit_id, true, true);
152
    if ($unit_count > 0) {
153
      $unit_cost = get_unit_param($unit_id, 'cost');
154
      foreach ($sn_group_resources_loot as $resource_id) {
155
        $final_cost[$resource_id] += isset($unit_cost[$resource_id]) && $unit_cost[$resource_id] > 0 ? floor($unit_cost[$resource_id] * $unit_count) : 0;
156
      }
157
    }
158
  }
159
  // Adding plain resources
160
  foreach ($sn_group_resources_loot as $resource_id) {
161
    $final_cost[$resource_id] += floor(mrc_get_level($user, $planet, $resource_id, true, true));
0 ignored issues
show
It seems like mrc_get_level($user, $pl...esource_id, true, true) can also be of type boolean; however, parameter $num of floor() does only seem to accept double|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

161
    $final_cost[$resource_id] += floor(/** @scrutinizer ignore-type */ mrc_get_level($user, $planet, $resource_id, true, true));
Loading history...
162
  }
163
164
  return $final_cost;
165
}
166