Test Failed
Push — master ( 0d5d14...c17ef6 )
by SuperNova.WS
06:37
created

DbFleetStatic::acsAddUser2()   B

Complexity

Conditions 11
Paths 43

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 37
nc 43
nop 4
dl 0
loc 61
rs 7.3166
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = 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;");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

89
      $result = /** @scrutinizer ignore-deprecated */ doquery("UPDATE `{{fleets}}` SET {$set_safe_string} WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1;");
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

108
    $result = /** @scrutinizer ignore-deprecated */ doquery("SELECT * FROM `{{fleets}}` WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1 FOR UPDATE;", true);
Loading history...
Bug introduced by
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

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

108
    $result = doquery("SELECT * FROM `{{fleets}}` WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1 FOR UPDATE;", /** @scrutinizer ignore-type */ true);
Loading history...
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;");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

124
      $result = /** @scrutinizer ignore-deprecated */ doquery("DELETE FROM `{{fleets}}` WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1;");
Loading history...
125
    } else {
126
      $result = false;
127
    }
128
129
    return $result;
130
  }
131
132
133
  /**
134
   * LOCK - Lock all records which can be used with mission
135
   *
136
   * @param $mission_data
137
   * @param $fleet_id
138
   *
139
   * @return array|bool|mysqli_result|null
140
   */
141
  public static function db_fleet_lock_flying($fleet_id, &$mission_data) {
142
    // Тупо лочим всех юзеров, чьи флоты летят или улетают с координат отбытия/прибытия $fleet_row
143
    // Что бы делать это умно - надо учитывать fleet_mess во $fleet_row и в таблице fleets
144
    $fleet_id_safe = idval($fleet_id);
145
146
    $query = [];
147
    /** @noinspection SqlResolve */
148
    $query[] = "SELECT 1 FROM `{{fleets}}` AS f";
149
    // Блокировка всех прилетающих и улетающих флотов, если нужно
150
    $mission_data['dst_fleets'] ? $query[] = 'LEFT JOIN `{{fleets}}` AS fd ON fd.fleet_end_planet_id = f.fleet_end_planet_id OR fd.fleet_start_planet_id = f.fleet_end_planet_id' : false;
151
152
    $mission_data['dst_user'] || $mission_data['dst_planet'] ? $query[] = 'LEFT JOIN `{{users}}` AS ud ON ud.id = f.fleet_target_owner' : false;
153
    $mission_data['dst_planet'] ? $query[] = 'LEFT JOIN `{{planets}}` AS pd ON pd.id = f.fleet_end_planet_id' : false;
154
155
    $mission_data['src_user'] || $mission_data['src_planet'] ? $query[] = 'LEFT JOIN `{{users}}` AS us ON us.id = f.fleet_owner' : false;
156
    $mission_data['src_planet'] ? $query[] = 'LEFT JOIN `{{planets}}` AS ps ON ps.id = f.fleet_start_planet_id' : false;
157
158
    $query[] = "WHERE f.fleet_id = {$fleet_id_safe} GROUP BY 1 FOR UPDATE";
159
160
    return doquery(implode(' ', $query));
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

160
    return /** @scrutinizer ignore-deprecated */ doquery(implode(' ', $query));
Loading history...
161
  }
162
163
164
165
  /* FLEET LIST & COUNT CRUD ===========================================================================================*/
166
  /**
167
   * COUNT - Get fleet count by condition
168
   *
169
   * @param string $where_safe
170
   *
171
   * @return int
172
   */
173
  public static function db_fleet_count($where_safe) {
174
    /** @noinspection SqlResolve */
175
    $result = doquery("SELECT COUNT(`fleet_id`) as 'fleet_count' FROM `{{fleets}}` WHERE {$where_safe}", true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

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

175
    $result = doquery("SELECT COUNT(`fleet_id`) as 'fleet_count' FROM `{{fleets}}` WHERE {$where_safe}", /** @scrutinizer ignore-type */ true);
Loading history...
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

175
    $result = /** @scrutinizer ignore-deprecated */ doquery("SELECT COUNT(`fleet_id`) as 'fleet_count' FROM `{{fleets}}` WHERE {$where_safe}", true);
Loading history...
176
177
    return !empty($result['fleet_count']) ? intval($result['fleet_count']) : 0;
178
  }
179
180
181
  /**
182
   * LIST - Get fleet list by condition
183
   *
184
   * @param string $where_safe
185
   *
186
   * @return array[]
187
   *
188
   * TODO - This function should NOT be used to query fleets to all planets - some aggregate function should be used
189
   */
190
  public static function db_fleet_list($where_safe, $for_update = DB_SELECT_FOR_UPDATE) {
191
    $row_list = [];
192
193
    $dbq = DbQuery::build()
194
      ->setTable('fleets');
195
196
    if (!empty($where_safe)) {
197
      $dbq->setWhereArrayDanger([$where_safe]);
198
    }
199
200
    if ($for_update == DB_SELECT_FOR_UPDATE) {
201
      $dbq->setForUpdate(DbQuery::DB_FOR_UPDATE);
202
    }
203
204
    $query = $dbq->doSelect();
205
206
    while ($row = db_fetch($query)) {
207
      $row_list[$row['fleet_id']] = $row;
208
    }
209
210
    return $row_list;
211
  }
212
213
  /**
214
   * LIST DELETE
215
   *
216
   * @param $owner_id
217
   *
218
   * @return array|bool|mysqli_result|null
219
   * @deprecated
220
   *
221
   * TODO - fleets should be deleted by DB itself via InnoDB FOREIHN KEY
222
   */
223
  public static function db_fleet_list_delete_by_owner($owner_id) {
224
    return doquery("DELETE FROM `{{fleets}}` WHERE `fleet_owner` = '{$owner_id}';");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

224
    return /** @scrutinizer ignore-deprecated */ doquery("DELETE FROM `{{fleets}}` WHERE `fleet_owner` = '{$owner_id}';");
Loading history...
225
  }
226
227
  /**
228
   * LIST STAT - DEPRECATED
229
   *
230
   * @return array|bool|mysqli_result|null
231
   *
232
   * TODO - deprecated
233
   */
234
  public static function db_fleet_list_query_all_stat() {
235
    return  doquery("SELECT fleet_owner, fleet_array, fleet_resource_metal, fleet_resource_crystal, fleet_resource_deuterium FROM `{{fleets}}`;");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

235
    return  /** @scrutinizer ignore-deprecated */ doquery("SELECT fleet_owner, fleet_array, fleet_resource_metal, fleet_resource_crystal, fleet_resource_deuterium FROM `{{fleets}}`;");
Loading history...
236
  }
237
238
239
240
  /* FLEET FUNCTIONS ===================================================================================================*/
241
  /**
242
   * Sends fleet back
243
   *
244
   * @param $fleet_row
245
   *
246
   * @return array|bool|mysqli_result|null
247
   *
248
   * TODO - Add another field which mark fleet as processed/completed task on destination point
249
   * By this flag fleet dispatcher should immediately return fleet (change STATUS/mess flag) to source planet
250
   */
251
  public static function fleet_send_back(&$fleet_row) {
252
    $fleet_id = round(!empty($fleet_row['fleet_id']) ? $fleet_row['fleet_id'] : $fleet_row);
253
    if (!$fleet_id) {
254
      return false;
255
    }
256
257
    $result = static::fleet_update_set($fleet_id, array(
0 ignored issues
show
Bug introduced by
$fleet_id of type double is incompatible with the type integer expected by parameter $fleet_id of Fleet\DbFleetStatic::fleet_update_set(). ( Ignorable by Annotation )

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

257
    $result = static::fleet_update_set(/** @scrutinizer ignore-type */ $fleet_id, array(
Loading history...
258
      'fleet_mess' => 1,
259
    ));
260
261
    return $result;
262
  }
263
264
265
  /* FLEET COUNT FUNCTIONS =============================================================================================*/
266
  /**
267
   * Get flying fleet count
268
   *
269
   * @param int $player_id  - Player ID
270
   * @param int $mission_id - mission ID. "0" means "all"
271
   *
272
   * @return int
273
   *
274
   * TODO - Player lock should be issued before to prevent fleet number change
275
   */
276
  public static function fleet_count_flying($player_id, $mission_id = 0) {
277
    $player_id_safe = idval($player_id);
278
    if (!empty($player_id_safe)) {
279
      $mission_id_safe = intval($mission_id);
280
      $result          = static::db_fleet_count(
281
        "`fleet_owner` = {$player_id_safe}" .
282
        ($mission_id_safe ? " AND `fleet_mission` = {$mission_id_safe}" : '')
283
      );
284
    } else {
285
      $result = 0;
286
    }
287
288
    return $result;
289
  }
290
291
  /**
292
   * Returns amount of incoming fleets to planet
293
   *
294
   * @param int $galaxy
295
   * @param int $system
296
   * @param int $planet
297
   *
298
   * @return int
299
   *
300
   * TODO - Через fleet_list_by_planet_coords() ????
301
   */
302
  public static function fleet_count_incoming($galaxy, $system, $planet) {
303
    return static::db_fleet_count(
304
      "(`fleet_start_galaxy` = {$galaxy} AND `fleet_start_system` = {$system} AND `fleet_start_planet` = {$planet})
305
    OR
306
    (`fleet_end_galaxy` = {$galaxy} AND `fleet_end_system` = {$system} AND `fleet_end_planet` = {$planet})"
307
    );
308
  }
309
310
  /* FLEET LIST FUNCTIONS =============================================================================================*/
311
  /**
312
   * Get fleet list by owner
313
   *
314
   * @param int $fleet_owner_id - Fleet owner record/ID. Can't be empty
315
   *
316
   * @return array[]
317
   */
318
  public static function fleet_list_by_owner_id($fleet_owner_id) {
319
    $fleet_owner_id_safe = idval($fleet_owner_id);
320
321
    return $fleet_owner_id_safe ? static::db_fleet_list("`fleet_owner` = {$fleet_owner_id_safe}", DB_SELECT_PLAIN) : array();
322
  }
323
324
  /**
325
   * Get fleet list flying/returning to planet/system coordinates
326
   *
327
   * @param int $galaxy
328
   * @param int $system
329
   * @param int $planet      - planet position. "0" means "any"
330
   * @param int $planet_type - planet type. "PT_ALL" means "any type"
331
   *
332
   * @return array
333
   * TODO - safe params
334
   */
335
  public static function fleet_list_by_planet_coords($galaxy, $system, $planet = 0, $planet_type = PT_ALL, $for_phalanx = false) {
336
    return static::db_fleet_list(
337
      "(
338
    fleet_start_galaxy = {$galaxy}
339
    AND fleet_start_system = {$system}" .
340
      ($planet ? " AND fleet_start_planet = {$planet}" : '') .
341
      ($planet_type != PT_ALL ? " AND fleet_start_type = {$planet_type}" : '') .
342
      ($for_phalanx ? '' : " AND fleet_mess = 1") .
343
      ")
344
    OR
345
    (
346
    fleet_end_galaxy = {$galaxy}
347
    AND fleet_end_system = {$system}" .
348
      ($planet ? " AND fleet_end_planet = {$planet}" : '') .
349
      ($planet_type != PT_ALL ? " AND fleet_end_type = {$planet_type} " : '') .
350
      ($for_phalanx ? '' : " AND fleet_mess = 0") .
351
      ")"
352
      , DB_SELECT_PLAIN
353
    );
354
  }
355
356
  /**
357
   * Fleets on hold on planet orbit
358
   *
359
   * @param $fleet_row
360
   * @param $ube_time
361
   *
362
   * @return array
363
   *
364
   * TODO - safe params
365
   */
366
  public static function fleet_list_on_hold($galaxy, $system, $planet, $planet_type, $ube_time) {
367
    return static::db_fleet_list(
368
      "`fleet_end_galaxy` = {$galaxy}
369
    AND `fleet_end_system` = {$system}
370
    AND `fleet_end_planet` = {$planet}
371
    AND `fleet_end_type` = {$planet_type}
372
    AND `fleet_start_time` <= {$ube_time}
373
    AND `fleet_end_stay` >= {$ube_time}
374
    AND `fleet_mess` = 0"
375
      , DB_SELECT_FOR_UPDATE
376
    );
377
  }
378
379
  /**
380
   * Get aggressive fleet list of chosen player on selected planet
381
   *
382
   * @param $fleet_owner_id
383
   * @param $planet_row
384
   *
385
   * @return array
386
   */
387
  public static function fleet_list_bashing($fleet_owner_id, $planet_row) {
388
    return static::db_fleet_list(
389
      "`fleet_end_galaxy` = {$planet_row['galaxy']}
390
    AND `fleet_end_system` = {$planet_row['system']}
391
    AND `fleet_end_planet` = {$planet_row['planet']}
392
    AND `fleet_end_type`   = {$planet_row['planet_type']}
393
    AND `fleet_owner` = {$fleet_owner_id}
394
    AND `fleet_mission` IN (" . MT_ATTACK . "," . MT_AKS . "," . MT_DESTROY . ")
395
    AND `fleet_mess` = 0"
396
      , DB_SELECT_FOR_UPDATE
397
    );
398
  }
399
400
  /**
401
   * Get fleets in group
402
   *
403
   * @param $group_id
404
   *
405
   * @return array
406
   */
407
  public static function fleet_list_by_group($group_id) {
408
    return static::db_fleet_list("`fleet_group` = {$group_id}", DB_SELECT_FOR_UPDATE);
409
  }
410
411
412
413
  /* MISSILE CRUD *******************************************************************************************************/
414
  /* MISSILE LIST & COUNT CRUD =========================================================================================*/
415
  /**
416
   * LIST - Get missile attack list by condition
417
   *
418
   * @param string $where      - WHERE condition - SQL SAFE!
419
   * @param bool   $for_update - lock record with FOR UPDATE statement
420
   *
421
   * @return array - lift of fleet records from DB
422
   */
423
  public static function db_missile_list($where, $for_update = DB_SELECT_FOR_UPDATE) {
424
    $row_list = [];
425
426
    /** @noinspection SqlResolve */
427
    $query = doquery(
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

427
    $query = /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
428
      "SELECT * FROM `{{iraks}}`" .
429
      (!empty($where) ? " WHERE {$where}" : '') .
430
      ($for_update == DB_SELECT_FOR_UPDATE ? " FOR UPDATE" : '')
431
    );
432
    while ($row = db_fetch($query)) {
433
      $row_list[$row['id']] = $row;
434
    }
435
436
    return $row_list;
437
  }
438
439
440
441
  /* FLEET/MISSILES LIST FUNCTIONS =====================================================================================*/
442
  /**
443
   * Get fleet and missile list by coordinates
444
   *
445
   * @param array $coordinates
446
   * @param bool  $for_phalanx - If true - this is phalanx scan so limiting output with fleet_mess
447
   *
448
   * @return array
449
   */
450
  public static function fleet_and_missiles_list_by_coordinates($coordinates, $for_phalanx = false) {
451
    if (empty($coordinates) || !is_array($coordinates)) {
452
      return array();
453
    }
454
455
    $fleet_db_list = static::fleet_list_by_planet_coords($coordinates['galaxy'], $coordinates['system'], $coordinates['planet'], $coordinates['planet_type'], $for_phalanx);
456
457
    $missile_db_list = static::db_missile_list(
458
      "(
459
      fleet_start_galaxy = {$coordinates['galaxy']}
460
      AND fleet_start_system = {$coordinates['system']}
461
      AND fleet_start_planet = {$coordinates['planet']}
462
      AND fleet_start_type = {$coordinates['planet_type']}
463
    )
464
    OR
465
    (
466
      fleet_end_galaxy = {$coordinates['galaxy']}
467
      AND fleet_end_system = {$coordinates['system']}
468
      AND fleet_end_planet = {$coordinates['planet']}
469
      AND fleet_end_type = {$coordinates['planet_type']}
470
    )"
471
      , DB_SELECT_PLAIN
472
    );
473
474
    missile_list_convert_to_fleet($missile_db_list, $fleet_db_list);
475
476
    return $fleet_db_list;
477
  }
478
479
  /**
480
   * Get fleet and missile list by that flies from player's planets OR to player's planets
481
   *
482
   * @param int $owner_id
483
   *
484
   * @return array
485
   */
486
  public static function fleet_and_missiles_list_incoming($owner_id) {
487
    $owner_id_safe = idval($owner_id);
488
    if (empty($owner_id_safe)) {
489
      return array();
490
    }
491
492
    $where           = "`fleet_owner` = '{$owner_id_safe}' OR `fleet_target_owner` = '{$owner_id_safe}'";
493
    $fleet_db_list   = static::db_fleet_list($where, DB_SELECT_PLAIN);
494
    $missile_db_list = static::db_missile_list($where, DB_SELECT_PLAIN);
495
496
    missile_list_convert_to_fleet($missile_db_list, $fleet_db_list);
497
498
    return $fleet_db_list;
499
  }
500
501
502
503
  /* ACS ****************************************************************************************************************/
504
  /**
505
   * Purges ACS list
506
   */
507
  public static function db_fleet_acs_purge() {
508
    DbQuery::build()
0 ignored issues
show
Deprecated Code introduced by
The function DBAL\DbQuery::doDelete() has been deprecated. ( Ignorable by Annotation )

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

508
    /** @scrutinizer ignore-deprecated */ DbQuery::build()
Loading history...
509
      ->setTable('aks')
510
      ->setWhereArrayDanger(['`id` NOT IN (SELECT DISTINCT `fleet_group` FROM `{{fleets}}`)'])
511
      ->doDelete();
512
  }
513
514
  public static function dbAcsGetById($acsId) {
515
    return DbQuery::build()
516
      ->setTable('aks')
517
      ->setWhereArray(['id' => $acsId])
518
      ->doSelectFetch();
519
  }
520
521
  public static function dbAcsGetByFleet($fleetId) {
522
    return DbQuery::build()
523
      ->setTable('aks')
524
      ->setWhereArray(['flotten' => $fleetId])
525
      ->doSelectFetch();
526
  }
527
528
  /**
529
   * @param int|array $fleetList [(int|string)fleetId,...]
530
   *
531
   * @return int
532
   */
533
  public static function dbAcsDelete($fleetList) {
534
    if (!is_array($fleetList) && !empty($fleetList)) {
535
      $fleetList = [$fleetList];
536
    }
537
538
    if (empty($fleetList)) {
539
      return 0;
540
    }
541
542
    $whereId = [];
543
    foreach ($fleetList as $fleetId) {
544
      $whereId[] = "'" . SN::$db->db_escape($fleetId) . "'";
545
    }
546
547
    return SN::$db->doquery("DELETE FROM `{{aks}}` WHERE `id` IN (" . implode(',', $whereId) . ")");
0 ignored issues
show
Bug Best Practice introduced by
The expression return SN::db->doquery('...e(',', $whereId) . ')') returns the type boolean|mysqli_result which is incompatible with the documented return type integer.
Loading history...
548
  }
549
550
  /**
551
   * @param $userToAddID
552
   * @param $fleetid
553
   *
554
   * @return array|bool|mysqli_result|null
555
   */
556
  public static function dbAcsAddUserByFleetId($userToAddID, $fleetid) {
557
    return SN::$db->doquery("UPDATE `{{aks}}` SET `eingeladen` = concat(`eingeladen`, ',{$userToAddID}') WHERE `flotten` = {$fleetid};");
558
  }
559
560
  /**
561
   * @param int   $userId
562
   * @param int   $fleetid
563
   * @param array $fleet
564
   *
565
   * @return array|bool|mysqli_result|null
566
   */
567
  public static function dbAcsInsert($userId, $fleetid, $fleet) {
568
    return doquery("INSERT INTO `{{aks}}` SET
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

568
    return /** @scrutinizer ignore-deprecated */ doquery("INSERT INTO `{{aks}}` SET
Loading history...
569
          `name` = '" . db_escape(SN::$lang['flt_acs_prefix'] . $fleetid) . "',
570
          `teilnehmer` = '" . $userId . "',
571
          `flotten` = '" . $fleetid . "',
572
          `ankunft` = '" . $fleet['fleet_start_time'] . "',
573
          `galaxy` = '" . $fleet['fleet_end_galaxy'] . "',
574
          `system` = '" . $fleet['fleet_end_system'] . "',
575
          `planet` = '" . $fleet['fleet_end_planet'] . "',
576
          `planet_type` = '" . $fleet['fleet_end_type'] . "',
577
          `eingeladen` = '" . $userId . "',
578
          `fleet_end_time` = '" . $fleet['fleet_end_time'] . "'");
579
  }
580
581
  /**
582
   * @return array|bool|mysqli_result|null
583
   */
584
  public static function dbAcsGetAll() {
585
    return doquery('SELECT * FROM `{{aks}}`;');
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

585
    return /** @scrutinizer ignore-deprecated */ doquery('SELECT * FROM `{{aks}}`;');
Loading history...
586
  }
587
588
589
  /**
590
   * @param $user
591
   * @param $fleet
592
   * @param $userToAddRecord
593
   *
594
   * @return array
595
   * @throws Exception
596
   */
597
  public static function acsAddUser($user, $fleet, $userToAddRecord) {
598
    $userToAddID = !empty($userToAddRecord['id']) ? $userToAddRecord['id'] : 0;
599
600
    if (empty($userToAddID)) {
601
      throw new Exception(SN::$lang['fl_aks_player_error']);
602
    }
603
604
    if ($fleet['fleet_target_owner'] == $userToAddID) {
605
      throw new Exception(SN::$lang['flt_aks_player_same']);
606
    }
607
608
    $aks = DbFleetStatic::dbAcsGetByFleet($fleet['fleet_id']);
609
610
    $aks = DbFleetStatic::acsAddUser2($aks, $fleet, $userToAddID, $user['id']);
611
612
    return $aks;
613
  }
614
615
616
  /**
617
   * @param array $aks
618
   * @param array $fleet
619
   * @param       $userToAddID
620
   * @param       $userId
621
   *
622
   * @return array
623
   *
624
   * @throws Exception
625
   */
626
  public static function acsAddUser2($aks, $fleet, $userToAddID, $userId) {
627
    $fleetid = $fleet['fleet_id'];
628
629
    if (!$aks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $aks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
630
      // No AСS exists - making one
631
      if (!$fleet['fleet_group']) {
632
        DbFleetStatic::dbAcsInsert($userId, $fleetid, $fleet);
633
        $aks = DbFleetStatic::dbAcsGetByFleet($fleetid);
634
635
        DbFleetStatic::fleet_update_set($fleetid, array(
636
          'fleet_group'   => $aks['id'],
637
          'fleet_mission' => MT_AKS,
638
        ));
639
        $fleet['fleet_group']   = $aks['id'];
640
        $fleet['fleet_mission'] = MT_AKS;
641
      } else {
642
        throw new Exception(SN::$lang['fl_aks_already_in_aks']);
643
      }
644
    }
645
646
    $invited_ar = explode(",", $aks['eingeladen']);
647
    if (count($invited_ar) >= 5) {
648
      throw new Exception(SN::$lang['flt_aks_error_too_much_players']);
649
    }
650
651
    $acsPoints = 0;
652
    $isUserExists = false;
653
    foreach ($invited_ar as $inv) {
654
      if ($userToAddID == $inv) {
655
        $isUserExists = true;
656
      }
657
658
      $invitedUserRecord = db_user_by_id($inv);
0 ignored issues
show
Bug introduced by
$inv of type string is incompatible with the type array|integer expected by parameter $user_id_unsafe of db_user_by_id(). ( Ignorable by Annotation )

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

658
      $invitedUserRecord = db_user_by_id(/** @scrutinizer ignore-type */ $inv);
Loading history...
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

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

658
      $invitedUserRecord = /** @scrutinizer ignore-deprecated */ db_user_by_id($inv);
Loading history...
659
      if(!empty($invitedUserRecord)) {
660
        $acsPoints += $invitedUserRecord['total_points'];
661
      }
662
    }
663
664
    $attackedPlayer = db_user_by_id($fleet['fleet_target_owner']);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

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

664
    $attackedPlayer = /** @scrutinizer ignore-deprecated */ db_user_by_id($fleet['fleet_target_owner']);
Loading history...
665
    if (
666
      !empty($attackedPlayer['total_points'])
667
      &&
668
      SN::$gc->general->playerIs1stStrongerThen2nd($acsPoints, $attackedPlayer['total_points'])
669
    ) {
670
      throw new Exception(SN::$lang['fl_aks_too_power']);
671
    }
672
673
674
    if(self::acsIsAcsFull($aks['id'])) {
675
      throw new Exception(SN::$lang['fl_aks_too_power']);
676
    }
677
678
    if ($isUserExists) {
679
//      if ($userToAddID != $userId)
680
        throw new Exception(SN::$lang['fl_aks_player_invited_already']);
681
    } else {
682
      DbFleetStatic::dbAcsAddUserByFleetId($userToAddID, $fleetid);
683
      $aks['eingeladen'] .= ',' . $userToAddID;
684
    }
685
686
    return $aks;
687
  }
688
689
  /**
690
   * @param int $acsId
691
   *
692
   * @return bool
693
   */
694
  public static function acsIsAcsFull($acsId) {
695
    $fleetInAcs      = self::fleet_list_by_group($acsId);
696
    $isMaxSubReached = count($fleetInAcs) >= 5;
697
698
    return $isMaxSubReached;
699
  }
700
701
}
702