1
|
|
|
<?php /** @noinspection SqlResolve */ |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by Gorlum 26.04.2018 14:00 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Fleet; |
8
|
|
|
|
9
|
|
|
use DBAL\DbQuery; |
10
|
|
|
use Exception; |
11
|
|
|
use mysqli_result; |
12
|
|
|
use SN; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DbFleetStatic |
16
|
|
|
* @package Fleet |
17
|
|
|
* |
18
|
|
|
* deprecated |
19
|
|
|
*/ |
20
|
|
|
class DbFleetStatic { |
21
|
|
|
|
22
|
|
|
/* HELPERS ******************************************************************************************************** */ |
23
|
|
|
/** |
24
|
|
|
* @return DbQuery |
25
|
|
|
*/ |
26
|
|
|
protected static function dbq() { |
27
|
|
|
return DbQuery::build()->setTable('fleets'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/* FLEET CRUD ===================================================================================================== */ |
33
|
|
|
/** |
34
|
|
|
* Inserts fleet record by ID with array |
35
|
|
|
* |
36
|
|
|
* @param array $fieldArray - [fieldName => fieldValue] |
37
|
|
|
* |
38
|
|
|
* @return int|string - fleet inserted ID or 0 if no fleets inserted |
39
|
|
|
*/ |
40
|
|
|
public static function fleet_insert_set_dbq($fieldArray) { |
41
|
|
|
if (!empty($fieldArray)) { |
42
|
|
|
static::dbq() |
43
|
|
|
->setValues($fieldArray) |
44
|
|
|
->doInsert(DbQuery::DB_INSERT_PLAIN, true); |
45
|
|
|
|
46
|
|
|
$fleet_id = SN::$db->db_insert_id(); |
47
|
|
|
} else { |
48
|
|
|
$fleet_id = 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $fleet_id; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Updates fleet record by ID with SET |
56
|
|
|
* |
57
|
|
|
* @param int $fleet_id |
58
|
|
|
* @param array $set - REPLACE-set, i.e. replacement of existing values |
59
|
|
|
* @param array $delta - DELTA-set, i.e. changes to existing values |
60
|
|
|
* |
61
|
|
|
* @return array|bool|mysqli_result|null |
62
|
|
|
*/ |
63
|
|
|
public static function fleet_update_set($fleet_id, $set, $delta = array()) { |
64
|
|
|
$result = false; |
65
|
|
|
|
66
|
|
|
$fleet_id_safe = idval($fleet_id); |
67
|
|
|
$set_string_safe = db_set_make_safe_string($set); |
68
|
|
|
!empty($delta) ? $set_string_safe = implode(',', array($set_string_safe, db_set_make_safe_string($delta, true))) : false; |
69
|
|
|
if (!empty($fleet_id_safe) && !empty($set_string_safe)) { |
70
|
|
|
$result = static::db_fleet_update_set_safe_string($fleet_id, $set_string_safe); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* UPDATE - Updates fleet record by ID with SET |
78
|
|
|
* |
79
|
|
|
* @param int $fleet_id |
80
|
|
|
* @param string $set_safe_string |
81
|
|
|
* |
82
|
|
|
* @return array|bool|mysqli_result|null |
83
|
|
|
*/ |
84
|
|
|
protected static function db_fleet_update_set_safe_string($fleet_id, $set_safe_string) { |
85
|
|
|
$fleet_id_safe = idval($fleet_id); |
86
|
|
|
if (!empty($fleet_id_safe) && !empty($set_safe_string)) { |
87
|
|
|
/** @noinspection SqlResolve */ |
88
|
|
|
/** @noinspection SqlWithoutWhere */ |
89
|
|
|
$result = doquery("UPDATE `{{fleets}}` SET {$set_safe_string} WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1;"); |
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
$result = false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* READ - Gets fleet record by ID |
100
|
|
|
* |
101
|
|
|
* @param int $fleet_id |
102
|
|
|
* |
103
|
|
|
* @return array|false |
104
|
|
|
*/ |
105
|
|
|
public static function db_fleet_get($fleet_id) { |
106
|
|
|
$fleet_id_safe = idval($fleet_id); |
107
|
|
|
/** @noinspection SqlResolve */ |
108
|
|
|
$result = doquery("SELECT * FROM `{{fleets}}` WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1 FOR UPDATE;", true); |
|
|
|
|
109
|
|
|
|
110
|
|
|
return is_array($result) ? $result : false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* DELETE |
115
|
|
|
* |
116
|
|
|
* @param $fleet_id |
117
|
|
|
* |
118
|
|
|
* @return array|bool|mysqli_result|null |
119
|
|
|
*/ |
120
|
|
|
public static function db_fleet_delete($fleet_id) { |
121
|
|
|
$fleet_id_safe = idval($fleet_id); |
122
|
|
|
if (!empty($fleet_id_safe)) { |
123
|
|
|
/** @noinspection SqlResolve */ |
124
|
|
|
$result = doquery("DELETE FROM `{{fleets}}` WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1;"); |
|
|
|
|
125
|
|
|
} else { |
126
|
|
|
$result = false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/* FLEET LIST & COUNT CRUD ===========================================================================================*/ |
135
|
|
|
/** |
136
|
|
|
* COUNT - Get fleet count by condition |
137
|
|
|
* |
138
|
|
|
* @param string $where_safe |
139
|
|
|
* |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
|
|
public static function db_fleet_count($where_safe) { |
143
|
|
|
/** @noinspection SqlResolve */ |
144
|
|
|
$result = doquery("SELECT COUNT(`fleet_id`) as 'fleet_count' FROM `{{fleets}}` WHERE {$where_safe}", true); |
|
|
|
|
145
|
|
|
|
146
|
|
|
return !empty($result['fleet_count']) ? intval($result['fleet_count']) : 0; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* LIST - Get fleet list by condition |
152
|
|
|
* |
153
|
|
|
* @param string $where_safe |
154
|
|
|
* |
155
|
|
|
* @return array[] |
156
|
|
|
* |
157
|
|
|
* TODO - This function should NOT be used to query fleets to all planets - some aggregate function should be used |
158
|
|
|
*/ |
159
|
|
|
public static function db_fleet_list($where_safe, $for_update = DB_SELECT_FOR_UPDATE) { |
160
|
|
|
$row_list = []; |
161
|
|
|
|
162
|
|
|
$dbq = DbQuery::build() |
163
|
|
|
->setTable('fleets'); |
164
|
|
|
|
165
|
|
|
if (!empty($where_safe)) { |
166
|
|
|
$dbq->setWhereArrayDanger([$where_safe]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($for_update == DB_SELECT_FOR_UPDATE) { |
170
|
|
|
$dbq->setForUpdate(DbQuery::DB_FOR_UPDATE); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$query = $dbq->doSelect(); |
174
|
|
|
|
175
|
|
|
while ($row = db_fetch($query)) { |
|
|
|
|
176
|
|
|
$row_list[$row['fleet_id']] = $row; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $row_list; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* LIST DELETE |
184
|
|
|
* |
185
|
|
|
* @param $owner_id |
186
|
|
|
* |
187
|
|
|
* @return array|bool|mysqli_result|null |
188
|
|
|
* @deprecated |
189
|
|
|
* |
190
|
|
|
* TODO - fleets should be deleted by DB itself via InnoDB FOREIHN KEY |
191
|
|
|
*/ |
192
|
|
|
public static function db_fleet_list_delete_by_owner($owner_id) { |
193
|
|
|
return doquery("DELETE FROM `{{fleets}}` WHERE `fleet_owner` = '{$owner_id}';"); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* LIST STAT - DEPRECATED |
198
|
|
|
* |
199
|
|
|
* @return array|bool|mysqli_result|null |
200
|
|
|
* |
201
|
|
|
* TODO - deprecated |
202
|
|
|
*/ |
203
|
|
|
public static function db_fleet_list_query_all_stat() { |
204
|
|
|
return doquery("SELECT fleet_owner, fleet_array, fleet_resource_metal, fleet_resource_crystal, fleet_resource_deuterium FROM `{{fleets}}`;"); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/* FLEET FUNCTIONS ===================================================================================================*/ |
210
|
|
|
/** |
211
|
|
|
* Sends fleet back |
212
|
|
|
* |
213
|
|
|
* @param $fleet_row |
214
|
|
|
* |
215
|
|
|
* @return array|bool|mysqli_result|null |
216
|
|
|
* |
217
|
|
|
* TODO - Add another field which mark fleet as processed/completed task on destination point |
218
|
|
|
* By this flag fleet dispatcher should immediately return fleet (change STATUS/mess flag) to source planet |
219
|
|
|
*/ |
220
|
|
|
public static function fleet_send_back(&$fleet_row) { |
221
|
|
|
$fleet_id = round(!empty($fleet_row['fleet_id']) ? $fleet_row['fleet_id'] : $fleet_row); |
222
|
|
|
if (!$fleet_id) { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$result = static::fleet_update_set($fleet_id, array( |
|
|
|
|
227
|
|
|
'fleet_mess' => 1, |
228
|
|
|
)); |
229
|
|
|
|
230
|
|
|
return $result; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
/* FLEET COUNT FUNCTIONS =============================================================================================*/ |
235
|
|
|
/** |
236
|
|
|
* Get flying fleet count |
237
|
|
|
* |
238
|
|
|
* @param int $player_id - Player ID |
239
|
|
|
* @param int $mission_id - mission ID. "0" means "all" |
240
|
|
|
* |
241
|
|
|
* @return int |
242
|
|
|
* |
243
|
|
|
* TODO - Player lock should be issued before to prevent fleet number change |
244
|
|
|
*/ |
245
|
|
|
public static function fleet_count_flying($player_id, $mission_id = 0) { |
246
|
|
|
$player_id_safe = idval($player_id); |
247
|
|
|
if (!empty($player_id_safe)) { |
248
|
|
|
$mission_id_safe = intval($mission_id); |
249
|
|
|
$result = static::db_fleet_count( |
250
|
|
|
"`fleet_owner` = {$player_id_safe}" . |
251
|
|
|
($mission_id_safe ? " AND `fleet_mission` = {$mission_id_safe}" : '') |
252
|
|
|
); |
253
|
|
|
} else { |
254
|
|
|
$result = 0; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $result; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Returns amount of incoming fleets to planet |
262
|
|
|
* |
263
|
|
|
* @param int $galaxy |
264
|
|
|
* @param int $system |
265
|
|
|
* @param int $planet |
266
|
|
|
* |
267
|
|
|
* @return int |
268
|
|
|
* |
269
|
|
|
* TODO - Через fleet_list_by_planet_coords() ???? |
270
|
|
|
*/ |
271
|
|
|
public static function fleet_count_incoming($galaxy, $system, $planet) { |
272
|
|
|
return static::db_fleet_count( |
273
|
|
|
"(`fleet_start_galaxy` = {$galaxy} AND `fleet_start_system` = {$system} AND `fleet_start_planet` = {$planet}) |
274
|
|
|
OR |
275
|
|
|
(`fleet_end_galaxy` = {$galaxy} AND `fleet_end_system` = {$system} AND `fleet_end_planet` = {$planet})" |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/* FLEET LIST FUNCTIONS =============================================================================================*/ |
280
|
|
|
/** |
281
|
|
|
* Get fleet list by owner |
282
|
|
|
* |
283
|
|
|
* @param int $fleet_owner_id - Fleet owner record/ID. Can't be empty |
284
|
|
|
* |
285
|
|
|
* @return array[] |
286
|
|
|
*/ |
287
|
|
|
public static function fleet_list_by_owner_id($fleet_owner_id) { |
288
|
|
|
$fleet_owner_id_safe = idval($fleet_owner_id); |
289
|
|
|
|
290
|
|
|
return $fleet_owner_id_safe ? static::db_fleet_list("`fleet_owner` = {$fleet_owner_id_safe}", DB_SELECT_PLAIN) : array(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get fleet list flying/returning to planet/system coordinates |
295
|
|
|
* |
296
|
|
|
* @param int $galaxy |
297
|
|
|
* @param int $system |
298
|
|
|
* @param int $planet - planet position. "0" means "any" |
299
|
|
|
* @param int $planet_type - planet type. "PT_ALL" means "any type" |
300
|
|
|
* |
301
|
|
|
* @return array |
302
|
|
|
* TODO - safe params |
303
|
|
|
*/ |
304
|
|
|
public static function fleet_list_by_planet_coords($galaxy, $system, $planet = 0, $planet_type = PT_ALL, $for_phalanx = false) { |
305
|
|
|
return static::db_fleet_list( |
306
|
|
|
"( |
307
|
|
|
fleet_start_galaxy = {$galaxy} |
308
|
|
|
AND fleet_start_system = {$system}" . |
309
|
|
|
($planet ? " AND fleet_start_planet = {$planet}" : '') . |
310
|
|
|
($planet_type != PT_ALL ? " AND fleet_start_type = {$planet_type}" : '') . |
311
|
|
|
($for_phalanx ? '' : " AND fleet_mess = 1") . |
312
|
|
|
") |
313
|
|
|
OR |
314
|
|
|
( |
315
|
|
|
fleet_end_galaxy = {$galaxy} |
316
|
|
|
AND fleet_end_system = {$system}" . |
317
|
|
|
($planet ? " AND fleet_end_planet = {$planet}" : '') . |
318
|
|
|
($planet_type != PT_ALL ? " AND fleet_end_type = {$planet_type} " : '') . |
319
|
|
|
($for_phalanx ? '' : " AND fleet_mess = 0") . |
320
|
|
|
")" |
321
|
|
|
, DB_SELECT_PLAIN |
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Fleets on hold on planet orbit |
327
|
|
|
* |
328
|
|
|
* @param int $galaxy |
329
|
|
|
* @param int $system |
330
|
|
|
* @param int $planet |
331
|
|
|
* @param int $planet_type |
332
|
|
|
* @param int $ube_time |
333
|
|
|
* |
334
|
|
|
* @return array |
335
|
|
|
* |
336
|
|
|
* TODO - safe params |
337
|
|
|
*/ |
338
|
|
|
public static function fleet_list_on_hold($galaxy, $system, $planet, $planet_type, $ube_time) { |
339
|
|
|
/** @noinspection PhpRedundantOptionalArgumentInspection */ |
340
|
|
|
return DbFleetStatic::db_fleet_list( |
341
|
|
|
"`fleet_end_galaxy` = {$galaxy} |
342
|
|
|
AND `fleet_end_system` = {$system} |
343
|
|
|
AND `fleet_end_planet` = {$planet} |
344
|
|
|
AND `fleet_end_type` = {$planet_type} |
345
|
|
|
AND `fleet_start_time` <= {$ube_time} |
346
|
|
|
AND `fleet_end_stay` >= {$ube_time} |
347
|
|
|
AND `fleet_mess` = 0" |
348
|
|
|
, DB_SELECT_FOR_UPDATE |
349
|
|
|
); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get aggressive fleet list of chosen player on selected planet |
354
|
|
|
* |
355
|
|
|
* @param $fleet_owner_id |
356
|
|
|
* @param $planet_row |
357
|
|
|
* |
358
|
|
|
* @return array |
359
|
|
|
*/ |
360
|
|
|
public static function fleet_list_bashing($fleet_owner_id, $planet_row) { |
361
|
|
|
return static::db_fleet_list( |
362
|
|
|
"`fleet_end_galaxy` = {$planet_row['galaxy']} |
363
|
|
|
AND `fleet_end_system` = {$planet_row['system']} |
364
|
|
|
AND `fleet_end_planet` = {$planet_row['planet']} |
365
|
|
|
AND `fleet_end_type` = {$planet_row['planet_type']} |
366
|
|
|
AND `fleet_owner` = {$fleet_owner_id} |
367
|
|
|
AND `fleet_mission` IN (" . MT_ATTACK . "," . MT_AKS . "," . MT_DESTROY . ") |
368
|
|
|
AND `fleet_mess` = 0" |
369
|
|
|
, DB_SELECT_FOR_UPDATE |
370
|
|
|
); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Get fleets in group |
375
|
|
|
* |
376
|
|
|
* @param $group_id |
377
|
|
|
* |
378
|
|
|
* @return array |
379
|
|
|
*/ |
380
|
|
|
public static function fleet_list_by_group($group_id) { |
381
|
|
|
return static::db_fleet_list("`fleet_group` = {$group_id}", DB_SELECT_FOR_UPDATE); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
|
385
|
|
|
|
386
|
|
|
/* MISSILE CRUD *******************************************************************************************************/ |
387
|
|
|
/* MISSILE LIST & COUNT CRUD =========================================================================================*/ |
388
|
|
|
/** |
389
|
|
|
* LIST - Get missile attack list by condition |
390
|
|
|
* |
391
|
|
|
* @param string $where - WHERE condition - SQL SAFE! |
392
|
|
|
* @param bool $for_update - lock record with FOR UPDATE statement |
393
|
|
|
* |
394
|
|
|
* @return array - lift of fleet records from DB |
395
|
|
|
*/ |
396
|
|
|
public static function db_missile_list($where, $for_update = DB_SELECT_FOR_UPDATE) { |
397
|
|
|
$row_list = []; |
398
|
|
|
|
399
|
|
|
/** @noinspection SqlResolve */ |
400
|
|
|
$query = doquery( |
|
|
|
|
401
|
|
|
"SELECT * FROM `{{iraks}}`" . |
402
|
|
|
(!empty($where) ? " WHERE {$where}" : '') . |
403
|
|
|
($for_update == DB_SELECT_FOR_UPDATE ? " FOR UPDATE" : '') |
404
|
|
|
); |
405
|
|
|
while ($row = db_fetch($query)) { |
|
|
|
|
406
|
|
|
$row_list[$row['id']] = $row; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return $row_list; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
|
413
|
|
|
|
414
|
|
|
/* FLEET/MISSILES LIST FUNCTIONS =====================================================================================*/ |
415
|
|
|
/** |
416
|
|
|
* Get fleet and missile list by coordinates |
417
|
|
|
* |
418
|
|
|
* @param array $coordinates |
419
|
|
|
* @param bool $for_phalanx - If true - this is phalanx scan so limiting output with fleet_mess |
420
|
|
|
* |
421
|
|
|
* @return array |
422
|
|
|
*/ |
423
|
|
|
public static function fleet_and_missiles_list_by_coordinates($coordinates, $for_phalanx = false) { |
424
|
|
|
if (empty($coordinates) || !is_array($coordinates)) { |
425
|
|
|
return array(); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
$fleet_db_list = static::fleet_list_by_planet_coords($coordinates['galaxy'], $coordinates['system'], $coordinates['planet'], $coordinates['planet_type'], $for_phalanx); |
429
|
|
|
|
430
|
|
|
$missile_db_list = static::db_missile_list( |
431
|
|
|
"( |
432
|
|
|
fleet_start_galaxy = {$coordinates['galaxy']} |
433
|
|
|
AND fleet_start_system = {$coordinates['system']} |
434
|
|
|
AND fleet_start_planet = {$coordinates['planet']} |
435
|
|
|
AND fleet_start_type = {$coordinates['planet_type']} |
436
|
|
|
) |
437
|
|
|
OR |
438
|
|
|
( |
439
|
|
|
fleet_end_galaxy = {$coordinates['galaxy']} |
440
|
|
|
AND fleet_end_system = {$coordinates['system']} |
441
|
|
|
AND fleet_end_planet = {$coordinates['planet']} |
442
|
|
|
AND fleet_end_type = {$coordinates['planet_type']} |
443
|
|
|
)" |
444
|
|
|
, DB_SELECT_PLAIN |
445
|
|
|
); |
446
|
|
|
|
447
|
|
|
missile_list_convert_to_fleet($missile_db_list, $fleet_db_list); |
448
|
|
|
|
449
|
|
|
return $fleet_db_list; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Get fleet and missile list by that flies from player's planets OR to player's planets |
454
|
|
|
* |
455
|
|
|
* @param int $owner_id |
456
|
|
|
* |
457
|
|
|
* @return array |
458
|
|
|
*/ |
459
|
|
|
public static function fleet_and_missiles_list_incoming($owner_id) { |
460
|
|
|
$owner_id_safe = idval($owner_id); |
461
|
|
|
if (empty($owner_id_safe)) { |
462
|
|
|
return array(); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
$where = "`fleet_owner` = '{$owner_id_safe}' OR `fleet_target_owner` = '{$owner_id_safe}'"; |
466
|
|
|
$fleet_db_list = static::db_fleet_list($where, DB_SELECT_PLAIN); |
467
|
|
|
$missile_db_list = static::db_missile_list($where, DB_SELECT_PLAIN); |
468
|
|
|
|
469
|
|
|
missile_list_convert_to_fleet($missile_db_list, $fleet_db_list); |
470
|
|
|
|
471
|
|
|
return $fleet_db_list; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
|
475
|
|
|
|
476
|
|
|
/* ACS ****************************************************************************************************************/ |
477
|
|
|
/** |
478
|
|
|
* Purges ACS list |
479
|
|
|
*/ |
480
|
|
|
public static function db_fleet_acs_purge() { |
481
|
|
|
DbQuery::build() |
|
|
|
|
482
|
|
|
->setTable('aks') |
483
|
|
|
->setWhereArrayDanger(['`id` NOT IN (SELECT DISTINCT `fleet_group` FROM `{{fleets}}`)']) |
484
|
|
|
->doDelete(); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
public static function dbAcsGetById($acsId) { |
488
|
|
|
return DbQuery::build() |
489
|
|
|
->setTable('aks') |
490
|
|
|
->setWhereArray(['id' => $acsId]) |
491
|
|
|
->doSelectFetch(); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
public static function dbAcsGetByFleet($fleetId) { |
495
|
|
|
return DbQuery::build() |
496
|
|
|
->setTable('aks') |
497
|
|
|
->setWhereArray(['flotten' => $fleetId]) |
498
|
|
|
->doSelectFetch(); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param int|array $fleetList [(int|string)fleetId,...] |
503
|
|
|
* |
504
|
|
|
* @return int |
505
|
|
|
*/ |
506
|
|
|
public static function dbAcsDelete($fleetList) { |
507
|
|
|
if (!is_array($fleetList) && !empty($fleetList)) { |
508
|
|
|
$fleetList = [$fleetList]; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
if (empty($fleetList)) { |
512
|
|
|
return 0; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
$whereId = []; |
516
|
|
|
foreach ($fleetList as $fleetId) { |
517
|
|
|
$whereId[] = "'" . SN::$db->db_escape($fleetId) . "'"; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
return SN::$db->doquery("DELETE FROM `{{aks}}` WHERE `id` IN (" . implode(',', $whereId) . ")"); |
|
|
|
|
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* @param $userToAddID |
525
|
|
|
* @param $fleetid |
526
|
|
|
* |
527
|
|
|
* @return array|bool|mysqli_result|null |
528
|
|
|
*/ |
529
|
|
|
public static function dbAcsAddUserByFleetId($userToAddID, $fleetid) { |
530
|
|
|
return SN::$db->doquery("UPDATE `{{aks}}` SET `eingeladen` = concat(`eingeladen`, ',{$userToAddID}') WHERE `flotten` = {$fleetid};"); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* @param int $userId |
535
|
|
|
* @param int $fleetid |
536
|
|
|
* @param array $fleet |
537
|
|
|
* |
538
|
|
|
* @return array|bool|mysqli_result|null |
539
|
|
|
*/ |
540
|
|
|
public static function dbAcsInsert($userId, $fleetid, $fleet) { |
541
|
|
|
return doquery("INSERT INTO `{{aks}}` SET |
|
|
|
|
542
|
|
|
`name` = '" . SN::$db->db_escape(SN::$lang['flt_acs_prefix'] . $fleetid) . "', |
543
|
|
|
`teilnehmer` = '" . $userId . "', |
544
|
|
|
`flotten` = '" . $fleetid . "', |
545
|
|
|
`ankunft` = '" . $fleet['fleet_start_time'] . "', |
546
|
|
|
`galaxy` = '" . $fleet['fleet_end_galaxy'] . "', |
547
|
|
|
`system` = '" . $fleet['fleet_end_system'] . "', |
548
|
|
|
`planet` = '" . $fleet['fleet_end_planet'] . "', |
549
|
|
|
`planet_type` = '" . $fleet['fleet_end_type'] . "', |
550
|
|
|
`eingeladen` = '" . $userId . "', |
551
|
|
|
`fleet_end_time` = '" . $fleet['fleet_end_time'] . "'"); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* @return array|bool|mysqli_result|null |
556
|
|
|
*/ |
557
|
|
|
public static function dbAcsGetAll() { |
558
|
|
|
return doquery('SELECT * FROM `{{aks}}`;'); |
|
|
|
|
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* @param $user |
564
|
|
|
* @param $fleet |
565
|
|
|
* @param $userToAddRecord |
566
|
|
|
* |
567
|
|
|
* @return array |
568
|
|
|
* @throws Exception |
569
|
|
|
*/ |
570
|
|
|
public static function acsAddUser($user, $fleet, $userToAddRecord) { |
571
|
|
|
$userToAddID = !empty($userToAddRecord['id']) ? $userToAddRecord['id'] : 0; |
572
|
|
|
|
573
|
|
|
if (empty($userToAddID)) { |
574
|
|
|
throw new Exception(SN::$lang['fl_aks_player_error']); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
if ($fleet['fleet_target_owner'] == $userToAddID) { |
578
|
|
|
throw new Exception(SN::$lang['flt_aks_player_same']); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
$aks = DbFleetStatic::dbAcsGetByFleet($fleet['fleet_id']); |
582
|
|
|
|
583
|
|
|
$aks = DbFleetStatic::acsAddUser2($aks, $fleet, $userToAddID, $user['id']); |
584
|
|
|
|
585
|
|
|
return $aks; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @param array $aks |
591
|
|
|
* @param array $fleet |
592
|
|
|
* @param $userToAddID |
593
|
|
|
* @param $userId |
594
|
|
|
* |
595
|
|
|
* @return array |
596
|
|
|
* |
597
|
|
|
* @throws Exception |
598
|
|
|
*/ |
599
|
|
|
public static function acsAddUser2($aks, $fleet, $userToAddID, $userId) { |
600
|
|
|
$fleetid = $fleet['fleet_id']; |
601
|
|
|
|
602
|
|
|
if (!$aks) { |
|
|
|
|
603
|
|
|
// No AСS exists - making one |
604
|
|
|
if (!$fleet['fleet_group']) { |
605
|
|
|
DbFleetStatic::dbAcsInsert($userId, $fleetid, $fleet); |
606
|
|
|
$aks = DbFleetStatic::dbAcsGetByFleet($fleetid); |
607
|
|
|
|
608
|
|
|
DbFleetStatic::fleet_update_set($fleetid, array( |
609
|
|
|
'fleet_group' => $aks['id'], |
610
|
|
|
'fleet_mission' => MT_AKS, |
611
|
|
|
)); |
612
|
|
|
$fleet['fleet_group'] = $aks['id']; |
613
|
|
|
$fleet['fleet_mission'] = MT_AKS; |
614
|
|
|
} else { |
615
|
|
|
throw new Exception(SN::$lang['fl_aks_already_in_aks']); |
616
|
|
|
} |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
$invited_ar = explode(",", $aks['eingeladen']); |
620
|
|
|
if (count($invited_ar) >= 5) { |
621
|
|
|
throw new Exception(SN::$lang['flt_aks_error_too_much_players']); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
$acsPoints = 0; |
625
|
|
|
$isUserExists = false; |
626
|
|
|
foreach ($invited_ar as $inv) { |
627
|
|
|
if ($userToAddID == $inv) { |
628
|
|
|
$isUserExists = true; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
$invitedUserRecord = db_user_by_id($inv); |
|
|
|
|
632
|
|
|
if (!empty($invitedUserRecord)) { |
633
|
|
|
$acsPoints += $invitedUserRecord['total_points']; |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
$attackedPlayer = db_user_by_id($fleet['fleet_target_owner']); |
|
|
|
|
638
|
|
|
if ( |
639
|
|
|
!empty($attackedPlayer['total_points']) |
640
|
|
|
&& |
641
|
|
|
SN::$gc->general->playerIs1stStrongerThen2nd($acsPoints, $attackedPlayer['total_points']) |
642
|
|
|
) { |
643
|
|
|
throw new Exception(SN::$lang['fl_aks_too_power']); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
|
647
|
|
|
if (self::acsIsAcsFull($aks['id'])) { |
648
|
|
|
throw new Exception(SN::$lang['fl_aks_too_power']); |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
if ($isUserExists) { |
652
|
|
|
// if ($userToAddID != $userId) |
653
|
|
|
throw new Exception(SN::$lang['fl_aks_player_invited_already']); |
654
|
|
|
} else { |
655
|
|
|
DbFleetStatic::dbAcsAddUserByFleetId($userToAddID, $fleetid); |
656
|
|
|
$aks['eingeladen'] .= ',' . $userToAddID; |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
return $aks; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* @param int $acsId |
664
|
|
|
* |
665
|
|
|
* @return bool |
666
|
|
|
*/ |
667
|
|
|
public static function acsIsAcsFull($acsId) { |
668
|
|
|
$fleetInAcs = self::fleet_list_by_group($acsId); |
669
|
|
|
$isMaxSubReached = count($fleetInAcs) >= 5; |
670
|
|
|
|
671
|
|
|
return $isMaxSubReached; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* @param array $user |
676
|
|
|
* |
677
|
|
|
* @return array |
678
|
|
|
*/ |
679
|
|
|
public static function tpl_get_fleets_flying(&$user) { |
680
|
|
|
$fleet_flying_list = array(); |
681
|
|
|
|
682
|
|
|
$fleet_flying_list[0] = DbFleetStatic::fleet_list_by_owner_id($user['id']); |
683
|
|
|
foreach ($fleet_flying_list[0] as $fleet_id => $fleet_flying_row) { |
684
|
|
|
$fleet_flying_list[$fleet_flying_row['fleet_mission']][$fleet_id] = &$fleet_flying_list[0][$fleet_id]; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
return $fleet_flying_list; |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
} |
691
|
|
|
|