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 getContainer() |
14
|
|
|
* |
15
|
|
|
* @package V2Unit |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class V2UnitModel extends \EntityModel { |
20
|
|
|
/** |
21
|
|
|
* Name of table for this entity |
22
|
|
|
* |
23
|
|
|
* @var string $tableName |
24
|
|
|
*/ |
25
|
|
|
protected $tableName = 'unit'; |
26
|
|
|
/** |
27
|
|
|
* Name of key field field in this table |
28
|
|
|
* |
29
|
|
|
* @var string $idField |
30
|
|
|
*/ |
31
|
|
|
protected $idField = 'unit_id'; |
32
|
|
|
|
33
|
|
|
protected $exceptionClass = 'EntityException'; |
34
|
|
|
protected $entityContainerClass = 'V2Unit\V2UnitContainer'; |
35
|
|
|
|
36
|
|
|
protected $properties = array( |
37
|
|
|
'dbId' => array( |
38
|
|
|
P_DB_FIELD => 'unit_id', |
39
|
|
|
), |
40
|
|
|
'playerOwnerId' => array( |
41
|
|
|
P_DB_FIELD => 'unit_player_id', |
42
|
|
|
), |
43
|
|
|
'locationType' => array( |
44
|
|
|
P_DB_FIELD => 'unit_location_type', |
45
|
|
|
), |
46
|
|
|
'locationId' => array( |
47
|
|
|
P_DB_FIELD => 'unit_location_id', |
48
|
|
|
), |
49
|
|
|
'type' => array( |
50
|
|
|
P_DB_FIELD => 'unit_type', |
51
|
|
|
), |
52
|
|
|
'snId' => array( |
53
|
|
|
P_DB_FIELD => 'unit_snid', |
54
|
|
|
), |
55
|
|
|
// Order is important! |
56
|
|
|
// TODO - split dbLevel to level and count |
57
|
|
|
'level' => array( |
58
|
|
|
P_DB_FIELD => 'unit_level', |
59
|
|
|
), |
60
|
|
|
'count' => array(), |
61
|
|
|
// TODO - move to child class |
62
|
|
|
'timeStart' => array( |
63
|
|
|
P_DB_FIELD => 'unit_time_start', |
64
|
|
|
), |
65
|
|
|
'timeFinish' => array( |
66
|
|
|
P_DB_FIELD => 'unit_time_finish', |
67
|
|
|
), |
68
|
|
|
// Do we need it? Or internal no info/getters/setters should be ignored? |
69
|
|
|
'unitInfo' => array(), |
70
|
|
|
'isStackable' => array(), |
71
|
|
|
'locationDefaultType' => array(), |
72
|
|
|
'bonusType' => array(), |
73
|
|
|
); |
74
|
|
|
|
75
|
1 |
|
public function __construct(\Common\GlobalContainer $gc) { |
76
|
1 |
|
parent::__construct($gc); |
77
|
|
|
|
78
|
1 |
|
$this->assignAccessor('snId', P_CONTAINER_SET, array($this, 'setSnId')); |
79
|
1 |
|
$this->assignAccessor('snId', P_CONTAINER_UNSET, array($this, 'unsetSnId')); |
80
|
|
|
|
81
|
|
|
// This crap code is until php 5.4+. There we can use $this binding for lambdas |
82
|
1 |
|
$propertyName = 'timeStart'; |
83
|
1 |
|
$this->assignAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport')); |
84
|
1 |
|
$this->assignAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport')); |
85
|
|
|
|
86
|
1 |
|
$propertyName = 'timeFinish'; |
87
|
1 |
|
$this->assignAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport')); |
88
|
1 |
|
$this->assignAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport')); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
1 |
|
public function setSnId(V2UnitContainer $that, $value) { |
92
|
1 |
|
$that->setDirect('snId', $value); |
93
|
|
|
|
94
|
1 |
|
$array = get_unit_param($value); |
95
|
1 |
|
$that->unitInfo = $array; |
96
|
1 |
|
$that->type = $array[P_UNIT_TYPE]; |
97
|
|
|
// Mandatory |
98
|
1 |
|
$that->isStackable = empty($array[P_STACKABLE]) ? false : true; |
99
|
1 |
|
$that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT]; |
100
|
|
|
// Optional |
101
|
1 |
|
$that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE]; |
102
|
|
|
// TODO - Записывать перечень фич для модуля, определяемых по его типу |
103
|
|
|
// А фичи сначала должны быть где-то зарегестрированы - в каком-то сервис-локаторе |
104
|
|
|
// Что-то типа classSupernova::registerUnitFeature |
105
|
|
|
// Кэш фич для разных типов юнитов |
106
|
1 |
|
$that->features = array(); //new FeatureList($that->unitInfo['features']); |
|
|
|
|
107
|
1 |
|
} |
108
|
|
|
|
109
|
1 |
|
public function unsetSnId(V2UnitContainer $that) { |
110
|
1 |
|
unset($that->type); |
111
|
1 |
|
unset($that->unitInfo); |
112
|
|
|
// Mandatory |
113
|
1 |
|
unset($that->isStackable); |
114
|
1 |
|
unset($that->locationDefaultType); |
115
|
|
|
// Optional |
116
|
1 |
|
unset($that->bonusType); |
117
|
1 |
|
unset($that->features); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param V2UnitContainer $unitCaptain |
122
|
|
|
* @param int|string $userId |
123
|
|
|
* |
124
|
|
|
* @throws \EntityException |
125
|
|
|
*/ |
126
|
|
|
// TODO - move to unitCaptain |
127
|
|
|
public function validateCaptainVsUser($unitCaptain, $userId) { |
128
|
|
|
if (!is_object($unitCaptain) || $this->isNew($unitCaptain) || $this->isEmpty($unitCaptain)) { |
129
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_not_found', ERR_ERROR); |
130
|
|
|
} |
131
|
|
|
if ($unitCaptain->snId != UNIT_CAPTAIN) { |
132
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_wrong_unit', ERR_ERROR); |
133
|
|
|
} |
134
|
|
|
if ($unitCaptain->playerOwnerId != $userId) { |
135
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_wrong_captain', ERR_ERROR); |
136
|
|
|
} |
137
|
|
|
if ($unitCaptain->locationType != LOC_PLANET) { |
138
|
|
|
throw new $this->$exceptionClass('module_unit_captain_error_wrong_location', ERR_ERROR); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param V2UnitContainer $cUnit |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isEmpty($cUnit) { |
148
|
|
|
return |
149
|
|
|
empty($cUnit->playerOwnerId) |
150
|
|
|
|| |
151
|
|
|
is_null($cUnit->locationType) |
152
|
|
|
|| |
153
|
|
|
$cUnit->locationType === LOC_NONE |
154
|
|
|
|| |
155
|
|
|
empty($cUnit->locationId) |
156
|
|
|
|| |
157
|
|
|
empty($cUnit->type) |
158
|
|
|
|| |
159
|
|
|
empty($cUnit->snId) |
160
|
|
|
|| |
161
|
|
|
empty($cUnit->level); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param V2UnitContainer $cUnit |
166
|
|
|
* @param string $featureName |
167
|
|
|
* |
168
|
|
|
* return UnitFeature |
169
|
|
|
* |
170
|
|
|
* @return mixed|null |
171
|
|
|
*/ |
172
|
|
|
public function feature($cUnit, $featureName) { |
173
|
|
|
return isset($cUnit->features[$featureName]) ? $cUnit->features[$featureName] : null; |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.