Completed
Push — trunk ( 8dcff2...338765 )
by SuperNova.WS
04:11
created

RecordFleet::getResourceList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 07.12.2017 14:38
4
 */
5
6
namespace Fleet;
7
8
9
use Core\GlobalContainer;
10
use DBAL\ActiveRecord;
11
12
/**
13
 * Class RecordFleet
14
 * @package Fleet
15
 *
16
 * property int|string $fleet_id                 - bigint     -
17
 * @property int|string $fleet_owner              - bigint     -
18
 * @property int        $fleet_mission            - int        -
19
 * @property int|string $fleet_amount             - bigint     -
20
 * @property string     $fleet_array              - mediumtext -
21
 * @property int        $fleet_start_time         - int        -
22
 * @property int|string $fleet_start_planet_id    - bigint     -
23
 * @property int        $fleet_start_galaxy       - int        -
24
 * @property int        $fleet_start_system       - int        -
25
 * @property int        $fleet_start_planet       - int        -
26
 * @property int        $fleet_start_type         - int        -
27
 * @property int        $fleet_end_time           - int        -
28
 * @property int        $fleet_end_stay           - 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        $fleet_mess               - int        -
40
 * @property int        $start_time               - int        -
41
 *
42
 */
43
class RecordFleet extends ActiveRecord {
44
  protected static $_primaryIndexField = 'fleet_id';
45
46
  protected static $_tableName = 'fleets';
47
48
//  /**
49
//   * Information about ships
50
//   *
51
//   * @var array[] $shipInfo
52
//   */
53
//  protected static $shipInfo = [];
54
55
  /**
56
   * List of fleet ships
57
   *
58
   * @var float[] $shipList
59
   */
60
  protected $shipList = [];
61
62
  /**
63
   * @var float[] $resources
64
   */
65
  protected $resources = [
66
    RES_METAL     => 0,
67
    RES_CRYSTAL   => 0,
68
    RES_DEUTERIUM => 0,
69
  ];
70
71
  /**
72
   * RecordFleet constructor.
73
   *
74
   * @param GlobalContainer|null $services
75
   */
76
  public function __construct(GlobalContainer $services = null) {
77
    parent::__construct($services);
78
79
//    if(empty(static::$shipInfo)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
80
//      foreach(sn_get_groups('fleet') as $unit_id) {
81
//        static::$shipInfo[$unit_id] = get_unit_param($unit_id);
82
//        static::$shipInfo[$unit_id][P_COST_METAL] = get_unit_cost_in(static::$shipInfo[$unit_id][P_COST]);
83
//      }
84
//    }
85
  }
86
87
  /**
88
   * @inheritdoc
89
   */
90
  protected function fromProperties(array $properties) {
91
    parent::fromProperties($properties);
92
93
    $this->shipList = !empty($this->fleet_array) ? sys_unit_str2arr($this->fleet_array) : [];
94
95
    $this->resources = [
96
      RES_METAL     => !empty($this->fleet_resource_metal) ? floatval($this->fleet_resource_metal) : 0,
97
      RES_CRYSTAL   => !empty($this->fleet_resource_crystal) ? floatval($this->fleet_resource_crystal) : 0,
98
      RES_DEUTERIUM => !empty($this->fleet_resource_deuterium) ? floatval($this->fleet_resource_deuterium) : 0,
99
    ];
100
  }
101
102
  /**
103
   * @inheritdoc
104
   */
105
  public function update() {
106
    if($this->getShipCount() < 1) {
107
      return $this->delete();
108
    } else {
109
      return parent::update();
110
    }
111
  }
112
113
//  /**
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
114
//   * @param int $shipId
115
//   *
116
//   * @return int|float
117
//   */
118
//  public function getShipCostInMetal($shipId) {
119
//    return !empty(static::$shipInfo[$shipId][P_COST_METAL]) ? static::$shipInfo[$shipId][P_COST_METAL] : 0;
120
//  }
121
//
122
//  /**
123
//   * Get fleet cost in metal
124
//   *
125
//   * @return float|int
126
//   */
127
//  public function getCostInMetal() {
128
//    $result = 0;
129
//    foreach($this->shipList as $shipId => $amount) {
130
//      $result += $amount * $this->getShipCostInMetal($shipId);
131
//    }
132
//
133
//    return $result;
134
//  }
135
//
136
//  /**
137
//   * @param int $shipId
138
//   *
139
//   * @return int|mixed
140
//   */
141
//  public function getShipCapacity($shipId) {
142
//    return !empty(static::$shipInfo[$shipId][P_CAPACITY]) ? static::$shipInfo[$shipId][P_CAPACITY] : 0;
143
//  }
144
//
145
//  /**
146
//   * @return float|int
147
//   */
148
//  public function getCapacity() {
149
//    $result = 0;
150
//    foreach($this->shipList as $shipId => $amount) {
151
//      $result += $amount * $this->getShipCapacity($shipId);
152
//    }
153
//
154
//    $result = max(0, $result - array_sum($this->resources));
155
//
156
//    return $result;
157
//  }
158
159
  /**
160
   * @param int   $shipSnId
161
   * @param float $shipCount
162
   *
163
   * @throws \Exception
164
   */
165
  public function changeShipCount($shipSnId, $shipCount) {
166
    !isset($this->shipList[$shipSnId]) ? $this->shipList[$shipSnId] = 0 : false;
167
168
    $shipCount = floor($shipCount);
169
170
    if($this->shipList[$shipSnId] + $shipCount < 0) {
171
      throw new \Exception("Trying to deduct more ships [{$shipSnId}] '{$shipCount}' when fleet has only {$this->shipList[$shipSnId]}");
172
    }
173
174
    $this->shipList[$shipSnId] += $shipCount;
175
    if($this->shipList[$shipSnId] < 1) {
176
      unset($this->shipList[$shipSnId]);
177
    }
178
179
    $this->fleet_array = sys_unit_arr2str($this->shipList);
180
    $this->fleet_amount = $this->getShipCount();
181
  }
182
183
  /**
184
   * @param int   $resourceId
185
   * @param float $resourceCount
186
   *
187
   * @throws \Exception
188
   */
189
  public function changeResource($resourceId, $resourceCount) {
190
    if(!array_key_exists($resourceId, $this->resources) || empty($resourceCount)) {
191
      return;
192
    }
193
194
    $resourceCount = ceil($resourceCount);
195
196
    if($this->resources[$resourceId] + $resourceCount < 0) {
197
      throw new \Exception("Trying to deduct more resources [{$resourceId}] '{$resourceCount}' when fleet has only {$this->resources[$resourceId]}");
198
    }
199
200
    $this->resources[$resourceId] += $resourceCount;
201
202
    $this->fleet_resource_metal = $this->resources[RES_METAL];
203
    $this->fleet_resource_crystal = $this->resources[RES_CRYSTAL];
204
    $this->fleet_resource_deuterium = $this->resources[RES_DEUTERIUM];
205
  }
206
207
  public function getShipCount() {
208
    return array_sum($this->getShipList());
209
  }
210
211
212
213
214
  public function isEmpty() {
215
    return array_sum($this->getShipList()) >= 1 && array_sum($this->getResourceList()) >= 1;
216
  }
217
218
  // Getters/Setters ---------------------------------------------------------------------------------------------------
219
  /**
220
   * @return float[] - [shipSnId => $shipAmount]
221
   */
222
  public function getShipList() {
223
    return $this->shipList;
224
  }
225
226
  /**
227
   * @return float[] - [$resourceSnId => $resourceAmount]
228
   */
229
  public function getResourceList() {
230
    return $this->resources;
231
  }
232
233
}
234