Completed
Push — work-fleets ( d6880d...ad253d )
by SuperNova.WS
07:04
created

V2FleetModel::doReturn()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 34
ccs 0
cts 20
cp 0
crap 20
rs 8.5806
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
   * Name of key field field in this table
33
   *
34
   * @var string $idFieldName
35
   */
36
  protected $idFieldName = 'fleet_id';
37
38
  protected $exceptionClass = 'Entity\EntityException';
39
  protected $entityContainerClass = 'V2Fleet\V2FleetContainer';
40
41
  public function __construct(\Common\GlobalContainer $gc) {
42
    parent::__construct($gc);
43
44
    $this->extendProperties(array(
45
      'ownerId'           => array(P_DB_FIELD => 'fleet_owner',),
46
      'arriveOwnerId'     => array(P_DB_FIELD => 'fleet_target_owner'),
47
      'departurePlanetId' => array(P_DB_FIELD => 'fleet_start_planet_id'),
48
      'arrivePlanetId'    => array(P_DB_FIELD => 'fleet_end_planet_id'),
49
50
      'missionType' => array(P_DB_FIELD => 'fleet_mission'),
51
      'status'      => array(P_DB_FIELD => 'fleet_mess'),
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(P_DB_FIELD => 'start_time'),
72
      'timeArrive'    => array(P_DB_FIELD => 'fleet_start_time'),
73
      'timeComplete'  => array(P_DB_FIELD => 'fleet_end_stay'),
74
      'timeReturn'    => array(P_DB_FIELD => 'fleet_end_time'),
75
76
      'shipsCount'  => array(P_DB_FIELD => 'fleet_amount'),
77
      'units'       => array(),
78
      'isReturning' => array(),
79
    ));
80
81 View Code Duplication
    $this->accessors->setAccessor('location', P_CONTAINER_GET, 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...
82
      if (is_null($location = $that->getDirect('location'))) {
83
        $location = new V2Location(LOC_FLEET);
84
        $that->setDirect('location', $location);
85
      }
86
87
      return $location;
88
    });
89
90
    $this->accessors->setAccessor('dbId', P_CONTAINER_SET, function (V2FleetContainer $that, $value) {
91
      $that->setDirect('dbId', $value);
92
      $that->location->setLocationId($value);
93
    });
94
95
    $this->accessors->setAccessor('ownerId', P_CONTAINER_SET, function (V2FleetContainer $that, $value) {
96
      $that->setDirect('ownerId', $value);
97
      $that->location->setLocationPlayerId($value);
98
    });
99
100
    $this->accessors->setAccessor('vectorDeparture', P_CONTAINER_IMPORT, array($this, 'importVector'));
101
    $this->accessors->setAccessor('vectorDeparture', P_CONTAINER_EXPORT, array($this, 'exportVector'));
102
    $this->accessors->setAccessor('vectorArrive', P_CONTAINER_IMPORT, array($this, 'importVector'));
103
    $this->accessors->setAccessor('vectorArrive', P_CONTAINER_EXPORT, array($this, 'exportVector'));
104
105
106 View Code Duplication
    $this->accessors->setAccessor('units', P_CONTAINER_GET, 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...
107
      if (is_null($units = $that->getDirect('units'))) {
108
        $units = \classSupernova::$gc->unitList;
109
        $that->setDirect('units', $units);
110
      }
111
112
      return $units;
113
    });
114
115
    $this->accessors->setAccessor('isReturning', P_CONTAINER_GET, function (V2FleetContainer $that) {
116
      return $that->status == FLEET_FLAG_RETURNING;
117
    });
118
119
  }
120
121
  public function importVector(V2FleetContainer $that, $propertyName) {
122
    if ($propertyName == 'vectorDeparture') {
123
      $that->vectorDeparture = Vector::convertToVector($that->row, FLEET_START_PREFIX);
124
    } else {
125
      $that->vectorArrive = Vector::convertToVector($that->row, FLEET_END_PREFIX);
126
    }
127
  }
128
129
  public function exportVector(V2FleetContainer $that, $propertyName) {
130
    if ($propertyName == 'vectorDeparture') {
131
      $that->row += $that->vectorDeparture->toArray(FLEET_START_PREFIX);
132
    } else {
133
      $that->row += $that->vectorArrive->toArray(FLEET_END_PREFIX);
134
    }
135
  }
136
137
  /**
138
   * @param array $array
139
   *
140
   * @return V2FleetContainer
141
   */
142
  public function fromArray($array) {
143
    /**
144
     * @var V2FleetContainer $cFleet
145
     */
146
    $cFleet = parent::fromArray($array);
147
148
    $cFleet->units->load($cFleet->location);
149
150
    foreach (array(
151
      RES_METAL     => 'fleet_resource_metal',
152
      RES_CRYSTAL   => 'fleet_resource_crystal',
153
      RES_DEUTERIUM => 'fleet_resource_deuterium',
154
    ) as $resourceId => $fieldName) {
155
      $cFleet->units->unitAdd($resourceId, $array[$fieldName]);
156
    }
157
158
    return $cFleet;
159
  }
160
161
  protected function dbSave($cFleet) {
0 ignored issues
show
Unused Code introduced by
The parameter $cFleet is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
162
    throw new \Exception('V2FleetModel::dbSave() is not yet implemented');
163
  }
164
165
  /**
166
   * Forcibly returns fleet before time outs
167
   */
168
  protected function doReturn(V2FleetContainer $cFleet) {
169
    if ($cFleet->isReturning) {
170
      return;
171
    }
172
173
    // Marking fleet as returning
174
    $cFleet->status = FLEET_FLAG_RETURNING;
175
176
    // If fleet not yet arrived - return time is equal already fled time
177
    if ($cFleet->timeArrive <= SN_TIME_NOW) {
178
      $returnTime = SN_TIME_NOW - $cFleet->timeDeparture;
179
    } else {
180
      // Arrived fleet on mission will return in same time as it takes to get to the destination
181
      $returnTime = $cFleet->timeArrive - $cFleet->timeDeparture;
182
    }
183
//    $ReturnFlyingTime = ($cFleet->timeComplete != 0 && $cFleet->timeArrive < SN_TIME_NOW ? $cFleet->timeArrive : SN_TIME_NOW) - $cFleet->timeDeparture + SN_TIME_NOW + 1;
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
184
    $cFleet->timeReturn = SN_TIME_NOW + $returnTime;
185
186
    // Считаем, что флот уже долетел
187
    $cFleet->timeArrive = SN_TIME_NOW;
188
    // Отменяем работу в точке назначения
189
    $cFleet->timeComplete = 0;
190
    // Убираем флот из группы
191
    $oldGroupId = $cFleet->groupId;
192
    $cFleet->groupId = 0;
193
194
    // Записываем изменения в БД
195
    $this->dbSave($cFleet);
196
197
    if ($oldGroupId) {
198
      // TODO: Make here to delete only one AKS - by adding aks_fleet_count to AKS table
199
      DBStaticFleetACS::db_fleet_aks_purge();
200
    }
201
  }
202
203
  public function commandReturn($userId, $fleet_id) {
204
    $fleetV2 = $this->loadById($fleet_id);
205
    if (!$fleetV2->dbId) {
206
      return;
207
    }
208
209
    if ($fleetV2->ownerId != $userId) {
210
      throw new \Exception('Hack attempt 302');
211
    }
212
213
    $this->doReturn($fleetV2);
214
  }
215
216
}
217