1 | <?php /** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection */ |
||||||
2 | |||||||
3 | namespace Planet; |
||||||
4 | use SN; |
||||||
5 | |||||||
6 | class DBStaticPlanet { |
||||||
7 | /** |
||||||
8 | * @param int $planet_id |
||||||
9 | * @param bool $for_update |
||||||
10 | * |
||||||
11 | * @return array|null |
||||||
12 | */ |
||||||
13 | public static function db_planet_by_id($planet_id, $for_update = false) { |
||||||
0 ignored issues
–
show
|
|||||||
14 | $result = SN::db_get_record_by_id(LOC_PLANET, $planet_id); |
||||||
15 | |||||||
16 | return empty($result) ? null : $result; |
||||||
17 | } |
||||||
18 | |||||||
19 | /** |
||||||
20 | * @param int $galaxy |
||||||
21 | * @param int $system |
||||||
22 | * @param int $planet |
||||||
23 | * @param int $planet_type |
||||||
24 | * |
||||||
25 | * @return bool|mixed |
||||||
26 | */ |
||||||
27 | public static function db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type) { |
||||||
28 | return SN::db_get_record_list(LOC_PLANET, |
||||||
29 | "{{planets}}.`galaxy` = {$galaxy} AND {{planets}}.`system` = {$system} AND {{planets}}.`planet` = {$planet} AND {{planets}}.`planet_type` = {$planet_type}", true); |
||||||
30 | } |
||||||
31 | |||||||
32 | public static function db_planet_by_gspt($galaxy, $system, $planet, $planet_type) { |
||||||
33 | return DBStaticPlanet::db_planet_by_gspt_safe(intval($galaxy), intval($system), intval($planet), intval($planet_type)); |
||||||
34 | } |
||||||
35 | |||||||
36 | public static function db_planet_by_vector($vector, $prefix = '') { |
||||||
37 | $galaxy = isset($vector[$prefix . 'galaxy']) ? intval($vector[$prefix . 'galaxy']) : 0; |
||||||
38 | $system = isset($vector[$prefix . 'system']) ? intval($vector[$prefix . 'system']) : 0; |
||||||
39 | $planet = isset($vector[$prefix . 'planet']) ? intval($vector[$prefix . 'planet']) : 0; |
||||||
40 | $planet_type = isset($vector[$prefix . 'planet_type']) ? intval($vector[$prefix . 'planet_type']) : |
||||||
41 | (isset($vector[$prefix . 'type']) ? intval($vector[$prefix . 'type']) : 0); |
||||||
42 | $planet_type = $planet_type == PT_DEBRIS ? PT_PLANET : $planet_type; |
||||||
43 | |||||||
44 | return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type); |
||||||
45 | } |
||||||
46 | |||||||
47 | // /** |
||||||
48 | // * @param Vector $vector |
||||||
49 | // * @param bool $for_update |
||||||
50 | // * @param string $fields |
||||||
51 | // * |
||||||
52 | // * @return array |
||||||
53 | // */ |
||||||
54 | // public static function db_planet_by_vector_object($vector, $for_update = false, $fields = '*') { |
||||||
55 | // $planet_type = $vector->type == PT_DEBRIS ? PT_PLANET : $vector->type; |
||||||
56 | // $result = Planet\DBStaticPlanet::db_planet_by_gspt_safe($vector->galaxy, $vector->system, $vector->planet, $planet_type, $for_update, $fields); |
||||||
57 | // |
||||||
58 | // return !empty($result) ? $result : array(); |
||||||
59 | // } |
||||||
60 | |||||||
61 | public static function db_planet_by_parent($parent_id, $for_update = false, $fields = '*') { |
||||||
0 ignored issues
–
show
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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
62 | if (!($parent_id = idval($parent_id))) { |
||||||
63 | return false; |
||||||
64 | } |
||||||
65 | |||||||
66 | return SN::db_get_record_list(LOC_PLANET, |
||||||
67 | "`parent_planet` = {$parent_id} AND `planet_type` = " . PT_MOON, true); |
||||||
68 | } |
||||||
69 | |||||||
70 | public static function db_planet_by_id_and_owner($planet_id, $owner_id, $for_update = false, $fields = '*') { |
||||||
0 ignored issues
–
show
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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() 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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
71 | if (!($planet_id = idval($planet_id)) || !($owner_id = idval($owner_id))) { |
||||||
72 | return false; |
||||||
73 | } |
||||||
74 | |||||||
75 | return SN::db_get_record_list(LOC_PLANET, |
||||||
76 | "`id` = {$planet_id} AND `id_owner` = {$owner_id}", true); |
||||||
77 | } |
||||||
78 | |||||||
79 | |||||||
80 | public static function db_planet_list_moon_other($user_id, $this_moon_id) { |
||||||
81 | if (!($user_id = idval($user_id)) || !($this_moon_id = idval($this_moon_id))) { |
||||||
82 | return false; |
||||||
83 | } |
||||||
84 | |||||||
85 | return SN::db_get_record_list(LOC_PLANET, |
||||||
86 | "`planet_type` = " . PT_MOON . " AND `id_owner` = {$user_id} AND `id` != {$this_moon_id}"); |
||||||
87 | } |
||||||
88 | |||||||
89 | public static function db_planet_list_in_system($galaxy, $system) { |
||||||
90 | $galaxy = intval($galaxy); |
||||||
91 | $system = intval($system); |
||||||
92 | |||||||
93 | return SN::db_get_record_list(LOC_PLANET, |
||||||
94 | "`galaxy` = {$galaxy} AND `system` = {$system}"); |
||||||
95 | } |
||||||
96 | |||||||
97 | public static function db_planet_list_sorted($user_row, $skip_planet_id = false, $conditions = '') { |
||||||
98 | if (!is_array($user_row)) { |
||||||
99 | return false; |
||||||
100 | } |
||||||
101 | $conditions .= $skip_planet_id ? " AND `id` <> {$skip_planet_id} " : ''; |
||||||
102 | |||||||
103 | $sort_orders = array( |
||||||
104 | SORT_ID => '{{planets}}.`id`', |
||||||
105 | SORT_LOCATION => '{{planets}}.`galaxy`, {{planets}}.`system`, {{planets}}.`planet`, {{planets}}.`planet_type`', |
||||||
106 | SORT_NAME => '`name`', |
||||||
107 | SORT_SIZE => '({{planets}}.`field_max`)', |
||||||
108 | ); |
||||||
109 | $order_by = SN::$user_options[PLAYER_OPTION_PLANET_SORT]; |
||||||
110 | if (empty($sort_orders[$order_by])) { |
||||||
111 | $order_by = SORT_ID; |
||||||
112 | } |
||||||
113 | $order_by = $sort_orders[$order_by] . ' ' . (SN::$user_options[PLAYER_OPTION_PLANET_SORT_INVERSE] == SORT_ASCENDING ? 'ASC' : 'DESC'); |
||||||
114 | |||||||
115 | // Compilating query |
||||||
116 | return SN::db_get_record_list(LOC_PLANET, |
||||||
117 | "`id_owner` = '{$user_row['id']}' {$conditions} ORDER BY {$order_by}"); |
||||||
118 | } |
||||||
119 | |||||||
120 | public static function db_planet_list_by_user_or_planet($user_id, $planet_id) { |
||||||
121 | if (!($user_id = idval($user_id)) && !($planet_id = idval($planet_id))) { |
||||||
122 | return false; |
||||||
123 | } |
||||||
124 | |||||||
125 | return SN::db_get_record_list(LOC_PLANET, |
||||||
126 | $planet_id = idval($planet_id) ? "{{planets}}.`id` = {$planet_id}" : "`id_owner` = {$user_id}", $planet_id); |
||||||
127 | } |
||||||
128 | |||||||
129 | public static function db_planet_set_by_id($planet_id, $set) { |
||||||
130 | if (!($planet_id = idval($planet_id))) { |
||||||
131 | return false; |
||||||
132 | } |
||||||
133 | |||||||
134 | return SN::db_upd_record_by_id(LOC_PLANET, $planet_id, $set); |
||||||
135 | } |
||||||
136 | |||||||
137 | public static function db_planet_set_by_gspt($ui_galaxy, $ui_system, $ui_planet, $set, $ui_planet_type = PT_ALL) { |
||||||
138 | if (!($set = trim($set))) { |
||||||
139 | return false; |
||||||
140 | } |
||||||
141 | |||||||
142 | $si_galaxy = intval($ui_galaxy); |
||||||
143 | $si_system = intval($ui_system); |
||||||
144 | $si_planet = intval($ui_planet); |
||||||
145 | $si_planet_type = ($si_planet_type = intval($ui_planet_type)) ? "AND `planet_type` = {$si_planet_type}" : ''; |
||||||
146 | |||||||
147 | return SN::db_upd_record_list(LOC_PLANET, "`galaxy` = {$si_galaxy} AND `system` = {$si_system} AND `planet` = {$si_planet} {$si_planet_type}", $set); |
||||||
148 | } |
||||||
149 | |||||||
150 | public static function db_planet_set_by_parent($ui_parent_id, $ss_set) { |
||||||
151 | if (!($si_parent_id = idval($ui_parent_id)) || !($ss_set = trim($ss_set))) { |
||||||
152 | return false; |
||||||
153 | } |
||||||
154 | |||||||
155 | return SN::db_upd_record_list(LOC_PLANET, "`parent_planet` = {$si_parent_id}", $ss_set); |
||||||
156 | } |
||||||
157 | |||||||
158 | public static function db_planet_set_by_owner($ui_owner_id, $ss_set) { |
||||||
159 | if (!($si_owner_id = idval($ui_owner_id)) || !($ss_set = trim($ss_set))) { |
||||||
160 | return false; |
||||||
161 | } |
||||||
162 | |||||||
163 | return SN::db_upd_record_list(LOC_PLANET, "`id_owner` = {$si_owner_id}", $ss_set); |
||||||
164 | } |
||||||
165 | |||||||
166 | |||||||
167 | public static function db_planet_delete_by_id($planet_id) { |
||||||
168 | if (!($planet_id = idval($planet_id))) { |
||||||
169 | return false; |
||||||
170 | } |
||||||
171 | SN::db_del_record_by_id(LOC_PLANET, $planet_id); |
||||||
172 | SN::db_del_record_list(LOC_UNIT, "`unit_location_type` = " . LOC_PLANET . " AND `unit_location_id` = " . $planet_id); |
||||||
173 | |||||||
174 | // Очереди очистятся автоматически по FOREIGN KEY |
||||||
175 | return true; |
||||||
176 | } |
||||||
177 | |||||||
178 | public static function db_planet_list_delete_by_owner($ui_owner_id) { |
||||||
179 | if (!($si_owner_id = idval($ui_owner_id))) { |
||||||
180 | return false; |
||||||
181 | } |
||||||
182 | SN::db_del_record_list(LOC_PLANET, "`id_owner` = {$si_owner_id}"); |
||||||
183 | SN::db_del_record_list(LOC_UNIT, "`unit_location_type` = " . LOC_PLANET . " AND `unit_player_id` = " . $si_owner_id); |
||||||
184 | |||||||
185 | // Очереди очистятся автоматически по FOREIGN KEY |
||||||
186 | return true; |
||||||
187 | } |
||||||
188 | |||||||
189 | |||||||
190 | public static function db_planet_count_by_type($ui_user_id, $ui_planet_type = PT_PLANET) { |
||||||
191 | $si_user_id = idval($ui_user_id); |
||||||
192 | $si_planet_type = intval($ui_planet_type); |
||||||
193 | |||||||
194 | // Лочим запись-родителя - если она есть и еще не залочена |
||||||
195 | $record_list = SN::db_get_record_list(LOC_PLANET, "`id_owner` = {$si_user_id} AND `planet_type` = {$si_planet_type}"); |
||||||
196 | |||||||
197 | return is_array($record_list) ? count($record_list) : 0; |
||||||
198 | } |
||||||
199 | |||||||
200 | public static function db_planet_list_resources_by_owner() { |
||||||
201 | /** @noinspection SqlResolve */ |
||||||
202 | return SN::$db->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 | /** @noinspection SqlResolve */ |
||||||
207 | SN::$db->doquery("DELETE FROM `{{planets}}` WHERE id_owner NOT IN (SELECT id FROM `{{users}}`)"); |
||||||
208 | } |
||||||
209 | |||||||
210 | } |
||||||
211 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.