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

Governor::setPlanetData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
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 13:23
5
 */
6
7
namespace Unit;
8
9
use \classSupernova;
10
use Planet\Planet;
11
12
class Governor extends Unit {
13
//  protected $type = UNIT_GOVERNOR_PRIMARY;
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
14
//  protected $typeIdField = 'PLANET_GOVERNOR_ID';
15
//  protected $typeLevelField = 'PLANET_GOVERNOR_LEVEL';
16
17
  protected $snId = 0;
18
  protected $level = 0;
19
20
  /**
21
   * @var RecordUnit $unit
22
   */
23
  protected $unit;
24
25
  /**
26
   * @var Planet $planet
27
   */
28
  protected $planet;
29
30
  /**
31
   * Governor constructor.
32
   */
33
  public function __construct() {
34
    $this->reset();
35
  }
36
37
  /**
38
   * @param Planet $planet
39
   */
40
  public function setPlanet($planet) {
41
    $this->reset();
42
43
    $this->planet = $planet;
44
    $this->getExternalData();
45
  }
46
47
  /**
48
   * @param int $hireId - Hire unit SN ID
49
   */
50
  public function hire($hireId) {
51
    if (!in_array($hireId, sn_get_groups('governors'))) {
52
      return;
53
    }
54
55
    if ($hireId == $this->getSnId() && $this->getMaxLevel() && $this->getMaxLevel() >= $this->getLevel()) {
56
      return;
57
    }
58
59
    sn_db_transaction_start();
60
    $user = db_user_by_id($this->planet->id_owner, true);
61
//    $this->planetRow = DBStaticPlanet::db_planet_by_id($this->planet->id, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
62
//    $build_data = eco_get_build_data($user, $this->planetRow, $hireId, $this->getId() == $hireId ? $this->getLevel() : 0);
63
    $this->planet->dbLoadRecord($this->planet->id);
64
65
    $build_data = eco_get_build_data($user, $this->planet->asArray(), $hireId, $this->getSnId() == $hireId ? $this->getLevel() : 0);
66
    if (
67
      $build_data['CAN'][BUILD_CREATE]
68
      &&
69
      mrc_get_level($user, [], RES_DARK_MATTER) >= $build_data[BUILD_CREATE][RES_DARK_MATTER]
70
      &&
71
      rpg_points_change(
72
        $user['id'],
73
        RPG_GOVERNOR,
74
        -$build_data[BUILD_CREATE][RES_DARK_MATTER],
75
        sprintf(classSupernova::$lang['ov_governor_purchase'],
76
          classSupernova::$lang['tech'][$hireId],
77
          $hireId,
78
          $this->level,
79
          uni_render_planet_object_full($this->planet, false, true)
80
        )
81
      )
82
    ) {
83
      $this->addLevel($hireId);
84
      $this->planet->update();
85
    }
86
    sn_db_transaction_commit();
87
  }
88
89
90
  /**
91
   * @return int
92
   */
93
  public function getSnId() {
94
    return $this->snId;
95
  }
96
97
  /**
98
   * @return int
99
   */
100
  public function getLevel() {
101
    return $this->level;
102
  }
103
104
  /**
105
   * @return int
106
   */
107
  public function getMaxLevel() {
108
    $snId =  $this->getSnId();
109
    return !empty($snId) ? get_unit_param($snId, P_MAX_STACK) : 0;
110
  }
111
112
  /**
113
   * @param $hireId
114
   */
115
  protected function addLevel($hireId) {
116
    if ($this->getSnId() == $hireId) {
117
      $this->level++;
118
    } else {
119
      $this->level = 1;
120
    }
121
122
    $this->snId = $hireId;
123
124
    $this->setExternalData();
125
  }
126
127
128
  protected function reset() {
129
    unset($this->unit);
130
    $this->planet = null;
131
132
    $this->snId = 0;
133
    $this->level = 0;
134
  }
135
136
  /**
137
   * Sets data on external sources from internal properties
138
   */
139
  protected function setExternalData() {
140
    $this->planet->PLANET_GOVERNOR_ID = $this->getSnId();
141
    $this->planet->PLANET_GOVERNOR_LEVEL = $this->level;
142
  }
143
144
  /**
145
   * Loads some data from external sources
146
   */
147
  protected function getExternalData() {
148
    $this->snId = !empty($this->planet->PLANET_GOVERNOR_ID) ? intval($this->planet->PLANET_GOVERNOR_ID) : 0;
149
    $this->level = !empty($this->planet->PLANET_GOVERNOR_LEVEL) ? intval($this->planet->PLANET_GOVERNOR_LEVEL) : 0;
150
  }
151
152
}
153