1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 30.09.2017 11:01 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Pages\Deprecated; |
7
|
|
|
|
8
|
|
|
use Planet\DBStaticPlanet; |
9
|
|
|
use \HelperString; |
10
|
|
|
|
11
|
|
|
class PageFleet5Gathering { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \classLocale $lang |
15
|
|
|
*/ |
16
|
|
|
protected $lang; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var float[] $infoTransportCapacity |
20
|
|
|
*/ |
21
|
|
|
protected $infoTransportCapacity; |
22
|
|
|
|
23
|
|
|
public function __construct() { |
24
|
|
|
global $lang; |
|
|
|
|
25
|
|
|
|
26
|
|
|
$this->lang = $lang; |
27
|
|
|
$this->infoTransportCapacity = $this->getTransportUnitsCapacity(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $user |
32
|
|
|
* @param array $planetCurrent |
33
|
|
|
* @param float[][] $resources_taken - [int|string $planetId][int $resourceId] => float $resourceAmount |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function flt_build_gathering(&$user, &$planetCurrent, $resources_taken = []) { |
38
|
|
|
// Caching sn_data names for quick access |
39
|
|
|
$planet_list = []; |
40
|
|
|
|
41
|
|
|
$takeAllResources = !is_array($resources_taken) || empty($resources_taken); |
|
|
|
|
42
|
|
|
if ($takeAllResources) { |
43
|
|
|
$query = ''; |
44
|
|
|
} else { |
45
|
|
|
$query = implode(',', array_keys($resources_taken)); |
46
|
|
|
$query = " AND `destruyed` = 0 AND `id` IN ({$query})"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$planets_db_list = DBStaticPlanet::db_planet_list_sorted($user, $planetCurrent['id'], '*', $query); |
50
|
|
|
!is_array($planets_db_list) ? $planets_db_list = [] : false; |
|
|
|
|
51
|
|
|
|
52
|
|
|
foreach ($planets_db_list as $planet_id => &$planetRecord) { |
53
|
|
|
// begin planet loop |
54
|
|
|
sn_db_transaction_start(); |
55
|
|
|
// Вот тут надо посчитать - отработать очереди и выяснить, сколько ресов на каждой планете |
56
|
|
|
$planetRecord = sys_o_get_updated($user, $planetRecord, SN_TIME_NOW, true); |
57
|
|
|
$planetRecord = $planetRecord['planet']; |
58
|
|
|
sn_db_transaction_commit(); |
59
|
|
|
|
60
|
|
|
if ($takeAllResources) { |
61
|
|
|
$resources_taken[$planet_id] = 1; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$planetResources = $this->calcPlanetResources($user, $resources_taken, $planetRecord); |
65
|
|
|
$fleetCapacityList = $this->calcFleetCapacity($user, $planetRecord); |
66
|
|
|
$fleetFullCapacity = array_sum($fleetCapacityList); |
67
|
|
|
|
68
|
|
|
$fleet = $this->calcShipAmount($fleetCapacityList, min($planetResources, $fleetFullCapacity)); |
69
|
|
|
|
70
|
|
|
$result = ATTACK_NO_FLEET; |
71
|
|
|
$travel_data = null; |
72
|
|
|
if (!empty($fleet)) { |
73
|
|
|
$travel_data = flt_travel_data($user, $planetCurrent, $planetRecord, $fleet, 10); |
74
|
|
|
|
75
|
|
|
if (floor(mrc_get_level($user, $planetRecord, RES_DEUTERIUM, true)) >= $travel_data['consumption']) { |
76
|
|
|
$will_take = min($planetResources, $fleetFullCapacity) - $travel_data['consumption']; |
77
|
|
|
|
78
|
|
|
$resourcesTaken = $this->fillFleetResources($user, $resources_taken, $planetRecord, $will_take, $fleet); |
79
|
|
|
$result = ATTACK_ALLOWED; |
80
|
|
|
} else { |
81
|
|
|
$result = ATTACK_NO_FUEL; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$planet_list[$planet_id] = |
86
|
|
|
[ |
87
|
|
|
'PLANET_DB_DATA' => $planetRecord, |
88
|
|
|
'ID' => $planetRecord['id'], |
89
|
|
|
'NAME' => $planetRecord['name'], |
90
|
|
|
'GALAXY' => $planetRecord['galaxy'], |
91
|
|
|
'SYSTEM' => $planetRecord['system'], |
92
|
|
|
'PLANET' => $planetRecord['planet'], |
93
|
|
|
'TYPE' => $planetRecord['planet_type'], |
94
|
|
|
'TYPE_PRINT' => $this->lang['sys_planet_type'][$planetRecord['planet_type']], |
95
|
|
|
'METAL' => floor($planetRecord['metal']), |
96
|
|
|
'CRYSTAL' => floor($planetRecord['crystal']), |
97
|
|
|
'DEUTERIUM' => floor($planetRecord['deuterium']), |
98
|
|
|
'METAL_TEXT' => HelperString::numberFloorAndFormat($planetRecord['metal']), |
99
|
|
|
'CRYSTAL_TEXT' => HelperString::numberFloorAndFormat($planetRecord['crystal']), |
100
|
|
|
'DEUTERIUM_TEXT' => HelperString::numberFloorAndFormat($planetRecord['deuterium']), |
101
|
|
|
'RESOURCES' => $planetResources, |
102
|
|
|
'RESOURCES_TEXT' => HelperString::numberFloorAndFormat($planetResources), |
103
|
|
|
|
104
|
|
|
'FLEET' => $fleet, |
105
|
|
|
'FLEET_RESOURCES' => $resourcesTaken, |
|
|
|
|
106
|
|
|
'FLEET_CAPACITY' => $fleetFullCapacity, |
107
|
|
|
'FLEET_CAPACITY_TEXT' => prettyNumberStyledCompare($fleetFullCapacity, -$planetResources), |
108
|
|
|
|
109
|
|
|
'RESULT' => $result, |
110
|
|
|
// 'MESSAGE' => $this->lang['fl_attack_error'][$result], |
|
|
|
|
111
|
|
|
] |
112
|
|
|
+ (!empty($travel_data) ? |
113
|
|
|
[ |
114
|
|
|
'FLEET_SPEED' => $travel_data['fleet_speed'], |
115
|
|
|
'DISTANCE' => $travel_data['distance'], |
116
|
|
|
'DURATION' => $travel_data['duration'], |
117
|
|
|
'DURATION_TEXT' => $travel_data['duration'] ? pretty_time($travel_data['duration']) : $this->lang['flt_no_fuel'], |
118
|
|
|
'CONSUMPTION' => $travel_data['consumption'], |
119
|
|
|
] |
120
|
|
|
: []); |
121
|
|
|
} // end planet loop |
122
|
|
|
|
123
|
|
|
return $planet_list; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function getTransportUnitsCapacity() { |
130
|
|
|
$transports = []; |
131
|
|
|
foreach (sn_get_groups('flt_transports') as $transport_id) { |
132
|
|
|
$transports[$transport_id] = get_unit_param($transport_id, P_CAPACITY); |
133
|
|
|
} |
134
|
|
|
arsort($transports); |
135
|
|
|
|
136
|
|
|
return $transports; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param array $user |
141
|
|
|
* @param array $resources_taken |
142
|
|
|
* @param array $planet_db_data |
143
|
|
|
* |
144
|
|
|
* @return float |
145
|
|
|
*/ |
146
|
|
|
protected function calcPlanetResources(&$user, $resources_taken, $planet_db_data) { |
147
|
|
|
$planet_resources = 0; |
148
|
|
|
foreach (sn_get_groups('resources_loot') as $resource_id) { |
149
|
|
|
if ($resources_taken[$planet_db_data['id']] == 1 || $resources_taken[$planet_db_data['id']][$resource_id] > 0) { |
150
|
|
|
$planet_resources += floor(mrc_get_level($user, $planet_db_data, $resource_id, true, true)); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $planet_resources; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $user |
159
|
|
|
* @param array $planet_db_data |
160
|
|
|
* |
161
|
|
|
* @return float[] |
162
|
|
|
*/ |
163
|
|
|
protected function calcFleetCapacity(&$user, $planet_db_data) { |
164
|
|
|
$fleetCapacityList = []; |
165
|
|
|
foreach ($this->infoTransportCapacity as $ship_id => $ship_capacity) { |
166
|
|
|
if (($ship_count = mrc_get_level($user, $planet_db_data, $ship_id, true, true)) > 0) { |
167
|
|
|
$fleetCapacityList[$ship_id] = $ship_count * $ship_capacity; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $fleetCapacityList; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param float[] $fleetCapacityList - List of capacities per ship |
176
|
|
|
* @param float $maxResourcesToTake - Maximum resources that can be taken from this planet with whole transport fleet |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
protected function calcShipAmount($fleetCapacityList, $maxResourcesToTake) { |
181
|
|
|
$fleet = []; |
182
|
|
|
foreach ($fleetCapacityList as $ship_id => $shipCapacity) { |
183
|
|
|
$can_take = min($maxResourcesToTake, $shipCapacity); |
184
|
|
|
if ($can_take <= 0) { |
185
|
|
|
continue; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$fleet[$ship_id] = ceil($can_take / $this->infoTransportCapacity[$ship_id]); |
189
|
|
|
|
190
|
|
|
$maxResourcesToTake -= $can_take; |
191
|
|
|
if ($maxResourcesToTake <= 0) { |
192
|
|
|
break; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $fleet; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $user |
201
|
|
|
* @param array $resources_taken |
202
|
|
|
* @param array $planetRecord |
203
|
|
|
* @param float $will_take |
204
|
|
|
* @param array $fleet |
205
|
|
|
*/ |
206
|
|
|
protected function fillFleetResources(&$user, $resources_taken, $planetRecord, $will_take, &$fleet) { |
|
|
|
|
207
|
|
|
$result = []; |
208
|
|
|
foreach (sn_get_groups('resources_loot') as $resource_id) { |
209
|
|
|
if ($resources_taken[$planetRecord['id']] != 1 && !$resources_taken[$planetRecord['id']][$resource_id]) { |
210
|
|
|
continue; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$resource_amount = floor(mrc_get_level($user, $planetRecord, $resource_id, true, true)); |
|
|
|
|
214
|
|
|
|
215
|
|
|
$result[$resource_id] = min($will_take, $resource_amount); |
216
|
|
|
$will_take -= $resource_amount; |
217
|
|
|
|
218
|
|
|
if ($will_take <= 0) { |
219
|
|
|
break; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $result; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param array $playerRecord |
229
|
|
|
* @param array $planetRecord |
230
|
|
|
* @param \template $template |
231
|
|
|
*/ |
232
|
|
|
public function modelFleet5Gathering(&$playerRecord, &$planetRecord, $template) { |
233
|
|
|
if (empty($resources_taken = sys_get_param('resources')) || !is_array($resources_taken)) { |
234
|
|
|
return; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$planet_list = $this->flt_build_gathering($playerRecord, $planetRecord, $resources_taken); |
238
|
|
|
|
239
|
|
|
foreach ($planet_list as $planet_id => $planet_data) { |
240
|
|
|
if ($planet_data['RESULT'] == ATTACK_ALLOWED) { |
241
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
242
|
|
|
$planet_data['RESULT'] = flt_t_send_fleet( |
243
|
|
|
$playerRecord, |
244
|
|
|
$planet_data['PLANET_DB_DATA'], |
245
|
|
|
$planetRecord, |
246
|
|
|
$planet_data['FLEET'], |
247
|
|
|
$planet_data['FLEET_RESOURCES'], |
248
|
|
|
MT_TRANSPORT); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$planet_data['MESSAGE'] = $this->lang['fl_attack_error'][$planet_data['RESULT']]; |
252
|
|
|
|
253
|
|
|
$template->assign_block_vars('results', $planet_data); |
254
|
|
|
if (!empty($planet_data['FLEET']) && $planet_data['RESULT'] == ATTACK_ALLOWED) { |
255
|
|
|
foreach ($planet_data['FLEET'] as $unit_id => $amount) { |
256
|
|
|
$template->assign_block_vars('results.units', [ |
257
|
|
|
'ID' => $unit_id, |
258
|
|
|
'NAME' => $this->lang['tech'][$unit_id], |
259
|
|
|
'AMOUNT' => $amount |
260
|
|
|
]); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param array $user |
268
|
|
|
* @param array $planetrow |
269
|
|
|
* @param \template $template |
270
|
|
|
*/ |
271
|
|
|
public function viewPage5Gathering(&$user, &$planetrow, $template) { |
272
|
|
|
$planet_list = $this->flt_build_gathering($user, $planetrow, []); |
273
|
|
|
foreach ($planet_list as $planet_data) { |
274
|
|
|
// $planet_data['DURATION'] = $planet_data['DURATION'] ? pretty_time($planet_data['DURATION']) : $this->lang['flt_no_fuel']; |
|
|
|
|
275
|
|
|
$template->assign_block_vars('colonies', $planet_data); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$template->assign_vars([ |
279
|
|
|
'PAGE_HINT' => $this->lang['fl_page5_hint'], |
280
|
|
|
'METAL_NEED' => HelperString::numberFloorAndFormat(max(0, -sys_get_param_float('metal'))), |
281
|
|
|
'CRYSTAL_NEED' => HelperString::numberFloorAndFormat(max(0, -sys_get_param_float('crystal'))), |
282
|
|
|
'DEUTERIUM_NEED' => HelperString::numberFloorAndFormat(max(0, -sys_get_param_float('deuterium'))), |
283
|
|
|
]); |
284
|
|
|
|
285
|
|
|
tpl_set_resource_info($template, $planetrow, []); |
286
|
|
|
|
287
|
|
|
display($template, $this->lang['fl_title']); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state