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

Planet::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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 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 Governor $governor
87
   */
88
  protected $governor;
89
90
  /**
91
   * Planet constructor.
92
   */
93
  public function __construct() {
94
    parent::__construct();
95
  }
96
97
  public function getGovernor() {
98
    if (empty($this->governor)) {
99
      $this->governor = new Governor();
100
      $this->governor->setPlanet($this);
101
    }
102
103
    return $this->governor;
104
  }
105
106
  public function governorHire($hireId) {
107
    $this->getGovernor()->hire($hireId);
108
  }
109
110
  /**
111
   * @param string $redirect
112
   *
113
   * @deprecated
114
   * TODO - change saveing
115
   */
116
  public function sn_sys_sector_buy($redirect = 'overview.php') {
117
    if (!sys_get_param_str('sector_buy') || $this->planet_type != PT_PLANET) {
118
      return;
119
    }
120
121
    sn_db_transaction_start();
122
    $user = db_user_by_id($this->id_owner, true, '*');
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

122
    $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($this->id_owner, true, '*');
Loading history...
123
    $this->setForUpdate()->dbLoadRecord($this->id);
124
125
    $sector_cost = eco_get_build_data($user, $this->asArray(), UNIT_SECTOR, mrc_get_level($user, $this->asArray(), UNIT_SECTOR), true);
126
    $sector_cost = $sector_cost[BUILD_CREATE][RES_DARK_MATTER];
127
    if ($sector_cost <= mrc_get_level($user, [], RES_DARK_MATTER)) {
128
      $planet_name_text = uni_render_planet($this->asArray());
129
      if (rpg_points_change($user['id'], RPG_SECTOR, -$sector_cost,
130
        sprintf(
131
          SN::$lang['sys_sector_purchase_log'],
132
          $user['username'],
133
          $user['id'],
134
          $planet_name_text,
135
          SN::$lang['sys_planet_type'][$this->planet_type],
136
          $this->id,
137
          $sector_cost
138
        )
139
      )) {
140
        $this->field_max++;
141
        $this->update();
142
      } else {
143
        sn_db_transaction_rollback();
144
      }
145
    }
146
    sn_db_transaction_commit();
147
148
    sys_redirect($redirect);
149
  }
150
151
  /**
152
   * @param $user
153
   *
154
   * @return array
155
   *
156
   * @deprecated
157
   * TODO - change saveing
158
   */
159
  public function sn_sys_planet_core_transmute(&$user) {
160
    if (!sys_get_param_str('transmute')) {
161
      return array();
162
    }
163
164
    try {
165
      if ($this->planet_type != PT_PLANET) {
166
        throw new exception(SN::$lang['ov_core_err_not_a_planet'], ERR_ERROR);
167
      }
168
169
      if ($this->density_index == ($new_density_index = sys_get_param_id('density_type'))) {
170
        throw new exception(SN::$lang['ov_core_err_same_density'], ERR_WARNING);
171
      }
172
173
      sn_db_transaction_start();
174
      $user = db_user_by_id($user['id'], true, '*');
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

174
      $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true, '*');
Loading history...
175
      $this->setForUpdate()->dbLoadRecord($this->id);
176
177
      $planet_density_index = $this->density_index;
178
      $density_price_chart = $this->planet_density_price_chart();
179
180
      if (!isset($density_price_chart[$new_density_index])) {
181
        // Hack attempt
182
        throw new exception(SN::$lang['ov_core_err_denisty_type_wrong'], ERR_ERROR);
183
      }
184
185
      $user_dark_matter = mrc_get_level($user, false, RES_DARK_MATTER);
186
      $transmute_cost = $density_price_chart[$new_density_index];
187
      if ($user_dark_matter < $transmute_cost) {
188
        throw new exception(SN::$lang['ov_core_err_no_dark_matter'], ERR_ERROR);
189
      }
190
191
      $sn_data_planet_density = sn_get_groups('planet_density');
192
      foreach ($sn_data_planet_density as $key => $value) {
193
        if ($key == $new_density_index) {
194
          break;
195
        }
196
        $prev_density_index = $key;
197
      }
198
199
      $new_density = round(($sn_data_planet_density[$new_density_index][UNIT_PLANET_DENSITY] + $sn_data_planet_density[$prev_density_index][UNIT_PLANET_DENSITY]) / 2);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $prev_density_index seems to be defined by a foreach iteration on line 192. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
200
201
      rpg_points_change($user['id'], RPG_PLANET_DENSITY_CHANGE, -$transmute_cost,
202
        array(
203
          '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',
204
          $this->name,
205
          $this->id,
206
          uni_render_coordinates($this->asArray()),
207
          $planet_density_index,
208
          SN::$lang['uni_planet_density_types'][$planet_density_index],
209
          $new_density_index,
210
          SN::$lang['uni_planet_density_types'][$new_density_index],
211
          $new_density
212
        )
213
      );
214
215
      DBStaticPlanet::db_planet_set_by_id($this->id, "`density` = {$new_density}, `density_index` = {$new_density_index}");
216
      sn_db_transaction_commit();
217
218
      $this->density = $new_density;
219
      $this->density_index = $new_density_index;
220
      $result = array(
221
        'STATUS'  => ERR_NONE,
222
        '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),
223
      );
224
    } catch (Exception $e) {
225
      sn_db_transaction_rollback();
226
      $result = array(
227
        'STATUS'  => $e->getCode(),
228
        'MESSAGE' => $e->getMessage(),
229
      );
230
    }
231
232
    return $result;
233
  }
234
235
  /**
236
   * @return array
237
   */
238
  public function planet_density_price_chart() {
239
    $sn_data_density = sn_get_groups('planet_density');
240
    $density_price_chart = array();
241
242
    foreach ($sn_data_density as $density_id => $density_data) {
243
      // Отсекаем записи с RARITY = 0 - служебные записи и супер-ядра
244
      $density_data[UNIT_PLANET_DENSITY_RARITY] ? $density_price_chart[$density_id] = $density_data[UNIT_PLANET_DENSITY_RARITY] : false;
245
    }
246
    unset($density_price_chart[PLANET_DENSITY_NONE]);
247
248
    $total_rarity = array_sum($density_price_chart);
249
250
    foreach ($density_price_chart as &$density_data) {
251
      $density_data = ceil($total_rarity / $density_data * $this->field_max * PLANET_DENSITY_TO_DARK_MATTER_RATE);
252
    }
253
254
    return $density_price_chart;
255
  }
256
257
  /**
258
   * @param int $user_dark_matter
259
   *
260
   * @return array
261
   */
262
  public function tpl_planet_density_info($user_dark_matter) {
263
    $result = [];
264
265
    $density_price_chart = Planet::planet_density_price_chart();
0 ignored issues
show
Bug Best Practice introduced by
The method Planet\Planet::planet_density_price_chart() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

265
    /** @scrutinizer ignore-call */ 
266
    $density_price_chart = Planet::planet_density_price_chart();
Loading history...
266
267
    foreach ($density_price_chart as $density_price_index => &$density_price_data) {
268
      $density_cost = $density_price_data;
269
      $density_price_data = array(
270
        'COST'            => $density_cost,
271
        'COST_TEXT'       => HelperString::numberFloorAndFormat($density_cost),
272
        'COST_TEXT_CLASS' => prettyNumberGetClass($density_cost, $user_dark_matter),
273
        'REST'            => $user_dark_matter - $density_cost,
274
        'ID'              => $density_price_index,
275
        'TEXT'            => SN::$lang['uni_planet_density_types'][$density_price_index],
276
      );
277
      $result[] = $density_price_data;
278
    }
279
280
    $planet_density_index = $this->density_index;
281
282
    return [
283
      '.'                    => [
284
        'densities' => $result,
285
      ],
286
      'PLANET_DENSITY_INDEX' => $planet_density_index,
287
      'PLANET_CORE_TEXT'     => \SN::$lang['uni_planet_density_types'][$planet_density_index],
288
    ];
289
  }
290
291
  /**
292
   * @param array $user
293
   *
294
   * @return array
295
   */
296
  public function int_planet_pretemplate($user) {
297
    $governor_id = $this->PLANET_GOVERNOR_ID;
298
    $governor_level_plain = mrc_get_level($user, $this->asArray(), $governor_id, false, true);
299
300
    return [
301
      'PLANET_ID'        => $this->id,
302
      'PLANET_NAME'      => htmlentities($this->name, ENT_QUOTES, 'UTF-8'),
303
      'PLANET_NAME_JS'   => htmlentities(js_safe_string($this->name), ENT_QUOTES, 'UTF-8'),
304
      'PLANET_GALAXY'    => $this->galaxy,
305
      'PLANET_SYSTEM'    => $this->system,
306
      'PLANET_PLANET'    => $this->planet,
307
      'PLANET_TYPE'      => $this->planet_type,
308
      'PLANET_TYPE_TEXT' => SN::$lang['sys_planet_type'][$this->planet_type],
309
      'PLANET_DEBRIS'    => $this->debris_metal + $this->debris_crystal,
310
311
      'PLANET_GOVERNOR_ID'         => $governor_id,
312
      'PLANET_GOVERNOR_NAME'       => SN::$lang['tech'][$governor_id],
313
      'PLANET_GOVERNOR_LEVEL'      => $governor_level_plain,
314
      'PLANET_GOVERNOR_LEVEL_PLUS' => mrc_get_level($user, $this->asArray(), $governor_id, false, false) - $governor_level_plain,
315
      'PLANET_GOVERNOR_LEVEL_MAX'  => get_unit_param($governor_id, P_MAX_STACK),
316
    ];
317
  }
318
319
  public function reset() {
320
    $this->governor = null;
321
322
    return parent::reset();
323
  }
324
}
325