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 { |
13
|
|
|
// protected $type = UNIT_GOVERNOR_PRIMARY; |
|
|
|
|
14
|
|
|
// protected $typeIdField = 'PLANET_GOVERNOR_ID'; |
15
|
|
|
// protected $typeLevelField = 'PLANET_GOVERNOR_LEVEL'; |
16
|
|
|
|
17
|
|
|
protected $id = 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->getPlanetData(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $hireId |
49
|
|
|
*/ |
50
|
|
|
public function hire($hireId) { |
51
|
|
|
if (!in_array($hireId, sn_get_groups('governors'))) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($hireId == $this->id && $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); |
|
|
|
|
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->_getContainer()->asArray(), $hireId, $this->getId() == $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->_getContainer()->update(); |
85
|
|
|
// DBStaticPlanet::db_planet_set_by_id($this->planet->id, "`{$this->typeIdField}` = {$this->id}, `{$this->typeLevelField}` = {$this->level}"); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
sn_db_transaction_commit(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function getId() { |
95
|
|
|
return $this->id; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getLevel() { |
102
|
|
|
return $this->level; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getMaxLevel() { |
109
|
|
|
return $this->id ? get_unit_param($this->id, P_MAX_STACK) : 0; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $hireId |
114
|
|
|
*/ |
115
|
|
|
protected function addLevel($hireId) { |
116
|
|
|
if ($this->id == $hireId) { |
117
|
|
|
$this->level++; |
118
|
|
|
} else { |
119
|
|
|
$this->level = 1; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->id = $hireId; |
123
|
|
|
|
124
|
|
|
$this->setPlanetData(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
protected function reset() { |
129
|
|
|
unset($this->unit); |
130
|
|
|
$this->planet = null; |
131
|
|
|
|
132
|
|
|
$this->id = 0; |
133
|
|
|
$this->level = 0; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function setPlanetData() { |
137
|
|
|
$this->planet->PLANET_GOVERNOR_ID = $this->id; |
138
|
|
|
$this->planet->PLANET_GOVERNOR_LEVEL = $this->level; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function getPlanetData() { |
142
|
|
|
$this->id = !empty($this->planet->PLANET_GOVERNOR_ID) ? intval($this->planet->PLANET_GOVERNOR_ID) : 0; |
143
|
|
|
$this->level = !empty($this->planet->PLANET_GOVERNOR_LEVEL) ? intval($this->planet->PLANET_GOVERNOR_LEVEL) : 0; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
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.