Completed
Push — work-fleets ( b1376e...d6880d )
by SuperNova.WS
11:23
created

V2FleetModel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 1 Features 1
Metric Value
c 9
b 1
f 1
dl 0
loc 182
ccs 0
cts 94
cp 0
rs 10
wmc 14
lcom 0
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 79 3
A importVector() 0 7 2
A exportVector() 0 7 2
A fromArray() 0 18 2
B commandReturn() 0 34 4
A dbSave() 0 3 1
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
    $this->accessors->setAccessor('location', P_CONTAINER_GET, function (V2FleetContainer $that) {
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
    $this->accessors->setAccessor('units', P_CONTAINER_GET, function (V2FleetContainer $that) {
107
      if (is_null($units = $that->getDirect('units'))) {
108
        $units = new V2UnitList();
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
  /**
162
   * Forcibly returns fleet before time outs
163
   */
164
  public function commandReturn(V2FleetContainer $cFleet) {
165
    if($cFleet->isReturning) {
166
      return;
167
    }
168
169
    // Marking fleet as returning
170
    $cFleet->status = FLEET_FLAG_RETURNING;
171
172
    // If fleet not yet arrived - return time is equal already fled time
173
    if($cFleet->timeArrive <= SN_TIME_NOW) {
174
      $returnTime = SN_TIME_NOW - $cFleet->timeDeparture;
175
    } else {
176
      // Arrived fleet on mission will return in same time as it takes to get to the destination
177
      $returnTime = $cFleet->timeArrive - $cFleet->timeDeparture;
178
    }
179
//    $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...
180
    $cFleet->timeReturn = SN_TIME_NOW + $returnTime;
181
182
    // Считаем, что флот уже долетел
183
    $cFleet->timeArrive = SN_TIME_NOW;
184
    // Отменяем работу в точке назначения
185
    $cFleet->timeComplete = 0;
186
    // Убираем флот из группы
187
    $oldGroupId = $cFleet->groupId;
188
    $cFleet->groupId = 0;
189
190
    // Записываем изменения в БД
191
    $this->dbSave($cFleet);
192
193
    if ($oldGroupId) {
194
      // TODO: Make here to delete only one AKS - by adding aks_fleet_count to AKS table
195
      DBStaticFleetACS::db_fleet_aks_purge();
196
    }
197
  }
198
199
  public 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...
200
201
  }
202
203
}
204