Completed
Push — work-fleets ( 961997...006942 )
by SuperNova.WS
06:22
created

V2FleetModel::loadById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
1
<?php
2
/**
3
 * Created by Gorlum 17.08.2016 22:07
4
 */
5
6
namespace V2Fleet;
7
8
use Common\V2Location;
9
use DBStatic\DBStaticFleetACS;
10
use V2Unit\V2UnitList;
11
use Vector\Vector;
12
use Entity\KeyedModel;
13
14
/**
15
 * Class V2FleetModel
16
 *
17
 * @method V2FleetContainer buildContainer()
18
 * method V2FleetContainer loadById(mixed $dbId)
19
 *
20
 * @package V2Fleet
21
 */
22
class V2FleetModel extends KeyedModel {
23
  protected $location;
24
25
  /**
26
   * Name of table for this entity
27
   *
28
   * @var string $tableName
29
   */
30
  protected $tableName = 'fleets';
31
32
  protected $exceptionClass = 'Entity\EntityException';
33
  protected $entityContainerClass = 'V2Fleet\V2FleetContainer';
34
35
  protected $newProperties = array(
36
    'dbId'              => array(
37
      P_DB_FIELD => 'fleet_id',
38
    ),
39
    'ownerId'           => array(P_DB_FIELD => 'fleet_owner',),
40
    'arriveOwnerId'     => array(P_DB_FIELD => 'fleet_target_owner'),
41
    'departurePlanetId' => array(P_DB_FIELD => 'fleet_start_planet_id'),
42
    'arrivePlanetId'    => array(P_DB_FIELD => 'fleet_end_planet_id'),
43
44
    'missionType' => array(
45
      P_DB_FIELD      => 'fleet_mission',
46
      P_DB_FIELD_TYPE => TYPE_INTEGER,
47
    ),
48
    'status'      => array(
49
      P_DB_FIELD      => 'fleet_mess',
50
      P_DB_FIELD_TYPE => TYPE_INTEGER,
51
    ),
52
    'groupId'     => array(P_DB_FIELD => 'fleet_group',),
53
54
55
//    'fleet_start_galaxy'       => array(P_DB_FIELD => 'fleet_start_galaxy'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
56
//    'fleet_start_system'       => array(P_DB_FIELD => 'fleet_start_system'),
57
//    'fleet_start_planet'       => array(P_DB_FIELD => 'fleet_start_planet'),
58
//    'fleet_start_type'         => array(P_DB_FIELD => 'fleet_start_type'),
59
//    'fleet_end_galaxy'         => array(P_DB_FIELD => 'fleet_end_galaxy'),
60
//    'fleet_end_system'         => array(P_DB_FIELD => 'fleet_end_system'),
61
//    'fleet_end_planet'         => array(P_DB_FIELD => 'fleet_end_planet'),
62
//    'fleet_end_type'           => array(P_DB_FIELD => 'fleet_end_type'),
63
//    'fleet_resource_metal'     => array(P_DB_FIELD => 'fleet_resource_metal'),
64
//    'fleet_resource_crystal'   => array(P_DB_FIELD => 'fleet_resource_crystal'),
65
//    'fleet_resource_deuterium' => array(P_DB_FIELD => 'fleet_resource_deuterium'),
66
67
68
    'vectorDeparture' => array(P_DB_FIELD => 'fleet_start_galaxy'),
69
    'vectorArrive'    => array(P_DB_FIELD => 'fleet_end_galaxy'),
70
71
    'timeDeparture' => array(
72
      P_DB_FIELD      => 'start_time',
73
      P_DB_FIELD_TYPE => TYPE_INTEGER,
74
    ),
75
    'timeArrive'    => array(
76
      P_DB_FIELD      => 'fleet_start_time',
77
      P_DB_FIELD_TYPE => TYPE_INTEGER,
78
    ),
79
    'timeComplete'  => array(
80
      P_DB_FIELD      => 'fleet_end_stay',
81
      P_DB_FIELD_TYPE => TYPE_INTEGER,
82
    ),
83
    'timeReturn'    => array(
84
      P_DB_FIELD      => 'fleet_end_time',
85
      P_DB_FIELD_TYPE => TYPE_INTEGER,
86
    ),
87
88
    'shipsCount'  => array(P_DB_FIELD => 'fleet_amount'),
89
    'units'       => array(),
90
    'isReturning' => array(),
91
  );
92
93
  public function __construct(\Common\GlobalContainer $gc) {
94
    parent::__construct($gc);
95
96
    $this->accessors->set(P_CONTAINER_SET, 'dbId', function (V2FleetContainer $that, $value) {
97
      $that->setDirect('dbId', $value);
98
      $that->location->setLocationId($value);
99
    });
100
101
    $this->accessors->set(P_CONTAINER_SET, 'ownerId', function (V2FleetContainer $that, $value) {
102
      $that->setDirect('ownerId', $value);
103
      $that->location->setLocationPlayerId($value);
104
    });
105
106
    $this->accessors->set(P_CONTAINER_SET, 'owner', function (V2FleetContainer $that, $value) {
107
      $that->owner = $value;
108
      if(isset($value['id'])) {
109
        $that->ownerId = $value['id'];
110
      } else {
111
        $that->ownerId = 0;
112
      }
113
    });
114
115
    $this->accessors->set(P_CONTAINER_SET, 'departure', function (V2FleetContainer $that, $value) {
116
      $that->departure = $value;
117
      if(isset($value['id'])) {
118
        $that->departurePlanetId = $value['id'];
119
        $that->vectorDeparture = $value;
120
      } else {
121
        $that->departurePlanetId = 0;
122
      }
123
    });
124
125
    $this->accessors->set(P_CONTAINER_GET, 'vectorDeparture', function (V2FleetContainer $that) {
126
      $vector = new Vector();
127
      $that->setDirect('vectorDeparture', $vector);
128
129
      return $vector;
130
    }, ACCESSOR_SHARED);
131
    $this->accessors->set(P_CONTAINER_SET, 'vectorDeparture', function (V2FleetContainer $that, $value) {
132
      $vector = $that->vectorDeparture;
133
134
      if(is_array($value)) {
135
        $value = Vector::convertToVector($value);
136
      }
137
138
      if($value instanceof Vector) {
139
        $vector->readFromVector($value);
140
      } else {
141
        throw new \Exception('V2Fleet::vectorDeparture setter - value is not a Vector or array!', ERR_ERROR);
142
      }
143
    });
144
    $this->accessors->set(P_CONTAINER_IMPORT, 'vectorDeparture', array($this, 'importVector'));
145
    $this->accessors->set(P_CONTAINER_EXPORT, 'vectorDeparture', array($this, 'exportVector'));
146
147
    $this->accessors->set(P_CONTAINER_IMPORT, 'vectorArrive', array($this, 'importVector'));
148
    $this->accessors->set(P_CONTAINER_EXPORT, 'vectorArrive', array($this, 'exportVector'));
149
150
151
    $this->accessors->set(P_CONTAINER_GET, 'location', function (V2FleetContainer $that) {
152
//      if (is_null($location = $that->getDirect('location'))) {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
153
      $location = new V2Location(LOC_FLEET);
154
      $that->setDirect('location', $location);
155
156
      return $location;
157
    }, ACCESSOR_SHARED);
158
159
    $this->accessors->set(P_CONTAINER_GET, 'units', function (V2FleetContainer $that) {
160
//      if (is_null($units = $that->getDirect('units'))) {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
161
      $units = \classSupernova::$gc->unitList;
162
      $that->setDirect('units', $units);
163
164
      return $units;
165
    }, ACCESSOR_SHARED);
166
167
    $this->accessors->set(P_CONTAINER_GET, 'isReturning', function (V2FleetContainer $that) {
168
      return $that->status == FLEET_FLAG_RETURNING;
169
    });
170
171
  }
172
173
  public function importVector(V2FleetContainer $that, $propertyName) {
174
    if ($propertyName == 'vectorDeparture') {
175
      $that->vectorDeparture = Vector::convertToVector($that->row, FLEET_START_PREFIX);
176
    } else {
177
      $that->vectorArrive = Vector::convertToVector($that->row, FLEET_END_PREFIX);
178
    }
179
  }
180
181
  public function exportVector(V2FleetContainer $that, $propertyName) {
182
    if ($propertyName == 'vectorDeparture') {
183
      $that->row += $that->vectorDeparture->toArray(FLEET_START_PREFIX);
184
    } else {
185
      $that->row += $that->vectorArrive->toArray(FLEET_END_PREFIX);
186
    }
187
  }
188
189
  /**
190
   * @param array $array
191
   *
192
   * @return V2FleetContainer
193
   */
194
  public function fromArray($array) {
195
    /**
196
     * @var V2FleetContainer $cFleet
197
     */
198
    $cFleet = parent::fromArray($array);
199
200
    // TODO - load unit info from array
201
202
    foreach (array(
203
      RES_METAL     => 'fleet_resource_metal',
204
      RES_CRYSTAL   => 'fleet_resource_crystal',
205
      RES_DEUTERIUM => 'fleet_resource_deuterium',
206
    ) as $resourceId => $fieldName) {
207
      $cFleet->units->unitAdd($resourceId, $array[$fieldName]);
208
    }
209
210
    return $cFleet;
211
  }
212
213
  /**
214
   * @param int|string $dbId
215
   *
216
   * @return V2FleetContainer|false
217
   */
218
  public function loadById($dbId) {
219
    /**
220
     * @var V2FleetContainer $cFleet
221
     */
222
    $cFleet = parent::loadById($dbId);
223
224
    $cFleet->units->load($cFleet->location);
225
226
    return $cFleet;
227
  }
228
229
230
  protected function save(V2FleetContainer $cFleet) {
231
    throw new \Exception('V2FleetModel::dbSave() is not yet implemented');
232
  }
233
234
  public function isEmpty(V2FleetContainer $cFleet) {
235
    return
236
      // Fleet is empty if not units in it
237
      $cFleet->units->isEmpty()// parent::isEmpty($cFleet)
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
238
      ;
239
  }
240
241
  /**
242
   * Forcibly returns fleet before time outs
243
   */
244
  protected function doReturn(V2FleetContainer $cFleet) {
245
    if ($cFleet->isReturning) {
246
      return;
247
    }
248
249
    // Marking fleet as returning
250
    $cFleet->status = FLEET_FLAG_RETURNING;
251
252
    // If fleet not yet arrived - return time is equal already fled time
253
    if ($cFleet->timeArrive <= SN_TIME_NOW) {
254
      $returnTime = SN_TIME_NOW - $cFleet->timeDeparture;
255
    } else {
256
      // Arrived fleet on mission will return in same time as it takes to get to the destination
257
      $returnTime = $cFleet->timeArrive - $cFleet->timeDeparture;
258
    }
259
    $cFleet->timeReturn = SN_TIME_NOW + $returnTime;
260
261
    // Считаем, что флот уже долетел
262
    $cFleet->timeArrive = SN_TIME_NOW;
263
    // Отменяем работу в точке назначения
264
    $cFleet->timeComplete = 0;
265
    // Убираем флот из группы
266
    $oldGroupId = $cFleet->groupId;
267
    $cFleet->groupId = 0;
268
269
    // Записываем изменения в БД
270
    $this->save($cFleet);
271
272
    if ($oldGroupId) {
273
      // TODO: Make here to delete only one AKS - by adding aks_fleet_count to AKS table
274
      DBStaticFleetACS::db_fleet_aks_purge();
275
    }
276
  }
277
278
  public function commandReturn($userId, $fleet_id) {
279
    $fleetV2 = $this->loadById($fleet_id);
280
    if (!$fleetV2->dbId) {
281
      return;
282
    }
283
284
    if ($fleetV2->ownerId != $userId) {
285
      throw new \Exception($fleetV2->ownerId, ERR_ERROR);
286
    }
287
288
    $this->doReturn($fleetV2);
0 ignored issues
show
Security Bug introduced by
It seems like $fleetV2 defined by $this->loadById($fleet_id) on line 279 can also be of type false; however, V2Fleet\V2FleetModel::doReturn() does only seem to accept object<V2Fleet\V2FleetContainer>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
289
  }
290
291
}
292