Completed
Push — work-fleets ( b30f48...88e4e3 )
by SuperNova.WS
08:13
created

V2FleetModel::importVector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 5
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 6
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 View Code Duplication
    $this->accessors->set(P_CONTAINER_GET, 'location', function (V2FleetContainer $that) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
      if (is_null($location = $that->getDirect('location'))) {
98
        $location = new V2Location(LOC_FLEET);
99
        $that->setDirect('location', $location);
100
      }
101
102
      return $location;
103
    });
104
105
    $this->accessors->set(P_CONTAINER_SET, 'dbId', function (V2FleetContainer $that, $value) {
106
      $that->setDirect('dbId', $value);
107
      $that->location->setLocationId($value);
108
    });
109
110
    $this->accessors->set(P_CONTAINER_SET, 'ownerId', function (V2FleetContainer $that, $value) {
111
      $that->setDirect('ownerId', $value);
112
      $that->location->setLocationPlayerId($value);
113
    });
114
115
    $this->accessors->set(P_CONTAINER_IMPORT, 'vectorDeparture', array($this, 'importVector'));
116
    $this->accessors->set(P_CONTAINER_EXPORT, 'vectorDeparture', array($this, 'exportVector'));
117
    $this->accessors->set(P_CONTAINER_IMPORT, 'vectorArrive', array($this, 'importVector'));
118
    $this->accessors->set(P_CONTAINER_EXPORT, 'vectorArrive', array($this, 'exportVector'));
119
120
121 View Code Duplication
    $this->accessors->set(P_CONTAINER_GET, 'units', function (V2FleetContainer $that) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
      if (is_null($units = $that->getDirect('units'))) {
123
        $units = \classSupernova::$gc->unitList;
124
        $that->setDirect('units', $units);
125
      }
126
127
      return $units;
128
    });
129
130
    $this->accessors->set(P_CONTAINER_GET, 'isReturning', function (V2FleetContainer $that) {
131
      return $that->status == FLEET_FLAG_RETURNING;
132
    });
133
134
  }
135
136
  public function importVector(V2FleetContainer $that, $propertyName) {
137
    if ($propertyName == 'vectorDeparture') {
138
      $that->vectorDeparture = Vector::convertToVector($that->row, FLEET_START_PREFIX);
139
    } else {
140
      $that->vectorArrive = Vector::convertToVector($that->row, FLEET_END_PREFIX);
141
    }
142
  }
143
144
  public function exportVector(V2FleetContainer $that, $propertyName) {
145
    if ($propertyName == 'vectorDeparture') {
146
      $that->row += $that->vectorDeparture->toArray(FLEET_START_PREFIX);
147
    } else {
148
      $that->row += $that->vectorArrive->toArray(FLEET_END_PREFIX);
149
    }
150
  }
151
152
  /**
153
   * @param array $array
154
   *
155
   * @return V2FleetContainer
156
   */
157
  public function fromArray($array) {
158
    /**
159
     * @var V2FleetContainer $cFleet
160
     */
161
    $cFleet = parent::fromArray($array);
162
163
    $cFleet->units->load($cFleet->location);
164
165
    foreach (array(
166
      RES_METAL     => 'fleet_resource_metal',
167
      RES_CRYSTAL   => 'fleet_resource_crystal',
168
      RES_DEUTERIUM => 'fleet_resource_deuterium',
169
    ) as $resourceId => $fieldName) {
170
      $cFleet->units->unitAdd($resourceId, $array[$fieldName]);
171
    }
172
173
    return $cFleet;
174
  }
175
176
  protected function save(V2FleetContainer $cFleet) {
177
    throw new \Exception('V2FleetModel::dbSave() is not yet implemented');
178
  }
179
180
  public function isEmpty(V2FleetContainer $cFleet) {
181
    return
182
      // Fleet is empty if not units in it
183
      $cFleet->units->isEmpty()
184
185
      // 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...
186
      ;
187
  }
188
189
  /**
190
   * Forcibly returns fleet before time outs
191
   */
192
  protected function doReturn(V2FleetContainer $cFleet) {
193
    if ($cFleet->isReturning) {
194
      return;
195
    }
196
197
    // Marking fleet as returning
198
    $cFleet->status = FLEET_FLAG_RETURNING;
199
200
    // If fleet not yet arrived - return time is equal already fled time
201
    if ($cFleet->timeArrive <= SN_TIME_NOW) {
202
      $returnTime = SN_TIME_NOW - $cFleet->timeDeparture;
203
    } else {
204
      // Arrived fleet on mission will return in same time as it takes to get to the destination
205
      $returnTime = $cFleet->timeArrive - $cFleet->timeDeparture;
206
    }
207
    $cFleet->timeReturn = SN_TIME_NOW + $returnTime;
208
209
    // Считаем, что флот уже долетел
210
    $cFleet->timeArrive = SN_TIME_NOW;
211
    // Отменяем работу в точке назначения
212
    $cFleet->timeComplete = 0;
213
    // Убираем флот из группы
214
    $oldGroupId = $cFleet->groupId;
215
    $cFleet->groupId = 0;
216
217
    // Записываем изменения в БД
218
    $this->save($cFleet);
219
220
    if ($oldGroupId) {
221
      // TODO: Make here to delete only one AKS - by adding aks_fleet_count to AKS table
222
      DBStaticFleetACS::db_fleet_aks_purge();
223
    }
224
  }
225
226
  public function commandReturn($userId, $fleet_id) {
227
    $fleetV2 = $this->loadById($fleet_id);
228
    if (!$fleetV2->dbId) {
229
      return;
230
    }
231
232
    if ($fleetV2->ownerId != $userId) {
233
      throw new \Exception($fleetV2->ownerId, ERR_ERROR);
234
    }
235
236
    $this->doReturn($fleetV2);
237
  }
238
239
}
240