forked from exodus4d/pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemmodel.php
More file actions
312 lines (259 loc) · 8.52 KB
/
Copy pathsystemmodel.php
File metadata and controls
312 lines (259 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* Created by PhpStorm.
* User: exodus4d
* Date: 23.02.15
* Time: 23:56
*/
namespace Model;
class SystemModel extends BasicModel {
const MAX_POS_X = 2300;
const MAX_POS_Y = 498;
protected $table = 'system';
protected $fieldConf = [
'mapId' => [
'belongs-to-one' => 'Model\MapModel'
],
'typeId' => [
'belongs-to-one' => 'Model\SystemTypeModel'
],
'statusId' => [
'belongs-to-one' => 'Model\SystemStatusModel'
],
'createdCharacterId' => [
'belongs-to-one' => 'Model\CharacterModel'
],
'updatedCharacterId' => [
'belongs-to-one' => 'Model\CharacterModel'
],
'signatures' => [
'has-many' => ['Model\SystemSignatureModel', 'systemId']
],
];
/**
* set an array with all data for a system
* @param $systemData
*/
public function setData($systemData){
foreach((array)$systemData as $key => $value){
if(!is_array($value)){
if($this->exists($key)){
$this->$key = $value;
}
}else{
// special array data
if($key == 'constellation'){
$this->constellationId = $value['id'];
$this->constellation = $value['name'];
}elseif($key == 'region'){
$this->regionId = $value['id'];
$this->region = $value['name'];
}elseif($key == 'type'){
$this->typeId = $value['id'];
}elseif($key == 'status'){
$this->statusId = $value['id'];
}elseif($key == 'position'){
$this->posX = $value['x'];
$this->posY = $value['y'];
}
}
}
}
/**
* get map data as object
* @return object
*/
public function getData(){
// check if there is cached data
$systemData = $this->getCacheData();
if(is_null($systemData)){
// no cached system data found
$systemData = (object) [];
$systemData->id = $this->id;
$systemData->mapId = is_object($this->mapId) ? $this->mapId->id : 0;
$systemData->systemId = $this->systemId;
$systemData->name = $this->name;
$systemData->alias = $this->alias;
$systemData->effect = $this->effect;
$systemData->security = $this->security;
$systemData->trueSec = $this->trueSec;
$systemData->region = (object) [];
$systemData->region->id = $this->regionId;
$systemData->region->name = $this->region;
$systemData->constellation = (object) [];
$systemData->constellation->id = $this->constellationId;
$systemData->constellation->name = $this->constellation;
$systemData->type = (object) [];
$systemData->type->id = $this->typeId->id;
$systemData->type->name = $this->typeId->name;
$systemData->status = (object) [];
$systemData->status->id = is_object($this->statusId) ? $this->statusId->id : 0;
$systemData->status->name = is_object($this->statusId) ? $this->statusId->name : '';
$systemData->locked = $this->locked;
$systemData->rally = $this->rally;
$systemData->description = $this->description;
$systemData->statics = $this->getStaticWormholeData();
$systemData->position = (object) [];
$systemData->position->x = $this->posX;
$systemData->position->y = $this->posY;
if($this->createdCharacterId){
$systemData->created = (object) [];
$systemData->created->character = $this->createdCharacterId->getData();
$systemData->created->created = strtotime($this->created);
}
if($this->updatedCharacterId){
$systemData->updated = (object) [];
$systemData->updated->character = $this->updatedCharacterId->getData();
$systemData->updated->updated = strtotime($this->updated);
}
// max caching time for a system
// the cached date has to be cleared manually on any change
// this includes system, connection,... changes (all dependencies)
$this->updateCacheData($systemData, '', 300);
}
return $systemData;
}
/**
* setter validation for x coordinate
* @param $posX
* @return int|number
*/
public function set_posX($posX){
$posX = abs($posX);
if($posX > self::MAX_POS_X){
$posX = self::MAX_POS_X;
}
return $posX;
}
/**
* setter validation for y coordinate
* @param $posY
* @return int|number
*/
public function set_posY($posY){
$posY = abs($posY);
if($posY > self::MAX_POS_Y){
$posY = self::MAX_POS_Y;
}
return $posY;
}
/**
* check object for model access
* @param $accessObject
* @return bool
*/
public function hasAccess($accessObject){
return $this->mapId->hasAccess($accessObject);
}
/**
* delete a system from a map
* hint: signatures and connections will be deleted on cascade
* @param $accessObject
*/
public function delete($accessObject){
if(! $this->dry()){
// check if user has access
if($this->hasAccess($accessObject)){
$this->erase();
}
}
}
/**
* get all signatures of this system
* @return array
*/
public function getSignatures(){
$this->filter('signatures', ['active = ?', 1], ['order' => 'name']);
$signatures = [];
if($this->signatures){
$signatures = $this->signatures;
}
return $signatures;
}
/**
* get all data for all Signatures in this system
* @return array
*/
public function getSignaturesData(){
$signatures = $this->getSignatures();
$signaturesData = [];
foreach($signatures as $signature){
$signaturesData[] = $signature->getData();
}
return $signaturesData;
}
/**
* get Signature by id and check for access
* @param $accessObject
* @param $id
* @return bool|null
*/
public function getSignatureById($accessObject, $id){
$signature = null;
if($this->hasAccess($accessObject)){
$this->filter('signatures', ['active = ? AND id = ?', 1, $id]);
if($this->signatures){
$signature = reset( $this->signatures );
}
}
return $signature;
}
/**
* get a signature by its "unique" 3-digit name
* @param $accessObject
* @param $name
* @return mixed|null
*/
public function getSignatureByName($accessObject, $name){
$signature = null;
if($this->hasAccess($accessObject)){
$this->filter('signatures', ['active = ? AND name = ?', 1, $name]);
if($this->signatures){
$signature = reset( $this->signatures );
}
}
return $signature;
}
/**
* checks weather this system is a wormhole
* @return bool
*/
protected function isWormhole(){
$isWormhole = false;
if($this->typeId->id == 1){
$isWormhole = true;
}
return $isWormhole;
}
/**
* get static WH data for this system
* -> any WH system has at least one static WH
* @return array
* @throws \Exception
*/
protected function getStaticWormholeData(){
$wormholeData = [];
// check if this system is a wormhole
if($this->isWormhole()){
$systemStaticModel = self::getNew('SystemStaticModel');
$systemStatics = $systemStaticModel->find([
'constellationId = :constellationId',
':constellationId' => $this->constellationId
]);
if( is_object($systemStatics) ){
foreach($systemStatics as $systemStatic){
$wormholeData[] = $systemStatic->getData();
}
}
}
return $wormholeData;
}
/**
* see parent
*/
public function clearCacheData(){
parent::clearCacheData();
// clear map cache as well
$this->mapId->clearCacheData();
}
}