Completed
Push — work-fleets ( 13a233...1735a9 )
by SuperNova.WS
05:19
created

FleetValidator::checkMultiAccountNot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

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