Completed
Push — work-fleets ( feefd8...38b5dd )
by SuperNova.WS
06:06
created

FleetValidator::unsetMissionColonize()   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
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
  /**
503
   * Check mission type OR no mission - and limits available missions to this type if positive
504
   *
505
   * @param int $missionType
506
   *
507
   * @return bool
508
   */
509
  protected function forceMission($missionType) {
510
    $result = !$this->fleet->mission_type || $this->fleet->mission_type == $missionType;
511 View Code Duplication
    if ($result) {
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...
512
      $this->fleet->allowed_missions = array(
513
        $missionType => $this->fleet->exists_missions[$missionType],
514
      );
515
    } else {
516
      unset($this->fleet->allowed_missions[$missionType]);
517
    }
518
519
    return $result;
520
  }
521
522
  /**
523
   * @return bool
524
   */
525
  protected function forceMissionExplore() {
526
    return $this->forceMission(MT_EXPLORE);
527
  }
528
529
  /**
530
   * @return bool
531
   */
532
  protected function forceMissionColonize() {
533
    return $this->forceMission(MT_COLONIZE);
534
  }
535
536
  /**
537
   * @return bool
538
   */
539
  protected function forceMissionRecycle() {
540
    return $this->forceMission(MT_RECYCLE);
541
  }
542
543
  /**
544
   * @return bool
545
   */
546
  protected function forceMissionMissile() {
547
    return $this->forceMission(MT_MISSILE);
548
  }
549
550
  /**
551
   * @return bool
552
   */
553
  protected function forceMissionSpy() {
554
    return $this->forceMission(MT_SPY);
555
  }
556
557
  /**
558
   * Just checks mission type
559
   *
560
   * @param int $missionType
561
   *
562
   * @return bool
563
   */
564
  protected function checkMissionExact($missionType) {
565
    return $this->fleet->mission_type == $missionType;
566
  }
567
568
569
  /**
570
   * @return bool
571
   */
572
  protected function checkNotEmptyMission() {
573
    return !empty($this->fleet->mission_type);
574
  }
575
576
  /**
577
   * @return bool
578
   */
579
  protected function checkMissionRelocate() {
580
    return $this->checkMissionExact(MT_RELOCATE);
581
  }
582
583
  /**
584
   * @return bool
585
   */
586
  protected function checkMissionExactHold() {
587
    return $this->checkMissionExact(MT_HOLD);
588
  }
589
590
  /**
591
   * @return bool
592
   */
593
  protected function checkMissionTransport() {
594
    return $this->checkMissionExact(MT_TRANSPORT);
595
  }
596
597
  /**
598
   * @return bool
599
   */
600
  protected function checkRealFlight() {
601
    return $this->fleet->isRealFlight;
602
  }
603
604
605
  /**
606
   * @return bool
607
   */
608
  protected function unsetMissionSpyComplex() {
609
    unset($this->fleet->allowed_missions[MT_SPY]);
610
    if ($this->fleet->mission_type == MT_SPY) {
611
      if ($this->checkRealFlight()) {
612
        return false;
613
      }
614
      $this->fleet->mission_type = MT_NONE;
615
    }
616
617
    return true;
618
  }
619
620
621
  /**
622
   * @return bool
623
   */
624
  protected function checkMissionExists() {
625
    return !empty($this->fleet->exists_missions[$this->fleet->mission_type]);
626
  }
627
628
  /**
629
   * @return bool
630
   */
631
  protected function checkMissionAllowed() {
632
    return !empty($this->fleet->allowed_missions[$this->fleet->mission_type]);
633
  }
634
635
  /**
636
   * @return bool
637
   */
638
  protected function checkPlayerInactiveOrNotNoob() {
639
    return
640
      $this->checkTargetNotActive()
641
      ||
642
      $this->checkTargetNotNoob();
643
  }
644
645
  /**
646
   * @return bool
647
   */
648
  protected function checkTargetActive() {
649
    return
650
      empty($this->fleet->dbTargetOwnerRow['onlinetime'])
651
      ||
652
      SN_TIME_NOW - $this->fleet->dbTargetOwnerRow['onlinetime'] >= PLAYER_TIME_ACTIVE_SECONDS;
653
  }
654
655
  /**
656
   * @return bool
657
   */
658
  // TODO - REDO MAIN FUNCTION
659
  protected function checkTargetNotActive() {
660
    return !$this->checkTargetActive();
661
  }
662
663
664
  /**
665
   * @return bool
666
   */
667
  protected function checkSameAlly() {
668
    return !empty($this->fleet->dbTargetOwnerRow['ally_id']) && $this->fleet->dbTargetOwnerRow['ally_id'] == $this->fleet->dbOwnerRow['ally_id'];
669
  }
670
671
  /**
672
   * @return bool
673
   */
674
  protected function checkTargetNoob() {
675
    $user_points = $this->fleet->dbTargetOwnerRow['total_points'];
676
    $enemy_points = $this->fleet->dbTargetOwnerRow['total_points'];
677
678
    return
679
      // Target is under Noob Protection but Fleet owner is not
680
      (
681
        classSupernova::$config->game_noob_points
682
        &&
683
        $enemy_points <= classSupernova::$config->game_noob_points
684
        &&
685
        $user_points > classSupernova::$config->game_noob_points
686
      ) || (
687
        classSupernova::$config->game_noob_factor
688
        &&
689
        $user_points > $enemy_points * classSupernova::$config->game_noob_factor
690
      );
691
  }
692
693
  /**
694
   * @return bool
695
   */
696
  // TODO - REDO MAIN FUNCTION
697
  protected function checkTargetNotNoob() {
698
    return !$this->checkTargetNoob();
699
  }
700
701
702
  /**
703
   * @return bool
704
   */
705
  protected function checkMissionHoldReal() {
706
    return
707
      $this->checkRealFlight()
708
      &&
709
      $this->checkMissionExactHold();
710
  }
711
712
  /**
713
   * @return bool
714
   */
715
  protected function checkMissionHoldOnNotNoob() {
716
    return
717
      $this->checkTargetNotActive()
718
      ||
719
      ($this->checkSameAlly() && classSupernova::$config->ally_help_weak)
720
      ||
721
      $this->checkTargetNotNoob();
722
  }
723
724
725
  // Missiles
726
727
  /**
728
   * @return bool
729
   */
730
  protected function checkOnlyAttackMissiles() {
731
    $missilesAttack = $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERPLANET);
732
733
    return $missilesAttack != 0 && $missilesAttack == $this->fleet->shipsGetTotal();
734
  }
735
736
  /**
737
   * @return bool
738
   */
739
  protected function checkSiloLevel() {
740
    $sn_data_mip = get_unit_param(UNIT_DEF_MISSILE_INTERPLANET);
741
742
    return mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, STRUC_SILO) >= $sn_data_mip[P_REQUIRE][STRUC_SILO];
743
  }
744
745
  /**
746
   * @return bool
747
   */
748
  protected function checkSameGalaxy() {
749
    return $this->fleet->targetVector->galaxy == $this->fleet->dbSourcePlanetRow['galaxy'];
750
  }
751
752
  /**
753
   * @return bool
754
   */
755
  protected function checkMissileDistance() {
756
    return abs($this->fleet->dbSourcePlanetRow['system'] - $this->fleet->targetVector->system) <= flt_get_missile_range($this->fleet->dbOwnerRow);
757
  }
758
759
  /**
760
   * @return bool
761
   */
762
  protected function checkMissileTarget() {
763
    return empty($this->fleet->targetedUnitId) || in_array($this->fleet->targetedUnitId, sn_get_groups('defense_active'));
764
  }
765
766
767
  /**
768
   * @return int
769
   */
770
  protected function checkExpeditionsMax() {
771
    return get_player_max_expeditons($this->fleet->dbOwnerRow) > 0;
772
  }
773
774
  /**
775
   * @return bool
776
   */
777
  protected function checkExpeditionsFree() {
778
    return get_player_max_expeditons($this->fleet->dbOwnerRow) > FleetList::fleet_count_flying($this->fleet->dbOwnerRow['id'], MT_EXPLORE);
779
  }
780
781
  /**
782
   * @return bool
783
   */
784
  protected function checkCaptainSent() {
785
    return $this->fleet->captainId >= 1;
786
  }
787
788
  /**
789
   * @return bool
790
   */
791
  protected function checkCaptainExists() {
792
    return !empty($this->fleet->captain) && is_array($this->fleet->captain);
793
  }
794
795
  /**
796
   * @return bool
797
   */
798
  protected function checkCaptainOnPlanet() {
799
    return $this->fleet->captain['unit_location_type'] == LOC_PLANET;
800
  }
801
802
  /**
803
   * @return bool
804
   */
805
  protected function checkCaptainNotRelocating() {
806
    if ($this->fleet->mission_type == MT_RELOCATE) {
807
      $arriving_captain = mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbTargetRow, UNIT_CAPTAIN, true);
808
    } else {
809
      $arriving_captain = false;
810
    }
811
812
    return empty($arriving_captain) || !is_array($arriving_captain);
813
  }
814
815
816
  /**
817
   * @return bool
818
   */
819
  protected function checkMissionDestroyReal() {
820
    return
821
      $this->checkRealFlight()
822
      &&
823
      $this->checkMissionExact(MT_DESTROY);
824
  }
825
826
  /**
827
   * @return bool
828
   */
829
  protected function checkHaveReapers() {
830
    $unitsTyped = 0;
831
    foreach (sn_get_groups('flt_reapers') as $unit_id) {
832
      $unitsTyped += $this->fleet->shipsGetTotalById($unit_id);
833
    }
834
835
    return $unitsTyped >= 1;
836
  }
837
838
839
  /**
840
   * @return bool
841
   */
842
  protected function checkMissionACSReal() {
843
    return
844
      $this->checkRealFlight()
845
      &&
846
      $this->checkMissionExact(MT_ACS);
847
  }
848
849
  protected function checkACSInTime() {
850
    return $this->fleet->acs['ankunft'] - $this->fleet->time_launch >= $this->fleet->travelData['duration'];
851
  }
852
853
854
  protected function checkMissionRealAndSelected($missionType) {
855
    return
856
      $this->checkRealFlight()
857
      &&
858
      $this->checkMissionExact($missionType);
859
  }
860
861
  protected function unsetMission($missionType, $result, $restrictToMission = false) {
862 View Code Duplication
    if (!$result) {
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...
863
      unset($this->fleet->allowed_missions[$missionType]);
864
    } elseif ($restrictToMission) {
865
      $this->fleet->allowed_missions = array(
866
        $missionType => $this->fleet->exists_missions[$missionType],
867
      );
868
    }
869
  }
870
871
  protected function unsetMissionColonize() {
872
    $this->unsetMission(MT_COLONIZE, false);
873
  }
874
875
  protected function unsetMissionRecycle() {
876
    $this->unsetMission(MT_RECYCLE, false);
877
  }
878
879
  protected function unsetMissionMissile() {
880
    $this->unsetMission(MT_MISSILE, false);
881
  }
882
883
  protected function unsetMissionExplore() {
884
    $this->unsetMission(MT_EXPLORE, false);
885
  }
886
887
  protected function unsetMissionSpy() {
888
    $this->unsetMission(MT_SPY, false);
889
  }
890
891
  protected function checkMissionResultAndUnset($missionType, $result, $forceMission = false) {
892
    $this->unsetMission($missionType, $result, $forceMission);
893
894
    return $result && $this->checkMissionRealAndSelected($missionType);
895
  }
896
897
898
  /**
899
   * @return bool
900
   */
901
  protected function checkMissionSpyPossibleAndReal() {
902
    return $this->checkMissionResultAndUnset(
903
      MT_SPY,
904
      $this->checkSpiesOnly() && $this->checkTargetOther(),
905
      true
906
    );
907
  }
908
909
  /**
910
   * @return bool
911
   */
912
  protected function checkMissionDestroyAndReal() {
913
    return $this->checkMissionResultAndUnset(
914
      MT_DESTROY,
915
      $this->checkTargetIsMoon() && $this->checkHaveReapers()
916
    );
917
  }
918
919
  /**
920
   * @return bool
921
   */
922
  protected function checkMissionHoldPossibleAndReal() {
923
    return $this->checkMissionResultAndUnset(
924
      MT_HOLD,
925
      $this->checkTargetAllyDeposit() && $this->checkMissionHoldOnNotNoob() && $this->checkNotOnlySpies()
926
    );
927
  }
928
929
  /**
930
   * @return bool
931
   */
932
  protected function checkSpiesOnlyFriendlyRestrictsToRelocate() {
933
    if ($result = $this->checkSpiesOnly()) {
934
      $this->fleet->allowed_missions = array(
935
        MT_RELOCATE => $this->fleet->exists_missions[MT_RELOCATE],
936
      );
937
    }
938
939
    return $result;
940
  }
941
942
943
  protected function checkFleetGroupACS() {
944
    $result = !empty($this->fleet->group_id) && !empty($this->fleet->acs);
945
    $this->unsetMission(MT_ACS, $result, true);
946
    if ($result) {
947
      $this->fleet->mission_type = MT_ACS;
948
    } else {
949
      $this->fleet->group_id = 0;
950
    }
951
952
    return $result;
953
  }
954
955
  protected function checkACSNotEmpty() {
956
    return !empty($this->fleet->acs);
957
  }
958
959
  /**
960
   * @return bool
961
   */
962
  protected function checkACSInvited() {
963
    $playersInvited = !empty($this->fleet->acs['eingeladen']) ? explode(',', $this->fleet->acs['eingeladen']) : array();
964
    foreach ($playersInvited as $playerId) {
965
      if (intval($playerId) == $this->fleet->dbOwnerRow['id']) {
966
        return true;
967
      }
968
    }
969
970
    return false;
971
  }
972
973
  /**
974
   * @return bool
975
   */
976
  protected function checkMissionACSPossibleAndReal() {
977
    return $this->checkMissionResultAndUnset(
978
      MT_ACS,
979
      $this->checkACSNotEmpty() && $this->checkACSInvited() && $this->checkACSInTime(),
980
      true
981
    );
982
  }
983
984
  /**
985
   * @return bool
986
   */
987
  protected function checkMissionAttack() {
988
    return $this->checkMissionExact(MT_ATTACK);
989
  }
990
991
  /**
992
   * @return bool
993
   */
994
  protected function checkMissionExactSpy() {
995
    return $this->checkMissionExact(MT_SPY);
996
  }
997
998
  /**
999
   * @return bool
1000
   */
1001
  protected function checkMissionTransportPossibleAndReal() {
1002
    return $this->checkMissionResultAndUnset(
1003
      MT_TRANSPORT,
1004
      $this->checkCargo() && $this->checkPlayerInactiveOrNotNoob() && $this->checkNotOnlySpies()
1005
    );
1006
  }
1007
1008
  /**
1009
   * @return bool
1010
   */
1011
  protected function checkMissionTransportReal() {
1012
    return
1013
      $this->checkMissionTransport()
1014
      &&
1015
      $this->checkRealFlight();
1016
  }
1017
1018
1019
  protected function checkBashingNotRestricted() {
1020
    return classSupernova::$config->fleet_bashing_attacks <= 0;
1021
  }
1022
1023
  protected function checkBashingBothAllies() {
1024
    return $this->fleet->dbOwnerRow['ally_id'] && $this->fleet->dbTargetOwnerRow['ally_id'];
1025
  }
1026
1027
  protected function checkBashingAlliesHaveRelationWar() {
1028
    return ali_relation($this->fleet->dbOwnerRow['ally_id'], $this->fleet->dbTargetOwnerRow['ally_id']) == ALLY_DIPLOMACY_WAR;
1029
  }
1030
1031
  protected function checkBashingBothAlliesAndRelationWar() {
1032
    return $this->checkBashingBothAllies() && $this->checkBashingAlliesHaveRelationWar();
1033
  }
1034
1035
  protected function checkBashingAlliesWarNoDelay() {
1036
    $user = $this->fleet->dbOwnerRow;
1037
    $enemy = $this->fleet->dbTargetOwnerRow;
1038
1039
    $relations = ali_relations($user['ally_id'], $enemy['ally_id']);
1040
1041
    return SN_TIME_NOW - $relations[$enemy['ally_id']]['alliance_diplomacy_time'] > classSupernova::$config->fleet_bashing_war_delay;
1042
  }
1043
1044
1045
  protected function checkBashingNone() {
1046
    $user = $this->fleet->dbOwnerRow;
1047
1048
    $time_limit = SN_TIME_NOW + $this->fleet->travelData['duration'] - classSupernova::$config->fleet_bashing_scope;
1049
    $bashing_list = array(SN_TIME_NOW);
1050
1051
    // Retrieving flying fleets
1052
    $objFleetsBashing = FleetList::dbGetFleetListBashing($user['id'], $this->fleet->dbTargetRow);
1053 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...
1054
      // Checking for ACS - each ACS count only once
1055
      if ($fleetBashing->group_id) {
1056
        $bashing_list["{$user['id']}_{$fleetBashing->group_id}"] = $fleetBashing->time_arrive_to_target;
1057
      } else {
1058
        $bashing_list[] = $fleetBashing->time_arrive_to_target;
1059
      }
1060
    }
1061
1062
    // Check for joining to ACS - if there are already fleets in ACS no checks should be done
1063
    if ($this->fleet->mission_type == MT_ACS && $bashing_list["{$user['id']}_{$this->fleet->group_id}"]) {
1064
      return true;
1065
    }
1066
1067
    $query = DBStaticFleetBashing::db_bashing_list_get($user, $this->fleet->dbTargetRow, $time_limit);
1068
    while ($bashing_row = db_fetch($query)) {
1069
      $bashing_list[] = $bashing_row['bashing_time'];
1070
    }
1071
1072
    sort($bashing_list);
1073
1074
    $last_attack = 0;
1075
    $wave = 0;
1076
    $attack = 1;
1077
    foreach ($bashing_list as &$bash_time) {
1078
      $attack++;
1079
      if (
1080
        $bash_time - $last_attack > classSupernova::$config->fleet_bashing_interval
1081
        ||
1082
        $attack > classSupernova::$config->fleet_bashing_attacks
1083
      ) {
1084
        $wave++;
1085
        $attack = 1;
1086
      }
1087
1088
      $last_attack = $bash_time;
1089
    }
1090
1091
    return $wave <= classSupernova::$config->fleet_bashing_waves;
1092
  }
1093
1094
}
1095