Test Failed
Branch work-fleets (d40084)
by SuperNova.WS
05:20
created

FleetValidator::checkACSNotEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
/**
4
 * Class FleetValidator
5
 */
6
class FleetValidator {
7
  /**
8
   * @var Fleet $fleet
9
   */
10
  protected $fleet;
11
12
  /**
13
   * FleetValidator constructor.
14
   *
15
   * @param Fleet $fleet
16
   */
17
  public function __construct($fleet) {
18
    $this->fleet = $fleet;
19
  }
20
21
  /**
22
   *
23
   */
24
  public function validate() {
25
    $checklist = sn_get_groups('mission_checks');
26
    try {
27
      // TODO - Do the restrictMission checks
28
29
      // TODO - Кое-какие проверки дают FLIGHT_ALLOWED - ЧТО НЕПРАВДА В ДАННОМ СЛУЧАЕ!!!
30
      // На странице 1 некоторые проверки ДОЛЖНЫ БЫТЬ опущены - иначе будет некрасиво
31
      // А вот здесь надо проверять много дополнительной хуйни
32
      $this->checkMissionRestrictions($checklist);
33
//pdump('passed');
34
35
      // 2nd level restrictions
36
      // Still cheap
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...
37
//      $this->restrict2ToAllowedMissions();
38
//      $this->restrict2ToAllowedPlanetTypes();
39
    } catch (ExceptionFleetInvalid $e) {
40
//pdump($e->getCode(), '$e->getCode()');
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
41
//pdump($e->getMessage(), '$e->getMessage()');
42
      if ($e->getCode() != FLIGHT_ALLOWED) {
43
        pdie(classLocale::$lang['fl_attack_error'][$e->getCode()]);
44
      } else {
45
        pdump('FLIGHT_ALLOWED', FLIGHT_ALLOWED);
46
      }
47
    }
48
    pdump('// TODO - Сделать flletvalidator DI - внутре контейнер для методов, а методы - анонимные функции, вызывающие другие методы же', FLIGHT_ALLOWED);
49
  }
50
51
  /**
52
   * @param array $checklist
53
   *
54
   * @throws Exception
55
   */
56
  public function checkMissionRestrictions($checklist) {
57
    foreach ($checklist as $condition => $action) {
58
59
      $checkResult = call_user_func(array($this, $condition));
60
      pdump($action, $condition . ' ' . ($checkResult ? 'TRUE' : 'FALSE'));
61
62
//      // Simple action on failed check
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...
63
//      if(!is_array($action)) {
64
//        if(!$checkResult) {
65
//          throw new ExceptionFleetInvalid($action, $action);
66
//        } else {
67
//          continue;
68
//        }
69
//      }
70
      // If check failed and there no alternative actions - throw exception
71
      // Shortcut ACTION => FAIL_STATUS instead of ACTION => array(false => FAIL_STATUS)
72
      if (!$checkResult && !is_array($action)) {
73
        throw new ExceptionFleetInvalid($action, $action);
74
      }
75
76
      // If no actions on current result - just skipping to next condition
77
      if (!isset($action[$checkResult])) {
78
        // If not - just continuing
79
        continue;
80
      }
81
82
      // Otherwise - we got some action for current result
83
      $action = $action[$checkResult];
84
85
      // Is it a list of conditions?
86
      if (is_array($action)) {
87
        // Yes - performing condition check
88
        $this->checkMissionRestrictions($action);
89
      } else {
90
        // No - then just performing action
91
        throw new ExceptionFleetInvalid($action, $action);
92
      }
93
94
95
//      // Is there some alternatives?
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
96
//      if (is_array($action)) {
97
//        if (!empty($action[$checkResult])) {
98
//          // Now action is selected alternative
99
//          $action = $action[$checkResult];
100
//        } else {
101
//          continue;
102
//        }
103
//        // No alternatives - just action
104
//      } elseif (!$checkResult) {
105
//        // Action launched if check failed
106
//
107
//        throw new ExceptionFleetInvalid($action, $action);
108
//      }
109
//
110
//      // Here we got action that should be processed
111
//
112
////pdump($action, $condition);
113
//
114
////var_dump($checkResult);
115
//      // Is new action an array - i.e. list of other checks?
116
//      if (is_array($action)) {
117
//        // Yes - performing check
118
//        $this->checkMissionRestrictions($action);
119
//      } else {
120
////        if (!$checkResult)
121
//        {
122
//          throw new ExceptionFleetInvalid($action, $action);
123
//        }
124
//      }
125
    }
126
  }
127
128
129
  /**
130
   * @throws Exception
131
   */
132
  protected function restrict2ToAllowedMissions() {
133
    if (empty($this->fleet->allowed_missions[$this->fleet->mission_type])) {
134
      throw new Exception('FLIGHT_MISSION_IMPOSSIBLE', FLIGHT_MISSION_IMPOSSIBLE);
135
    }
136
  }
137
138
  /**
139
   * @throws Exception
140
   */
141
  protected function restrict2ToAllowedPlanetTypes() {
142
    if (empty($this->fleet->allowed_planet_types[$this->fleet->targetVector->type])) {
143
      throw new Exception('FLIGHT_MISSION_IMPOSSIBLE', FLIGHT_MISSION_IMPOSSIBLE);
144
    }
145
  }
146
147
148
  /**
149
   * @return bool
150
   */
151
  protected function checkSpeedPercentOld() {
152
    return in_array($this->fleet->oldSpeedInTens, array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
153
  }
154
155
  /**
156
   * @return bool
157
   */
158
  protected function checkSenderNoVacation() {
159
    return empty($this->fleet->dbOwnerRow['vacation']) || $this->fleet->dbOwnerRow['vacation'] >= SN_TIME_NOW;
160
  }
161
162
  /**
163
   * @return bool
164
   */
165
  protected function checkTargetNoVacation() {
166
    return empty($this->fleet->dbTargetOwnerRow['vacation']) || $this->fleet->dbTargetOwnerRow['vacation'] >= SN_TIME_NOW;
167
  }
168
169
  /**
170
   * @return bool
171
   */
172
  protected function checkMultiAccountNot() {
173
    return !sys_is_multiaccount($this->fleet->dbOwnerRow, $this->fleet->dbTargetOwnerRow);
174
  }
175
176
  /**
177
   * @return bool
178
   */
179
  protected function checkTargetNotSource() {
180
    return !$this->fleet->targetVector->isSameLocation($this->fleet->dbSourcePlanetRow);
181
  }
182
183
  /**
184
   * @return bool
185
   */
186
  protected function checkTargetInUniverse() {
187
    return $this->fleet->targetVector->isInUniverse();
188
  }
189
190
  /**
191
   * @return bool
192
   */
193
  protected function checkUnitsPositive() {
194
    return $this->fleet->shipsAllPositive();
195
  }
196
197
  /**
198
   * @return bool
199
   */
200
  protected function checkOnlyFleetUnits() {
201
    return $this->fleet->shipsAllFlying();
202
  }
203
204
  /**
205
   * @return bool
206
   */
207
  protected function checkOnlyFlyingUnits() {
208
    return $this->fleet->shipsAllMovable();
209
  }
210
211
  /**
212
   * @return bool
213
   */
214
  protected function checkEnoughFleetSlots() {
215
    return FleetList::fleet_count_flying($this->fleet->getPlayerOwnerId()) < GetMaxFleets($this->fleet->dbOwnerRow);
216
  }
217
218
219
  /**
220
   * @return bool
221
   */
222
  protected function checkEnoughCapacity($includeResources = true) {
223
    $checkVia = $this->fleet->travelData['consumption'];
224
    $checkVia = ceil(($includeResources ? array_sum($this->fleet->resource_list) : 0) + $checkVia);
225
226
    return
227
      !empty($this->fleet->travelData) &&
228
      is_array($this->fleet->travelData) &&
229
      floor($this->fleet->travelData['capacity']) >= $checkVia;
230
  }
231
232
  /**
233
   * @return bool
234
   */
235
  protected function checkNotTooFar() {
236
    return $this->checkEnoughCapacity(false);
237
  }
238
239
  /**
240
   * @return bool
241
   */
242
  protected function checkDebrisExists() {
243
    return is_array($this->fleet->dbTargetRow) && ($this->fleet->dbTargetRow['debris_metal'] + $this->fleet->dbTargetRow['debris_crystal'] > 0);
244
  }
245
246
247
248
249
250
251
252
253
254
255
256
257
  // Resources checks
258
259
  /**
260
   * @return bool
261
   */
262
  protected function checkResourcesPositive() {
263
    foreach ($this->fleet->resource_list as $resourceId => $resourceAmount) {
264
      if ($resourceAmount < 0) {
265
        return false;
266
      }
267
    }
268
269
    return true;
270
  }
271
272
  /**
273
   * @return bool
274
   */
275
  protected function checkCargo() {
276
    return array_sum($this->fleet->resource_list) >= 1;
277
  }
278
279
  /**
280
   * @return bool
281
   */
282
  protected function checkSourceEnoughFuel() {
283
    $deuteriumOnPlanet = mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, RES_DEUTERIUM);
284
285
    return $deuteriumOnPlanet > ceil($this->fleet->travelData['consumption']);
286
  }
287
288
  /**
289
   * @return bool
290
   */
291
  protected function checkSourceEnoughResources() {
292
    $fleetResources = $this->fleet->resource_list;
293
    $fleetResources[RES_DEUTERIUM] = ceil($fleetResources[RES_DEUTERIUM] + $this->fleet->travelData['consumption']);
294
    foreach ($fleetResources as $resourceId => $resourceAmount) {
295
      if (mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, $resourceId) < ceil($fleetResources[$resourceId])) {
296
        return false;
297
      }
298
    }
299
300
    return true;
301
  }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  // Target vector checks (????????)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
328
329
  /**
330
   * @return bool
331
   */
332
  protected function checkKnownSpace() {
333
    return $this->fleet->targetVector->isInKnownSpace();
334
  }
335
336
  /**
337
   * @return bool
338
   */
339
  protected function checkTargetExists() {
340
    return !empty($this->fleet->dbTargetRow['id']);
341
  }
342
343
  /**
344
   * @return bool
345
   */
346
  protected function checkTargetIsPlanet() {
347
    return $this->fleet->targetVector->type == PT_PLANET;
348
  }
349
350
  /**
351
   * @return bool
352
   */
353
  protected function checkTargetIsDebris() {
354
    return $this->fleet->targetVector->type == PT_DEBRIS;
355
  }
356
357
  /**
358
   * @return bool
359
   */
360
  protected function checkTargetIsMoon() {
361
    return $this->fleet->targetVector->type == PT_MOON;
362
  }
363
364
365
366
367
368
369
  // Ships checks
370
371
  /**
372
   * @return bool
373
   */
374
  protected function checkFleetNotEmpty() {
375
    return $this->fleet->shipsGetTotal() >= 1;
376
  }
377
378
379
  /**
380
   * @return bool
381
   */
382
  protected function checkSourceEnoughShips() {
383
    return $this->fleet->shipsIsEnoughOnPlanet();
384
  }
385
386
387
  /**
388
   * @return bool
389
   */
390
  protected function checkHaveColonizer() {
391
    // Colonization fleet should have at least one colonizer
392
    return $this->fleet->shipsGetTotalById(SHIP_COLONIZER) >= 1;
393
  }
394
395
  /**
396
   * @return bool
397
   */
398
  protected function checkHaveRecyclers() {
399
    $recyclers = 0;
400
    foreach (Fleet::$snGroupRecyclers as $recycler_id) {
401
      $recyclers += $this->fleet->shipsGetTotalById($recycler_id);
402
    }
403
404
    return $recyclers >= 1;
405
  }
406
407
  /**
408
   * @return bool
409
   */
410
  // TODO - used only as callable. Redo inversion
411
  protected function checkSpiesOnly() {
412
    return $this->fleet->shipsGetTotalById(SHIP_SPY) == $this->fleet->shipsGetTotal();
413
  }
414
415
  /**
416
   * @return bool
417
   */
418
  protected function checkNotOnlySpies() {
419
    return !$this->checkSpiesOnly();
420
  }
421
422
  /**
423
   * @return bool
424
   */
425
  protected function checkNoMissiles() {
426
    return
427
      $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERPLANET) == 0
428
      &&
429
      $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERCEPTOR) == 0;
430
  }
431
432
433
  /**
434
   * @return bool
435
   */
436
  protected function checkTargetOwn() {
437
    return $this->fleet->dbTargetRow['id_owner'] == $this->fleet->dbSourcePlanetRow['id_owner'];
438
  }
439
440
  /**
441
   * @return bool
442
   */
443
  protected function forceTargetOwn() {
444
    if ($result = $this->checkTargetOwn()) {
445
      unset($this->fleet->allowed_missions[MT_EXPLORE]);
446
      unset($this->fleet->allowed_missions[MT_COLONIZE]);
447
      unset($this->fleet->allowed_missions[MT_RECYCLE]);
448
      unset($this->fleet->allowed_missions[MT_MISSILE]);
449
450
      unset($this->fleet->allowed_missions[MT_SPY]);
451
452
      unset($this->fleet->allowed_missions[MT_ATTACK]);
453
      unset($this->fleet->allowed_missions[MT_ACS]);
454
      unset($this->fleet->allowed_missions[MT_DESTROY]);
455
    } else {
456
      unset($this->fleet->allowed_missions[MT_RELOCATE]);
457
    }
458
459
    return $result;
460
  }
461
462
  protected function checkMissionPeaceful() {
463
    return
464
      !$this->fleet->mission_type
465
      ||
466
      in_array($this->fleet->mission_type, array(
467
        MT_HOLD,
468
        MT_RELOCATE,
469
        MT_TRANSPORT,
470
      ));
471
  }
472
473
  /**
474
   * @return bool
475
   */
476
  protected function checkTargetOther() {
477
    return !$this->checkTargetOwn();
478
  }
479
480
481
  /**
482
   * @return bool
483
   */
484
  protected function alwaysFalse() {
485
    return false;
486
  }
487
488
489
  /**
490
   * @return bool
491
   */
492
  protected function checkTargetAllyDeposit() {
493
    $result = mrc_get_level($this->fleet->dbTargetOwnerRow, $this->fleet->dbTargetRow, STRUC_ALLY_DEPOSIT) >= 1;
494
    if (!$result) {
495
      unset($this->fleet->allowed_missions[MT_HOLD]);
496
    }
497
498
    return $result;
499
  }
500
501
  /**
502
   * @param int  $missionType
503
   * @param bool $exact
504
   *
505
   * @return bool
506
   */
507
  protected function checkMission($missionType, $exact = false) {
508
    return $this->fleet->mission_type == $missionType || (!$exact && $this->fleet->mission_type == MT_NONE);
509
  }
510
511
512
  /**
513
   * Check mission type OR no mission - and limits available missions to this type if positive
514
   *
515
   * @param int $missionType
516
   *
517
   * @return bool
518
   */
519
  protected function forceMission($missionType, $exact = false) {
520
    return $this->unsetMission($missionType, $this->checkMission($missionType, $exact), true);
521
  }
522
523
  protected function unsetMission($missionType, $result, $forceMission = false) {
524
    if (!$result) {
525
      unset($this->fleet->allowed_missions[$missionType]);
526
    } elseif ($forceMission) {
527
      $this->fleet->allowed_missions = array(
528
        $missionType => $this->fleet->exists_missions[$missionType],
529
      );
530
    }
531
532
    return $result;
533
  }
534
535
  /**
536
   * @param string $name
537
   * @param string $prefix
538
   *
539
   * @return int|false
540
   * @throws Exception
541
   */
542
  protected function checkMissionPrefix($name, $prefix) {
543
    $result = false;
544
    if (strpos($name, $prefix) === 0) {
545
      $mission = 'MT_' . strtoupper(substr($name, strlen($prefix)));
546
      if (!defined($mission)) {
547
        // TODO - Ну, как-то получше это обделать
548
        throw new Exception('Mission type "' . $mission . '" is not defined');
549
      }
550
551
      $result = constant($mission);
552
    }
553
554
    return $result;
555
  }
556
557
  public function __call($name, $arguments) {
558
    $result = null;
559
    if (($missionType = $this->checkMissionPrefix($name, 'unsetMission')) !== false) {
560
      $result = $this->unsetMission($missionType, false);
561
    } elseif (($missionType = $this->checkMissionPrefix($name, 'forceMissionExact')) !== false) {
562
      $result = $this->forceMission($missionType, true);
563
    } elseif (($missionType = $this->checkMissionPrefix($name, 'forceMission')) !== false) {
564
      $result = $this->forceMission($missionType, false);
565
    } elseif (($missionType = $this->checkMissionPrefix($name, 'checkMissionExact')) !== false) {
566
      $result = $this->checkMission($missionType, true);
567
    } elseif (($missionType = $this->checkMissionPrefix($name, 'checkMission')) !== false) {
568
      $result = $this->checkMission($missionType, false);
569
    }
570
571
    return $result;
572
  }
573
574
  /**
575
   * Just checks mission type
576
   *
577
   * @param int $missionType
578
   *
579
   * @return bool
580
   */
581
  // TODO - obsolete ??
582
  protected function checkMissionExact($missionType) {
583
    return $this->checkMission($missionType, true);
584
  }
585
586
587
  /**
588
   * @return bool
589
   */
590
  protected function checkNotEmptyMission() {
591
    return !empty($this->fleet->mission_type);
592
  }
593
594
  /**
595
   * @return bool
596
   */
597
  protected function checkRealFlight() {
598
    return $this->fleet->isRealFlight;
599
  }
600
601
602
  /**
603
   * @return bool
604
   */
605
  protected function unsetMissionSpyComplex() {
606
    unset($this->fleet->allowed_missions[MT_SPY]);
607
    if ($this->fleet->mission_type == MT_SPY) {
608
      if ($this->checkRealFlight()) {
609
        return false;
610
      }
611
      $this->fleet->mission_type = MT_NONE;
612
    }
613
614
    return true;
615
  }
616
617
618
  /**
619
   * @return bool
620
   */
621
  protected function checkMissionExists() {
622
    return !empty($this->fleet->exists_missions[$this->fleet->mission_type]);
623
  }
624
625
  /**
626
   * @return bool
627
   */
628
  protected function checkMissionAllowed() {
629
    return !empty($this->fleet->allowed_missions[$this->fleet->mission_type]);
630
  }
631
632
  /**
633
   * @return bool
634
   */
635
  protected function checkPlayerInactiveOrNotNoob() {
636
    return
637
      $this->checkTargetNotActive()
638
      ||
639
      $this->checkTargetNotNoob();
640
  }
641
642
  /**
643
   * @return bool
644
   */
645
  protected function checkTargetActive() {
646
    return
647
      empty($this->fleet->dbTargetOwnerRow['onlinetime'])
648
      ||
649
      SN_TIME_NOW - $this->fleet->dbTargetOwnerRow['onlinetime'] >= PLAYER_TIME_ACTIVE_SECONDS;
650
  }
651
652
  /**
653
   * @return bool
654
   */
655
  // TODO - REDO MAIN FUNCTION
656
  protected function checkTargetNotActive() {
657
    return !$this->checkTargetActive();
658
  }
659
660
661
  /**
662
   * @return bool
663
   */
664
  protected function checkSameAlly() {
665
    return !empty($this->fleet->dbTargetOwnerRow['ally_id']) && $this->fleet->dbTargetOwnerRow['ally_id'] == $this->fleet->dbOwnerRow['ally_id'];
666
  }
667
668
  /**
669
   * @return bool
670
   */
671
  protected function checkTargetNoob() {
672
    $user_points = $this->fleet->dbTargetOwnerRow['total_points'];
673
    $enemy_points = $this->fleet->dbTargetOwnerRow['total_points'];
674
675
    return
676
      // Target is under Noob Protection but Fleet owner is not
677
      (
678
        classSupernova::$config->game_noob_points
679
        &&
680
        $enemy_points <= classSupernova::$config->game_noob_points
681
        &&
682
        $user_points > classSupernova::$config->game_noob_points
683
      ) || (
684
        classSupernova::$config->game_noob_factor
685
        &&
686
        $user_points > $enemy_points * classSupernova::$config->game_noob_factor
687
      );
688
  }
689
690
  /**
691
   * @return bool
692
   */
693
  // TODO - REDO MAIN FUNCTION
694
  protected function checkTargetNotNoob() {
695
    return !$this->checkTargetNoob();
696
  }
697
698
699
  /**
700
   * @return bool
701
   */
702
  protected function checkMissionHoldReal() {
703
    return
704
      $this->checkRealFlight()
705
      &&
706
      $this->checkMissionExact(MT_HOLD);
707
  }
708
709
  /**
710
   * @return bool
711
   */
712
  protected function checkMissionHoldOnNotNoob() {
713
    return
714
      $this->checkTargetNotActive()
715
      ||
716
      ($this->checkSameAlly() && classSupernova::$config->ally_help_weak)
717
      ||
718
      $this->checkTargetNotNoob();
719
  }
720
721
722
  // Missiles
723
724
  /**
725
   * @return bool
726
   */
727
  protected function checkOnlyAttackMissiles() {
728
    $missilesAttack = $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERPLANET);
729
730
    return $missilesAttack != 0 && $missilesAttack == $this->fleet->shipsGetTotal();
731
  }
732
733
  /**
734
   * @return bool
735
   */
736
  protected function checkSiloLevel() {
737
    $sn_data_mip = get_unit_param(UNIT_DEF_MISSILE_INTERPLANET);
738
739
    return mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, STRUC_SILO) >= $sn_data_mip[P_REQUIRE][STRUC_SILO];
740
  }
741
742
  /**
743
   * @return bool
744
   */
745
  protected function checkSameGalaxy() {
746
    return $this->fleet->targetVector->galaxy == $this->fleet->dbSourcePlanetRow['galaxy'];
747
  }
748
749
  /**
750
   * @return bool
751
   */
752
  protected function checkMissileDistance() {
753
    return abs($this->fleet->dbSourcePlanetRow['system'] - $this->fleet->targetVector->system) <= flt_get_missile_range($this->fleet->dbOwnerRow);
754
  }
755
756
  /**
757
   * @return bool
758
   */
759
  protected function checkMissileTarget() {
760
    return empty($this->fleet->targetedUnitId) || in_array($this->fleet->targetedUnitId, sn_get_groups('defense_active'));
761
  }
762
763
764
  /**
765
   * @return int
766
   */
767
  protected function checkExpeditionsMax() {
768
    return get_player_max_expeditons($this->fleet->dbOwnerRow) > 0;
769
  }
770
771
  /**
772
   * @return bool
773
   */
774
  protected function checkExpeditionsFree() {
775
    return get_player_max_expeditons($this->fleet->dbOwnerRow) > FleetList::fleet_count_flying($this->fleet->dbOwnerRow['id'], MT_EXPLORE);
776
  }
777
778
  /**
779
   * @return bool
780
   */
781
  protected function checkCaptainSent() {
782
    return $this->fleet->captainId >= 1;
783
  }
784
785
  /**
786
   * @return bool
787
   */
788
  protected function checkCaptainExists() {
789
    return !empty($this->fleet->captain) && is_array($this->fleet->captain);
790
  }
791
792
  /**
793
   * @return bool
794
   */
795
  protected function checkCaptainOnPlanet() {
796
    return $this->fleet->captain['unit_location_type'] == LOC_PLANET;
797
  }
798
799
  /**
800
   * @return bool
801
   */
802
  protected function checkCaptainNotRelocating() {
803
    if ($this->fleet->mission_type == MT_RELOCATE) {
804
      $arriving_captain = mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbTargetRow, UNIT_CAPTAIN, true);
805
    } else {
806
      $arriving_captain = false;
807
    }
808
809
    return empty($arriving_captain) || !is_array($arriving_captain);
810
  }
811
812
813
  /**
814
   * @return bool
815
   */
816
  protected function checkMissionDestroyReal() {
817
    return
818
      $this->checkRealFlight()
819
      &&
820
      $this->checkMissionExact(MT_DESTROY);
821
  }
822
823
  /**
824
   * @return bool
825
   */
826
  protected function checkHaveReapers() {
827
    $unitsTyped = 0;
828
    foreach (sn_get_groups('flt_reapers') as $unit_id) {
829
      $unitsTyped += $this->fleet->shipsGetTotalById($unit_id);
830
    }
831
832
    return $unitsTyped >= 1;
833
  }
834
835
836
  /**
837
   * @return bool
838
   */
839
  protected function checkMissionACSReal() {
840
    return
841
      $this->checkRealFlight()
842
      &&
843
      $this->checkMissionExact(MT_ACS);
844
  }
845
846
  protected function checkACSInTime() {
847
    return $this->fleet->acs['ankunft'] - $this->fleet->time_launch >= $this->fleet->travelData['duration'];
848
  }
849
850
851
  protected function checkMissionRealAndSelected($missionType) {
852
    return
853
      $this->checkRealFlight()
854
      &&
855
      $this->checkMissionExact($missionType);
856
  }
857
858
  protected function checkMissionResultAndUnset($missionType, $result, $forceMission = false) {
859
    $this->unsetMission($missionType, $result, $forceMission);
860
861
    return $result && $this->checkMissionRealAndSelected($missionType);
862
  }
863
864
865
  /**
866
   * @return bool
867
   */
868
  protected function checkMissionSpyPossibleAndReal() {
869
    return $this->checkMissionResultAndUnset(
870
      MT_SPY,
871
      $this->checkSpiesOnly() && $this->checkTargetOther(),
872
      true
873
    );
874
  }
875
876
  /**
877
   * @return bool
878
   */
879
  protected function checkMissionDestroyAndReal() {
880
    return $this->checkMissionResultAndUnset(
881
      MT_DESTROY,
882
      $this->checkTargetIsMoon() && $this->checkHaveReapers()
883
    );
884
  }
885
886
  /**
887
   * @return bool
888
   */
889
  protected function checkMissionHoldPossibleAndReal() {
890
    return $this->checkMissionResultAndUnset(
891
      MT_HOLD,
892
      $this->checkTargetAllyDeposit() && $this->checkMissionHoldOnNotNoob() && $this->checkNotOnlySpies()
893
    );
894
  }
895
896
  /**
897
   * @return bool
898
   */
899
  protected function checkSpiesOnlyFriendlyRestrictsToRelocate() {
900
    if ($result = $this->checkSpiesOnly()) {
901
      $this->fleet->allowed_missions = array(
902
        MT_RELOCATE => $this->fleet->exists_missions[MT_RELOCATE],
903
      );
904
    }
905
906
    return $result;
907
  }
908
909
910
  protected function checkFleetGroupACS() {
911
    $result = !empty($this->fleet->group_id) && !empty($this->fleet->acs);
912
    $this->unsetMission(MT_ACS, $result, true);
913
    if ($result) {
914
      $this->fleet->mission_type = MT_ACS;
915
    } else {
916
      $this->fleet->group_id = 0;
917
    }
918
919
    return $result;
920
  }
921
922
  protected function checkACSNotEmpty() {
923
    return !empty($this->fleet->acs);
924
  }
925
926
  /**
927
   * @return bool
928
   */
929
  protected function checkACSInvited() {
930
    $playersInvited = !empty($this->fleet->acs['eingeladen']) ? explode(',', $this->fleet->acs['eingeladen']) : array();
931
    foreach ($playersInvited as $playerId) {
932
      if (intval($playerId) == $this->fleet->dbOwnerRow['id']) {
933
        return true;
934
      }
935
    }
936
937
    return false;
938
  }
939
940
  /**
941
   * @return bool
942
   */
943
  protected function checkMissionACSPossibleAndReal() {
944
    return $this->checkMissionResultAndUnset(
945
      MT_ACS,
946
      $this->checkACSNotEmpty() && $this->checkACSInvited() && $this->checkACSInTime(),
947
      true
948
    );
949
  }
950
951
  /**
952
   * @return bool
953
   */
954
  protected function checkMissionTransportPossibleAndReal() {
955
    return $this->checkMissionResultAndUnset(
956
      MT_TRANSPORT,
957
      $this->checkCargo() && $this->checkPlayerInactiveOrNotNoob() && $this->checkNotOnlySpies()
958
    );
959
  }
960
961
  /**
962
   * @return bool
963
   */
964
  protected function checkMissionTransportReal() {
965
    return
966
      $this->checkMissionExact(MT_TRANSPORT)
967
      &&
968
      $this->checkRealFlight();
969
  }
970
971
972
  protected function checkBashingNotRestricted() {
973
    return classSupernova::$config->fleet_bashing_attacks <= 0;
974
  }
975
976
  protected function checkBashingBothAllies() {
977
    return $this->fleet->dbOwnerRow['ally_id'] && $this->fleet->dbTargetOwnerRow['ally_id'];
978
  }
979
980
  protected function checkBashingAlliesHaveRelationWar() {
981
    return ali_relation($this->fleet->dbOwnerRow['ally_id'], $this->fleet->dbTargetOwnerRow['ally_id']) == ALLY_DIPLOMACY_WAR;
982
  }
983
984
  protected function checkBashingBothAlliesAndRelationWar() {
985
    return $this->checkBashingBothAllies() && $this->checkBashingAlliesHaveRelationWar();
986
  }
987
988
  protected function checkBashingAlliesWarNoDelay() {
989
    $user = $this->fleet->dbOwnerRow;
990
    $enemy = $this->fleet->dbTargetOwnerRow;
991
992
    $relations = ali_relations($user['ally_id'], $enemy['ally_id']);
993
994
    return SN_TIME_NOW - $relations[$enemy['ally_id']]['alliance_diplomacy_time'] > classSupernova::$config->fleet_bashing_war_delay;
995
  }
996
997
998
  protected function checkBashingNone() {
999
    $user = $this->fleet->dbOwnerRow;
1000
1001
    $time_limit = SN_TIME_NOW + $this->fleet->travelData['duration'] - classSupernova::$config->fleet_bashing_scope;
1002
    $bashing_list = array(SN_TIME_NOW);
1003
1004
    // Retrieving flying fleets
1005
    $objFleetsBashing = FleetList::dbGetFleetListBashing($user['id'], $this->fleet->dbTargetRow);
1006 View Code Duplication
    foreach ($objFleetsBashing->_container as $fleetBashing) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1007
      // Checking for ACS - each ACS count only once
1008
      if ($fleetBashing->group_id) {
1009
        $bashing_list["{$user['id']}_{$fleetBashing->group_id}"] = $fleetBashing->time_arrive_to_target;
1010
      } else {
1011
        $bashing_list[] = $fleetBashing->time_arrive_to_target;
1012
      }
1013
    }
1014
1015
    // Check for joining to ACS - if there are already fleets in ACS no checks should be done
1016
    if ($this->fleet->mission_type == MT_ACS && $bashing_list["{$user['id']}_{$this->fleet->group_id}"]) {
1017
      return true;
1018
    }
1019
1020
    $query = DBStaticFleetBashing::db_bashing_list_get($user, $this->fleet->dbTargetRow, $time_limit);
1021
    while ($bashing_row = db_fetch($query)) {
1022
      $bashing_list[] = $bashing_row['bashing_time'];
1023
    }
1024
1025
    sort($bashing_list);
1026
1027
    $last_attack = 0;
1028
    $wave = 0;
1029
    $attack = 1;
1030
    foreach ($bashing_list as &$bash_time) {
1031
      $attack++;
1032
      if (
1033
        $bash_time - $last_attack > classSupernova::$config->fleet_bashing_interval
1034
        ||
1035
        $attack > classSupernova::$config->fleet_bashing_attacks
1036
      ) {
1037
        $wave++;
1038
        $attack = 1;
1039
      }
1040
1041
      $last_attack = $bash_time;
1042
    }
1043
1044
    return $wave <= classSupernova::$config->fleet_bashing_waves;
1045
  }
1046
1047
}
1048