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

Vector::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace Vector;
4
5
use classConfig;
6
7
class Vector {
8
9
  const READ_VECTOR = 'readVector';
10
  const READ_PARAMS_FLEET = 'readParamsFleet';
11
12
  public static $knownGalaxies = 0;
13
  public static $knownSystems = 0;
14
  public static $knownPlanets = 0;
15
  public static $galaxyDistance = 20000;
16
  protected static $_isStaticInit = false;
17
18
  public $galaxy = 0;
19
  public $system = 0;
20
  public $planet = 0;
21
  public $type = PT_NONE;
22
23
  /**
24
   * @param classConfig $config
25
   */
26 1
  public static function _staticInit($config) {
27 1
    if (static::$_isStaticInit) {
28
      return;
29
    }
30
31 1
    static::$knownGalaxies = intval($config->game_maxGalaxy);
32 1
    static::$knownSystems = intval($config->game_maxSystem);
33 1
    static::$knownPlanets = intval($config->game_maxPlanet);
34 1
    static::$galaxyDistance = intval($config->uni_galaxy_distance);
35 1
    static::$_isStaticInit = true;
36 1
  }
37
38
  /**
39
   * @param Vector $vector
40
   */
41 1
  public function readFromVector($vector) {
42 1
    $this->galaxy = $vector->galaxy;
43 1
    $this->system = $vector->system;
44 1
    $this->planet = $vector->planet;
45 1
    $this->type = $vector->type;
46 1
  }
47
48
  /**
49
   * @param string $paramName
50
   * @param array  $planetRow
51
   *
52
   * @return int
53
   */
54
  protected function getParamInt($paramName, $planetRow) {
55
    $default = empty($planetRow[$paramName]) ? 0 : $planetRow[$paramName];
56
57
    return sys_get_param_int($paramName, $default);
58
  }
59
60
  /**
61
   * @param array $planetRow
62
   */
63
  public function readFromParamFleets($planetRow = array()) {
64
    $this->galaxy = $this->getParamInt('galaxy', $planetRow);
65
    $this->system = $this->getParamInt('system', $planetRow);
66
    $this->planet = $this->getParamInt('planet', $planetRow);
67
    $this->type = $this->getParamInt('planet_type', $planetRow);
68
  }
69
70
  /**
71
   * Vector constructor.
72
   *
73
   * @param int|string       $galaxy
74
   * @param int|Vector|array $system
75
   * @param int              $planet
76
   * @param int              $type
77
   */
78 2
  public function __construct($galaxy = 0, $system = 0, $planet = 0, $type = PT_NONE) {
79
    // static::_staticInit();
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...
80
81 2
    if (is_string($galaxy) && $galaxy == Vector::READ_VECTOR && is_object($system) && $system instanceof Vector) {
82 1
      $this->readFromVector($system);
83 2
    } elseif (is_string($galaxy) && $galaxy == Vector::READ_PARAMS_FLEET && is_array($system)) {
84
      $this->readFromParamFleets($system);
85
    } else {
86 2
      $this->galaxy = intval($galaxy);
87 2
      $this->system = intval($system);
88 2
      $this->planet = intval($planet);
89 2
      $this->type = intval($type);
90
    }
91 2
  }
92
93
  /**
94
   * @param Vector $vector
95
   * @param bool   $returnZero
96
   *
97
   * @return int|number
98
   */
99 60
  public function distance($vector, $returnZero = false) {
100 60
    if ($this->galaxy != $vector->galaxy) {
101 18
      $distance = abs($this->galaxy - $vector->galaxy) * static::$galaxyDistance;
102 60
    } elseif ($this->system != $vector->system) {
103 18
      $distance = abs($this->system - $vector->system) * 5 * 19 + 2700;
104 42
    } elseif ($this->planet != $vector->planet) {
105 18
      $distance = abs($this->planet - $vector->planet) * 5 + 1000;
106 24
    } elseif ($returnZero && $this->type == $vector->type) {
107
      // && $this->type != PT_NONE && $vector->type != PT_NONE
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
108 1
      $distance = 0;
109 1
    } else {
110 5
      $distance = 5;
111
    }
112
113 60
    return $distance;
114
  }
115
116
  /**
117
   * @param array  $coordinates
118
   * @param string $prefix
119
   *
120
   * @return static
121
   */
122 8
  public static function convertToVector($coordinates, $prefix = '') {
123 8
    $galaxy = !empty($coordinates[$prefix . 'galaxy']) ? intval($coordinates[$prefix . 'galaxy']) : 0;
124 8
    $system = !empty($coordinates[$prefix . 'system']) ? intval($coordinates[$prefix . 'system']) : 0;
125 8
    $planet = !empty($coordinates[$prefix . 'planet']) ? intval($coordinates[$prefix . 'planet']) : 0;
126 8
    $type = !empty($coordinates[$prefix . 'type'])
127 8
      ? intval($coordinates[$prefix . 'type'])
128 8
      : (!empty($coordinates[$prefix . 'planet_type']) ? intval($coordinates[$prefix . 'planet_type']) : 0);
129
130 8
    return new static($galaxy, $system, $planet, $type);
131
  }
132
133
  /**
134
   * @param string $prefix
135
   *
136
   * @return array
137
   */
138
  public function toArray($prefix = '') {
139
    $array = array(
140
      $prefix . 'galaxy' => $this->galaxy,
141
      $prefix . 'system' => $this->system,
142
      $prefix . 'planet' => $this->planet,
143
      $prefix . 'type'   => $this->type,
144
    );
145
146
    return $array;
147
  }
148
149
  /**
150
   * @param array $coordinates
151
   * @param bool  $returnZero
152
   *
153
   * @return int|number
154
   */
155 8
  public function distanceFromCoordinates($coordinates, $returnZero = false) {
156 8
    return $this->distance(static::convertToVector($coordinates), $returnZero);
157
  }
158
159
  /**
160
   * @param array $from
161
   * @param array $to
162
   * @param bool  $returnZero
163
   *
164
   * @return int|number
165
   */
166
  public static function distanceBetweenCoordinates($from, $to, $returnZero = false) {
167
    return static::convertToVector($from)->distanceFromCoordinates($to, $returnZero);
168
  }
169
170
  /**
171
   * @param array $planetRow
172
   *
173
   * @return bool
174
   */
175
  public function isSameLocation($planetRow) {
176
    return $this->distanceFromCoordinates($planetRow, true) == 0;
177
  }
178
179
  /**
180
   * @return bool
181
   */
182
  public function isInUniverse() {
183
    return
184
      $this->galaxy >= 1 && $this->galaxy <= static::$knownGalaxies &&
185
      $this->system >= 1 && $this->system <= static::$knownSystems;
186
  }
187
188
  /**
189
   * @return bool
190
   */
191
  public function isInSystem() {
192
    return $this->planet >= 1 && $this->planet <= static::$knownPlanets;
193
  }
194
195
  /**
196
   * @return bool
197
   */
198
  public function isInKnownSpace() {
199
    return $this->isInUniverse() && $this->isInSystem();
200
  }
201
202
}
203