1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by Gorlum 08.01.2018 14:30 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Planet; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
use Core\EntityDb; |
11
|
|
|
use Unit\Governor; |
12
|
|
|
use Exception; |
13
|
|
|
use HelperString; |
14
|
|
|
use SN; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Planet |
18
|
|
|
* @package Planet |
19
|
|
|
* |
20
|
|
|
* @method bool insert() |
21
|
|
|
* |
22
|
|
|
* @property int|string $id - Record ID name would be normalized to 'id' |
23
|
|
|
* @property string $name |
24
|
|
|
* @property int|float $id_owner |
25
|
|
|
* @property int $galaxy |
26
|
|
|
* @property int $system |
27
|
|
|
* @property int $planet |
28
|
|
|
* @property int $planet_type |
29
|
|
|
* @property int|float $metal |
30
|
|
|
* @property int|float $crystal |
31
|
|
|
* @property int|float $deuterium |
32
|
|
|
* property int|float $energy_max |
33
|
|
|
* property int|float $energy_used |
34
|
|
|
* property int $last_jump_time |
35
|
|
|
* property int $metal_perhour |
36
|
|
|
* property int $crystal_perhour |
37
|
|
|
* property int $deuterium_perhour |
38
|
|
|
* property int $metal_mine_porcent |
39
|
|
|
* property int $crystal_mine_porcent |
40
|
|
|
* property int $deuterium_sintetizer_porcent |
41
|
|
|
* property int $solar_plant_porcent |
42
|
|
|
* property int $fusion_plant_porcent |
43
|
|
|
* property int $solar_satelit_porcent |
44
|
|
|
* property int $last_update |
45
|
|
|
* property int $que_processed |
46
|
|
|
* property string $image |
47
|
|
|
* property int|float $points |
48
|
|
|
* property int|float $ranks |
49
|
|
|
* property int $id_level |
50
|
|
|
* property int $destruyed |
51
|
|
|
* property int $diameter |
52
|
|
|
* @property int $field_max - maximum allowed number of fields |
53
|
|
|
* property int $field_current |
54
|
|
|
* property int $temp_min |
55
|
|
|
* property int $temp_max |
56
|
|
|
* property int|float $metal_max |
57
|
|
|
* property int|float $crystal_max |
58
|
|
|
* property int|float $deuterium_max |
59
|
|
|
* property int|float $parent_planet |
60
|
|
|
* @property int|float $debris_metal |
61
|
|
|
* @property int|float $debris_crystal |
62
|
|
|
* @property int $PLANET_GOVERNOR_ID |
63
|
|
|
* @property int $PLANET_GOVERNOR_LEVEL |
64
|
|
|
* property int $planet_teleport_next |
65
|
|
|
* property int $ship_sattelite_sloth_porcent |
66
|
|
|
* @property int $density |
67
|
|
|
* @property int $density_index |
68
|
|
|
* property int $position_original |
69
|
|
|
* property int $field_max_original |
70
|
|
|
* property int $temp_min_original |
71
|
|
|
* property int $temp_max_original |
72
|
|
|
*/ |
73
|
|
|
class Planet extends EntityDb { |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string $_activeClass |
77
|
|
|
*/ |
78
|
|
|
protected $_activeClass = '\\Planet\\RecordPlanet'; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var RecordPlanet $_container |
82
|
|
|
*/ |
83
|
|
|
protected $_container; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var float[] $resources |
87
|
|
|
*/ |
88
|
|
|
protected $resources = [ |
89
|
|
|
RES_METAL => 0, |
90
|
|
|
RES_CRYSTAL => 0, |
91
|
|
|
RES_DEUTERIUM => 0, |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var Governor $governor |
96
|
|
|
*/ |
97
|
|
|
protected $governor; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Planet constructor. |
101
|
|
|
*/ |
102
|
|
|
public function __construct() { |
103
|
|
|
parent::__construct(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getGovernor() { |
107
|
|
|
if (empty($this->governor)) { |
108
|
|
|
$this->governor = new Governor(); |
109
|
|
|
$this->governor->setPlanet($this); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->governor; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function governorHire($hireId) { |
116
|
|
|
$this->getGovernor()->hire($hireId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $redirect |
121
|
|
|
* |
122
|
|
|
* @deprecated |
123
|
|
|
* TODO - change saveing |
124
|
|
|
*/ |
125
|
|
|
public function sn_sys_sector_buy($redirect = 'overview.php') { |
126
|
|
|
if (!sys_get_param_str('sector_buy') || $this->planet_type != PT_PLANET) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
sn_db_transaction_start(); |
131
|
|
|
$user = db_user_by_id($this->id_owner, true, '*'); |
|
|
|
|
132
|
|
|
$this->setForUpdate()->dbLoadRecord($this->id); |
133
|
|
|
|
134
|
|
|
$sector_cost = eco_get_build_data($user, $this->asArray(), UNIT_SECTOR, mrc_get_level($user, $this->asArray(), UNIT_SECTOR), true); |
135
|
|
|
$sector_cost = $sector_cost[BUILD_CREATE][RES_DARK_MATTER]; |
136
|
|
|
if ($sector_cost <= mrc_get_level($user, [], RES_DARK_MATTER)) { |
137
|
|
|
$planet_name_text = uni_render_planet($this->asArray()); |
138
|
|
|
if (rpg_points_change($user['id'], RPG_SECTOR, -$sector_cost, |
139
|
|
|
sprintf( |
140
|
|
|
SN::$lang['sys_sector_purchase_log'], |
141
|
|
|
$user['username'], |
142
|
|
|
$user['id'], |
143
|
|
|
$planet_name_text, |
144
|
|
|
SN::$lang['sys_planet_type'][$this->planet_type], |
145
|
|
|
$this->id, |
146
|
|
|
$sector_cost |
147
|
|
|
) |
148
|
|
|
)) { |
149
|
|
|
$this->field_max++; |
150
|
|
|
$this->update(); |
151
|
|
|
} else { |
152
|
|
|
sn_db_transaction_rollback(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
sn_db_transaction_commit(); |
156
|
|
|
|
157
|
|
|
sys_redirect($redirect); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $user |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
* |
165
|
|
|
* @deprecated |
166
|
|
|
* TODO - change saveing |
167
|
|
|
*/ |
168
|
|
|
public function sn_sys_planet_core_transmute(&$user) { |
169
|
|
|
if (!sys_get_param_str('transmute')) { |
170
|
|
|
return array(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
try { |
174
|
|
|
if ($this->planet_type != PT_PLANET) { |
175
|
|
|
throw new exception(SN::$lang['ov_core_err_not_a_planet'], ERR_ERROR); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ($this->density_index == ($new_density_index = sys_get_param_id('density_type'))) { |
179
|
|
|
throw new exception(SN::$lang['ov_core_err_same_density'], ERR_WARNING); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
sn_db_transaction_start(); |
183
|
|
|
$user = db_user_by_id($user['id'], true, '*'); |
|
|
|
|
184
|
|
|
$this->setForUpdate()->dbLoadRecord($this->id); |
185
|
|
|
|
186
|
|
|
$planet_density_index = $this->density_index; |
187
|
|
|
$density_price_chart = $this->planet_density_price_chart(); |
188
|
|
|
|
189
|
|
|
if (!isset($density_price_chart[$new_density_index])) { |
190
|
|
|
// Hack attempt |
191
|
|
|
throw new exception(SN::$lang['ov_core_err_denisty_type_wrong'], ERR_ERROR); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$user_dark_matter = mrc_get_level($user, false, RES_DARK_MATTER); |
195
|
|
|
$transmute_cost = $density_price_chart[$new_density_index]; |
196
|
|
|
if ($user_dark_matter < $transmute_cost) { |
197
|
|
|
throw new exception(SN::$lang['ov_core_err_no_dark_matter'], ERR_ERROR); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$sn_data_planet_density = sn_get_groups('planet_density'); |
201
|
|
|
foreach ($sn_data_planet_density as $key => $value) { |
202
|
|
|
if ($key == $new_density_index) { |
203
|
|
|
break; |
204
|
|
|
} |
205
|
|
|
$prev_density_index = $key; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$new_density = round(($sn_data_planet_density[$new_density_index][UNIT_PLANET_DENSITY] + $sn_data_planet_density[$prev_density_index][UNIT_PLANET_DENSITY]) / 2); |
|
|
|
|
209
|
|
|
|
210
|
|
|
rpg_points_change($user['id'], RPG_PLANET_DENSITY_CHANGE, -$transmute_cost, |
211
|
|
|
array( |
212
|
|
|
'Planet %1$s ID %2$d at coordinates %3$s changed density type from %4$d "%5$s" to %6$d "%7$s". New density is %8$d kg/m3', |
213
|
|
|
$this->name, |
214
|
|
|
$this->id, |
215
|
|
|
uni_render_coordinates($this->asArray()), |
216
|
|
|
$planet_density_index, |
217
|
|
|
SN::$lang['uni_planet_density_types'][$planet_density_index], |
218
|
|
|
$new_density_index, |
219
|
|
|
SN::$lang['uni_planet_density_types'][$new_density_index], |
220
|
|
|
$new_density |
221
|
|
|
) |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
DBStaticPlanet::db_planet_set_by_id($this->id, "`density` = {$new_density}, `density_index` = {$new_density_index}"); |
225
|
|
|
sn_db_transaction_commit(); |
226
|
|
|
|
227
|
|
|
$this->density = $new_density; |
228
|
|
|
$this->density_index = $new_density_index; |
229
|
|
|
$result = array( |
230
|
|
|
'STATUS' => ERR_NONE, |
231
|
|
|
'MESSAGE' => sprintf(SN::$lang['ov_core_err_none'], SN::$lang['uni_planet_density_types'][$planet_density_index], SN::$lang['uni_planet_density_types'][$new_density_index], $new_density), |
232
|
|
|
); |
233
|
|
|
} catch (Exception $e) { |
234
|
|
|
sn_db_transaction_rollback(); |
235
|
|
|
$result = array( |
236
|
|
|
'STATUS' => $e->getCode(), |
237
|
|
|
'MESSAGE' => $e->getMessage(), |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $result; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
public function planet_density_price_chart() { |
248
|
|
|
$sn_data_density = sn_get_groups('planet_density'); |
249
|
|
|
$density_price_chart = array(); |
250
|
|
|
|
251
|
|
|
foreach ($sn_data_density as $density_id => $density_data) { |
252
|
|
|
// Отсекаем записи с RARITY = 0 - служебные записи и супер-ядра |
253
|
|
|
$density_data[UNIT_PLANET_DENSITY_RARITY] ? $density_price_chart[$density_id] = $density_data[UNIT_PLANET_DENSITY_RARITY] : false; |
254
|
|
|
} |
255
|
|
|
unset($density_price_chart[PLANET_DENSITY_NONE]); |
256
|
|
|
|
257
|
|
|
$total_rarity = array_sum($density_price_chart); |
258
|
|
|
|
259
|
|
|
foreach ($density_price_chart as &$density_data) { |
260
|
|
|
$density_data = ceil($total_rarity / $density_data * $this->field_max * PLANET_DENSITY_TO_DARK_MATTER_RATE); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $density_price_chart; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param int $user_dark_matter |
268
|
|
|
* |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
|
|
public function tpl_planet_density_info($user_dark_matter) { |
272
|
|
|
$result = []; |
273
|
|
|
|
274
|
|
|
$density_price_chart = Planet::planet_density_price_chart(); |
|
|
|
|
275
|
|
|
|
276
|
|
|
foreach ($density_price_chart as $density_price_index => &$density_price_data) { |
277
|
|
|
$density_cost = $density_price_data; |
278
|
|
|
$density_price_data = array( |
279
|
|
|
'COST' => $density_cost, |
280
|
|
|
'COST_TEXT' => HelperString::numberFloorAndFormat($density_cost), |
281
|
|
|
'COST_TEXT_CLASS' => prettyNumberGetClass($density_cost, $user_dark_matter), |
282
|
|
|
'REST' => $user_dark_matter - $density_cost, |
283
|
|
|
'ID' => $density_price_index, |
284
|
|
|
'TEXT' => SN::$lang['uni_planet_density_types'][$density_price_index], |
285
|
|
|
); |
286
|
|
|
$result[] = $density_price_data; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$planet_density_index = $this->density_index; |
290
|
|
|
|
291
|
|
|
return [ |
292
|
|
|
'.' => [ |
293
|
|
|
'densities' => $result, |
294
|
|
|
], |
295
|
|
|
'PLANET_DENSITY_INDEX' => $planet_density_index, |
296
|
|
|
'PLANET_CORE_TEXT' => \SN::$lang['uni_planet_density_types'][$planet_density_index], |
297
|
|
|
]; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param array $user |
302
|
|
|
* |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
|
|
public function int_planet_pretemplate($user) { |
306
|
|
|
$governor_id = $this->PLANET_GOVERNOR_ID; |
307
|
|
|
$governor_level_plain = mrc_get_level($user, $this->asArray(), $governor_id, false, true); |
308
|
|
|
|
309
|
|
|
return [ |
310
|
|
|
'PLANET_ID' => $this->id, |
311
|
|
|
'PLANET_NAME' => htmlentities($this->name, ENT_QUOTES, 'UTF-8'), |
312
|
|
|
'PLANET_NAME_JS' => htmlentities(js_safe_string($this->name), ENT_QUOTES, 'UTF-8'), |
313
|
|
|
'PLANET_GALAXY' => $this->galaxy, |
314
|
|
|
'PLANET_SYSTEM' => $this->system, |
315
|
|
|
'PLANET_PLANET' => $this->planet, |
316
|
|
|
'PLANET_TYPE' => $this->planet_type, |
317
|
|
|
'PLANET_TYPE_TEXT' => SN::$lang['sys_planet_type'][$this->planet_type], |
318
|
|
|
'PLANET_DEBRIS' => $this->debris_metal + $this->debris_crystal, |
319
|
|
|
|
320
|
|
|
'PLANET_GOVERNOR_ID' => $governor_id, |
321
|
|
|
'PLANET_GOVERNOR_NAME' => SN::$lang['tech'][$governor_id], |
322
|
|
|
'PLANET_GOVERNOR_LEVEL' => $governor_level_plain, |
323
|
|
|
'PLANET_GOVERNOR_LEVEL_PLUS' => mrc_get_level($user, $this->asArray(), $governor_id, false, false) - $governor_level_plain, |
324
|
|
|
'PLANET_GOVERNOR_LEVEL_MAX' => get_unit_param($governor_id, P_MAX_STACK), |
325
|
|
|
]; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function reset() { |
329
|
|
|
$this->governor = null; |
330
|
|
|
|
331
|
|
|
$this->resources = [ |
332
|
|
|
RES_METAL => 0, |
333
|
|
|
RES_CRYSTAL => 0, |
334
|
|
|
RES_DEUTERIUM => 0, |
335
|
|
|
]; |
336
|
|
|
|
337
|
|
|
return parent::reset(); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @return RecordPlanet |
342
|
|
|
*/ |
343
|
|
|
public function _getContainer() { |
344
|
|
|
return $this->_container; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param int $resourceId |
350
|
|
|
* @param float $resourceCount |
351
|
|
|
* |
352
|
|
|
* @throws \Exception |
353
|
|
|
*/ |
354
|
|
|
public function changeResource($resourceId, $resourceCount) { |
355
|
|
|
if (empty($resourceCount)) { |
356
|
|
|
return; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
if (!array_key_exists($resourceId, $this->resources)) { |
360
|
|
|
throw new \Exception("PLANET ERROR! Trying to change unknown resource type [{$resourceId}] '{$resourceCount}' on planet [{$this->id}]"); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$resourceCount = ceil($resourceCount); |
364
|
|
|
|
365
|
|
|
if ($this->resources[$resourceId] + $resourceCount < 0) { |
366
|
|
|
throw new \Exception("PLANET ERROR! Trying to deduct more resources [{$resourceId}] '{$resourceCount}' when planet [{$this->id}] has only {$this->resources[$resourceId]}"); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$this->resources[$resourceId] += $resourceCount; |
370
|
|
|
|
371
|
|
|
$fieldName = pname_resource_name($resourceId); |
372
|
|
|
$this->_getContainer()->inc()->$fieldName = $resourceCount; |
373
|
|
|
|
374
|
|
|
// $this->metal = $this->resources[RES_METAL]; |
|
|
|
|
375
|
|
|
// $this->crystal = $this->resources[RES_CRYSTAL]; |
376
|
|
|
// $this->deuterium = $this->resources[RES_DEUTERIUM]; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public function dbLoadRecord($id) { |
380
|
|
|
$result = parent::dbLoadRecord($id); |
381
|
|
|
|
382
|
|
|
if(!$this->isNew()) { |
383
|
|
|
$this->resources[RES_METAL] = $this->_getContainer()->metal; |
384
|
|
|
$this->resources[RES_CRYSTAL] = $this->_getContainer()->crystal; |
385
|
|
|
$this->resources[RES_DEUTERIUM] = $this->_getContainer()->deuterium; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return $result; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
} |
392
|
|
|
|