Issues (1369)

classes/Fleet/MissionEspionageReport.php (3 issues)

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;
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $reportTime is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84
    $this->fleetTime = $missionData->fleet['fleet_end_time'];
85
86
    $this->attackerPlayerId = $missionData->fleetOwnerRow['id'];
87
    $this->attackerPlayerName = $missionData->fleetOwnerRow['username'];
88
    $this->attackerPlayerAllyTag = $missionData->fleetOwnerRow['ally_tag'];
89
    $this->attackerPlanetId = $missionData->srcPlanetRow['id'];
90
    $this->attackerPlanetName = $missionData->srcPlanetRow['name'];
91
    $this->attackerPlanetGalaxy = intval($missionData->srcPlanetRow['galaxy']);
92
    $this->attackerPlanetSystem = intval($missionData->srcPlanetRow['system']);
93
    $this->attackerPlanetPlanet = intval($missionData->srcPlanetRow['planet']);
94
    $this->attackerPlanetPlanetType = intval($missionData->srcPlanetRow['planet_type']);
95
96
    $this->targetPlayerId = $missionData->dstUserRow['id'];
97
    $this->targetPlayerName = $missionData->dstUserRow['username'];
98
    $this->targetPlayerAllyTag = $missionData->dstUserRow['ally_tag'];
99
    $this->targetPlanetId = $missionData->dstPlanetRow['id'];
100
    $this->targetPlanetName = $missionData->dstPlanetRow['name'];
101
    $this->targetPlanetGalaxy = intval($missionData->dstPlanetRow['galaxy']);
102
    $this->targetPlanetSystem = intval($missionData->dstPlanetRow['system']);
103
    $this->targetPlanetPlanet = intval($missionData->dstPlanetRow['planet']);
104
    $this->targetPlanetPlanetType = intval($missionData->dstPlanetRow['planet_type']);
105
106
    $this->targetSpyLevel = intval(GetSpyLevel($missionData->dstUserRow));
107
    $this->attackerSpyLevel = intval(GetSpyLevel($missionData->fleetOwnerRow));
108
109
    $this->fleetUnits = sys_unit_str2arr($missionData->fleet['fleet_array']);
110
111
    $this->spiedUnits[RES_METAL] = floor($missionData->dstPlanetRow['metal']);
112
    $this->spiedUnits[RES_CRYSTAL] = floor($missionData->dstPlanetRow['crystal']);
113
    $this->spiedUnits[RES_DEUTERIUM] = floor($missionData->dstPlanetRow['deuterium']);
114
    $this->spiedUnits[RES_ENERGY] = floor($missionData->dstPlanetRow['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->dstUserRow, $missionData->dstPlanetRow, $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
    $u = ['id' => $this->targetPlayerId];
137
    $p = [
138
      'id'       => $this->targetPlanetId,
139
      'id_owner' => $this->targetPlayerId,
140
    ];
141
    $onPlanet = mrc_get_level($u, $p, SHIP_SATELLITE_SPUTNIK, false, true);
142
143
    return !empty($onPlanet) && $onPlanet >= 1
144
      ? floor(pow($onPlanet, 0.52))
0 ignored issues
show
It seems like $onPlanet can also be of type boolean; however, parameter $base of pow() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
      ? floor(pow(/** @scrutinizer ignore-type */ $onPlanet, 0.52))
Loading history...
145
      : 0;
146
  }
147
148
  public function getPlanetSpyDiff() {
149
    return $this->getEmpireSpyDiff() + sqrt($this->getProbesNumber()) - 1 - $this->getAntiSpyDiff();
150
  }
151
152
  /**
153
   * @param int       $unitId
154
   * @param int|float $unitAmount
155
   */
156
  public function addUnit($unitId, $unitAmount) {
157
    if (($unitAmount = floor($unitAmount)) >= 1) {
158
      $this->spiedUnits[intval($unitId)] = floor($unitAmount);
159
      $this->simulatorLink = '';
160
    }
161
  }
162
163
  public function getSimulatorLink() {
164
    if (empty($this->simulatorLink)) {
165
      $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...
166
      foreach ($this->spiedUnits as $unitId => $unitAmount) {
167
        $unitGroup = get_unit_param($unitId, P_UNIT_TYPE);
168
        if (in_array($unitGroup, static::SIMULATOR_GROUPS) || in_array($unitId, static::SIMULATOR_UNITS)) {
169
          $combat_pack[0][$unitId] = $unitAmount;
170
        }
171
      }
172
      $this->simulatorLink = sn_ube_simulator_encode_replay($combat_pack, 'D');
173
    }
174
175
    return $this->simulatorLink;
176
  }
177
178
  /**
179
   * Chance for target to detect spying fleet
180
   *
181
   * @return float|null
182
   */
183
  public function getDetectionTrashold() {
184
    return $this->getProbesNumber() * $this->enemyShips / 4 * pow(2, -$this->getEmpireSpyDiff());
185
  }
186
187
  public function rollChance() {
188
    if ($this->rolledChance === null) {
189
      $this->rolledChance = mt_rand(0, 99);
190
    }
191
192
    return $this->rolledChance;
193
  }
194
195
  public function isSpyDetected() {
196
    return $this->rollChance() < $this->getDetectionTrashold();
197
//    return $this->getDetectionChance() > 99 || $this->getDetectionTrashold() > $this->getDetectionChance();
198
  }
199
200
}
201