1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 29.07.2016 13:18 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace V2Unit; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class V2UnitModel |
10
|
|
|
* |
11
|
|
|
* Second iteration of revised Unit |
12
|
|
|
* |
13
|
|
|
* @method V2UnitContainer buildContainer() |
14
|
|
|
* @method V2UnitContainer fromArray(array $array) |
15
|
|
|
* |
16
|
|
|
* @package V2Unit |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class V2UnitModel extends \Entity\KeyedModel { |
21
|
|
|
/** |
22
|
|
|
* Name of table for this entity |
23
|
|
|
* |
24
|
|
|
* @var string $tableName |
25
|
|
|
*/ |
26
|
|
|
protected $tableName = 'unit'; |
27
|
|
|
/** |
28
|
|
|
* Name of key field field in this table |
29
|
|
|
* |
30
|
|
|
* @var string $idFieldName |
31
|
|
|
*/ |
32
|
|
|
protected $idFieldName = 'unit_id'; |
33
|
|
|
|
34
|
|
|
// protected $exceptionClass = 'Entity\EntityException'; |
|
|
|
|
35
|
|
|
protected $entityContainerClass = 'V2Unit\V2UnitContainer'; |
36
|
|
|
|
37
|
|
|
// protected $properties = array( |
|
|
|
|
38
|
|
|
// 'dbId' => array( |
39
|
|
|
// P_DB_FIELD => 'unit_id', |
40
|
|
|
// ), |
41
|
|
|
// 'playerOwnerId' => array( |
42
|
|
|
// P_DB_FIELD => 'unit_player_id', |
43
|
|
|
// ), |
44
|
|
|
// 'locationType' => array( |
45
|
|
|
// P_DB_FIELD => 'unit_location_type', |
46
|
|
|
// ), |
47
|
|
|
// 'locationId' => array( |
48
|
|
|
// P_DB_FIELD => 'unit_location_id', |
49
|
|
|
// ), |
50
|
|
|
// 'type' => array( |
51
|
|
|
// P_DB_FIELD => 'unit_type', |
52
|
|
|
// ), |
53
|
|
|
// 'snId' => array( |
54
|
|
|
// P_DB_FIELD => 'unit_snid', |
55
|
|
|
// ), |
56
|
|
|
// // Order is important! |
57
|
|
|
// // TODO - split dbLevel to level and count |
58
|
|
|
// 'level' => array( |
59
|
|
|
// P_DB_FIELD => 'unit_level', |
60
|
|
|
// ), |
61
|
|
|
// 'count' => array(), |
62
|
|
|
// // TODO - move to child class |
63
|
|
|
// 'timeStart' => array( |
64
|
|
|
// P_DB_FIELD => 'unit_time_start', |
65
|
|
|
// ), |
66
|
|
|
// 'timeFinish' => array( |
67
|
|
|
// P_DB_FIELD => 'unit_time_finish', |
68
|
|
|
// ), |
69
|
|
|
// // Do we need it? Or internal no info/getters/setters should be ignored? |
70
|
|
|
// 'unitInfo' => array(), |
71
|
|
|
// 'isStackable' => array(), |
72
|
|
|
// 'locationDefaultType' => array(), |
73
|
|
|
// 'bonusType' => array(), |
74
|
|
|
// ); |
75
|
|
|
|
76
|
|
|
public function __construct(\Common\GlobalContainer $gc) { |
77
|
|
|
parent::__construct($gc); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->extendProperties( |
81
|
|
|
array( |
82
|
|
|
'playerOwnerId' => array( |
83
|
|
|
P_DB_FIELD => 'unit_player_id', |
84
|
|
|
), |
85
|
|
|
'locationType' => array( |
86
|
|
|
P_DB_FIELD => 'unit_location_type', |
87
|
|
|
), |
88
|
|
|
'locationId' => array( |
89
|
|
|
P_DB_FIELD => 'unit_location_id', |
90
|
|
|
), |
91
|
|
|
'type' => array( |
92
|
|
|
P_DB_FIELD => 'unit_type', |
93
|
|
|
), |
94
|
|
|
'snId' => array( |
95
|
|
|
P_DB_FIELD => 'unit_snid', |
96
|
|
|
), |
97
|
|
|
// Order is important! |
98
|
|
|
// TODO - split dbLevel to level and count |
99
|
|
|
'level' => array( |
100
|
|
|
P_DB_FIELD => 'unit_level', |
101
|
|
|
), |
102
|
|
|
'count' => array(), |
103
|
|
|
// TODO - move to child class |
104
|
|
|
'timeStart' => array( |
105
|
|
|
P_DB_FIELD => 'unit_time_start', |
106
|
|
|
), |
107
|
|
|
'timeFinish' => array( |
108
|
|
|
P_DB_FIELD => 'unit_time_finish', |
109
|
|
|
), |
110
|
|
|
// Do we need it? Or internal no info/getters/setters should be ignored? |
111
|
|
|
'unitInfo' => array(), |
112
|
|
|
'isStackable' => array(), |
113
|
|
|
'locationDefaultType' => array(), |
114
|
|
|
'bonusType' => array(), |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->accessors->setAccessor('snId', P_CONTAINER_SET, array($this, 'setSnId')); |
119
|
|
|
$this->accessors->setAccessor('snId', P_CONTAINER_UNSET, array($this, 'unsetSnId')); |
120
|
|
|
|
121
|
|
|
// This crap code is until php 5.4+. There we can use $this binding for lambdas |
122
|
|
|
$propertyName = 'timeStart'; |
123
|
|
|
$this->accessors->setAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport')); |
124
|
|
|
$this->accessors->setAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport')); |
125
|
|
|
|
126
|
|
|
$propertyName = 'timeFinish'; |
127
|
|
|
$this->accessors->setAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport')); |
128
|
|
|
$this->accessors->setAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setSnId(V2UnitContainer $that, $value) { |
132
|
|
|
$that->setDirect('snId', $value); |
133
|
|
|
|
134
|
|
|
$array = get_unit_param($value); |
135
|
|
|
$that->unitInfo = $array; |
136
|
|
|
$that->type = $array[P_UNIT_TYPE]; |
137
|
|
|
// Mandatory |
138
|
|
|
$that->isStackable = empty($array[P_STACKABLE]) ? false : true; |
139
|
|
|
$that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT]; |
140
|
|
|
// Optional |
141
|
|
|
$that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE]; |
142
|
|
|
// TODO - Записывать перечень фич для модуля, определяемых по его типу |
143
|
|
|
// А фичи сначала должны быть где-то зарегестрированы - в каком-то сервис-локаторе |
144
|
|
|
// Что-то типа classSupernova::registerUnitFeature |
145
|
|
|
// Кэш фич для разных типов юнитов |
146
|
|
|
$that->features = array(); //new FeatureList($that->unitInfo['features']); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function unsetSnId(V2UnitContainer $that) { |
150
|
|
|
unset($that->type); |
151
|
|
|
unset($that->unitInfo); |
152
|
|
|
// Mandatory |
153
|
|
|
unset($that->isStackable); |
154
|
|
|
unset($that->locationDefaultType); |
155
|
|
|
// Optional |
156
|
|
|
unset($that->bonusType); |
157
|
|
|
unset($that->features); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param V2UnitContainer $unitCaptain |
162
|
|
|
* @param int|string $userId |
163
|
|
|
* |
164
|
|
|
* @throws \Entity\EntityException |
165
|
|
|
*/ |
166
|
|
|
// TODO - move to unitCaptain |
167
|
|
|
public function validateCaptainVsUser($unitCaptain, $userId) { |
168
|
|
|
if (!is_object($unitCaptain) || $this->isNew($unitCaptain) || $this->isEmpty($unitCaptain)) { |
169
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_not_found', ERR_ERROR); |
170
|
|
|
} |
171
|
|
|
if ($unitCaptain->snId != UNIT_CAPTAIN) { |
172
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_wrong_unit', ERR_ERROR); |
173
|
|
|
} |
174
|
|
|
if ($unitCaptain->playerOwnerId != $userId) { |
175
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_wrong_captain', ERR_ERROR); |
176
|
|
|
} |
177
|
|
|
if ($unitCaptain->locationType != LOC_PLANET) { |
178
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_wrong_location', ERR_ERROR); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param V2UnitContainer $cUnit |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function isEmpty($cUnit) { |
188
|
|
|
return |
189
|
|
|
empty($cUnit->playerOwnerId) |
190
|
|
|
|| |
191
|
|
|
is_null($cUnit->locationType) |
192
|
|
|
|| |
193
|
|
|
$cUnit->locationType === LOC_NONE |
194
|
|
|
|| |
195
|
|
|
empty($cUnit->locationId) |
196
|
|
|
|| |
197
|
|
|
empty($cUnit->type) |
198
|
|
|
|| |
199
|
|
|
empty($cUnit->snId) |
200
|
|
|
|| |
201
|
|
|
empty($cUnit->level); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param V2UnitContainer $cUnit |
206
|
|
|
* @param string $featureName |
207
|
|
|
* |
208
|
|
|
* return UnitFeature |
209
|
|
|
* |
210
|
|
|
* @return mixed|null |
211
|
|
|
*/ |
212
|
|
|
public function feature($cUnit, $featureName) { |
213
|
|
|
return isset($cUnit->features[$featureName]) ? $cUnit->features[$featureName] : null; |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
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.