Completed
Push — trunk ( 153731...fde30a )
by SuperNova.WS
04:15
created

fltDistanceAsSystem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
use DBAL\OldDbChangeSet;
4
use Planet\DBStaticPlanet;
5
6
function flt_fleet_speed($user, $fleet, $shipData = []) {
7
  if (!is_array($fleet)) {
8
    $fleet = array($fleet => 1);
9
  }
10
11
  $speeds = array();
12
  if (!empty($fleet)) {
13
    foreach ($fleet as $ship_id => $amount) {
14
      if ($amount && in_array($ship_id, sn_get_groups(array('fleet', 'missile')))) {
15
        $single_ship_data = !empty($shipData[$ship_id]) ? $shipData[$ship_id] : get_ship_data($ship_id, $user);
16
        $speeds[] = $single_ship_data['speed'];
17
      }
18
    }
19
  }
20
21
  return empty($speeds) ? 0 : min($speeds);
22
}
23
24
function flt_get_galaxy_distance() {
25
  return SN::$config->uni_galaxy_distance ? SN::$config->uni_galaxy_distance : UNIVERSE_GALAXY_DISTANCE;
26
}
27
28
function flt_travel_distance($from, $to) {
29
  if ($from['galaxy'] != $to['galaxy']) {
30
    $distance = abs($from['galaxy'] - $to['galaxy']) * flt_get_galaxy_distance();
31
  } elseif ($from['system'] != $to['system']) {
32
    $distance = abs($from['system'] - $to['system']) * 5 * 19 + 2700;
33
  } elseif ($from['planet'] != $to['planet']) {
34
    $distance = abs($from['planet'] - $to['planet']) * 5 + 1000;
35
  } else {
36
    $distance = 5;
37
  }
38
39
  return $distance;
40
}
41
42
43
function fltDistanceAsGalaxy($distance) {
44
  return $distance ? $distance / flt_get_galaxy_distance() : 0;
45
}
46
47
function fltDistanceAsSystem($distance) {
48
  return $distance ? ($distance - 2700) / 5 / 19 : 0;
49
}
50
51
/**
52
 * @param int   $ship_id
53
 * @param int   $speed_percent
54
 * @param array $shipsData
55
 *
56
 * @return float|int
57
 */
58
function flt_get_max_distance($ship_id, $speed_percent = 100, $shipsData = []) {
59
  $single_ship_data = $shipsData[$ship_id];
60
61
  if (!$single_ship_data['capacity'] || !$single_ship_data['consumption']) {
62
    return 0;
63
  }
64
65
  return $distance = floor(($single_ship_data['capacity'] - 1) / $single_ship_data['consumption'] / pow($speed_percent / 100 + 1, 2) * 35000);
0 ignored issues
show
Unused Code introduced by
The assignment to $distance is dead and can be removed.
Loading history...
66
}
67
68
69
/**
70
 * @param         $user_row
71
 * @param         $from
72
 * @param         $to
73
 * @param         $fleet_array
74
 * @param int     $speed_percent
75
 * @param array[] $shipsData - prepared ships data to use in calculations
76
 *
77
 * @return array
78
 */
79
function flt_travel_data($user_row, $from, $to, $fleet_array, $speed_percent = 10, $shipsData = [], $distance = null) {
80
  $distance = $distance === null ? flt_travel_distance($from, $to) : $distance;
81
82
  $consumption = 0;
83
  $capacity = 0;
84
  $duration = 0;
85
86
  $game_fleet_speed = flt_server_flight_speed_multiplier();
87
  $fleet_speed = flt_fleet_speed($user_row, $fleet_array, $shipsData);
88
  if (!empty($fleet_array) && $fleet_speed && $game_fleet_speed) {
89
    $speed_percent = $speed_percent ? max(min($speed_percent, 10), 1) : 10;
90
    $real_speed = $speed_percent * sqrt($fleet_speed);
91
92
    $duration = max(1, round((35000 / $speed_percent * sqrt($distance * 10 / $fleet_speed) + 10) / $game_fleet_speed));
93
94
    foreach ($fleet_array as $ship_id => $ship_count) {
95
      if (!$ship_id || !$ship_count) {
96
        continue;
97
      }
98
99
      $single_ship_data = !empty($shipsData[$ship_id]) ? $shipsData[$ship_id] : get_ship_data($ship_id, $user_row);
100
      $single_ship_data['speed'] = $single_ship_data['speed'] < 1 ? 1 : $single_ship_data['speed'];
101
102
      $consumption += $single_ship_data['consumption'] * $ship_count * pow($real_speed / sqrt($single_ship_data['speed']) / 10 + 1, 2);
103
      $capacity += $single_ship_data['capacity'] * $ship_count;
104
    }
105
106
    $consumption = ceil($distance * $consumption / 35000) + 1;
107
  }
108
109
  return array(
110
    'fleet_speed'            => $fleet_speed,
111
    'distance'               => $distance,
112
    'duration'               => $duration,
113
    'consumption'            => $consumption,
114
    'capacity'               => $capacity,
115
    'hold'                   => $capacity - $consumption,
116
    'transport_effectivness' => $consumption ? $capacity / $consumption : 0,
117
  );
118
}
119
120
function flt_bashing_check($user, $enemy, $planet_dst, $mission, $flight_duration, $fleet_group = 0) {
121
  global $config;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
122
123
  $config_bashing_attacks = $config->fleet_bashing_attacks;
124
  $config_bashing_interval = $config->fleet_bashing_interval;
125
  if (!$config_bashing_attacks) {
126
    // Bashing allowed - protection disabled
127
    return ATTACK_ALLOWED;
128
  }
129
130
  $bashing_result = ATTACK_BASHING;
131
  if ($user['ally_id'] && $enemy['ally_id']) {
132
    $relations = ali_relations($user['ally_id'], $enemy['ally_id']);
133
    if (!empty($relations)) {
134
      $relations = $relations[$enemy['ally_id']];
135
      switch ($relations['alliance_diplomacy_relation']) {
136
        case ALLY_DIPLOMACY_WAR:
137
          if (SN_TIME_NOW - $relations['alliance_diplomacy_time'] <= $config->fleet_bashing_war_delay) {
138
            $bashing_result = ATTACK_BASHING_WAR_DELAY;
139
          } else {
140
            return ATTACK_ALLOWED;
141
          }
142
        break;
143
        // Here goes other relations
144
145
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
146
                default:
147
                  return ATTACK_ALLOWED;
148
                break;
149
        */
150
      }
151
    }
152
  }
153
154
  $time_limit = SN_TIME_NOW + $flight_duration - $config->fleet_bashing_scope;
155
  $bashing_list = array(SN_TIME_NOW);
156
157
  // Retrieving flying fleets
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
158
//  $flying_fleets = array();
159
//  $query = doquery("SELECT fleet_group, fleet_start_time FROM {{fleets}} WHERE
160
//  fleet_end_galaxy = {$planet_dst['galaxy']} AND
161
//  fleet_end_system = {$planet_dst['system']} AND
162
//  fleet_end_planet = {$planet_dst['planet']} AND
163
//  fleet_end_type   = {$planet_dst['planet_type']} AND
164
//  fleet_owner = {$user['id']} AND fleet_mission IN (" . MT_ATTACK . "," . MT_AKS . "," . MT_DESTROY . ") AND fleet_mess = 0;");
165
//  while($bashing_fleets = db_fetch($query)) {
166
//    // Checking for ACS - each ACS count only once
167
//    if($bashing_fleets['fleet_group']) {
168
//      $bashing_list["{$user['id']}_{$bashing_fleets['fleet_group']}"] = $bashing_fleets['fleet_start_time'];
169
//    } else {
170
//      $bashing_list[] = $bashing_fleets['fleet_start_time'];
171
//    }
172
//  }
173
  $bashing_fleet_list = fleet_list_bashing($user['id'], $planet_dst);
174
  foreach ($bashing_fleet_list as $fleet_row) {
175
    // Checking for ACS - each ACS count only once
176
    if ($fleet_row['fleet_group']) {
177
      $bashing_list["{$user['id']}_{$fleet_row['fleet_group']}"] = $fleet_row['fleet_start_time'];
178
    } else {
179
      $bashing_list[] = $fleet_row['fleet_start_time'];
180
    }
181
  }
182
183
  // Check for joining to ACS - if there are already fleets in ACS no checks should be done
184
  if ($mission == MT_AKS && $bashing_list["{$user['id']}_{$fleet_group}"]) {
185
    return ATTACK_ALLOWED;
186
  }
187
188
  $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};");
189
  while ($bashing_row = db_fetch($query)) {
190
    $bashing_list[] = $bashing_row['bashing_time'];
191
  }
192
193
  sort($bashing_list);
194
195
  $last_attack = 0;
196
  $wave = 0;
197
  $attack = 1;
198
  foreach ($bashing_list as &$bash_time) {
199
    $attack++;
200
    if ($bash_time - $last_attack > $config_bashing_interval || $attack > $config_bashing_attacks) {
201
      $attack = 1;
202
      $wave++;
203
    }
204
205
    $last_attack = $bash_time;
206
  }
207
208
  return ($wave > $config->fleet_bashing_waves ? $bashing_result : ATTACK_ALLOWED);
209
}
210
211
function flt_can_attack($planet_src, $planet_dst, $fleet = array(), $mission, $options = false) {
212
  $result = null;
213
214
  return sn_function_call('flt_can_attack', array($planet_src, $planet_dst, $fleet, $mission, $options, &$result));
215
}
216
217
function sn_flt_can_attack($planet_src, $planet_dst, $fleet = array(), $mission, $options = false, &$result) {
218
  //TODO: try..catch
219
  global $config, $user;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
220
221
  if ($user['vacation']) {
222
    return $result = ATTACK_OWN_VACATION;
223
  }
224
225
  if (empty($fleet) || !is_array($fleet)) {
226
    return $result = ATTACK_NO_FLEET;
227
  }
228
229
  $sn_groups_mission = sn_get_groups('missions');
230
  if (!isset($sn_groups_mission[$mission])) {
231
    return $result = ATTACK_MISSION_ABSENT;
232
  }
233
  $sn_data_mission = $sn_groups_mission[$mission];
234
235
//TODO: Проверка на отстуствие ресурсов в нетранспортных миссиях (Транспорт, Передислокация, Колонизация)
236
237
  //TODO: Проверка на наличие ресурсов при Транспорте
238
  // TODO: Проверка на отрицательные ресурсы при транспорте
239
  // TODO: Проверка на перегрузку при транспорте
240
241
  // TODO: В ракетных миссиях могут лететь только ракеты
242
  // TODO: В неракетных миссиях ракеты должны отсутствовать
243
  $ships = 0;
244
  $recyclers = 0;
245
  $spies = 0;
246
  $resources = 0;
247
  $ship_ids = sn_get_groups('fleet');
248
  $resource_ids = sn_get_groups('resources_loot');
249
  foreach ($fleet as $ship_id => $ship_count) {
250
    $is_ship = in_array($ship_id, $ship_ids);
251
    $is_resource = in_array($ship_id, $resource_ids);
252
    if (!$is_ship && !$is_resource) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
253
      // TODO Спецобработчик для Капитана и модулей
254
//      return ATTACK_WRONG_UNIT;
255
    }
256
257
    if ($ship_count < 0) {
258
      return $result = $is_ship ? ATTACK_SHIP_COUNT_WRONG : ATTACK_RESOURCE_COUNT_WRONG;
259
    }
260
261
    if ($ship_count > mrc_get_level($user, $planet_src, $ship_id)) {
262
      // TODO ATTACK_NO_MISSILE
263
      return $result = $is_ship ? ATTACK_NO_SHIPS : ATTACK_NO_RESOURCES;
264
    }
265
266
    if ($is_ship) {
267
      $single_ship_data = get_ship_data($ship_id, $user);
268
      if ($single_ship_data[P_SPEED] <= 0) {
269
        return $result = ATTACK_ZERO_SPEED;
270
      }
271
      $ships += $ship_count;
272
      $recyclers += in_array($ship_id, sn_get_groups('flt_recyclers')) ? $ship_count : 0;
273
      $spies += $ship_id == SHIP_SPY ? $ship_count : 0;
274
    } elseif ($is_resource) {
275
      $resources += $ship_count;
276
    }
277
  }
278
  /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
279
    if($ships <= 0)
280
    {
281
      return ATTACK_NO_FLEET;
282
    }
283
  */
284
285
  if (isset($options['resources']) && $options['resources'] > 0 && !(isset($sn_data_mission['transport']) && $sn_data_mission['transport'])) {
286
    return $result = ATTACK_RESOURCE_FORBIDDEN;
287
  }
288
289
  /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
290
    elseif($mission == MT_TRANSPORT)
291
    {
292
      return ATTACK_TRANSPORT_EMPTY;
293
    }
294
  */
295
296
  $speed = $options['fleet_speed_percent'];
297
  if ($speed && ($speed != intval($speed) || $speed < 1 || $speed > 10)) {
298
    return $result = ATTACK_WRONG_SPEED;
299
  }
300
301
  $travel_data = flt_travel_data($user, $planet_src, $planet_dst, $fleet, $options['fleet_speed_percent']);
302
303
304
  if (mrc_get_level($user, $planet_src, RES_DEUTERIUM) < $fleet[RES_DEUTERIUM] + $travel_data['consumption']) {
305
    return $result = ATTACK_NO_FUEL;
306
  }
307
308
  if ($travel_data['consumption'] > $travel_data['capacity']) {
309
    return $result = ATTACK_TOO_FAR;
310
  }
311
312
  if ($travel_data['hold'] < $resources) {
313
    return $result = ATTACK_OVERLOADED;
314
  }
315
316
  $fleet_start_time = SN_TIME_NOW + $travel_data['duration'];
317
318
  $fleet_group = $options['fleet_group'];
319
  if ($fleet_group) {
320
    if ($mission != MT_AKS) {
321
      return $result = ATTACK_WRONG_MISSION;
322
    };
323
324
    $acs = doquery("SELECT * FROM {{aks}} WHERE id = '{$fleet_group}' LIMIT 1;", '', true);
325
    if (!$acs['id']) {
326
      return $result = ATTACK_NO_ACS;
327
    }
328
329
    if ($planet_dst['galaxy'] != $acs['galaxy'] || $planet_dst['system'] != $acs['system'] || $planet_dst['planet'] != $acs['planet'] || $planet_dst['planet_type'] != $acs['planet_type']) {
330
      return $result = ATTACK_ACS_WRONG_TARGET;
331
    }
332
333
    if ($fleet_start_time > $acs['ankunft']) {
334
      return $result = ATTACK_ACS_TOO_LATE;
335
    }
336
  }
337
338
  $flying_fleets = $options['flying_fleets'];
339
  if (!$flying_fleets) {
340
//    $flying_fleets = doquery("SELECT COUNT(fleet_id) AS `flying_fleets` FROM {{fleets}} WHERE `fleet_owner` = '{$user['id']}';", '', true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
341
//    $flying_fleets = $flying_fleets['flying_fleets'];
342
    $flying_fleets = fleet_count_flying($user['id']);
343
  }
344
  if (GetMaxFleets($user) <= $flying_fleets && $mission != MT_MISSILE) {
345
    return $result = ATTACK_NO_SLOTS;
346
  }
347
348
  // В одиночку шпионские зонды могут летать только в миссии Шпионаж, Передислокация и Транспорт
349
  if ($ships && $spies && $spies == $ships && !($mission == MT_SPY || $mission == MT_RELOCATE || $mission == MT_TRANSPORT)) {
350
    return $result = ATTACK_SPIES_LONLY;
351
  }
352
353
  // Checking for no planet
354
  if (!$planet_dst['id_owner']) {
355
    if ($mission == MT_COLONIZE && !$fleet[SHIP_COLONIZER]) {
356
      return $result = ATTACK_NO_COLONIZER;
357
    }
358
359
    if ($mission == MT_EXPLORE || $mission == MT_COLONIZE) {
360
      return $result = ATTACK_ALLOWED;
361
    }
362
363
    return $result = ATTACK_NO_TARGET;
364
  }
365
366
  if ($mission == MT_RECYCLE) {
367
    if ($planet_dst['debris_metal'] + $planet_dst['debris_crystal'] <= 0) {
368
      return $result = ATTACK_NO_DEBRIS;
369
    }
370
    if ($recyclers <= 0) {
371
      return $result = ATTACK_NO_RECYCLERS;
372
    }
373
374
    return $result = ATTACK_ALLOWED;
375
  }
376
377
  // Got planet. Checking if it is ours
378
  if ($planet_dst['id_owner'] == $user['id']) {
379
    if ($mission == MT_TRANSPORT || $mission == MT_RELOCATE) {
380
      return $result = ATTACK_ALLOWED;
381
    }
382
383
    return $planet_src['id'] == $planet_dst['id'] ? ATTACK_SAME : ATTACK_OWN;
384
  }
385
386
  // No, planet not ours. Cutting mission that can't be send to not-ours planet
387
  if ($mission == MT_RELOCATE || $mission == MT_COLONIZE || $mission == MT_EXPLORE) {
388
    return $result = ATTACK_WRONG_MISSION;
389
  }
390
391
  $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

391
  $enemy = /** @scrutinizer ignore-deprecated */ db_user_by_id($planet_dst['id_owner']);
Loading history...
392
  // We cannot attack or send resource to users in VACATION mode
393
  if ($enemy['vacation'] && $mission != MT_RECYCLE) {
394
    return $result = ATTACK_VACATION;
395
  }
396
397
  // Multi IP protection
398
  // TODO: Here we need a procedure to check proxies
399
  if (sys_is_multiaccount($user, $enemy)) {
400
    return $result = ATTACK_SAME_IP;
401
  }
402
403
  $user_points = $user['total_points'];
404
  $enemy_points = $enemy['total_points'];
405
406
  // Is it transport? If yes - checking for buffing to prevent mega-alliance destroyer
407
  if ($mission == MT_TRANSPORT) {
408
    if ($user_points >= $enemy_points || $config->allow_buffing) {
409
      return $result = ATTACK_ALLOWED;
410
    } else {
411
      return $result = ATTACK_BUFFING;
412
    }
413
  }
414
415
  // Only aggresive missions passed to this point. HOLD counts as passive but aggresive
416
417
  // Is it admin with planet protection?
418
  if ($planet_dst['id_level'] > $user['authlevel']) {
419
    return $result = ATTACK_ADMIN;
420
  }
421
422
  // Okay. Now skipping protection checks for inactive longer then 1 week
423
  if (!$enemy['onlinetime'] || $enemy['onlinetime'] >= (SN_TIME_NOW - 60 * 60 * 24 * 7)) {
424
    if (
425
      (SN::$gc->general->playerIsNoobByPoints($enemy_points) && !SN::$gc->general->playerIsNoobByPoints($user_points))
426
      ||
427
      (SN::$gc->general->playerIs1stStrongerThen2nd($user_points, $enemy_points))
428
    ) {
429
      if ($mission != MT_HOLD) {
430
        return $result = ATTACK_NOOB;
431
      }
432
      if ($mission == MT_HOLD && !($user['ally_id'] && $user['ally_id'] == $enemy['ally_id'] && $config->ally_help_weak)) {
433
        return $result = ATTACK_NOOB;
434
      }
435
    }
436
  }
437
438
  // Is it HOLD mission? If yes - there should be ally deposit
439
  if ($mission == MT_HOLD) {
440
    if (mrc_get_level($user, $planet_dst, STRUC_ALLY_DEPOSIT)) {
441
      return $result = ATTACK_ALLOWED;
442
    }
443
444
    return $result = ATTACK_NO_ALLY_DEPOSIT;
445
  }
446
447
  if ($mission == MT_SPY) {
448
    return $result = $spies >= 1 ? ATTACK_ALLOWED : ATTACK_NO_SPIES;
449
  }
450
451
  // Is it MISSILE mission?
452
  if ($mission == MT_MISSILE) {
453
    $sn_data_mip = get_unit_param(UNIT_DEF_MISSILE_INTERPLANET);
454
    if (mrc_get_level($user, $planet_src, STRUC_SILO) < $sn_data_mip[P_REQUIRE][STRUC_SILO]) {
455
      return $result = ATTACK_NO_SILO;
456
    }
457
458
    if (!$fleet[UNIT_DEF_MISSILE_INTERPLANET]) {
459
      return $result = ATTACK_NO_MISSILE;
460
    }
461
462
    $distance = abs($planet_dst['system'] - $planet_src['system']);
463
    $mip_range = flt_get_missile_range($user);
464
    if ($distance > $mip_range || $planet_dst['galaxy'] != $planet_src['galaxy']) {
465
      return $result = ATTACK_MISSILE_TOO_FAR;
466
    }
467
468
    if (isset($options['target_structure']) && $options['target_structure'] && !in_array($options['target_structure'], sn_get_groups('defense_active'))) {
469
      return $result = ATTACK_WRONG_STRUCTURE;
470
    }
471
  }
472
473
  if ($mission == MT_DESTROY && $planet_dst['planet_type'] != PT_MOON) {
474
    return $result = ATTACK_WRONG_MISSION;
475
  }
476
477
  if ($mission == MT_ATTACK || $mission == MT_AKS || $mission == MT_DESTROY) {
478
    return $result = flt_bashing_check($user, $enemy, $planet_dst, $mission, $travel_data['duration'], $fleet_group);
479
  }
480
481
  return $result = ATTACK_ALLOWED;
482
}
483
484
/*
485
$user - actual user record
486
$from - actual planet record
487
$to - actual planet record
488
$fleet - array of records $unit_id -> $amount
489
$mission - fleet mission
490
*/
491
492
function flt_t_send_fleet($user, &$from, $to, $fleet, $mission, $options = array()) {
493
//ini_set('error_reporting', E_ALL);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
494
495
  $internal_transaction = !sn_db_transaction_check(false) ? sn_db_transaction_start() : false;
496
497
  // TODO Потенциальный дедлок - если успела залочится запись пользователя - хозяина планеты
498
  $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

498
  $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true);
Loading history...
499
  $from = sys_o_get_updated($user, $from['id'], SN_TIME_NOW);
500
  $from = $from['planet'];
501
502
  $can_attack = flt_can_attack($from, $to, $fleet, $mission, $options);
503
  if ($can_attack != ATTACK_ALLOWED) {
504
    $internal_transaction ? sn_db_transaction_rollback() : false;
505
506
    return $can_attack;
507
  }
508
509
  $fleet_group = isset($options['fleet_group']) ? floatval($options['fleet_group']) : 0;
510
511
  $travel_data = flt_travel_data($user, $from, $to, $fleet, $options['fleet_speed_percent']);
512
513
  $fleet_start_time = SN_TIME_NOW + $travel_data['duration'];
514
515
  if ($mission == MT_EXPLORE || $mission == MT_HOLD) {
516
    $stay_duration = $options['stay_time'] * 3600;
517
    $stay_time = $fleet_start_time + $stay_duration;
518
  } else {
519
    $stay_duration = 0;
520
    $stay_time = 0;
521
  }
522
  $fleet_end_time = $fleet_start_time + $travel_data['duration'] + $stay_duration;
523
524
  $fleet_ship_count = 0;
525
  $fleet_string = '';
526
  $db_changeset = array();
527
  $planet_fields = array();
528
  foreach ($fleet as $unit_id => $amount) {
529
    if (!$amount || !$unit_id) {
530
      continue;
531
    }
532
533
    if (in_array($unit_id, sn_get_groups('fleet'))) {
534
      $fleet_ship_count += $amount;
535
      $fleet_string .= "{$unit_id},{$amount};";
536
      $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit($unit_id, -$amount, $user, $from['id']);
537
    } elseif (in_array($unit_id, sn_get_groups('resources_loot'))) {
538
      $planet_fields[pname_resource_name($unit_id)]['delta'] -= $amount;
539
    }
540
  }
541
542
  $to['id_owner'] = intval($to['id_owner']);
543
544
//  $QryInsertFleet  = "INSERT INTO {{fleets}} SET ";
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
545
//  $QryInsertFleet .= "`fleet_owner` = '{$user['id']}', ";
546
//  $QryInsertFleet .= "`fleet_mission` = '{$mission}', ";
547
//  $QryInsertFleet .= "`fleet_amount` = '{$fleet_ship_count}', ";
548
//  $QryInsertFleet .= "`fleet_array` = '{$fleet_string}', ";
549
//  $QryInsertFleet .= "`fleet_start_time` = '{$fleet_start_time}', ";
550
//  if($from['id'])
551
//  {
552
//    $QryInsertFleet .= "`fleet_start_planet_id` = '{$from['id']}', ";
553
//  }
554
//  $QryInsertFleet .= "`fleet_start_galaxy` = '{$from['galaxy']}', ";
555
//  $QryInsertFleet .= "`fleet_start_system` = '{$from['system']}', ";
556
//  $QryInsertFleet .= "`fleet_start_planet` = '{$from['planet']}', ";
557
//  $QryInsertFleet .= "`fleet_start_type` = '{$from['planet_type']}', ";
558
//  $QryInsertFleet .= "`fleet_end_time` = '{$fleet_end_time}', ";
559
//  $QryInsertFleet .= "`fleet_end_stay` = '{$stay_time}', ";
560
//  if($to['id'])
561
//  {
562
//    $QryInsertFleet .= "`fleet_end_planet_id` = '{$to['id']}', ";
563
//  }
564
//  $QryInsertFleet .= "`fleet_end_galaxy` = '{$to['galaxy']}', ";
565
//  $QryInsertFleet .= "`fleet_end_system` = '{$to['system']}', ";
566
//  $QryInsertFleet .= "`fleet_end_planet` = '{$to['planet']}', ";
567
//  $QryInsertFleet .= "`fleet_end_type` = '{$to['planet_type']}', ";
568
//  $QryInsertFleet .= "`fleet_resource_metal` = " . floatval($fleet[RES_METAL]) . ", ";
569
//  $QryInsertFleet .= "`fleet_resource_crystal` = " . floatval($fleet[RES_CRYSTAL]) . ", ";
570
//  $QryInsertFleet .= "`fleet_resource_deuterium` = " . floatval($fleet[RES_DEUTERIUM]) . ", ";
571
//  $QryInsertFleet .= "`fleet_target_owner` = '{$to['id_owner']}', ";
572
//  $QryInsertFleet .= "`fleet_group` = '{$fleet_group}', ";
573
//  $QryInsertFleet .= "`start_time` = " . SN_TIME_NOW . ";";
574
//  doquery( $QryInsertFleet);
575
576
  $fleet_set = array(
577
    'fleet_owner'   => $user['id'],
578
    'fleet_mission' => $mission,
579
    'fleet_amount'  => $fleet_ship_count,
580
    'fleet_array'   => $fleet_string,
581
582
    'fleet_start_time'      => $fleet_start_time,
583
    'fleet_start_planet_id' => intval($from['id']) ? $from['id'] : null,
584
    'fleet_start_galaxy'    => $from['galaxy'],
585
    'fleet_start_system'    => $from['system'],
586
    'fleet_start_planet'    => $from['planet'],
587
    'fleet_start_type'      => $from['planet_type'],
588
589
    'fleet_end_time'      => $fleet_end_time,
590
    'fleet_end_stay'      => $stay_time,
591
    'fleet_end_planet_id' => intval($to['id']) ? $to['id'] : null,
592
    'fleet_end_galaxy'    => $to['galaxy'],
593
    'fleet_end_system'    => $to['system'],
594
    'fleet_end_planet'    => $to['planet'],
595
    'fleet_end_type'      => $to['planet_type'],
596
    'fleet_target_owner'  => intval($to['id_owner']) ? $to['id_owner'] : 0,
597
598
    'fleet_resource_metal'     => floatval($fleet[RES_METAL]),
599
    'fleet_resource_crystal'   => floatval($fleet[RES_CRYSTAL]),
600
    'fleet_resource_deuterium' => floatval($fleet[RES_DEUTERIUM]),
601
602
    'fleet_group' => $fleet_group,
603
    'start_time'  => SN_TIME_NOW,
604
  );
605
  fleet_insert_set($fleet_set);
606
607
  $planet_fields[pname_resource_name(RES_DEUTERIUM)]['delta'] -= $travel_data['consumption'];
608
  $db_changeset['planets'][] = array(
609
    'action'  => SQL_OP_UPDATE,
610
    P_VERSION => 1,
611
    'where'   => array(
612
      'id' => $from['id'],
613
    ),
614
    'fields'  => $planet_fields,
615
  );
616
617
  OldDbChangeSet::db_changeset_apply($db_changeset);
618
619
  // $internal_transaction = false;sn_db_transaction_rollback(); // TODO - REMOVE !!!!!!!!!!!!!!!!!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
620
621
  $internal_transaction ? sn_db_transaction_commit() : false;
622
  $from = DBStaticPlanet::db_planet_by_id($from['id']);
623
624
  return ATTACK_ALLOWED;
625
//ini_set('error_reporting', E_ALL ^ E_NOTICE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
626
}
627
628
function flt_calculate_ship_to_transport_sort($a, $b) {
629
  return $a['transport_effectivness'] == $b['transport_effectivness'] ? 0 : ($a['transport_effectivness'] > $b['transport_effectivness'] ? -1 : 1);
630
}
631
632
// flt_calculate_ship_to_transport - calculates how many ships need to transport pointed amount of resources
633
// $ship_list - list of available ships
634
// $resource_amount - how much amount of resources need to be transported
635
// $from - transport from
636
// $to - transport to
637
function flt_calculate_fleet_to_transport($ship_list, $resource_amount, $from, $to) {
638
  global $user;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
639
640
  $ship_data = array();
641
  $fleet_array = array();
642
  foreach ($ship_list as $transport_id => $cork) {
643
    $ship_data[$transport_id] = flt_travel_data($user, $from, $to, array($transport_id => 1), 10);
644
  }
645
  uasort($ship_data, flt_calculate_ship_to_transport_sort);
0 ignored issues
show
Bug introduced by
The constant flt_calculate_ship_to_transport_sort was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
646
647
  $fleet_hold = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $fleet_hold is dead and can be removed.
Loading history...
648
  $fleet_capacity = 0;
649
  $fuel_total = $fuel_left = mrc_get_level($user, $from, RES_DEUTERIUM);
650
  foreach ($ship_data as $transport_id => &$ship_info) {
651
    $ship_loaded = min($ship_list[$transport_id], ceil($resource_amount / $ship_info['hold']), floor($fuel_left / $ship_info['consumption']));
652
    if ($ship_loaded) {
653
      $fleet_array[$transport_id] = $ship_loaded;
654
      $resource_amount -= min($resource_amount, $ship_info['hold'] * $ship_loaded);
655
      $fuel_left -= $ship_info['consumption'] * $ship_loaded;
656
657
      $fleet_capacity += $ship_info['capacity'] * $ship_loaded;
658
    }
659
  }
660
661
  return array('fleet' => $fleet_array, 'ship_data' => $ship_data, 'capacity' => $fleet_capacity, 'consumption' => $fuel_total - $fuel_left);
662
}
663