Issues (1369)

classes/Bonus/ValueStorage.php (10 issues)

1
<?php
2
/**
3
 * Created by Gorlum 01.12.2017 6:54
4
 */
5
6
namespace Bonus;
7
8
use Common\ContainerPlus;
9
use \SN;
0 ignored issues
show
The type \SN was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
12
/**
13
 * Class ValueStorage
14
 *
15
 * Store calculated bonus values
16
 *
17
 * In future can be used to cache data in memory cache
18
 *
19
 * @package Bonus
20
 */
21
class ValueStorage extends ContainerPlus {
22
  /**
23
   * @var \Core\GlobalContainer
24
   */
25
  protected $gc;
26
27
  /**
28
   * @var ValueBonused[][] $values
29
   */
30
  protected $values = [];
31
32
  /**
33
   * @return \Core\GlobalContainer
34
   */
35
  public function getGlobalContainer() {
36
    return $this->gc;
37
  }
38
39
  public function __construct(array $values = array()) {
40
    parent::__construct($values);
41
42
    $this->gc = SN::$gc;
43
44
    $this->initValues();
45
  }
46
47
  protected function initValues() {
48
    $this[UNIT_SERVER_SPEED_BUILDING] = function (ValueStorage $vs) {
49
      return new ValueBonused(UNIT_SERVER_SPEED_BUILDING, floatval($vs->getGlobalContainer()->config->game_speed));
0 ignored issues
show
Bonus\UNIT_SERVER_SPEED_BUILDING of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueBonused::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
      return new ValueBonused(/** @scrutinizer ignore-type */ UNIT_SERVER_SPEED_BUILDING, floatval($vs->getGlobalContainer()->config->game_speed));
Loading history...
50
    };
51
    $this[UNIT_SERVER_SPEED_MINING] = function (ValueStorage $vs) {
52
      return new ValueBonused(UNIT_SERVER_SPEED_MINING, floatval($vs->getGlobalContainer()->config->resource_multiplier));
0 ignored issues
show
Bonus\UNIT_SERVER_SPEED_MINING of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueBonused::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
      return new ValueBonused(/** @scrutinizer ignore-type */ UNIT_SERVER_SPEED_MINING, floatval($vs->getGlobalContainer()->config->resource_multiplier));
Loading history...
53
    };
54
    $this[UNIT_SERVER_SPEED_FLEET] = function (ValueStorage $vs) {
55
      return new ValueBonused(UNIT_SERVER_SPEED_FLEET, floatval($vs->getGlobalContainer()->config->fleet_speed));
0 ignored issues
show
Bonus\UNIT_SERVER_SPEED_FLEET of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueBonused::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
      return new ValueBonused(/** @scrutinizer ignore-type */ UNIT_SERVER_SPEED_FLEET, floatval($vs->getGlobalContainer()->config->fleet_speed));
Loading history...
56
    };
57
    $this[UNIT_SERVER_SPEED_EXPEDITION] = function (ValueStorage $vs) {
0 ignored issues
show
The parameter $vs is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    $this[UNIT_SERVER_SPEED_EXPEDITION] = function (/** @scrutinizer ignore-unused */ ValueStorage $vs) {

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

Loading history...
58
      return new ValueBonused(UNIT_SERVER_SPEED_EXPEDITION, floatval(1));
0 ignored issues
show
Bonus\UNIT_SERVER_SPEED_EXPEDITION of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueBonused::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
      return new ValueBonused(/** @scrutinizer ignore-type */ UNIT_SERVER_SPEED_EXPEDITION, floatval(1));
Loading history...
59
    };
60
61
    $this[UNIT_SERVER_FLEET_NOOB_POINTS] = function (ValueStorage $vs) {
62
      $config = $vs->getGlobalContainer()->config;
63
64
      return new ValueBonused(
65
        UNIT_SERVER_FLEET_NOOB_POINTS,
0 ignored issues
show
Bonus\UNIT_SERVER_FLEET_NOOB_POINTS of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueBonused::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        /** @scrutinizer ignore-type */ UNIT_SERVER_FLEET_NOOB_POINTS,
Loading history...
66
        floatval($config->game_noob_points * $vs->getBase(UNIT_SERVER_SPEED_MINING))
0 ignored issues
show
Bonus\UNIT_SERVER_SPEED_MINING of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueStorage::getBase(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        floatval($config->game_noob_points * $vs->getBase(/** @scrutinizer ignore-type */ UNIT_SERVER_SPEED_MINING))
Loading history...
67
      );
68
    };
69
    $this[UNIT_SERVER_FLEET_NOOB_FACTOR] = function (ValueStorage $vs) {
70
      return new ValueBonused(UNIT_SERVER_FLEET_NOOB_FACTOR, floatval($vs->getGlobalContainer()->config->game_noob_factor));
0 ignored issues
show
Bonus\UNIT_SERVER_FLEET_NOOB_FACTOR of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueBonused::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
      return new ValueBonused(/** @scrutinizer ignore-type */ UNIT_SERVER_FLEET_NOOB_FACTOR, floatval($vs->getGlobalContainer()->config->game_noob_factor));
Loading history...
71
    };
72
73
    $this[UNIT_SERVER_PAYMENT_MM_PER_CURRENCY] = function (ValueStorage $vs) {
74
      return new ValueBonused(UNIT_SERVER_PAYMENT_MM_PER_CURRENCY, floatval($vs->getGlobalContainer()->config->payment_currency_exchange_mm_));
0 ignored issues
show
Bonus\UNIT_SERVER_PAYMENT_MM_PER_CURRENCY of type string is incompatible with the type integer expected by parameter $unitSnId of Bonus\ValueBonused::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
      return new ValueBonused(/** @scrutinizer ignore-type */ UNIT_SERVER_PAYMENT_MM_PER_CURRENCY, floatval($vs->getGlobalContainer()->config->payment_currency_exchange_mm_));
Loading history...
75
    };
76
  }
77
78
  /**
79
   * Get value object for supplied ID
80
   *
81
   * Supports only server and player units... for now
82
   *
83
   * @param int   $unitSnId
84
   * @param array $context - Context list of locations: [LOC_xxx => (data)]
85
   *
86
   * @return ValueBonused|mixed
87
   */
88
  public function getValueObject($unitSnId, $context = []) {
89
    if (isset($this[$unitSnId])) {
90
      // Server var
91
      $valueObject = $this[$unitSnId];
92
    } else {
93
      // Not a server var
94
      $valueObject = new ValueBonused($unitSnId, $this->getLevelNonServer($unitSnId, $context));
95
    }
96
97
    if ($valueObject instanceof ValueBonused) {
98
      $valueObject->getValue($context);
99
    }
100
101
    return $valueObject;
102
  }
103
104
  /**
105
   * @param int   $unitSnId
106
   * @param array $context - Context list of locations: [LOC_xxx => (data)]
107
   *
108
   * @return float|int
109
   */
110
  public function getValue($unitSnId, $context = []) {
111
112
    if (($vo = $this->getValueObject($unitSnId, $context)) instanceof ValueBonused) {
113
      $result = $vo->getValue();
114
    } else {
115
      $result = $vo;
116
    }
117
118
    return $result;
119
  }
120
121
  /**
122
   * @param int   $unitSnId
123
   * @param array $context - Context list of locations: [LOC_xxx => (data)]
124
   *
125
   * @return float|int
126
   */
127
  public function getBase($unitSnId, $context = []) {
128
    if (($vo = $this->getValueObject($unitSnId, $context)) instanceof ValueBonused) {
129
      $result = $vo->base;
130
    } else {
131
      $result = $vo;
132
    }
133
134
    return $result;
135
  }
136
137
  /**
138
   * @param array $user
139
   * @param array $planet
140
   * @param int   $unitSnId
141
   *
142
   * @return int|float|bool
143
   */
144
  protected function getLevel($user, $planet, $unitSnId) {
145
    return mrc_get_level($user, $planet, $unitSnId, true, true);
146
  }
147
148
  /**
149
   * @param int   $unitSnId
150
   * @param array $context - Context list of locations: [LOC_xxx => (data)]
151
   *
152
   * @return int|float|bool
153
   */
154
  protected function getLevelNonServer($unitSnId, $context = []) {
155
//    pdump($unitSnId, 'NON-server');
156
//      list($locationType, $locationId) = getLocationFromContext($context);
157
//      $fleet = !empty($context[LOC_FLEET]) ? $context[LOC_FLEET] : [];
158
    $user = !empty($context[LOC_USER]) ? $context[LOC_USER] : [];
159
    $planet = !empty($context[LOC_PLANET]) ? $context[LOC_PLANET] : [];
160
161
    return $this->getLevel($user, $planet, $unitSnId);
162
  }
163
164
}
165