Completed
Push — work-fleets ( f6000a...17041b )
by SuperNova.WS
05:43
created

DBStaticPlanet::db_planet_by_gspt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 6
dl 0
loc 8
rs 9.4285
1
<?php
2
3
class DBStaticPlanet {
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 = '*') {
19
    $result = classSupernova::db_get_record_by_id(LOC_PLANET, $planet_id, $for_update, $fields);
20
21
    return empty($result) ? null : $result;
22
  }
23
24
  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.

This check looks from 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.

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

Loading history...
25
    return classSupernova::db_get_record_list(LOC_PLANET,
26
      "{{planets}}.`galaxy` = {$galaxy} AND {{planets}}.`system` = {$system} AND {{planets}}.`planet` = {$planet} AND {{planets}}.`planet_type` = {$planet_type}", true);
27
  }
28
29
  public static function db_planet_by_gspt($galaxy, $system, $planet, $planet_type, $for_update = false, $fields = '*') {
30
    $galaxy = intval($galaxy);
31
    $system = intval($system);
32
    $planet = intval($planet);
33
    $planet_type = intval($planet_type);
34
35
    return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields);
36
  }
37
38
  public static function db_planet_by_vector($vector, $prefix = '', $for_update = false, $fields = '*') {
39
    $galaxy = isset($vector[$prefix . 'galaxy']) ? intval($vector[$prefix . 'galaxy']) : 0;
40
    $system = isset($vector[$prefix . 'system']) ? intval($vector[$prefix . 'system']) : 0;
41
    $planet = isset($vector[$prefix . 'planet']) ? intval($vector[$prefix . 'planet']) : 0;
42
    $planet_type = isset($vector[$prefix . 'planet_type']) ? intval($vector[$prefix . 'planet_type']) :
43
      (isset($vector[$prefix . 'type']) ? intval($vector[$prefix . 'type']) : 0);
44
    $planet_type = $planet_type == PT_DEBRIS ? PT_PLANET : $planet_type;
45
46
    return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields);
47
  }
48
49
  /**
50
   * @param Vector $vector
51
   * @param bool   $for_update
52
   * @param string $fields
53
   *
54
   * @return array
55
   */
56
  public static function db_planet_by_vector_object($vector, $for_update = false, $fields = '*') {
57
    $planet_type = $vector->type == PT_DEBRIS ? PT_PLANET : $vector->type;
58
    $result = DBStaticPlanet::db_planet_by_gspt_safe($vector->galaxy, $vector->system, $vector->planet, $planet_type, $for_update, $fields);
59
60
    return !empty($result) ? $result : array();
61
  }
62
63
  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.

This check looks from 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.

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

Loading history...
64
    if(!($parent_id = idval($parent_id))) {
65
      return false;
66
    }
67
68
    return classSupernova::db_get_record_list(LOC_PLANET,
69
      "`parent_planet` = {$parent_id} AND `planet_type` = " . PT_MOON, true);
70
  }
71
72 View Code Duplication
  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.

This check looks from 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.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    //if(!($planet_id = intval($planet_id)) || !($owner_id = intval($owner_id))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
74
    if(!($planet_id = idval($planet_id)) || !($owner_id = idval($owner_id))) {
75
      return false;
76
    }
77
78
    return classSupernova::db_get_record_list(LOC_PLANET,
79
      "`id` = {$planet_id} AND `id_owner` = {$owner_id}", true);
80
  }
81
82
83 View Code Duplication
  public static function db_planet_list_moon_other($user_id, $this_moon_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    // if(!($user_id = intval($user_id)) || !($this_moon_id = intval($this_moon_id))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
85
    if(!($user_id = idval($user_id)) || !($this_moon_id = idval($this_moon_id))) {
86
      return false;
87
    }
88
89
    return classSupernova::db_get_record_list(LOC_PLANET,
90
      "`planet_type` = " . PT_MOON . " AND `id_owner` = {$user_id} AND `id` != {$this_moon_id}");
91
  }
92
93
  public static function db_planet_list_in_system($galaxy, $system) {
94
    $galaxy = intval($galaxy);
95
    $system = intval($system);
96
97
    return classSupernova::db_get_record_list(LOC_PLANET,
98
      "`galaxy` = {$galaxy} AND `system` = {$system}");
99
  }
100
101
  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.

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

Loading history...
102
    if(!is_array($user_row)) {
103
      return false;
104
    }
105
    // $field_list = $field_list != '*' ? "{{planets}}.`id`, `name`, `image`, {{planets}}.`galaxy`, {{planets}}.`system`, {{planets}}.`planet`, `planet_type`{$field_list}" : $field_list;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
106
    $conditions .= $skip_planet_id ? " AND `id` <> {$skip_planet_id} " : '';
107
108
    $sort_orders = array(
109
      SORT_ID       => '{{planets}}.`id`',
110
      SORT_LOCATION => '{{planets}}.`galaxy`, {{planets}}.`system`, {{planets}}.`planet`, {{planets}}.`planet_type`',
111
      SORT_NAME     => '`name`',
112
      SORT_SIZE     => '({{planets}}.`field_max`)',
113
    );
114
    $order_by = classSupernova::$user_options[PLAYER_OPTION_PLANET_SORT];
115
    empty($sort_orders[$order_by]) ? $order_by = SORT_ID : false;
116
    $order_by = $sort_orders[$order_by] . ' ' . (classSupernova::$user_options[PLAYER_OPTION_PLANET_SORT_INVERSE] == SORT_ASCENDING ? 'ASC' : 'DESC');
117
118
    // Compilating query
119
    return classSupernova::db_get_record_list(LOC_PLANET,
120
      "`id_owner` = '{$user_row['id']}' {$conditions} ORDER BY {$order_by}");
121
  }
122
123 View Code Duplication
  public static function db_planet_list_by_user_or_planet($user_id, $planet_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    if(!($user_id = idval($user_id)) && !($planet_id = idval($planet_id))) {
125
      return false;
126
    }
127
128
    return classSupernova::db_get_record_list(LOC_PLANET,
129
      $planet_id = idval($planet_id) ? "{{planets}}.`id` = {$planet_id}" : "`id_owner` = {$user_id}", $planet_id);
130
  }
131
132
  public static function db_planet_set_by_id($planet_id, $set) {
133
    // if(!($planet_id = intval($planet_id))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
134
    if(!($planet_id = idval($planet_id))) {
135
      return false;
136
    }
137
138
    return classSupernova::db_upd_record_by_id(LOC_PLANET, $planet_id, $set);
139
  }
140
141
  public static function db_planet_set_by_gspt($ui_galaxy, $ui_system, $ui_planet, $ui_planet_type = PT_ALL, $set) {
142
    if(!($set = trim($set))) {
143
      return false;
144
    }
145
146
    $si_galaxy = intval($ui_galaxy);
147
    $si_system = intval($ui_system);
148
    $si_planet = intval($ui_planet);
149
    $si_planet_type = ($si_planet_type = intval($ui_planet_type)) ? "AND `planet_type` = {$si_planet_type}" : '';
150
151
    return classSupernova::db_upd_record_list(LOC_PLANET, "`galaxy` = {$si_galaxy} AND `system` = {$si_system} AND `planet` = {$si_planet} {$si_planet_type}", $set);
152
  }
153
154 View Code Duplication
  public static function db_planet_set_by_parent($ui_parent_id, $ss_set) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    //if(!($si_parent_id = intval($ui_parent_id)) || !($ss_set = trim($ss_set))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
156
    if(!($si_parent_id = idval($ui_parent_id)) || !($ss_set = trim($ss_set))) {
157
      return false;
158
    }
159
160
    return classSupernova::db_upd_record_list(LOC_PLANET, "`parent_planet` = {$si_parent_id}", $ss_set);
161
  }
162
163 View Code Duplication
  public static function db_planet_set_by_owner($ui_owner_id, $ss_set) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    //if(!($si_owner_id = intval($ui_owner_id)) || !($ss_set = trim($ss_set))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
165
    if(!($si_owner_id = idval($ui_owner_id)) || !($ss_set = trim($ss_set))) {
166
      return false;
167
    }
168
169
    return classSupernova::db_upd_record_list(LOC_PLANET, "`id_owner` = {$si_owner_id}", $ss_set);
170
  }
171
172
173 View Code Duplication
  public static function db_planet_delete_by_id($planet_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    // if(!($planet_id = intval($planet_id))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
175
    if(!($planet_id = idval($planet_id))) {
176
      return false;
177
    }
178
    classSupernova::db_del_record_by_id(LOC_PLANET, $planet_id);
179
    classSupernova::db_del_record_list(LOC_UNIT, "`unit_location_type` = " . LOC_PLANET . " AND `unit_location_id` = " . $planet_id);
180
181
    // Очереди очистятся автоматически по FOREIGN KEY
182
    return true;
183
  }
184
185 View Code Duplication
  public static function db_planet_list_delete_by_owner($ui_owner_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    // if(!($si_owner_id = intval($ui_owner_id))) return false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
187
    if(!($si_owner_id = idval($ui_owner_id))) {
188
      return false;
189
    }
190
    classSupernova::db_del_record_list(LOC_PLANET, "`id_owner` = {$si_owner_id}");
191
    classSupernova::db_del_record_list(LOC_UNIT, "`unit_location_type` = " . LOC_PLANET . " AND `unit_player_id` = " . $si_owner_id);
192
193
    // Очереди очистятся автоматически по FOREIGN KEY
194
    return true;
195
  }
196
197
198 View Code Duplication
  public static function db_planet_count_by_type($ui_user_id, $ui_planet_type = PT_PLANET) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
    // $si_user_id = intval($ui_user_id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
200
    $si_user_id = idval($ui_user_id);
201
    $si_planet_type = intval($ui_planet_type);
202
203
    // Лочим запись-родителя - если она есть и еще не залочена
204
    $record_list = classSupernova::db_get_record_list(LOC_PLANET, "`id_owner` = {$si_user_id} AND `planet_type` = {$si_planet_type}");
205
206
    return is_array($record_list) ? count($record_list) : 0;
207
    // $planets = doquery("SELECT COUNT(*) AS planet_count FROM {{planets}} WHERE `id_owner` = {$si_user_id} AND `planet_type` = {$si_planet_type}", true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
208
    // return isset($planets['planet_count']) ? $planets['planet_count'] : 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
209
  }
210
211
  public static function db_planet_list_resources_by_owner() {
212
    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;");
213
  }
214
215
}
216