Test Failed
Push — trunk ( 769658...cc4c01 )
by SuperNova.WS
15:09
created

flt_fleet_speed()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 9
nc 8
nop 3
dl 0
loc 16
rs 8.4444
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A flt_get_max_distance() 0 8 3
1
<?php
2
3
use DBAL\OldDbChangeSet;
4
use Fleet\DbFleetStatic;
5
use Fleet\Fleet;
6
use Fleet\FleetStatic;
7
use Planet\DBStaticPlanet;
8
9
/**
10
 * @param int   $ship_id
11
 * @param int   $speed_percent
12
 * @param array $shipsData
13
 *
14
 * @return float|int
15
 */
16
function flt_get_max_distance($ship_id, $speed_percent = 100, $shipsData = []) {
17
  $single_ship_data = $shipsData[$ship_id];
18
19
  if (!$single_ship_data['capacity'] || !$single_ship_data['consumption']) {
20
    return 0;
21
  }
22
23
  return calcDistance($speed_percent, $single_ship_data);
24
}
25
26
/**
27
 * @param $speed_percent
28
 * @param $single_ship_data
29
 *
30
 * @return float
31
 */
32
function calcDistance($speed_percent, $single_ship_data) {
33
  return floor(($single_ship_data['capacity'] - 1) / $single_ship_data['consumption'] / pow($speed_percent / 100 + 1, 2) * 35000);
34
}
35
36
37
/**
38
 * @param         $user_row
39
 * @param         $from
40
 * @param         $to
41
 * @param         $fleet_array
42
 * @param int     $speed_percent
43
 * @param array[] $shipsData - prepared ships data to use in calculations
44
 *
45
 * @return array
46
 */
47
function flt_travel_data($user_row, $from, $to, $fleet_array, $speed_percent = 10, $shipsData = [], $distance = null) {
48
  $distance = $distance === null ? Universe::distance($from, $to) : $distance;
49
50
  $consumption = 0;
51
  $capacity = 0;
52
  $duration = 0;
53
54
  $game_fleet_speed = Universe::flt_server_flight_speed_multiplier();
55
  $fleet_speed = FleetStatic::flt_fleet_speed($user_row, $fleet_array, $shipsData);
56
  if (!empty($fleet_array) && $fleet_speed && $game_fleet_speed) {
57
    $speed_percent = $speed_percent ? max(min($speed_percent, 10), 1) : 10;
58
    $real_speed = $speed_percent * sqrt($fleet_speed);
59
60
    $duration = max(1, round(
61
      (35000 / $speed_percent * sqrt($distance * 10 / $fleet_speed) + 10) / $game_fleet_speed
62
    ));
63
64
    foreach ($fleet_array as $ship_id => $ship_count) {
65
      if (!$ship_id || !$ship_count) {
66
        continue;
67
      }
68
69
      $single_ship_data = !empty($shipsData[$ship_id]) ? $shipsData[$ship_id] : get_ship_data($ship_id, $user_row);
70
      $single_ship_data['speed'] = $single_ship_data['speed'] < 1 ? 1 : $single_ship_data['speed'];
71
72
      $consumption += $single_ship_data['consumption'] * $ship_count * pow($real_speed / sqrt($single_ship_data['speed']) / 10 + 1, 2);
73
      $capacity += $single_ship_data['capacity'] * $ship_count;
74
    }
75
76
    $consumption = ceil($distance * $consumption / 35000) + 1;
77
  }
78
79
  return array(
80
    'fleet_speed'            => $fleet_speed,
81
    'distance'               => $distance,
82
    'duration'               => $duration,
83
    'consumption'            => $consumption,
84
    'capacity'               => $capacity,
85
    'hold'                   => $capacity - $consumption,
86
    'transport_effectivness' => $consumption ? $capacity / $consumption : 0,
87
  );
88
}
89
90
function flt_bashing_check($user, $enemy, $planet_dst, $mission, $flight_duration, $fleet_group = 0) {
91
  global $config;
92
93
  $config_bashing_attacks = $config->fleet_bashing_attacks;
94
  $config_bashing_interval = $config->fleet_bashing_interval;
95
  if (!$config_bashing_attacks) {
96
    // Bashing allowed - protection disabled
97
    return ATTACK_ALLOWED;
98
  }
99
100
  $bashing_result = ATTACK_BASHING;
101
  if ($user['ally_id'] && $enemy['ally_id']) {
102
    $relations = ali_relations($user['ally_id'], $enemy['ally_id']);
103
    if (!empty($relations)) {
104
      $relations = $relations[$enemy['ally_id']];
105
      switch ($relations['alliance_diplomacy_relation']) {
106
        case ALLY_DIPLOMACY_WAR:
107
          if (SN_TIME_NOW - $relations['alliance_diplomacy_time'] <= $config->fleet_bashing_war_delay) {
108
            $bashing_result = ATTACK_BASHING_WAR_DELAY;
109
          } else {
110
            return ATTACK_ALLOWED;
111
          }
112
        break;
113
        // Here goes other relations
114
115
        /*
116
                default:
117
                  return ATTACK_ALLOWED;
118
                break;
119
        */
120
      }
121
    }
122
  }
123
124
  $time_limit = SN_TIME_NOW + $flight_duration - $config->fleet_bashing_scope;
125
  $bashing_list = array(SN_TIME_NOW);
126
127
  // Retrieving flying fleets
128
  $bashing_fleet_list = DbFleetStatic::fleet_list_bashing($user['id'], $planet_dst);
129
  foreach ($bashing_fleet_list as $fleet_row) {
130
    // Checking for ACS - each ACS count only once
131
    if ($fleet_row['fleet_group']) {
132
      $bashing_list["{$user['id']}_{$fleet_row['fleet_group']}"] = $fleet_row['fleet_start_time'];
133
    } else {
134
      $bashing_list[] = $fleet_row['fleet_start_time'];
135
    }
136
  }
137
138
  // Check for joining to ACS - if there are already fleets in ACS no checks should be done
139
  if ($mission == MT_AKS && $bashing_list["{$user['id']}_{$fleet_group}"]) {
140
    return ATTACK_ALLOWED;
141
  }
142
143
  $query = doquery("SELECT bashing_time FROM `{{bashing}}` WHERE bashing_user_id = {$user['id']} AND bashing_planet_id = {$planet_dst['id']} AND bashing_time >= {$time_limit};");
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

143
  $query = /** @scrutinizer ignore-deprecated */ doquery("SELECT bashing_time FROM `{{bashing}}` WHERE bashing_user_id = {$user['id']} AND bashing_planet_id = {$planet_dst['id']} AND bashing_time >= {$time_limit};");
Loading history...
144
  while ($bashing_row = db_fetch($query)) {
145
    $bashing_list[] = $bashing_row['bashing_time'];
146
  }
147
148
  sort($bashing_list);
149
150
  $last_attack = 0;
151
  $wave = 0;
152
  $attack = 1;
153
  foreach ($bashing_list as &$bash_time) {
154
    $attack++;
155
    if ($bash_time - $last_attack > $config_bashing_interval || $attack > $config_bashing_attacks) {
156
      $attack = 1;
157
      $wave++;
158
    }
159
160
    $last_attack = $bash_time;
161
  }
162
163
  return ($wave > $config->fleet_bashing_waves ? $bashing_result : ATTACK_ALLOWED);
164
}
165
166
/**
167
 * @param array $planet_src - source planet record/vector
168
 * @param array $planet_dst - destination planet record/vector
169
 * @param array $fleet      - array of ship amount [(int)shipId => (float)shipAmount]
170
 * @param int   $mission    - Mission ID
171
 * @param array $options    - [
172
 *                          P_FLEET_ATTACK_RESOURCES_SUM       => (float),
173
 *                          P_FLEET_ATTACK_RES_LIST            => [(int)resId => (float)amount]
174
 *                          P_FLEET_ATTACK_SPEED_PERCENT_TENTH => (int)1..10
175
 *                          P_FLEET_ATTACK_FLEET_GROUP         => (int|string)
176
 *                          P_FLEET_ATTACK_FLYING_COUNT        => (int)
177
 *                          P_FLEET_ATTACK_TARGET_STRUCTURE    => (int) - targeted defense structure snID for MISSILE missions
178
 *                          P_FLEET_ATTACK_STAY_TIME           => (int) - stay HOURS
179
 *                          ]
180
 *
181
 * @return int
182
 */
183
function flt_can_attack($planet_src, $planet_dst, $fleet = [], $mission, $options = []) {
184
  $result = null;
185
186
  return sn_function_call('flt_can_attack', [$planet_src, $planet_dst, $fleet, $mission, $options, &$result]);
187
}
188
189
/**
190
 * @param array $planet_src
191
 * @param array $planet_dst
192
 * @param array $fleet
193
 * @param int   $mission
194
 * @param array $options
195
 * @param int   $result
196
 *
197
 * @return int
198
 * @see flt_can_attack()
199
 */
200
function sn_flt_can_attack($planet_src, $planet_dst, $fleet = [], $mission, $options = [], &$result) {
201
  //TODO: try..catch
202
  global $config, $user;
203
204
  !is_array($options) ? $options = [] : false;
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
205
206
  if ($user['vacation']) {
207
    return $result = ATTACK_OWN_VACATION;
208
  }
209
210
  $sn_groups_mission = sn_get_groups('missions');
211
  if (!isset($sn_groups_mission[$mission])) {
212
    return $result = ATTACK_MISSION_ABSENT;
213
  }
214
  $sn_data_mission = $sn_groups_mission[$mission];
215
216
//TODO: Проверка на отстуствие ресурсов в нетранспортных миссиях (Транспорт, Передислокация, Колонизация)
217
218
  //TODO: Проверка на наличие ресурсов при Транспорте
219
  // TODO: Проверка на отрицательные ресурсы при транспорте
220
  // TODO: Проверка на перегрузку при транспорте
221
222
  // TODO: В ракетных миссиях могут лететь только ракеты
223
  // TODO: В неракетных миссиях ракеты должны отсутствовать
224
225
  if (empty($fleet) || !is_array($fleet)) {
226
    return $result = ATTACK_NO_FLEET;
227
  }
228
229
  $ships = 0;
230
  $recyclers = 0;
231
  $spies = 0;
232
  $resources = 0;
233
  $ship_ids = sn_get_groups('fleet');
234
  $resource_ids = sn_get_groups('resources_loot');
235
  foreach ($fleet as $ship_id => $ship_count) {
236
    $is_ship = in_array($ship_id, $ship_ids);
237
    $is_resource = in_array($ship_id, $resource_ids);
238
//    if (!$is_ship && !$is_resource) {
239
//      // TODO Спецобработчик для Капитана и модулей
240
//      return ATTACK_WRONG_UNIT;
241
//    }
242
243
    if ($ship_count < 0) {
244
      return $result = $is_ship ? ATTACK_SHIP_COUNT_WRONG : ATTACK_RESOURCE_COUNT_WRONG;
245
    }
246
247
    if ($ship_count > mrc_get_level($user, $planet_src, $ship_id)) {
248
      // TODO ATTACK_NO_MISSILE
249
      return $result = $is_ship ? ATTACK_NO_SHIPS : ATTACK_NO_RESOURCES;
250
    }
251
252
    if ($is_ship) {
253
      $single_ship_data = get_ship_data($ship_id, $user);
254
      if ($single_ship_data[P_SPEED] <= 0) {
255
        return $result = ATTACK_ZERO_SPEED;
256
      }
257
      $ships += $ship_count;
258
      $recyclers += in_array($ship_id, sn_get_groups('flt_recyclers')) ? $ship_count : 0;
259
      $spies += $ship_id == SHIP_SPY ? $ship_count : 0;
260
    } elseif ($is_resource) {
261
      $resources += $ship_count;
262
    }
263
  }
264
265
  if (empty($resources) && !empty($options[P_FLEET_ATTACK_RES_LIST]) && is_array($options[P_FLEET_ATTACK_RES_LIST])) {
266
    $resources = array_sum($options[P_FLEET_ATTACK_RES_LIST]);
267
  }
268
269
  if (
270
    isset($options[P_FLEET_ATTACK_RESOURCES_SUM])
271
    && $options[P_FLEET_ATTACK_RESOURCES_SUM] > 0
272
    && empty($sn_data_mission['transport'])
273
  ) {
274
    return $result = ATTACK_RESOURCE_FORBIDDEN;
275
  }
276
277
  /*
278
    elseif($mission == MT_TRANSPORT)
279
    {
280
      return ATTACK_TRANSPORT_EMPTY;
281
    }
282
  */
283
284
  $speed = $options[P_FLEET_ATTACK_SPEED_PERCENT_TENTH];
285
  if ($speed && ($speed != intval($speed) || $speed < 1 || $speed > 10)) {
286
    return $result = ATTACK_WRONG_SPEED;
287
  }
288
289
  $travel_data = flt_travel_data($user, $planet_src, $planet_dst, $fleet, $options[P_FLEET_ATTACK_SPEED_PERCENT_TENTH]);
290
291
292
  if (mrc_get_level($user, $planet_src, RES_DEUTERIUM) < $fleet[RES_DEUTERIUM] + $travel_data['consumption']) {
293
    return $result = ATTACK_NO_FUEL;
294
  }
295
296
  if ($travel_data['consumption'] > $travel_data['capacity']) {
297
    return $result = ATTACK_TOO_FAR;
298
  }
299
300
  if ($travel_data['hold'] < $resources) {
301
    return $result = ATTACK_OVERLOADED;
302
  }
303
304
  $fleet_start_time = SN_TIME_NOW + $travel_data['duration'];
305
306
  $fleet_group = $options[P_FLEET_ATTACK_FLEET_GROUP];
307
  if ($fleet_group) {
308
    if ($mission != MT_AKS) {
309
      return $result = ATTACK_WRONG_MISSION;
310
    };
311
312
    $acs = DbFleetStatic::dbAcsGetById($fleet_group);
313
    if (!$acs['id']) {
314
      return $result = ATTACK_NO_ACS;
315
    }
316
317
    if ($planet_dst['galaxy'] != $acs['galaxy'] || $planet_dst['system'] != $acs['system'] || $planet_dst['planet'] != $acs['planet'] || $planet_dst['planet_type'] != $acs['planet_type']) {
318
      return $result = ATTACK_ACS_WRONG_TARGET;
319
    }
320
321
    if ($fleet_start_time > $acs['ankunft']) {
322
      return $result = ATTACK_ACS_TOO_LATE;
323
    }
324
325
    if(DbFleetStatic::acsIsAcsFull($acs['id'])) {
326
      return $result = ATTACK_ACS_MAX_FLEETS;
327
    }
328
  }
329
330
  $flying_fleets = $options[P_FLEET_ATTACK_FLYING_COUNT];
331
  if (!$flying_fleets) {
332
    $flying_fleets = DbFleetStatic::fleet_count_flying($user['id']);
333
  }
334
  if (GetMaxFleets($user) <= $flying_fleets && $mission != MT_MISSILE) {
335
    return $result = ATTACK_NO_SLOTS;
336
  }
337
338
  // В одиночку шпионские зонды могут летать только в миссии Шпионаж, Передислокация и Транспорт
339
  if ($ships && $spies && $spies == $ships && !($mission == MT_SPY || $mission == MT_RELOCATE || $mission == MT_TRANSPORT)) {
340
    return $result = ATTACK_SPIES_LONLY;
341
  }
342
343
  // Checking for no planet
344
  if (!$planet_dst['id_owner']) {
345
    if ($mission == MT_COLONIZE && !$fleet[SHIP_COLONIZER]) {
346
      return $result = ATTACK_NO_COLONIZER;
347
    }
348
349
    if ($mission == MT_EXPLORE || $mission == MT_COLONIZE) {
350
      return $result = ATTACK_ALLOWED;
351
    }
352
353
    return $result = ATTACK_NO_TARGET;
354
  }
355
356
  if ($mission == MT_RECYCLE) {
357
    if ($planet_dst['debris_metal'] + $planet_dst['debris_crystal'] <= 0) {
358
      return $result = ATTACK_NO_DEBRIS;
359
    }
360
    if ($recyclers <= 0) {
361
      return $result = ATTACK_NO_RECYCLERS;
362
    }
363
364
    return $result = ATTACK_ALLOWED;
365
  }
366
367
  // Got planet. Checking if it is ours
368
  if ($planet_dst['id_owner'] == $user['id']) {
369
    if ($mission == MT_TRANSPORT || $mission == MT_RELOCATE) {
370
      return $result = ATTACK_ALLOWED;
371
    }
372
373
    return $planet_src['id'] == $planet_dst['id'] ? ATTACK_SAME : ATTACK_OWN;
374
  }
375
376
  // No, planet not ours. Cutting mission that can't be send to not-ours planet
377
  if ($mission == MT_RELOCATE || $mission == MT_COLONIZE || $mission == MT_EXPLORE) {
378
    return $result = ATTACK_WRONG_MISSION;
379
  }
380
381
  $enemy = db_user_by_id($planet_dst['id_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

381
  $enemy = /** @scrutinizer ignore-deprecated */ db_user_by_id($planet_dst['id_owner']);
Loading history...
382
  // We cannot attack or send resource to users in VACATION mode
383
  if ($enemy['vacation'] && $mission != MT_RECYCLE) {
384
    return $result = ATTACK_VACATION;
385
  }
386
387
  // Multi IP protection
388
  // TODO: Here we need a procedure to check proxies
389
  if (sys_is_multiaccount($user, $enemy)) {
390
    return $result = ATTACK_SAME_IP;
391
  }
392
393
  $user_points = $user['total_points'];
394
  $enemy_points = $enemy['total_points'];
395
396
  // Is it transport? If yes - checking for buffing to prevent mega-alliance destroyer
397
  if ($mission == MT_TRANSPORT) {
398
    if ($user_points >= $enemy_points || $config->allow_buffing) {
399
      return $result = ATTACK_ALLOWED;
400
    } else {
401
      return $result = ATTACK_BUFFING;
402
    }
403
  }
404
405
  // Only aggresive missions passed to this point. HOLD counts as passive but aggresive
406
407
  // Is it admin with planet protection?
408
  if ($planet_dst['id_level'] > $user['authlevel']) {
409
    return $result = ATTACK_ADMIN;
410
  }
411
412
  // Okay. Now skipping protection checks for inactive longer then 1 week
413
  if (!$enemy['onlinetime'] || $enemy['onlinetime'] >= (SN_TIME_NOW - 60 * 60 * 24 * 7)) {
414
    if (
415
      (SN::$gc->general->playerIsNoobByPoints($enemy_points) && !SN::$gc->general->playerIsNoobByPoints($user_points))
416
      ||
417
      (SN::$gc->general->playerIs1stStrongerThen2nd($user_points, $enemy_points))
418
    ) {
419
      if ($mission != MT_HOLD) {
420
        return $result = ATTACK_NOOB;
421
      }
422
      if ($mission == MT_HOLD && !($user['ally_id'] && $user['ally_id'] == $enemy['ally_id'] && $config->ally_help_weak)) {
423
        return $result = ATTACK_NOOB;
424
      }
425
    }
426
  }
427
428
  // Is it HOLD mission? If yes - there should be ally deposit
429
  if ($mission == MT_HOLD) {
430
    if (mrc_get_level($user, $planet_dst, STRUC_ALLY_DEPOSIT)) {
431
      return $result = ATTACK_ALLOWED;
432
    }
433
434
    return $result = ATTACK_NO_ALLY_DEPOSIT;
435
  }
436
437
  if ($mission == MT_SPY) {
438
    return $result = $spies >= 1 ? ATTACK_ALLOWED : ATTACK_NO_SPIES;
439
  }
440
441
  // Is it MISSILE mission?
442
  if ($mission == MT_MISSILE) {
443
    $sn_data_mip = get_unit_param(UNIT_DEF_MISSILE_INTERPLANET);
444
    if (mrc_get_level($user, $planet_src, STRUC_SILO) < $sn_data_mip[P_REQUIRE][STRUC_SILO]) {
445
      return $result = ATTACK_NO_SILO;
446
    }
447
448
    if (!$fleet[UNIT_DEF_MISSILE_INTERPLANET]) {
449
      return $result = ATTACK_NO_MISSILE;
450
    }
451
452
    $distance = abs($planet_dst['system'] - $planet_src['system']);
453
    $mip_range = flt_get_missile_range($user);
454
    if ($distance > $mip_range || $planet_dst['galaxy'] != $planet_src['galaxy']) {
455
      return $result = ATTACK_MISSILE_TOO_FAR;
456
    }
457
458
    if (!empty($options[P_FLEET_ATTACK_TARGET_STRUCTURE]) && !in_array($options[P_FLEET_ATTACK_TARGET_STRUCTURE], sn_get_groups('defense_active'))) {
459
      return $result = ATTACK_WRONG_STRUCTURE;
460
    }
461
  }
462
463
  if ($mission == MT_DESTROY && $planet_dst['planet_type'] != PT_MOON) {
464
    return $result = ATTACK_WRONG_MISSION;
465
  }
466
467
  if ($mission == MT_ATTACK || $mission == MT_AKS || $mission == MT_DESTROY) {
468
    return $result = flt_bashing_check($user, $enemy, $planet_dst, $mission, $travel_data['duration'], $fleet_group);
469
  }
470
471
  return $result = ATTACK_ALLOWED;
472
}
473
474
/**
475
 * @param array $user    - actual user record
476
 * @param array $from    - actual planet record
477
 * @param array $to      - actual planet record
478
 * @param array $fleet   - array of records $unit_id -> $amount
479
 * @param int   $mission - fleet mission
480
 * @param array $options
481
 *
482
 * @return int
483
 * @throws Exception
484
 * @see flt_can_attack()
485
 */
486
function flt_t_send_fleet($user, &$from, $to, $fleet, $resources, $mission, $options = array()) {
487
  $internal_transaction = !SN::db_transaction_check(false) ? SN::db_transaction_start() : false;
488
489
  // TODO Потенциальный дедлок - если успела залочится запись пользователя - хозяина планеты
490
  $user = db_user_by_id($user['id'], true);
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

490
  $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true);
Loading history...
491
  $from = sys_o_get_updated($user, $from['id'], SN_TIME_NOW);
492
  $from = $from['planet'];
493
494
//  $fleet = [
495
//    202 => 1,
496
//  ];
497
//  $resources = [
498
//    901 => 1,
499
//  ];
500
//  var_dump($fleet);
501
//  var_dump($resources);
502
//  var_dump(mrc_get_level($user, $from, 202));
503
//  var_dump($from['metal']);
504
//  var_dump($from['deuterium']);
505
//  die();
506
507
508
  !is_array($resources) ? $resources = [] : false;
509
  if(empty($options[P_FLEET_ATTACK_RES_LIST])) {
510
    $options[P_FLEET_ATTACK_RES_LIST] = $resources;
511
  }
512
  $can_attack = flt_can_attack($from, $to, $fleet, $mission, $options);
513
  if ($can_attack != ATTACK_ALLOWED) {
514
    $internal_transaction ? SN::db_transaction_rollback() : false;
515
516
    return $can_attack;
517
  }
518
519
  empty($options[P_FLEET_ATTACK_SPEED_PERCENT_TENTH]) ? $options[P_FLEET_ATTACK_SPEED_PERCENT_TENTH] = 10 : false;
520
  $options[P_FLEET_ATTACK_STAY_TIME] = !empty($options[P_FLEET_ATTACK_STAY_TIME]) ? $options[P_FLEET_ATTACK_STAY_TIME] * PERIOD_HOUR : 0;
521
522
  $fleetObj = new Fleet();
523
  $travel_data = $fleetObj
524
    ->setMission($mission)
525
    ->setSourceFromPlanetRecord($from)
526
    ->setDestinationFromPlanetRecord($to)
527
    ->setUnits($fleet)
528
    ->setUnits($resources)
529
    ->setSpeedPercentInTenth($options[P_FLEET_ATTACK_SPEED_PERCENT_TENTH])
530
    ->calcTravelTimes(SN_TIME_NOW, $options[P_FLEET_ATTACK_STAY_TIME]);
531
  $fleetObj->save();
532
533
  $result = fltSendFleetAdjustPlanetResources($from['id'], $resources, $travel_data['consumption']);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
534
535
  $result = fltSendFleetAdjustPlanetUnits($user, $from['id'], $fleet);
536
537
  $internal_transaction ? SN::db_transaction_commit() : false;
538
539
  $from = DBStaticPlanet::db_planet_by_id($from['id']);
540
541
//  var_dump(mrc_get_level($user, $from, 202));
542
//  var_dump($from['metal']);
543
//  var_dump($from['deuterium']);
544
//  die();
545
546
  return ATTACK_ALLOWED;
547
}
548
549
/**
550
 * @param array      $user
551
 * @param int|string $fromId
552
 * @param float[]    $fleet - [(int)shipId => (float)count]
553
 *
554
 * @return bool
555
 */
556
function fltSendFleetAdjustPlanetUnits($user, $fromId, $fleet) {
557
  $result = [];
558
559
  foreach ($fleet as $unit_id => $amount) {
560
    if (floatval($amount) >= 1 && intval($unit_id) && in_array($unit_id, sn_get_groups('fleet'))) {
561
      $result[] = OldDbChangeSet::db_changeset_prepare_unit($unit_id, -$amount, $user, $fromId);
0 ignored issues
show
Deprecated Code introduced by
The function DBAL\OldDbChangeSet::db_changeset_prepare_unit() 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

561
      $result[] = /** @scrutinizer ignore-deprecated */ OldDbChangeSet::db_changeset_prepare_unit($unit_id, -$amount, $user, $fromId);
Loading history...
562
    }
563
  }
564
565
  return OldDbChangeSet::db_changeset_apply(['unit' => $result]);
0 ignored issues
show
Deprecated Code introduced by
The function DBAL\OldDbChangeSet::db_changeset_apply() 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

565
  return /** @scrutinizer ignore-deprecated */ OldDbChangeSet::db_changeset_apply(['unit' => $result]);
Loading history...
566
}
567
568
/**
569
 * @param int|string $fromId      - Source planet ID
570
 * @param array      $resources   - Array of resources to transfer [(int)resourceId => (float)amount]
571
 * @param int|float  $consumption - Fleet consumption
572
 *
573
 * @return bool
574
 *
575
 * @throws Exception
576
 */
577
function fltSendFleetAdjustPlanetResources($fromId, $resources, $consumption) {
578
  $planetObj = SN::$gc->repoV2->getPlanet($fromId);
579
580
  $planetObj->changeResource(RES_DEUTERIUM, -$consumption);
581
582
  foreach ($resources as $resource_id => $amount) {
583
    if (floatval($amount) >= 1 && intval($resource_id) && in_array($resource_id, sn_get_groups('resources_loot'))) {
584
      $planetObj->changeResource($resource_id, -$amount);
585
    }
586
  }
587
588
  return $planetObj->save();
589
}
590
591
function flt_calculate_ship_to_transport_sort($a, $b) {
592
  return $a['transport_effectivness'] == $b['transport_effectivness'] ? 0 : ($a['transport_effectivness'] > $b['transport_effectivness'] ? -1 : 1);
593
}
594
595
// flt_calculate_ship_to_transport - calculates how many ships need to transport pointed amount of resources
596
// $ship_list - list of available ships
597
// $resource_amount - how much amount of resources need to be transported
598
// $from - transport from
599
// $to - transport to
600
function flt_calculate_fleet_to_transport($ship_list, $resource_amount, $from, $to) {
601
  global $user;
602
603
  $ship_data = array();
604
  $fleet_array = array();
605
  foreach ($ship_list as $transport_id => $cork) {
606
    $ship_data[$transport_id] = flt_travel_data($user, $from, $to, array($transport_id => 1), 10);
607
  }
608
  uasort($ship_data, 'flt_calculate_ship_to_transport_sort');
609
610
  $fleet_hold = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $fleet_hold is dead and can be removed.
Loading history...
611
  $fleet_capacity = 0;
612
  $fuel_total = $fuel_left = mrc_get_level($user, $from, RES_DEUTERIUM);
613
  foreach ($ship_data as $transport_id => &$ship_info) {
614
    $ship_loaded = min($ship_list[$transport_id], ceil($resource_amount / $ship_info['hold']), floor($fuel_left / $ship_info['consumption']));
615
    if ($ship_loaded) {
616
      $fleet_array[$transport_id] = $ship_loaded;
617
      $resource_amount -= min($resource_amount, $ship_info['hold'] * $ship_loaded);
618
      $fuel_left -= $ship_info['consumption'] * $ship_loaded;
619
620
      $fleet_capacity += $ship_info['capacity'] * $ship_loaded;
621
    }
622
  }
623
624
  return array('fleet' => $fleet_array, 'ship_data' => $ship_data, 'capacity' => $fleet_capacity, 'consumption' => $fuel_total - $fuel_left);
625
}
626