Passed
Push — trunk ( 338765...433e05 )
by SuperNova.WS
04:23
created

Fleet::calcShipLossByMultiplier()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6666
ccs 0
cts 7
cp 0
crap 6
1
<?php
2
/**
3
 * Created by Gorlum 18.04.2018 16:30
4
 */
5
6
namespace Fleet;
7
8
9
use Core\EntityDb;
10
11
/**
12
 * Class Fleet
13
 * @package Fleet
14
 *
15
 * @property int|string $id                       - bigint     -
16
 * @property int|string $ownerId                  - bigint     - Fleet player owner ID
17
 * @property int        $fleet_mission            - int        -
18
 * @property int|string $fleet_amount             - bigint     -
19
 * @property string     $fleet_array              - mediumtext -
20
 * @property int        $timeLaunch               - int        - Fleet launched from source planet (unix)
21
 * @property int        $timeArrive               - int        - Time fleet arrive to destination (unix)
22
 * @property int        $timeEndStay              - int        - Time when fleet operation on destination complete (if any) (unix)
23
 * @property int        $timeReturn               - int        - Time fleet would return to source planet (unix)
24
 * @property int|string $fleet_start_planet_id    - bigint     -
25
 * @property int        $fleet_start_galaxy       - int        -
26
 * @property int        $fleet_start_system       - int        -
27
 * @property int        $fleet_start_planet       - int        -
28
 * @property int        $fleet_start_type         - int        -
29
 * @property int|string $fleet_end_planet_id      - bigint     -
30
 * @property int        $fleet_end_galaxy         - int        -
31
 * @property int        $fleet_end_system         - int        -
32
 * @property int        $fleet_end_planet         - int        -
33
 * @property int        $fleet_end_type           - int        -
34
 * @property int|string $fleet_resource_metal     - decimal    -
35
 * @property int|string $fleet_resource_crystal   - decimal    -
36
 * @property int|string $fleet_resource_deuterium - decimal    -
37
 * @property int|string $fleet_target_owner       - int        -
38
 * @property int|string $fleet_group              - varchar    -
39
 * @property int        $status                   - int        - Current fleet status: flying to destination; returning
40
 */
41
class Fleet extends EntityDb {
42
43
  /**
44
   * @var string $_activeClass
45
   */
46
  protected $_activeClass = '\\Fleet\\RecordFleet';
47
48
  /**
49
   * @var RecordFleet $_container
50
   */
51
  protected $_container;
52
53
  protected $_containerTranslateNames = [
54
    'ownerId' => 'fleet_owner',
55
56
    'timeLaunch'  => 'start_time',
57
    'timeArrive'  => 'fleet_start_time',
58
    'timeEndStay' => 'fleet_end_stay',
59
    'timeReturn'  => 'fleet_end_time',
60
61
    'status' => 'fleet_mess',
62
  ];
63
64
  /**
65
   * Information about ships
66
   *
67
   * @var array[] $shipInfo
68
   */
69
  protected static $shipInfo = [];
70
71
72
  /**
73
   * Fleet constructor.
74
   *
75
   * @throws \Exception
76
   */
77
  public function __construct() {
78
    parent::__construct();
79
  }
80
81
  // Real fleet actions ------------------------------------------------------------------------------------------------
82
83
  /**
84
   * Forced return fleet to source planet
85
   *
86
   * @param int|string $byPlayerId
87
   *
88
   * @return bool
89
   */
90
  public function returnForce($byPlayerId) {
91
    if ($this->ownerId != $byPlayerId) {
92
      return false;
93
    }
94
95
    if ($this->status == FLEET_STATUS_RETURNING) {
96
      return true;
97
    }
98
99
//    $ReturnFlyingTime = ($this->timeEndStay != 0 && $this->timeArrive < SN_TIME_NOW ? $this->timeArrive : SN_TIME_NOW) - $this->timeLaunch + SN_TIME_NOW + 1;
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
100
    $timeToReturn = SN_TIME_NOW - $this->timeLaunch + 1;
101
    $ReturnFlyingTime = (!empty($this->timeEndStay) && $this->timeArrive < SN_TIME_NOW ? $this->timeArrive : SN_TIME_NOW) + $timeToReturn;
102
103
    // TODO - Those two lines should be removed - fleet times should be filtered on interface side
104
    $this->timeArrive = SN_TIME_NOW;
105
    !empty($this->timeEndStay) ? $this->timeEndStay = SN_TIME_NOW : false;
106
107
    $this->timeReturn = $ReturnFlyingTime;
108
    $this->status = FLEET_STATUS_RETURNING;
109
110
    return $this->dbUpdate();
111
  }
112
113
  // Service functions  -----------------------------------------------------------------------------------------------
114
  /**
115
   * @return RecordFleet
116
   */
117
  public function _getContainer() {
118
    return $this->_container;
119
  }
120
121
  /**
122
   * @param int $shipId
123
   *
124
   * @return array
125
   */
126
  protected static function getUnitInfo($shipId) {
127
    if (!isset(static::$shipInfo[$shipId])) {
128
      static::$shipInfo[$shipId] = get_unit_param($shipId);
129
    }
130
131
    return static::$shipInfo[$shipId];
132
  }
133
134
  /**
135
   * @param int $resourceId
136
   *
137
   * @return float[] - [(int)$shipId => (float)costInMetal]
138
   */
139
  public function getShipsBasicCosts($resourceId = RES_METAL) {
140
    $result = [];
141
    foreach ($this->getShipList() as $shipId => $shipAmount) {
142
      $result[$shipId] = getStackableUnitsCost([$shipId => 1], $resourceId);
143
    }
144
145
    return $result;
146
  }
147
148
  /**
149
   * Get cost of single ship in metal
150
   *
151
   * @param int $shipId
152
   *
153
   * @return int|float
154
   */
155
  public function getShipCostInMetal($shipId) {
156
    return getStackableUnitsCost([$shipId => 1], RES_METAL);
157
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
158
//    if(!isset(static::getUnitInfo($shipId)[P_COST_METAL])) {
159
//      static::$shipInfo[$shipId][P_COST_METAL] = get_unit_cost_in(static::getUnitInfo($shipId)[P_COST], RES_METAL);
160
//    }
161
//
162
//    return static::getUnitInfo($shipId)[P_COST_METAL];
163
  }
164
165
  /**
166
   * Get fleet cost in metal
167
   *
168
   * @return float|int
169
   */
170
  public function getCostInMetal() {
171
    return getStackableUnitsCost($this->getShipList(), RES_METAL);
172
//
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...
173
//    $result = 0;
174
//    foreach($this->getShipList() as $shipId => $amount) {
175
//      $result += $amount * $this->getShipCostInMetal($shipId);
176
//    }
177
//
178
//    return $result;
179
  }
180
181
  /**
182
   * Get single ship basic capacity
183
   *
184
   * @param int $shipId
185
   *
186
   * @return int|mixed
187
   */
188
  public function getShipCapacity($shipId) {
189
    if (!isset(static::getUnitInfo($shipId)[P_CAPACITY])) {
190
      static::$shipInfo[$shipId][P_CAPACITY] = 0;
191
    }
192
193
    return static::getUnitInfo($shipId)[P_CAPACITY];
194
  }
195
196
  /**
197
   * Get current fleet capacity counting loaded resources and fuel
198
   *
199
   * @return float|int
200
   */
201
  public function getCapacityActual() {
202
    $result = 0;
203
    foreach ($this->getShipList() as $shipId => $amount) {
204
      $result += $amount * $this->getShipCapacity($shipId);
205
    }
206
207
    $result = max(0, $result - array_sum($this->getResourceList()));
208
209
    return $result;
210
  }
211
212
  public function isEmpty() {
213
    return parent::isEmpty();
214
  }
215
216
  // Using RecordFleet functions ---------------------------------------------------------------------------------------
217
218
  /**
219
   * @param int   $shipSnId
220
   * @param float $shipCount
221
   *
222
   * @throws \Exception
223
   */
224
  public function changeShipCount($shipSnId, $shipCount) {
225
    $this->_getContainer()->changeShipCount($shipSnId, $shipCount);
226
  }
227
228
  /**
229
   * @param int   $resourceId
230
   * @param float $resourceCount
231
   *
232
   * @throws \Exception
233
   */
234
  public function changeResource($resourceId, $resourceCount) {
235
    $this->_getContainer()->changeResource($resourceId, $resourceCount);
236
  }
237
238
  /**
239
   * @return float[] - [shipSnId => $shipAmount]
240
   */
241
  public function getShipList() {
242
    return $this->_getContainer()->getShipList();
243
  }
244
245
  /**
246
   * @return float[] - [$resourceSnId => $resourceAmount]
247
   */
248
  public function getResourceList() {
249
    return $this->_getContainer()->getResourceList();
250
  }
251
252
  /**
253
   * @return float|int
254
   */
255
  public function getShipCount() {
256
    return $this->_getContainer()->getShipCount();
257
  }
258
259
  /**
260
   * @param float $multiplier
261
   *
262
   * @return int[]|float[]
263
   */
264
  public function calcShipLossByMultiplier($multiplier) {
265
    $result = [];
266
267
    foreach ($this->getShipList() as $unit_id => $unit_amount) {
268
      $shipsLost = ceil($unit_amount * $multiplier);
269
      $result[$unit_id] += $shipsLost;
270
    }
271
272
    return $result;
273
  }
274
275
}
276