Completed
Push — work-fleets ( c97fe0...81e6e5 )
by SuperNova.WS
05:36
created

FleetValidator::validate()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 11
nc 7
nop 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
34
      // 2nd level restrictions
35
      // Still cheap
36
      $this->restrict2ToAllowedMissions();
37
      $this->restrict2ToAllowedPlanetTypes();
38
    } catch (Exception $e) {
39
      if ($e->getCode() != FLIGHT_ALLOWED) {
40
        pdie(classLocale::$lang['fl_attack_error'][$e->getCode()]);
41
      } else {
42
        pdump('FLIGHT_ALLOWED', FLIGHT_ALLOWED);
43
      }
44
    }
45
  }
46
47
  /**
48
   * @param array $checklist
49
   *
50
   * @throws Exception
51
   */
52
  public function checkMissionRestrictions($checklist) {
53
    foreach ($checklist as $condition => $action) {
54
      $checkResult = call_user_func(array($this, $condition));
55
56
      if (is_array($action) && !empty($action[$checkResult])) {
57
        $action = $action[$checkResult];
58
      }
59
60
      if (is_array($action)) {
61
        $this->checkMissionRestrictions($action);
62
      } elseif (!$checkResult) {
63
        throw new Exception($action, $action);
64
      }
65
    }
66
  }
67
68
69
70
71
72
73
74
75
76
77
  /**
78
   * @throws Exception
79
   */
80
  protected function restrict2ToAllowedMissions() {
81
    if (empty($this->fleet->allowed_missions[$this->fleet->mission_type])) {
82
      throw new Exception('FLIGHT_MISSION_IMPOSSIBLE', FLIGHT_MISSION_IMPOSSIBLE);
83
    }
84
  }
85
86
  /**
87
   * @throws Exception
88
   */
89
  protected function restrict2ToAllowedPlanetTypes() {
90
    if (empty($this->fleet->allowed_planet_types[$this->fleet->targetVector->type])) {
91
      throw new Exception('FLIGHT_MISSION_IMPOSSIBLE', FLIGHT_MISSION_IMPOSSIBLE);
92
    }
93
  }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  /**
108
   * @return bool
109
   */
110
  protected function checkSpeedPercentOld() {
111
    return in_array($this->fleet->oldSpeedInTens, array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
112
  }
113
114
  /**
115
   * @return bool
116
   */
117
  protected function checkSenderNoVacation() {
118
    return empty($this->fleet->dbOwnerRow['vacation']) || $this->fleet->dbOwnerRow['vacation'] >= SN_TIME_NOW;
119
  }
120
121
  /**
122
   * @return bool
123
   */
124
  protected function checkTargetNoVacation() {
125
    return empty($this->fleet->dbTargetOwnerRow['vacation']) || $this->fleet->dbTargetOwnerRow['vacation'] >= SN_TIME_NOW;
126
  }
127
128
  /**
129
   * @return bool
130
   */
131
  protected function checkMultiAccount() {
132
    return sys_is_multiaccount($this->fleet->dbOwnerRow, $this->fleet->dbTargetOwnerRow);
133
  }
134
135
  /**
136
   * @return bool
137
   */
138
  protected function checkTargetNotSource() {
139
    return !$this->fleet->targetVector->isEqualToPlanet($this->fleet->dbSourcePlanetRow);
140
  }
141
142
  /**
143
   * @return bool
144
   */
145
  protected function checkTargetInUniverse() {
146
    return $this->fleet->targetVector->isInUniverse();
147
  }
148
149
  /**
150
   * @return bool
151
   */
152
  protected function checkUnitsPositive() {
153
    return $this->fleet->shipsAllPositive();
154
  }
155
156
  /**
157
   * @return bool
158
   */
159
  protected function checkOnlyFleetUnits() {
160
    return $this->fleet->shipsAllFlying();
161
  }
162
163
  /**
164
   * @return bool
165
   */
166
  protected function checkOnlyFlyingUnits() {
167
    return $this->fleet->shipsAllMovable();
168
  }
169
170
  /**
171
   * @return bool
172
   */
173
  protected function checkEnoughFleetSlots() {
174
    return FleetList::fleet_count_flying($this->fleet->getPlayerOwnerId()) < GetMaxFleets($this->fleet->dbOwnerRow);
175
  }
176
177
178
179
180
    /**
181
   * @return bool
182
   */
183
  protected function checkEnoughCapacity($includeResources = true) {
184
    $checkVia = $this->fleet->travelData['consumption'];
185
    $checkVia = ceil(($includeResources ? array_sum($this->fleet->resource_list) : 0) + $checkVia);
186
187
    return
188
      !empty($this->fleet->travelData) &&
189
      is_array($this->fleet->travelData) &&
190
      floor($this->fleet->travelData['capacity']) >= $checkVia;
191
  }
192
193
  /**
194
   * @return bool
195
   */
196
  protected function checkNotTooFar() {
197
    return $this->checkEnoughCapacity(false);
198
  }
199
200
  /**
201
   * @return bool
202
   */
203
  protected function checkDebrisExists() {
204
    return is_array($this->fleet->dbTargetRow) && ($this->fleet->dbTargetRow['debris_metal'] + $this->fleet->dbTargetRow['debris_crystal'] > 0);
205
  }
206
207
208
209
210
211
212
213
214
215
216
217
218
  // Resources checks
219
220
  /**
221
   * @return bool
222
   */
223
  protected function checkResourcesPositive() {
224
    foreach ($this->fleet->resource_list as $resourceId => $resourceAmount) {
225
      if ($resourceAmount < 0) {
226
        return false;
227
      }
228
    }
229
230
    return true;
231
  }
232
233
  /**
234
   * @return bool
235
   */
236
  protected function checkCargo() {
237
    return array_sum($this->fleet->resource_list) >= 1;
238
  }
239
240
  /**
241
   * @return bool
242
   */
243
  protected function checkSourceEnoughFuel() {
244
    $deuteriumOnPlanet = mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, RES_DEUTERIUM);
245
246
    return $deuteriumOnPlanet < ceil($this->fleet->travelData['consumption']);
247
  }
248
249
  /**
250
   * @return bool
251
   */
252
  protected function checkSourceEnoughResources() {
253
    $fleetResources = $this->fleet->resource_list;
254
    $fleetResources[RES_DEUTERIUM] = ceil($fleetResources[RES_DEUTERIUM] + $this->fleet->travelData['consumption']);
255
    foreach ($fleetResources as $resourceId => $resourceAmount) {
256
      if (mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, $resourceId) < ceil($fleetResources[$resourceId])) {
257
        return false;
258
      }
259
    }
260
261
    return true;
262
  }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  // 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...
289
290
  /**
291
   * @return bool
292
   */
293
  protected function checkKnownSpace() {
294
    return $this->fleet->targetVector->isInKnownSpace();
295
  }
296
297
  /**
298
   * @return bool
299
   */
300
  protected function checkTargetExists() {
301
    return !empty($this->fleet->dbTargetRow['id']);
302
  }
303
304
  /**
305
   * @return bool
306
   */
307
  protected function checkTargetIsPlanet() {
308
    return $this->fleet->targetVector->type == PT_PLANET;
309
  }
310
311
  /**
312
   * @return bool
313
   */
314
  protected function checkTargetIsDebris() {
315
    return $this->fleet->targetVector->type == PT_DEBRIS;
316
  }
317
318
319
320
321
322
323
  // Ships checks
324
325
  /**
326
   * @return bool
327
   */
328
  protected function checkFleetNotEmpty() {
329
    return $this->fleet->shipsGetTotal() >= 1;
330
  }
331
332
333
  /**
334
   * @return bool
335
   */
336
  protected function checkSourceEnoughShips() {
337
    return $this->fleet->shipsIsEnoughOnPlanet();
338
  }
339
340
341
  /**
342
   * @return bool
343
   */
344
  protected function checkHaveColonizer() {
345
    // Colonization fleet should have at least one colonizer
346
    return $this->fleet->shipsGetTotalById(SHIP_COLONIZER) >= 1;
347
  }
348
349
  /**
350
   * @return bool
351
   */
352
  protected function checkHaveRecyclers() {
353
    $recyclers = 0;
354
    foreach (sn_get_groups('flt_recyclers') as $recycler_id) {
355
      $recyclers += $this->fleet->shipsGetTotalById($recycler_id);
356
    }
357
358
    return $recyclers >= 1;
359
  }
360
361
  /**
362
   * @return bool
363
   */
364
  protected function checkSpiesOnly() {
365
    return $this->fleet->shipsGetTotalById(SHIP_SPY) == $this->fleet->shipsGetTotal();
366
  }
367
368
  /**
369
   * @return bool
370
   */
371
  protected function checkNotOnlySpies() {
372
    return !$this->checkSpiesOnly();
373
  }
374
375
  /**
376
   * @return bool
377
   */
378
  protected function checkNoMissiles() {
379
    return
380
      $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERPLANET) == 0
381
      &&
382
      $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERCEPTOR) == 0;
383
  }
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  /**
398
   * @return bool
399
   */
400
  protected function checkTargetOwn() {
401
    return $this->fleet->dbTargetRow['id_owner'] == $this->fleet->dbSourcePlanetRow['id_owner'];
402
  }
403
404
  /**
405
   * @return bool
406
   */
407
  protected function forceTargetOwn() {
408
    if ($result = $this->checkTargetOwn()) {
409
      // Spying can't be done on owner's planet/moon
410
      unset($this->fleet->allowed_missions[MT_SPY]);
411
      // Attack can't be done on owner's planet/moon
412
      unset($this->fleet->allowed_missions[MT_ATTACK]);
413
      // ACS can't be done on owner's planet/moon
414
      unset($this->fleet->allowed_missions[MT_ACS]);
415
      // Destroy can't be done on owner's moon
416
      unset($this->fleet->allowed_missions[MT_DESTROY]);
417
      unset($this->fleet->allowed_missions[MT_MISSILE]);
418
    } else {
419
      // Relocate can be done only on owner's planet/moon
420
      unset($this->fleet->allowed_missions[MT_RELOCATE]);
421
422
    }
423
424
    return $result; // this->getPlayerOwnerId();
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
425
  }
426
427
  /**
428
   * @return bool
429
   */
430
  protected function checkTargetOther() {
431
    return !$this->checkTargetOwn();
432
  }
433
434
435
  /**
436
   * @return bool
437
   */
438
  protected function alwaysFalse() {
439
    return false;
440
  }
441
442
443
  /**
444
   * @return bool
445
   */
446
  protected function checkTargetAllyDeposit() {
447
    $result = mrc_get_level($this->fleet->dbTargetOwnerRow, $this->fleet->dbTargetRow, STRUC_ALLY_DEPOSIT) >= 1;
448
    if (!$result) {
449
      unset($this->fleet->allowed_missions[MT_HOLD]);
450
    }
451
452
    return $result;
453
  }
454
455
456
  /**
457
   * Forces missions that can flight to OWN planets
458
   *
459
   * @return bool
460
   */
461
  protected function forceMissionsOwn() {
462
    $result =
463
      !$this->fleet->mission_type
464
      ||
465
      in_array($this->fleet->mission_type, array(
466
        MT_HOLD,
467
        MT_RECYCLE,
468
        MT_RELOCATE,
469
        MT_TRANSPORT,
470
      ));
471
472
    unset($this->fleet->allowed_missions[MT_ATTACK]);
473
    unset($this->fleet->allowed_missions[MT_COLONIZE]);
474
    unset($this->fleet->allowed_missions[MT_EXPLORE]);
475
    unset($this->fleet->allowed_missions[MT_ACS]);
476
    unset($this->fleet->allowed_missions[MT_SPY]);
477
    unset($this->fleet->allowed_missions[MT_DESTROY]);
478
    unset($this->fleet->allowed_missions[MT_MISSILE]);
479
480
    return $result;
481
  }
482
483
  /**
484
   * Check mission type OR no mission - and limits available missions to this type if positive
485
   *
486
   * @param int $missionType
487
   *
488
   * @return bool
489
   */
490
  protected function forceMission($missionType) {
491
    $result = !$this->fleet->mission_type || $this->fleet->mission_type == $missionType;
492
    if ($result) {
493
      $this->fleet->allowed_missions = array(
494
        $missionType => $this->fleet->exists_missions[$missionType],
495
      );
496
    } else {
497
      unset($this->fleet->allowed_missions[$missionType]);
498
    }
499
500
    return $result;
501
  }
502
503
  /**
504
   * @return bool
505
   */
506
  protected function forceMissionExplore() {
507
    return $this->forceMission(MT_EXPLORE);
508
  }
509
510
  /**
511
   * @return bool
512
   */
513
  protected function forceMissionColonize() {
514
    return $this->forceMission(MT_COLONIZE);
515
  }
516
517
  /**
518
   * @return bool
519
   */
520
  protected function forceMissionRecycle() {
521
    return $this->forceMission(MT_RECYCLE);
522
  }
523
524
  /**
525
   * @return bool
526
   */
527
  protected function forceMissionMissile() {
528
    return $this->forceMission(MT_MISSILE);
529
  }
530
531
  /**
532
   * Just checks mission type
533
   *
534
   * @param int $missionType
535
   *
536
   * @return bool
537
   */
538
  protected function checkMissionNonRestrict($missionType) {
539
    return $this->fleet->mission_type == $missionType;
540
  }
541
542
543
  /**
544
   * @return bool
545
   */
546
  protected function checkNotEmptyMission() {
547
    return !empty($this->fleet->mission_type);
548
  }
549
550
  /**
551
   * @return bool
552
   */
553
  protected function checkMissionRelocate() {
554
    return $this->checkMissionNonRestrict(MT_RELOCATE);
555
  }
556
557
  /**
558
   * @return bool
559
   */
560
  protected function checkMissionHoldNonUnique() {
561
    $result = $this->checkMissionNonRestrict(MT_HOLD);
562
563
    return $result;
564
  }
565
566
  /**
567
   * @return bool
568
   */
569
  protected function checkMissionTransport() {
570
    return $this->checkMissionNonRestrict(MT_TRANSPORT);
571
  }
572
573
  /**
574
   * @return bool
575
   */
576
  protected function checkMissionTransportReal() {
577
    return
578
      $this->checkRealFlight()
579
      &&
580
      $this->checkMissionTransport();
581
  }
582
583
584
  /**
585
   * @return bool
586
   */
587
  protected function forceMissionSpy() {
588
    return $this->forceMission(MT_SPY);
589
  }
590
591
  /**
592
   * @return bool
593
   */
594
  protected function checkRealFlight() {
595
    return $this->fleet->isRealFlight;
596
  }
597
598
599
  /**
600
   * @return bool
601
   */
602
  protected function unsetMissionSpyComplex() {
603
    unset($this->fleet->allowed_missions[MT_SPY]);
604
    if ($this->fleet->mission_type == MT_SPY) {
605
      if ($this->checkRealFlight()) {
606
        return false;
607
      }
608
      $this->fleet->mission_type = MT_NONE;
609
    }
610
611
    return true;
612
  }
613
614
615
  /**
616
   * @return bool
617
   */
618
  protected function checkMissionExists() {
619
    return !empty($this->fleet->exists_missions[$this->fleet->mission_type]);
620
  }
621
622
  /**
623
   * @return bool
624
   */
625
  protected function checkPlayerInactiveOrNotNoob() {
626
    return
627
      $this->checkTargetNotActive()
628
      ||
629
      $this->checkTargetNotNoob();
630
  }
631
632
  /**
633
   * @return bool
634
   */
635
  protected function checkTargetActive() {
636
    return
637
      empty($this->fleet->dbTargetOwnerRow['onlinetime'])
638
      ||
639
      SN_TIME_NOW - $this->fleet->dbTargetOwnerRow['onlinetime'] >= PLAYER_TIME_ACTIVE_SECONDS;
640
  }
641
642
  /**
643
   * @return bool
644
   */
645
  // TODO - REDO MAIN FUNCTION
646
  protected function checkTargetNotActive() {
647
    return !$this->checkTargetActive();
648
  }
649
650
651
  /**
652
   * @return bool
653
   */
654
  protected function checkSameAlly() {
655
    return !empty($this->fleet->dbTargetOwnerRow['ally_id']) && $this->fleet->dbTargetOwnerRow['ally_id'] == $this->fleet->dbOwnerRow['ally_id'];
656
  }
657
658
  /**
659
   * @return bool
660
   */
661
  protected function checkTargetNoob() {
662
    $user_points = $this->fleet->dbTargetOwnerRow['total_points'];
663
    $enemy_points = $this->fleet->dbTargetOwnerRow['total_points'];
664
665
    return
666
      // Target is under Noob Protection but Fleet owner is not
667
      (
668
        classSupernova::$config->game_noob_points
669
        &&
670
        $enemy_points <= classSupernova::$config->game_noob_points
671
        &&
672
        $user_points > classSupernova::$config->game_noob_points
673
      ) || (
674
        classSupernova::$config->game_noob_factor
675
        &&
676
        $user_points > $enemy_points * classSupernova::$config->game_noob_factor
677
      );
678
  }
679
680
  /**
681
   * @return bool
682
   */
683
  // TODO - REDO MAIN FUNCTION
684
  protected function checkTargetNotNoob() {
685
    return !$this->checkTargetNoob();
686
  }
687
688
689
  /**
690
   * @return bool
691
   */
692
  protected function checkMissionHoldReal() {
693
    return
694
      $this->checkRealFlight()
695
      &&
696
      $this->checkMissionHoldNonUnique();
697
  }
698
699
  /**
700
   * @return bool
701
   */
702
  protected function checkMissionHoldOnNotNoob() {
703
    return
704
      $this->checkTargetNotActive()
705
      ||
706
      ($this->checkSameAlly() && classSupernova::$config->ally_help_weak)
707
      ||
708
      $this->checkTargetNotNoob();
709
  }
710
711
712
  // Missiles
713
714
  /**
715
   * @return bool
716
   */
717
  protected function checkOnlyAttackMissiles() {
718
    $missilesAttack = $this->fleet->shipsGetTotalById(UNIT_DEF_MISSILE_INTERPLANET);
719
720
    return $missilesAttack != 0 && $missilesAttack == $this->fleet->shipsGetTotal();
721
  }
722
723
  /**
724
   * @return bool
725
   */
726
  protected function checkSiloLevel() {
727
    $sn_data_mip = get_unit_param(UNIT_DEF_MISSILE_INTERPLANET);
728
729
    return mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbSourcePlanetRow, STRUC_SILO) >= $sn_data_mip[P_REQUIRE][STRUC_SILO];
730
  }
731
732
  /**
733
   * @return bool
734
   */
735
  protected function checkSameGalaxy() {
736
    return $this->fleet->targetVector->galaxy == $this->fleet->dbSourcePlanetRow['galaxy'];
737
  }
738
739
  /**
740
   * @return bool
741
   */
742
  protected function checkMissileDistance() {
743
    return abs($this->fleet->dbSourcePlanetRow['system'] - $this->fleet->targetVector->system) <= flt_get_missile_range($this->fleet->dbOwnerRow);
744
  }
745
746
  /**
747
   * @return bool
748
   */
749
  protected function checkMissileTarget() {
750
    return empty($this->fleet->targetedUnitId) || in_array($this->fleet->targetedUnitId, sn_get_groups('defense_active'));
751
  }
752
753
754
  /**
755
   * @return int
756
   */
757
  protected function checkExpeditionsMax() {
758
    return get_player_max_expeditons($this->fleet->dbOwnerRow);
759
  }
760
761
  /**
762
   * @return bool
763
   */
764
  protected function checkExpeditionsFree() {
765
    return get_player_max_expeditons($this->fleet->dbOwnerRow) > FleetList::fleet_count_flying($this->fleet->dbOwnerRow['id'], MT_EXPLORE);
766
  }
767
768
  /**
769
   * @return bool
770
   */
771
  protected function checkCaptainSent() {
772
    return $this->fleet->captainId >= 1;
773
  }
774
775
  /**
776
   * @return bool
777
   */
778
  protected function checkCaptainExists() {
779
    return !empty($this->fleet->captain) && is_array($this->fleet->captain);
780
  }
781
782
  /**
783
   * @return bool
784
   */
785
  protected function checkCaptainOnPlanet() {
786
    return $this->fleet->captain['unit_location_type'] == LOC_PLANET;
787
  }
788
789
  /**
790
   * @return bool
791
   */
792
  protected function checkCaptainNotRelocating() {
793
    if($this->fleet->mission_type == MT_RELOCATE) {
794
      $arriving_captain = mrc_get_level($this->fleet->dbOwnerRow, $this->fleet->dbTargetRow, UNIT_CAPTAIN, true);
795
    } else {
796
      $arriving_captain = false;
797
    }
798
799
    return empty($arriving_captain) || !is_array($arriving_captain);
800
  }
801
802
}
803