Completed
Push — trunk ( 433e05...76e427 )
by SuperNova.WS
06:21
created

DbFleetStatic::fleet_send_back()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 26.04.2018 14:00
4
 */
5
6
namespace Fleet;
7
8
use DBAL\DbQuery;
9
use mysqli_result;
10
11
/**
12
 * Class DbFleetStatic
13
 * @package Fleet
14
 *
15
 * deprecated
16
 */
17
class DbFleetStatic {
18
19
  /* HELPERS ******************************************************************************************************** */
20
  /**
21
   * @return DbQuery
22
   */
23
  protected static function dbq() {
24
    return DbQuery::build()->setTable('fleets');
25
  }
26
27
28
29
  /* FLEET CRUD ===================================================================================================== */
30
  /**
31
   * Inserts fleet record by ID with array
32
   *
33
   * @param array $fieldArray - [fieldName => fieldValue]
34
   *
35
   * @return int|string - fleet inserted ID or 0 if no fleets inserted
36
   */
37
  public static function fleet_insert_set_dbq($fieldArray) {
38
    if (!empty($fieldArray)) {
39
      static::dbq()
40
        ->setValues($fieldArray)
41
        ->doInsert(DbQuery::DB_INSERT_PLAIN, true);
42
43
      $fleet_id = db_insert_id();
44
    } else {
45
      $fleet_id = 0;
46
    }
47
48
    return $fleet_id;
49
  }
50
51
  /**
52
   * Updates fleet record by ID with SET
53
   *
54
   * @param int   $fleet_id
55
   * @param array $set - REPLACE-set, i.e. replacement of existing values
56
   * @param array $delta - DELTA-set, i.e. changes to existing values
57
   *
58
   * @return array|bool|mysqli_result|null
59
   */
60
  public static function fleet_update_set($fleet_id, $set, $delta = array()) {
61
    $result = false;
62
63
    $fleet_id_safe = idval($fleet_id);
64
    $set_string_safe = db_set_make_safe_string($set);
65
    !empty($delta) ? $set_string_safe = implode(',', array($set_string_safe, db_set_make_safe_string($delta, true))) : false;
66
    if (!empty($fleet_id_safe) && !empty($set_string_safe)) {
67
      $result = static::db_fleet_update_set_safe_string($fleet_id, $set_string_safe);
68
    }
69
70
    return $result;
71
  }
72
73
  /**
74
   * UPDATE - Updates fleet record by ID with SET
75
   *
76
   * @param int    $fleet_id
77
   * @param string $set_safe_string
78
   *
79
   * @return array|bool|mysqli_result|null
80
   */
81
  protected static function db_fleet_update_set_safe_string($fleet_id, $set_safe_string) {
82
    $fleet_id_safe = idval($fleet_id);
83
    if (!empty($fleet_id_safe) && !empty($set_safe_string)) {
84
      /** @noinspection SqlResolve */
85
      $result = doquery("UPDATE `{{fleets}}` SET {$set_safe_string} WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1;");
86
    } else {
87
      $result = false;
88
    }
89
90
    return $result;
91
  }
92
93
94
  /**
95
   * READ - Gets fleet record by ID
96
   *
97
   * @param int $fleet_id
98
   *
99
   * @return array|false
100
   */
101
  public static function db_fleet_get($fleet_id) {
102
    $fleet_id_safe = idval($fleet_id);
103
    /** @noinspection SqlResolve */
104
    $result = doquery("SELECT * FROM `{{fleets}}` WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1 FOR UPDATE;", true);
105
106
    return is_array($result) ? $result : false;
107
  }
108
109
  /**
110
   * DELETE
111
   *
112
   * @param $fleet_id
113
   *
114
   * @return array|bool|mysqli_result|null
115
   */
116
  public static function db_fleet_delete($fleet_id) {
117
    $fleet_id_safe = idval($fleet_id);
118
    if (!empty($fleet_id_safe)) {
119
      /** @noinspection SqlResolve */
120
      $result = doquery("DELETE FROM `{{fleets}}` WHERE `fleet_id` = {$fleet_id_safe} LIMIT 1;");
121
    } else {
122
      $result = false;
123
    }
124
125
    return $result;
126
  }
127
128
129
  /**
130
   * LOCK - Lock all records which can be used with mission
131
   *
132
   * @param $mission_data
133
   * @param $fleet_id
134
   *
135
   * @return array|bool|mysqli_result|null
136
   */
137
  public static function db_fleet_lock_flying($fleet_id, &$mission_data) {
138
    // Тупо лочим всех юзеров, чьи флоты летят или улетают с координат отбытия/прибытия $fleet_row
139
    // Что бы делать это умно - надо учитывать fleet_mess во $fleet_row и в таблице fleets
140
    $fleet_id_safe = idval($fleet_id);
141
142
    $query = [];
143
    /** @noinspection SqlResolve */
144
    $query[] = "SELECT 1 FROM `{{fleets}}` AS f";
145
    // Блокировка всех прилетающих и улетающих флотов, если нужно
146
    $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;
147
148
    $mission_data['dst_user'] || $mission_data['dst_planet'] ? $query[] = 'LEFT JOIN `{{users}}` AS ud ON ud.id = f.fleet_target_owner' : false;
149
    $mission_data['dst_planet'] ? $query[] = 'LEFT JOIN `{{planets}}` AS pd ON pd.id = f.fleet_end_planet_id' : false;
150
151
    $mission_data['src_user'] || $mission_data['src_planet'] ? $query[] = 'LEFT JOIN `{{users}}` AS us ON us.id = f.fleet_owner' : false;
152
    $mission_data['src_planet'] ? $query[] = 'LEFT JOIN `{{planets}}` AS ps ON ps.id = f.fleet_start_planet_id' : false;
153
154
    $query[] = "WHERE f.fleet_id = {$fleet_id_safe} GROUP BY 1 FOR UPDATE";
155
156
    return doquery(implode(' ', $query));
157
  }
158
159
160
161
  /* FLEET LIST & COUNT CRUD ===========================================================================================*/
162
  /**
163
   * COUNT - Get fleet count by condition
164
   *
165
   * @param string $where_safe
166
   *
167
   * @return int
168
   */
169
  public static function db_fleet_count($where_safe) {
170
    /** @noinspection SqlResolve */
171
    $result = doquery("SELECT COUNT(`fleet_id`) as 'fleet_count' FROM `{{fleets}}` WHERE {$where_safe}", true);
172
173
    return !empty($result['fleet_count']) ? intval($result['fleet_count']) : 0;
174
  }
175
176
177
  /**
178
   * LIST - Get fleet list by condition
179
   *
180
   * @param string $where_safe
181
   *
182
   * @return array[]
183
   *
184
   * TODO - This function should NOT be used to query fleets to all planets - some aggregate function should be used
185
   */
186
  public static function db_fleet_list($where_safe, $for_update = DB_SELECT_FOR_UPDATE) {
187
    $row_list = [];
188
189
    $dbq = DbQuery::build()
190
      ->setTable('fleets');
191
192
    if (!empty($where_safe)) {
193
      $dbq->setWhereArrayDanger([$where_safe]);
194
    }
195
196
    if ($for_update == DB_SELECT_FOR_UPDATE) {
197
      $dbq->setForUpdate(DbQuery::DB_FOR_UPDATE);
198
    }
199
200
    $query = $dbq->doSelect();
201
202
    while ($row = db_fetch($query)) {
203
      $row_list[$row['fleet_id']] = $row;
204
    }
205
206
    return $row_list;
207
  }
208
209
  /**
210
   * LIST DELETE
211
   *
212
   * @param $owner_id
213
   *
214
   * @return array|bool|mysqli_result|null
215
   * @deprecated
216
   *
217
   * TODO - fleets should be deleted by DB itself via InnoDB FOREIHN KEY
218
   */
219
  public static function db_fleet_list_delete_by_owner($owner_id) {
220
    return doquery("DELETE FROM `{{fleets}}` WHERE `fleet_owner` = '{$owner_id}';");
221
  }
222
223
  /**
224
   * LIST STAT - DEPRECATED
225
   *
226
   * @return array|bool|mysqli_result|null
227
   *
228
   * TODO - deprecated
229
   */
230
  public static function db_fleet_list_query_all_stat() {
231
    return doquery("SELECT fleet_owner, fleet_array, fleet_resource_metal, fleet_resource_crystal, fleet_resource_deuterium FROM `{{fleets}}`;");
232
  }
233
234
235
236
  /* FLEET FUNCTIONS ===================================================================================================*/
237
  /**
238
   * Sends fleet back
239
   *
240
   * @param $fleet_row
241
   *
242
   * @return array|bool|mysqli_result|null
243
   *
244
   * TODO - Add another field which mark fleet as processed/completed task on destination point
245
   * By this flag fleet dispatcher should immediately return fleet (change STATUS/mess flag) to source planet
246
   */
247
  public static function fleet_send_back(&$fleet_row) {
248
    $fleet_id = round(!empty($fleet_row['fleet_id']) ? $fleet_row['fleet_id'] : $fleet_row);
249
    if (!$fleet_id) {
250
      return false;
251
    }
252
253
    $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

253
    $result = static::fleet_update_set(/** @scrutinizer ignore-type */ $fleet_id, array(
Loading history...
254
      'fleet_mess' => 1,
255
    ));
256
257
    return $result;
258
  }
259
260
261
  /* FLEET COUNT FUNCTIONS =============================================================================================*/
262
  /**
263
   * Get flying fleet count
264
   *
265
   * @param int $player_id - Player ID
266
   * @param int $mission_id - mission ID. "0" means "all"
267
   *
268
   * @return int
269
   *
270
   * TODO - Player lock should be issued before to prevent fleet number change
271
   */
272
  public static function fleet_count_flying($player_id, $mission_id = 0) {
273
    $player_id_safe = idval($player_id);
274
    if (!empty($player_id_safe)) {
275
      $mission_id_safe = intval($mission_id);
276
      $result = static::db_fleet_count(
277
        "`fleet_owner` = {$player_id_safe}" .
278
        ($mission_id_safe ? " AND `fleet_mission` = {$mission_id_safe}" : '')
279
      );
280
    } else {
281
      $result = 0;
282
    }
283
284
    return $result;
285
  }
286
287
  /**
288
   * Returns amount of incoming fleets to planet
289
   *
290
   * @param int $galaxy
291
   * @param int $system
292
   * @param int $planet
293
   *
294
   * @return int
295
   *
296
   * TODO - Через fleet_list_by_planet_coords() ????
297
   */
298
  public static function fleet_count_incoming($galaxy, $system, $planet) {
299
    return static::db_fleet_count(
300
      "(`fleet_start_galaxy` = {$galaxy} AND `fleet_start_system` = {$system} AND `fleet_start_planet` = {$planet})
301
    OR
302
    (`fleet_end_galaxy` = {$galaxy} AND `fleet_end_system` = {$system} AND `fleet_end_planet` = {$planet})"
303
    );
304
  }
305
306
  /* FLEET LIST FUNCTIONS =============================================================================================*/
307
  /**
308
   * Get fleet list by owner
309
   *
310
   * @param int $fleet_owner_id - Fleet owner record/ID. Can't be empty
311
   *
312
   * @return array[]
313
   */
314
  public static function fleet_list_by_owner_id($fleet_owner_id) {
315
    $fleet_owner_id_safe = idval($fleet_owner_id);
316
317
    return $fleet_owner_id_safe ? static::db_fleet_list("`fleet_owner` = {$fleet_owner_id_safe}", DB_SELECT_PLAIN) : array();
318
  }
319
320
  /**
321
   * Get fleet list flying/returning to planet/system coordinates
322
   *
323
   * @param int $galaxy
324
   * @param int $system
325
   * @param int $planet - planet position. "0" means "any"
326
   * @param int $planet_type - planet type. "PT_ALL" means "any type"
327
   *
328
   * @return array
329
   * TODO - safe params
330
   */
331
  public static function fleet_list_by_planet_coords($galaxy, $system, $planet = 0, $planet_type = PT_ALL, $for_phalanx = false) {
332
    return static::db_fleet_list(
333
      "(
334
    fleet_start_galaxy = {$galaxy}
335
    AND fleet_start_system = {$system}" .
336
      ($planet ? " AND fleet_start_planet = {$planet}" : '') .
337
      ($planet_type != PT_ALL ? " AND fleet_start_type = {$planet_type}" : '') .
338
      ($for_phalanx ? '' : " AND fleet_mess = 1") .
339
      ")
340
    OR
341
    (
342
    fleet_end_galaxy = {$galaxy}
343
    AND fleet_end_system = {$system}" .
344
      ($planet ? " AND fleet_end_planet = {$planet}" : '') .
345
      ($planet_type != PT_ALL ? " AND fleet_end_type = {$planet_type} " : '') .
346
      ($for_phalanx ? '' : " AND fleet_mess = 0") .
347
      ")"
348
      , DB_SELECT_PLAIN
349
    );
350
  }
351
352
  /**
353
   * Fleets on hold on planet orbit
354
   *
355
   * @param $fleet_row
356
   * @param $ube_time
357
   *
358
   * @return array
359
   *
360
   * TODO - safe params
361
   */
362
  public static function fleet_list_on_hold($galaxy, $system, $planet, $planet_type, $ube_time) {
363
    return static::db_fleet_list(
364
      "`fleet_end_galaxy` = {$galaxy}
365
    AND `fleet_end_system` = {$system}
366
    AND `fleet_end_planet` = {$planet}
367
    AND `fleet_end_type` = {$planet_type}
368
    AND `fleet_start_time` <= {$ube_time}
369
    AND `fleet_end_stay` >= {$ube_time}
370
    AND `fleet_mess` = 0"
371
      , DB_SELECT_FOR_UPDATE
372
    );
373
  }
374
375
  /**
376
   * Get aggressive fleet list of chosen player on selected planet
377
   *
378
   * @param $fleet_owner_id
379
   * @param $planet_row
380
   *
381
   * @return array
382
   */
383
  public static function fleet_list_bashing($fleet_owner_id, $planet_row) {
384
    return static::db_fleet_list(
385
      "`fleet_end_galaxy` = {$planet_row['galaxy']}
386
    AND `fleet_end_system` = {$planet_row['system']}
387
    AND `fleet_end_planet` = {$planet_row['planet']}
388
    AND `fleet_end_type`   = {$planet_row['planet_type']}
389
    AND `fleet_owner` = {$fleet_owner_id}
390
    AND `fleet_mission` IN (" . MT_ATTACK . "," . MT_AKS . "," . MT_DESTROY . ")
391
    AND `fleet_mess` = 0"
392
      , DB_SELECT_FOR_UPDATE
393
    );
394
  }
395
396
  /**
397
   * Get fleets in group
398
   *
399
   * @param $group_id
400
   *
401
   * @return array
402
   */
403
  public static function fleet_list_by_group($group_id) {
404
    return static::db_fleet_list("`fleet_group` = {$group_id}", DB_SELECT_FOR_UPDATE);
405
  }
406
407
408
409
  /* MISSILE CRUD *******************************************************************************************************/
410
  /* MISSILE LIST & COUNT CRUD =========================================================================================*/
411
  /**
412
   * LIST - Get missile attack list by condition
413
   *
414
   * @param string $where - WHERE condition - SQL SAFE!
415
   * @param bool   $for_update - lock record with FOR UPDATE statement
416
   *
417
   * @return array - lift of fleet records from DB
418
   */
419
  public static function db_missile_list($where, $for_update = DB_SELECT_FOR_UPDATE) {
420
    $row_list = [];
421
422
    /** @noinspection SqlResolve */
423
    $query = doquery(
424
      "SELECT * FROM `{{iraks}}`" .
425
      (!empty($where) ? " WHERE {$where}" : '') .
426
      ($for_update == DB_SELECT_FOR_UPDATE ? " FOR UPDATE" : '')
427
    );
428
    while ($row = db_fetch($query)) {
429
      $row_list[$row['id']] = $row;
430
    }
431
432
    return $row_list;
433
  }
434
435
436
437
  /* FLEET/MISSILES LIST FUNCTIONS =====================================================================================*/
438
  /**
439
   * Get fleet and missile list by coordinates
440
   *
441
   * @param array $coordinates
442
   * @param bool  $for_phalanx - If true - this is phalanx scan so limiting output with fleet_mess
443
   *
444
   * @return array
445
   */
446
  public static function fleet_and_missiles_list_by_coordinates($coordinates, $for_phalanx = false) {
447
    if (empty($coordinates) || !is_array($coordinates)) {
448
      return array();
449
    }
450
451
    $fleet_db_list = static::fleet_list_by_planet_coords($coordinates['galaxy'], $coordinates['system'], $coordinates['planet'], $coordinates['planet_type'], $for_phalanx);
452
453
    $missile_db_list = static::db_missile_list(
454
      "(
455
      fleet_start_galaxy = {$coordinates['galaxy']}
456
      AND fleet_start_system = {$coordinates['system']}
457
      AND fleet_start_planet = {$coordinates['planet']}
458
      AND fleet_start_type = {$coordinates['planet_type']}
459
    )
460
    OR
461
    (
462
      fleet_end_galaxy = {$coordinates['galaxy']}
463
      AND fleet_end_system = {$coordinates['system']}
464
      AND fleet_end_planet = {$coordinates['planet']}
465
      AND fleet_end_type = {$coordinates['planet_type']}
466
    )"
467
      , DB_SELECT_PLAIN
468
    );
469
470
    missile_list_convert_to_fleet($missile_db_list, $fleet_db_list);
471
472
    return $fleet_db_list;
473
  }
474
475
  /**
476
   * Get fleet and missile list by that flies from player's planets OR to player's planets
477
   *
478
   * @param int $owner_id
479
   *
480
   * @return array
481
   */
482
  public static function fleet_and_missiles_list_incoming($owner_id) {
483
    $owner_id_safe = idval($owner_id);
484
    if (empty($owner_id_safe)) {
485
      return array();
486
    }
487
488
    $where = "`fleet_owner` = '{$owner_id_safe}' OR `fleet_target_owner` = '{$owner_id_safe}'";
489
    $fleet_db_list = static::db_fleet_list($where, DB_SELECT_PLAIN);
490
    $missile_db_list = static::db_missile_list($where, DB_SELECT_PLAIN);
491
492
    missile_list_convert_to_fleet($missile_db_list, $fleet_db_list);
493
494
    return $fleet_db_list;
495
  }
496
497
498
499
  /* ACS ****************************************************************************************************************/
500
  /**
501
   * Purges ACS list
502
   */
503
  public static function db_fleet_acs_purge() {
504
    DbQuery::build()
505
      ->setTable('aks')
506
      ->setWhereArrayDanger(['`id` NOT IN (SELECT DISTINCT `fleet_group` FROM `{{fleets}}`)'])
507
      ->doDelete();
508
  }
509
510
}
511