Passed
Branch trunk (7dc288)
by SuperNova.WS
06:07
created

Planet::tpl_planet_density_info()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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