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

DBStaticPlanet::db_planet_by_vector()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 7
nc 64
nop 4
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 56
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Planet;
4
use SN;
5
6
class DBStaticPlanet {
7
8
9
  public static function db_planets_purge() {
10
    doquery("DELETE FROM {{planets}} WHERE id_owner NOT IN (SELECT `id` FROM {{users}});");
11
  }
12
13
14
  /**
15
   * @param int    $planet_id
16
   * @param bool   $for_update
17
   * @param string $fields
18
   *
19
   * @return array|null
20
   */
21
  public static function db_planet_by_id($planet_id, $for_update = false, $fields = '*') {
0 ignored issues
show
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

21
  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...
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

21
  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...
22
    $result = SN::db_get_record_by_id(LOC_PLANET, $planet_id);
23
24
    return empty($result) ? null : $result;
25
  }
26
27
  /**
28
   * @param        $galaxy
29
   * @param        $system
30
   * @param        $planet
31
   * @param        $planet_type
32
   * @param bool   $for_update
33
   * @param string $fields
34
   *
35
   * @return bool|mixed
36
   */
37
  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

37
  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

37
  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...
38
    return SN::db_get_record_list(LOC_PLANET,
39
      "{{planets}}.`galaxy` = {$galaxy} AND {{planets}}.`system` = {$system} AND {{planets}}.`planet` = {$planet} AND {{planets}}.`planet_type` = {$planet_type}", true);
40
  }
41
42
  public static function db_planet_by_gspt($galaxy, $system, $planet, $planet_type, $for_update = false, $fields = '*') {
43
    $galaxy = intval($galaxy);
44
    $system = intval($system);
45
    $planet = intval($planet);
46
    $planet_type = intval($planet_type);
47
48
    return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields);
49
  }
50
51
  public static function db_planet_by_vector($vector, $prefix = '', $for_update = false, $fields = '*') {
52
    $galaxy = isset($vector[$prefix . 'galaxy']) ? intval($vector[$prefix . 'galaxy']) : 0;
53
    $system = isset($vector[$prefix . 'system']) ? intval($vector[$prefix . 'system']) : 0;
54
    $planet = isset($vector[$prefix . 'planet']) ? intval($vector[$prefix . 'planet']) : 0;
55
    $planet_type = isset($vector[$prefix . 'planet_type']) ? intval($vector[$prefix . 'planet_type']) :
56
      (isset($vector[$prefix . 'type']) ? intval($vector[$prefix . 'type']) : 0);
57
    $planet_type = $planet_type == PT_DEBRIS ? PT_PLANET : $planet_type;
58
59
    return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields);
60
  }
61
62
//  /**
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...
63
//   * @param Vector $vector
64
//   * @param bool   $for_update
65
//   * @param string $fields
66
//   *
67
//   * @return array
68
//   */
69
//  public static function db_planet_by_vector_object($vector, $for_update = false, $fields = '*') {
70
//    $planet_type = $vector->type == PT_DEBRIS ? PT_PLANET : $vector->type;
71
//    $result = Planet\DBStaticPlanet::db_planet_by_gspt_safe($vector->galaxy, $vector->system, $vector->planet, $planet_type, $for_update, $fields);
72
//
73
//    return !empty($result) ? $result : array();
74
//  }
75
76
  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

76
  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

76
  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...
77
    if (!($parent_id = idval($parent_id))) {
78
      return false;
79
    }
80
81
    return SN::db_get_record_list(LOC_PLANET,
82
      "`parent_planet` = {$parent_id} AND `planet_type` = " . PT_MOON, true);
83
  }
84
85
  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

85
  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

85
  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...
86
    if (!($planet_id = idval($planet_id)) || !($owner_id = idval($owner_id))) {
87
      return false;
88
    }
89
90
    return SN::db_get_record_list(LOC_PLANET,
91
      "`id` = {$planet_id} AND `id_owner` = {$owner_id}", true);
92
  }
93
94
95
  public static function db_planet_list_moon_other($user_id, $this_moon_id) {
96
    if (!($user_id = idval($user_id)) || !($this_moon_id = idval($this_moon_id))) {
97
      return false;
98
    }
99
100
    return SN::db_get_record_list(LOC_PLANET,
101
      "`planet_type` = " . PT_MOON . " AND `id_owner` = {$user_id} AND `id` != {$this_moon_id}");
102
  }
103
104
  public static function db_planet_list_in_system($galaxy, $system) {
105
    $galaxy = intval($galaxy);
106
    $system = intval($system);
107
108
    return SN::db_get_record_list(LOC_PLANET,
109
      "`galaxy` = {$galaxy} AND `system` = {$system}");
110
  }
111
112
  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

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