Passed
Push — trunk ( 3e6278...a0c74d )
by SuperNova.WS
03:55
created

MissionEspionageReport::getAntiSpyDiff()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 4
rs 10
ccs 0
cts 4
cp 0
crap 12
1
<?php
2
/**
3
 * Created by Gorlum 12.10.2017 13:10
4
 */
5
6
namespace Fleet;
7
8
use Common\Traits\TJsonSerializable;
9
10
11
/**
12
 * Class MissionEspionageReport
13
 *
14
 * Result of MISSION_SPY
15
 *
16
 * @package Fleet
17
 */
18
class MissionEspionageReport {
19
  use TJsonSerializable;
20
21
  const SIMULATOR_GROUPS = [UNIT_SHIPS, UNIT_DEFENCE];
22
  const SIMULATOR_UNITS = [TECH_WEAPON, TECH_SHIELD, TECH_ARMOR, RES_METAL, RES_CRYSTAL, RES_DEUTERIUM];
23
24
  /**
25
   * Actual report time
26
   *
27
   * @var float $reportTime
28
   */
29
  public $reportTime = 0.0;
30
  /**
31
   * Fleet arrival time - i.e. when report supported to be made
32
   * Can differ from actual spy time due to delays in fleet dispatcher routines
33
   *
34
   * @var int $fleetTime
35
   */
36
  public $fleetTime = 0;
37
38
  public $attackerPlayerId = 0;
39
  public $attackerPlayerName = '';
40
  public $attackerPlayerAllyTag = '';
41
  public $attackerPlanetId = 0;
42
  public $attackerPlanetName = '';
43
  public $attackerPlanetGalaxy = 0;
44
  public $attackerPlanetSystem = 0;
45
  public $attackerPlanetPlanet = 0;
46
  public $attackerPlanetPlanetType = PT_NONE;
47
48
  public $targetPlayerId = 0;
49
  public $targetPlayerName = '';
50
  public $targetPlayerAllyTag = '';
51
  public $targetPlanetId = 0;
52
  public $targetPlanetName = '';
53
  public $targetPlanetGalaxy = 0;
54
  public $targetPlanetSystem = 0;
55
  public $targetPlanetPlanet = 0;
56
  public $targetPlanetPlanetType = PT_NONE;
57
58
  public $targetSpyLevel = 0;
59
  public $attackerSpyLevel = 0;
60
61
  public $fleetUnits = [];
62
63
  public $spiedUnits = [];
64
65
  private $simulatorLink = '';
66
67
  /**
68
   * Chance for target to detect spying fleet
69
   *
70
   * @var null|float $detectionChance
71
   */
72
//  public $detectionChance = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
73
  public $rolledChance = null;
74
75
  public $enemyShips = 0;
76
77
  /**
78
   * MissionEspionageReport constructor.
79
   *
80
   * @param MissionData $missionData
81
   */
82
  public function __construct(MissionData $missionData) {
83
    $this->reportTime = microtime(true);
84
    $this->fleetTime = $missionData->fleet['fleet_end_time'];
85
86
    $this->attackerPlayerId = $missionData->src_user['id'];
87
    $this->attackerPlayerName = $missionData->src_user['username'];
88
    $this->attackerPlayerAllyTag = $missionData->src_user['ally_tag'];
89
    $this->attackerPlanetId = $missionData->src_planet['id'];
90
    $this->attackerPlanetName = $missionData->src_planet['name'];
91
    $this->attackerPlanetGalaxy = intval($missionData->src_planet['galaxy']);
92
    $this->attackerPlanetSystem = intval($missionData->src_planet['system']);
93
    $this->attackerPlanetPlanet = intval($missionData->src_planet['planet']);
94
    $this->attackerPlanetPlanetType = intval($missionData->src_planet['planet_type']);
95
96
    $this->targetPlayerId = $missionData->dst_user['id'];
97
    $this->targetPlayerName = $missionData->dst_user['username'];
98
    $this->targetPlayerAllyTag = $missionData->dst_user['ally_tag'];
99
    $this->targetPlanetId = $missionData->dst_planet['id'];
100
    $this->targetPlanetName = $missionData->dst_planet['name'];
101
    $this->targetPlanetGalaxy = intval($missionData->dst_planet['galaxy']);
102
    $this->targetPlanetSystem = intval($missionData->dst_planet['system']);
103
    $this->targetPlanetPlanet = intval($missionData->dst_planet['planet']);
104
    $this->targetPlanetPlanetType = intval($missionData->dst_planet['planet_type']);
105
106
    $this->targetSpyLevel = intval(GetSpyLevel($missionData->dst_user));
107
    $this->attackerSpyLevel = intval(GetSpyLevel($missionData->src_user));
108
109
    $this->fleetUnits = sys_unit_str2arr($missionData->fleet['fleet_array']);
110
111
    $this->spiedUnits[RES_METAL] = floor($missionData->dst_planet['metal']);
112
    $this->spiedUnits[RES_CRYSTAL] = floor($missionData->dst_planet['crystal']);
113
    $this->spiedUnits[RES_DEUTERIUM] = floor($missionData->dst_planet['deuterium']);
114
    $this->spiedUnits[RES_ENERGY] = floor($missionData->dst_planet['energy_max']);
115
116
    $this->enemyShips = 0;
117
    foreach (sn_get_groups('fleet') as $unit_id) {
118
      $this->enemyShips += max(0, mrc_get_level($missionData->dst_user, $missionData->dst_planet, $unit_id, false, true));
119
    }
120
121
  }
122
123
124
  public function getEmpireSpyDiff() {
125
    return $this->attackerSpyLevel - $this->targetSpyLevel;
126
  }
127
128
  /**
129
   * @return float|int
130
   */
131
  public function getProbesNumber() {
132
    return !empty($this->fleetUnits[SHIP_SPY]) && $this->fleetUnits[SHIP_SPY] >= 1 ? floor($this->fleetUnits[SHIP_SPY]) : 0;
133
  }
134
135
  public function getAntiSpyDiff() {
136
    return !empty($this->fleetUnits[SHIP_SATELLITE_SPUTNIK]) && $this->fleetUnits[SHIP_SATELLITE_SPUTNIK] >= 1
137
      ? floor(pow($this->fleetUnits[SHIP_SATELLITE_SPUTNIK], 0.75))
138
      : 0;
139
  }
140
141
  public function getPlanetSpyDiff() {
142
    return $this->getEmpireSpyDiff() + sqrt($this->getProbesNumber()) - 1 - $this->getAntiSpyDiff();
143
  }
144
145
  /**
146
   * @param int       $unitId
147
   * @param int|float $unitAmount
148
   */
149
  public function addUnit($unitId, $unitAmount) {
150
    if (($unitAmount = floor($unitAmount)) >= 1) {
151
      $this->spiedUnits[intval($unitId)] = floor($unitAmount);
152
      $this->simulatorLink = '';
153
    }
154
  }
155
156
  public function getSimulatorLink() {
157
    if (empty($this->simulatorLink)) {
158
      $combat_pack[0] = [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$combat_pack was never initialized. Although not strictly required by PHP, it is generally a good practice to add $combat_pack = array(); before regardless.
Loading history...
159
      foreach ($this->spiedUnits as $unitId => $unitAmount) {
160
        $unitGroup = get_unit_param($unitId, P_UNIT_TYPE);
161
        if (in_array($unitGroup, static::SIMULATOR_GROUPS) || in_array($unitId, static::SIMULATOR_UNITS)) {
162
          $combat_pack[0][$unitId] = $unitAmount;
163
        }
164
      }
165
      $this->simulatorLink = sn_ube_simulator_encode_replay($combat_pack, 'D');
166
    }
167
168
    return $this->simulatorLink;
169
  }
170
171
  /**
172
   * Chance for target to detect spying fleet
173
   *
174
   * @return float|null
175
   */
176
  public function getDetectionTrashold() {
177
    return $this->getProbesNumber() * $this->enemyShips / 4 * pow(2, -$this->getEmpireSpyDiff());
178
  }
179
180
  public function rollChance() {
181
    if ($this->rolledChance === null) {
182
      $this->rolledChance = mt_rand(0, 99);
183
    }
184
185
    return $this->rolledChance;
186
  }
187
188
  public function isSpyDetected() {
189
    return $this->rollChance() < $this->getDetectionTrashold();
190
//    return $this->getDetectionChance() > 99 || $this->getDetectionTrashold() > $this->getDetectionChance();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
191
  }
192
193
}
194