Completed
Push — trunk ( c0c172...91a948 )
by SuperNova.WS
11:04
created

DBStaticPlanet::dbDeletePlanetsWithoutUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
class DBStaticPlanet {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
6
  public static function db_planets_purge() {
7
    doquery("DELETE FROM {{planets}} WHERE id_owner NOT IN (SELECT `id` FROM {{users}});");
8
  }
9
10
11
  /**
12
   * @param int    $planet_id
13
   * @param bool   $for_update
14
   * @param string $fields
15
   *
16
   * @return array|null
17
   */
18
  public static function db_planet_by_id($planet_id, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed. ( Ignorable by Annotation )

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

18
  public static function db_planet_by_id($planet_id, /** @scrutinizer ignore-unused */ $for_update = false, $fields = '*') {

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

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

18
  public static function db_planet_by_id($planet_id, $for_update = false, /** @scrutinizer ignore-unused */ $fields = '*') {

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

Loading history...
19
    $result = SN::db_get_record_by_id(LOC_PLANET, $planet_id);
20
21
    return empty($result) ? null : $result;
22
  }
23
24
  /**
25
   * @param        $galaxy
26
   * @param        $system
27
   * @param        $planet
28
   * @param        $planet_type
29
   * @param bool   $for_update
30
   * @param string $fields
31
   *
32
   * @return bool|mixed
33
   */
34
  public static function db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed. ( Ignorable by Annotation )

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

34
  public static function db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, /** @scrutinizer ignore-unused */ $for_update = false, $fields = '*') {

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

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

34
  public static function db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update = false, /** @scrutinizer ignore-unused */ $fields = '*') {

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

Loading history...
35
    return SN::db_get_record_list(LOC_PLANET,
36
      "{{planets}}.`galaxy` = {$galaxy} AND {{planets}}.`system` = {$system} AND {{planets}}.`planet` = {$planet} AND {{planets}}.`planet_type` = {$planet_type}", true);
37
  }
38
39
  public static function db_planet_by_gspt($galaxy, $system, $planet, $planet_type, $for_update = false, $fields = '*') {
40
    $galaxy = intval($galaxy);
41
    $system = intval($system);
42
    $planet = intval($planet);
43
    $planet_type = intval($planet_type);
44
45
    return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields);
46
  }
47
48
  public static function db_planet_by_vector($vector, $prefix = '', $for_update = false, $fields = '*') {
49
    $galaxy = isset($vector[$prefix . 'galaxy']) ? intval($vector[$prefix . 'galaxy']) : 0;
50
    $system = isset($vector[$prefix . 'system']) ? intval($vector[$prefix . 'system']) : 0;
51
    $planet = isset($vector[$prefix . 'planet']) ? intval($vector[$prefix . 'planet']) : 0;
52
    $planet_type = isset($vector[$prefix . 'planet_type']) ? intval($vector[$prefix . 'planet_type']) :
53
      (isset($vector[$prefix . 'type']) ? intval($vector[$prefix . 'type']) : 0);
54
    $planet_type = $planet_type == PT_DEBRIS ? PT_PLANET : $planet_type;
55
56
    return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields);
57
  }
58
59
//  /**
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
60
//   * @param Vector $vector
61
//   * @param bool   $for_update
62
//   * @param string $fields
63
//   *
64
//   * @return array
65
//   */
66
//  public static function db_planet_by_vector_object($vector, $for_update = false, $fields = '*') {
67
//    $planet_type = $vector->type == PT_DEBRIS ? PT_PLANET : $vector->type;
68
//    $result = DBStaticPlanet::db_planet_by_gspt_safe($vector->galaxy, $vector->system, $vector->planet, $planet_type, $for_update, $fields);
69
//
70
//    return !empty($result) ? $result : array();
71
//  }
72
73
  public static function db_planet_by_parent($parent_id, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed. ( Ignorable by Annotation )

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

73
  public static function db_planet_by_parent($parent_id, /** @scrutinizer ignore-unused */ $for_update = false, $fields = '*') {

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

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

73
  public static function db_planet_by_parent($parent_id, $for_update = false, /** @scrutinizer ignore-unused */ $fields = '*') {

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

Loading history...
74
    if(!($parent_id = idval($parent_id))) {
75
      return false;
76
    }
77
78
    return SN::db_get_record_list(LOC_PLANET,
79
      "`parent_planet` = {$parent_id} AND `planet_type` = " . PT_MOON, true);
80
  }
81
82
  public static function db_planet_by_id_and_owner($planet_id, $owner_id, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed. ( Ignorable by Annotation )

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

82
  public static function db_planet_by_id_and_owner($planet_id, $owner_id, /** @scrutinizer ignore-unused */ $for_update = false, $fields = '*') {

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

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

82
  public static function db_planet_by_id_and_owner($planet_id, $owner_id, $for_update = false, /** @scrutinizer ignore-unused */ $fields = '*') {

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

Loading history...
83
    if(!($planet_id = idval($planet_id)) || !($owner_id = idval($owner_id))) {
84
      return false;
85
    }
86
    return SN::db_get_record_list(LOC_PLANET,
87
      "`id` = {$planet_id} AND `id_owner` = {$owner_id}", true);
88
  }
89
90
91
  public static function db_planet_list_moon_other($user_id, $this_moon_id) {
92
    if(!($user_id = idval($user_id)) || !($this_moon_id = idval($this_moon_id))) {
93
      return false;
94
    }
95
    return SN::db_get_record_list(LOC_PLANET,
96
      "`planet_type` = " . PT_MOON . " AND `id_owner` = {$user_id} AND `id` != {$this_moon_id}");
97
  }
98
99
  public static function db_planet_list_in_system($galaxy, $system) {
100
    $galaxy = intval($galaxy);
101
    $system = intval($system);
102
    return SN::db_get_record_list(LOC_PLANET,
103
      "`galaxy` = {$galaxy} AND `system` = {$system}");
104
  }
105
106
  public static function db_planet_list_sorted($user_row, $skip_planet_id = false, $field_list = '', $conditions = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $field_list is not used and could be removed. ( Ignorable by Annotation )

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

106
  public static function db_planet_list_sorted($user_row, $skip_planet_id = false, /** @scrutinizer ignore-unused */ $field_list = '', $conditions = '') {

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

Loading history...
107
    if(!is_array($user_row)) {
108
      return false;
109
    }
110
    $conditions .= $skip_planet_id ? " AND `id` <> {$skip_planet_id} " : '';
111
112
    $sort_orders = array(
113
      SORT_ID       => '{{planets}}.`id`',
114
      SORT_LOCATION => '{{planets}}.`galaxy`, {{planets}}.`system`, {{planets}}.`planet`, {{planets}}.`planet_type`',
115
      SORT_NAME     => '`name`',
116
      SORT_SIZE     => '({{planets}}.`field_max`)',
117
    );
118
    $order_by = SN::$user_options[PLAYER_OPTION_PLANET_SORT];
119
    empty($sort_orders[$order_by]) ? $order_by = SORT_ID : false;
120
    $order_by = $sort_orders[$order_by] . ' ' . (SN::$user_options[PLAYER_OPTION_PLANET_SORT_INVERSE] == SORT_ASCENDING ? 'ASC' : 'DESC');
121
122
    // Compilating query
123
    return SN::db_get_record_list(LOC_PLANET,
124
      "`id_owner` = '{$user_row['id']}' {$conditions} ORDER BY {$order_by}");
125
  }
126
127
  public static function db_planet_list_by_user_or_planet($user_id, $planet_id) {
128
    if(!($user_id = idval($user_id)) && !($planet_id = idval($planet_id))) {
129
      return false;
130
    }
131
132
    return SN::db_get_record_list(LOC_PLANET,
133
      $planet_id = idval($planet_id) ? "{{planets}}.`id` = {$planet_id}" : "`id_owner` = {$user_id}", $planet_id);
134
  }
135
136
  public static function db_planet_set_by_id($planet_id, $set) {
137
    if(!($planet_id = idval($planet_id))) {
138
      return false;
139
    }
140
    return SN::db_upd_record_by_id(LOC_PLANET, $planet_id, $set);
141
  }
142
143
  public static function db_planet_set_by_gspt($ui_galaxy, $ui_system, $ui_planet, $ui_planet_type = PT_ALL, $set) {
144
    if(!($set = trim($set))) {
145
      return false;
146
    }
147
148
    $si_galaxy = intval($ui_galaxy);
149
    $si_system = intval($ui_system);
150
    $si_planet = intval($ui_planet);
151
    $si_planet_type = ($si_planet_type = intval($ui_planet_type)) ? "AND `planet_type` = {$si_planet_type}" : '';
152
153
    return SN::db_upd_record_list(LOC_PLANET, "`galaxy` = {$si_galaxy} AND `system` = {$si_system} AND `planet` = {$si_planet} {$si_planet_type}", $set);
154
  }
155
156
  public static function db_planet_set_by_parent($ui_parent_id, $ss_set) {
157
    if(!($si_parent_id = idval($ui_parent_id)) || !($ss_set = trim($ss_set))) {
158
      return false;
159
    }
160
    return SN::db_upd_record_list(LOC_PLANET, "`parent_planet` = {$si_parent_id}", $ss_set);
161
  }
162
163
  public static function db_planet_set_by_owner($ui_owner_id, $ss_set) {
164
    if(!($si_owner_id = idval($ui_owner_id)) || !($ss_set = trim($ss_set))) {
165
      return false;
166
    }
167
    return SN::db_upd_record_list(LOC_PLANET, "`id_owner` = {$si_owner_id}", $ss_set);
168
  }
169
170
171
  public static function db_planet_delete_by_id($planet_id) {
172
    if(!($planet_id = idval($planet_id))) {
173
      return false;
174
    }
175
    SN::db_del_record_by_id(LOC_PLANET, $planet_id);
176
    SN::db_del_record_list(LOC_UNIT, "`unit_location_type` = " . LOC_PLANET . " AND `unit_location_id` = " . $planet_id);
177
    // Очереди очистятся автоматически по FOREIGN KEY
178
    return true;
179
  }
180
181
  public static function db_planet_list_delete_by_owner($ui_owner_id) {
182
    if(!($si_owner_id = idval($ui_owner_id))) {
183
      return false;
184
    }
185
    SN::db_del_record_list(LOC_PLANET, "`id_owner` = {$si_owner_id}");
186
    SN::db_del_record_list(LOC_UNIT, "`unit_location_type` = " . LOC_PLANET . " AND `unit_player_id` = " . $si_owner_id);
187
    // Очереди очистятся автоматически по FOREIGN KEY
188
    return true;
189
  }
190
191
192
  public static function db_planet_count_by_type($ui_user_id, $ui_planet_type = PT_PLANET) {
193
    $si_user_id = idval($ui_user_id);
194
    $si_planet_type = intval($ui_planet_type);
195
196
    // Лочим запись-родителя - если она есть и еще не залочена
197
    $record_list = SN::db_get_record_list(LOC_PLANET, "`id_owner` = {$si_user_id} AND `planet_type` = {$si_planet_type}");
198
    return is_array($record_list) ? count($record_list) : 0;
0 ignored issues
show
introduced by
The condition is_array($record_list) is always false.
Loading history...
199
  }
200
201
  public static function db_planet_list_resources_by_owner() {
202
    return doquery("SELECT `id_owner`, sum(metal) AS metal, sum(crystal) AS crystal, sum(deuterium) AS deuterium FROM {{planets}} WHERE id_owner <> 0 /*AND id_owner is not null*/ GROUP BY id_owner;");
203
  }
204
205
  public static function dbDeletePlanetsWithoutUsers() {
206
    SN::$db->doquery("DELETE FROM `{{planets}}` WHERE id_owner NOT IN (SELECT id FROM `{{users}}`)");
207
  }
208
209
}
210