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
|
|
|
* List of fleet ships |
50
|
|
|
* |
51
|
|
|
* @var float[] $shipList |
52
|
|
|
*/ |
53
|
|
|
protected $shipList = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var float[] $resources |
57
|
|
|
*/ |
58
|
|
|
protected $resources = [ |
59
|
|
|
RES_METAL => 0, |
60
|
|
|
RES_CRYSTAL => 0, |
61
|
|
|
RES_DEUTERIUM => 0, |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* RecordFleet constructor. |
66
|
|
|
* |
67
|
|
|
* @param GlobalContainer|null $services |
68
|
|
|
*/ |
69
|
|
|
public function __construct(GlobalContainer $services = null) { |
70
|
|
|
parent::__construct($services); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
protected function fromProperties(array $properties) { |
77
|
|
|
parent::fromProperties($properties); |
78
|
|
|
|
79
|
|
|
$this->shipList = !empty($this->fleet_array) ? sys_unit_str2arr($this->fleet_array) : []; |
80
|
|
|
|
81
|
|
|
$this->resources = [ |
82
|
|
|
RES_METAL => !empty($this->fleet_resource_metal) ? floatval($this->fleet_resource_metal) : 0, |
83
|
|
|
RES_CRYSTAL => !empty($this->fleet_resource_crystal) ? floatval($this->fleet_resource_crystal) : 0, |
84
|
|
|
RES_DEUTERIUM => !empty($this->fleet_resource_deuterium) ? floatval($this->fleet_resource_deuterium) : 0, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function update() { |
92
|
|
|
if($this->getShipCount() < 1) { |
93
|
|
|
return $this->delete(); |
94
|
|
|
} else { |
95
|
|
|
return parent::update(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// /** |
|
|
|
|
100
|
|
|
// * @param int $shipId |
101
|
|
|
// * |
102
|
|
|
// * @return int|float |
103
|
|
|
// */ |
104
|
|
|
// public function getShipCostInMetal($shipId) { |
105
|
|
|
// return !empty(static::$shipInfo[$shipId][P_COST_METAL]) ? static::$shipInfo[$shipId][P_COST_METAL] : 0; |
106
|
|
|
// } |
107
|
|
|
// |
108
|
|
|
// /** |
109
|
|
|
// * Get fleet cost in metal |
110
|
|
|
// * |
111
|
|
|
// * @return float|int |
112
|
|
|
// */ |
113
|
|
|
// public function getCostInMetal() { |
114
|
|
|
// $result = 0; |
115
|
|
|
// foreach($this->shipList as $shipId => $amount) { |
116
|
|
|
// $result += $amount * $this->getShipCostInMetal($shipId); |
117
|
|
|
// } |
118
|
|
|
// |
119
|
|
|
// return $result; |
120
|
|
|
// } |
121
|
|
|
// |
122
|
|
|
// /** |
123
|
|
|
// * @param int $shipId |
124
|
|
|
// * |
125
|
|
|
// * @return int|mixed |
126
|
|
|
// */ |
127
|
|
|
// public function getShipCapacity($shipId) { |
128
|
|
|
// return !empty(static::$shipInfo[$shipId][P_CAPACITY]) ? static::$shipInfo[$shipId][P_CAPACITY] : 0; |
129
|
|
|
// } |
130
|
|
|
// |
131
|
|
|
// /** |
132
|
|
|
// * @return float|int |
133
|
|
|
// */ |
134
|
|
|
// public function getCapacity() { |
135
|
|
|
// $result = 0; |
136
|
|
|
// foreach($this->shipList as $shipId => $amount) { |
137
|
|
|
// $result += $amount * $this->getShipCapacity($shipId); |
138
|
|
|
// } |
139
|
|
|
// |
140
|
|
|
// $result = max(0, $result - array_sum($this->resources)); |
141
|
|
|
// |
142
|
|
|
// return $result; |
143
|
|
|
// } |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int $shipSnId |
147
|
|
|
* @param float $shipCount |
148
|
|
|
* |
149
|
|
|
* @throws \Exception |
150
|
|
|
*/ |
151
|
|
|
public function changeShipCount($shipSnId, $shipCount) { |
152
|
|
|
!isset($this->shipList[$shipSnId]) ? $this->shipList[$shipSnId] = 0 : false; |
153
|
|
|
|
154
|
|
|
$shipCount = floor($shipCount); |
155
|
|
|
|
156
|
|
|
if($this->shipList[$shipSnId] + $shipCount < 0) { |
157
|
|
|
throw new \Exception("Trying to deduct more ships [{$shipSnId}] '{$shipCount}' when fleet [{$this->id}] has only {$this->shipList[$shipSnId]}"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->shipList[$shipSnId] += $shipCount; |
161
|
|
|
if($this->shipList[$shipSnId] < 1) { |
162
|
|
|
unset($this->shipList[$shipSnId]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->fleet_array = sys_unit_arr2str($this->shipList); |
166
|
|
|
$this->fleet_amount = $this->getShipCount(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param int $resourceId |
171
|
|
|
* @param float $resourceCount |
172
|
|
|
* |
173
|
|
|
* @throws \Exception |
174
|
|
|
*/ |
175
|
|
|
public function changeResource($resourceId, $resourceCount) { |
176
|
|
|
if(!array_key_exists($resourceId, $this->resources) || empty($resourceCount)) { |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$resourceCount = ceil($resourceCount); |
181
|
|
|
|
182
|
|
|
if($this->resources[$resourceId] + $resourceCount < 0) { |
183
|
|
|
throw new \Exception("Trying to deduct more resources [{$resourceId}] '{$resourceCount}' when fleet [{$this->id}] has only {$this->resources[$resourceId]}"); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->resources[$resourceId] += $resourceCount; |
187
|
|
|
|
188
|
|
|
$this->fleet_resource_metal = $this->resources[RES_METAL]; |
189
|
|
|
$this->fleet_resource_crystal = $this->resources[RES_CRYSTAL]; |
190
|
|
|
$this->fleet_resource_deuterium = $this->resources[RES_DEUTERIUM]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
|
195
|
|
|
public function isEmpty() { |
196
|
|
|
return $this->getShipCount() < 1 && $this->getResourceCount() < 1; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getShipCount() { |
200
|
|
|
return array_sum($this->getShipList()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// Getters/Setters --------------------------------------------------------------------------------------------------- |
204
|
|
|
/** |
205
|
|
|
* @return float[] - [shipSnId => $shipAmount] |
206
|
|
|
*/ |
207
|
|
|
public function getShipList() { |
208
|
|
|
return $this->shipList; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return float|int |
213
|
|
|
*/ |
214
|
|
|
public function getResourceCount() { |
215
|
|
|
return array_sum($this->getResourceList()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return float[] - [$resourceSnId => $resourceAmount] |
220
|
|
|
*/ |
221
|
|
|
public function getResourceList() { |
222
|
|
|
return $this->resources; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
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.