Completed
Push — work-fleets ( d40084...46e295 )
by SuperNova.WS
05:22
created

Vector::_staticInit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
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
  public static function _staticInit($config) {
27
    if (static::$_isStaticInit) {
28
      return;
29
    }
30
31
    static::$knownGalaxies = intval($config->game_maxGalaxy);
32
    static::$knownSystems = intval($config->game_maxSystem);
33
    static::$knownPlanets = intval($config->game_maxPlanet);
34
    static::$galaxyDistance = intval($config->uni_galaxy_distance);
35
    static::$_isStaticInit = true;
36
  }
37
38
  /**
39
   * @param Vector $vector
40
   */
41
  public function readFromVector($vector) {
42
    $this->galaxy = $vector->galaxy;
43
    $this->system = $vector->system;
44
    $this->planet = $vector->planet;
45
    $this->type = $vector->type;
46
  }
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
  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
    if (is_string($galaxy) && $galaxy == Vector::READ_VECTOR && is_object($system) && $system instanceof Vector) {
82
      $this->readFromVector($system);
83
    } elseif (is_string($galaxy) && $galaxy == Vector::READ_PARAMS_FLEET && is_array($system)) {
84
      $this->readFromParamFleets($system);
85
    } else {
86
      $this->galaxy = intval($galaxy);
87
      $this->system = intval($system);
88
      $this->planet = intval($planet);
89
      $this->type = intval($type);
90
    }
91
  }
92
93
  /**
94
   * @param Vector $vector
95
   * @param bool   $returnZero
96
   *
97
   * @return int|number
98
   */
99
  public function distance($vector, $returnZero = false) {
100
    if ($this->galaxy != $vector->galaxy) {
101
      $distance = abs($this->galaxy - $vector->galaxy) * static::$galaxyDistance;
102
    } elseif ($this->system != $vector->system) {
103
      $distance = abs($this->system - $vector->system) * 5 * 19 + 2700;
104
    } elseif ($this->planet != $vector->planet) {
105
      $distance = abs($this->planet - $vector->planet) * 5 + 1000;
106
    } 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
      $distance = 0;
109
    } else {
110
      $distance = 5;
111
    }
112
113
    return $distance;
114
  }
115
116
  /**
117
   * @param array  $coordinates
118
   * @param string $prefix
119
   *
120
   * @return static
121
   */
122
  public static function convertToVector($coordinates, $prefix = '') {
123
    $galaxy = !empty($coordinates[$prefix . 'galaxy']) ? intval($coordinates[$prefix . 'galaxy']) : 0;
124
    $system = !empty($coordinates[$prefix . 'system']) ? intval($coordinates[$prefix . 'system']) : 0;
125
    $planet = !empty($coordinates[$prefix . 'planet']) ? intval($coordinates[$prefix . 'planet']) : 0;
126
    $type = !empty($coordinates[$prefix . 'type'])
127
      ? intval($coordinates[$prefix . 'type'])
128
      : (!empty($coordinates[$prefix . 'planet_type']) ? intval($coordinates[$prefix . 'planet_type']) : 0);
129
130
    return new static($galaxy, $system, $planet, $type);
131
  }
132
133
  /**
134
   * @param array $coordinates
135
   * @param bool  $returnZero
136
   *
137
   * @return int|number
138
   */
139
  public function distanceFromCoordinates($coordinates, $returnZero = false) {
140
    return $this->distance(static::convertToVector($coordinates), $returnZero);
141
  }
142
143
  /**
144
   * @param array $from
145
   * @param array $to
146
   * @param bool  $returnZero
147
   *
148
   * @return int|number
149
   */
150
  public static function distanceBetweenCoordinates($from, $to, $returnZero = false) {
151
    return static::convertToVector($from)->distanceFromCoordinates($to, $returnZero);
152
  }
153
154
  /**
155
   * @param array $planetRow
156
   *
157
   * @return bool
158
   */
159
  public function isSameLocation($planetRow) {
160
    return $this->distanceFromCoordinates($planetRow, true) == 0;
161
  }
162
163
  /**
164
   * @return bool
165
   */
166
  public function isInUniverse() {
167
    return
168
      $this->galaxy >= 1 && $this->galaxy <= static::$knownGalaxies &&
169
      $this->system >= 1 && $this->system <= static::$knownSystems;
170
  }
171
172
  /**
173
   * @return bool
174
   */
175
  public function isInSystem() {
176
    return $this->planet >= 1 && $this->planet <= static::$knownPlanets;
177
  }
178
179
  /**
180
   * @return bool
181
   */
182
  public function isInKnownSpace() {
183
    return $this->isInUniverse() && $this->isInSystem();
184
  }
185
186
}
187