Issues (1369)

classes/Pages/Deprecated/PageFleet5Gathering.php (6 issues)

1
<?php
2
/**
3
 * Created by Gorlum 30.09.2017 11:01
4
 */
5
6
namespace Pages\Deprecated;
7
8
use DBAL\db_mysql;
9
use Planet\DBStaticPlanet;
10
use \HelperString;
0 ignored issues
show
The type \HelperString 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...
11
use SN;
12
use SnTemplate;
13
14
class PageFleet5Gathering {
15
16
  /**
17
   * @var \classLocale $lang
18
   */
19
  protected $lang;
20
21
  /**
22
   * @var float[] $infoTransportCapacity
23
   */
24
  protected $infoTransportCapacity;
25
26
  public function __construct() {
27
    global $lang;
28
29
    $this->lang = $lang;
30
    $this->infoTransportCapacity = $this->getTransportUnitsCapacity();
31
  }
32
33
  /**
34
   * @param array     $user
35
   * @param array     $planetCurrent
36
   * @param float[][] $resources_taken - [int|string $planetId][int $resourceId] => float $resourceAmount
37
   *
38
   * @return array
39
   */
40
  public function flt_build_gathering(&$user, &$planetCurrent, $resources_taken = []) {
41
    // Caching sn_data names for quick access
42
    $planet_list = [];
43
44
    $takeAllResources = !is_array($resources_taken) || empty($resources_taken);
0 ignored issues
show
The condition is_array($resources_taken) is always true.
Loading history...
45
    if ($takeAllResources) {
46
      $query = '';
47
    } else {
48
      $query = implode(',', array_keys($resources_taken));
49
      $query = " AND `destruyed` = 0 AND `id` IN ({$query})";
50
    }
51
52
    $planets_db_list = DBStaticPlanet::db_planet_list_sorted($user, $planetCurrent['id'], $query);
53
    if (!is_array($planets_db_list)) {
54
      $planets_db_list = [];
55
    }
56
57
    foreach ($planets_db_list as $planet_id => &$planetRecord) {
58
      // begin planet loop
59
      db_mysql::db_transaction_start();
60
      // Вот тут надо посчитать - отработать очереди и выяснить, сколько ресов на каждой планете
61
      $planetRecord = sys_o_get_updated($user['id'], $planetRecord['id'], SN_TIME_NOW, true);
62
      $planetRecord = $planetRecord['planet'];
63
      db_mysql::db_transaction_commit();
64
65
      if ($takeAllResources) {
66
        $resources_taken[$planet_id] = 1;
67
      }
68
69
      $planetResources = $this->calcPlanetResources($user, $resources_taken, $planetRecord);
70
      $fleetCapacityList = $this->calcFleetCapacity($user, $planetRecord);
71
      $fleetFullCapacity = array_sum($fleetCapacityList);
72
73
      $fleet = $this->calcShipAmount($fleetCapacityList, min($planetResources, $fleetFullCapacity));
74
75
      $result = ATTACK_NO_FLEET;
76
      $travel_data = null;
77
      if (!empty($fleet)) {
78
        $travel_data = flt_travel_data($user, $planetCurrent, $planetRecord, $fleet, 10);
79
80
        if (floor(mrc_get_level($user, $planetRecord, RES_DEUTERIUM, true)) >= $travel_data['consumption']) {
81
          $will_take = min($planetResources, $fleetFullCapacity) - $travel_data['consumption'];
82
83
          $resourcesTaken = $this->fillFleetResources($user, $resources_taken, $planetRecord, $will_take, $fleet);
84
          $result = ATTACK_ALLOWED;
85
        } else {
86
          $result = ATTACK_NO_FUEL;
87
        }
88
      }
89
90
      $planet_list[$planet_id] =
91
        [
92
          'PLANET_DB_DATA' => $planetRecord,
93
          'ID'             => $planetRecord['id'],
94
          'NAME'           => $planetRecord['name'],
95
          'GALAXY'         => $planetRecord['galaxy'],
96
          'SYSTEM'         => $planetRecord['system'],
97
          'PLANET'         => $planetRecord['planet'],
98
          'TYPE'           => $planetRecord['planet_type'],
99
          'TYPE_PRINT'     => $this->lang['sys_planet_type'][$planetRecord['planet_type']],
100
          'METAL'          => floor($planetRecord['metal']),
101
          'CRYSTAL'        => floor($planetRecord['crystal']),
102
          'DEUTERIUM'      => floor($planetRecord['deuterium']),
103
          'METAL_TEXT'     => HelperString::numberFloorAndFormat($planetRecord['metal']),
104
          'CRYSTAL_TEXT'   => HelperString::numberFloorAndFormat($planetRecord['crystal']),
105
          'DEUTERIUM_TEXT' => HelperString::numberFloorAndFormat($planetRecord['deuterium']),
106
          'RESOURCES'      => $planetResources,
107
          'RESOURCES_TEXT' => HelperString::numberFloorAndFormat($planetResources),
108
109
          'FLEET'               => $fleet,
110
          'FLEET_RESOURCES'     => $resourcesTaken,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $resourcesTaken does not seem to be defined for all execution paths leading up to this point.
Loading history...
111
          'FLEET_CAPACITY'      => $fleetFullCapacity,
112
          'FLEET_CAPACITY_TEXT' => prettyNumberStyledCompare($fleetFullCapacity, -$planetResources),
113
114
          'RESULT' => $result,
115
//          'MESSAGE' => $this->lang['fl_attack_error'][$result],
116
        ]
117
        + (!empty($travel_data) ?
118
          [
119
            'FLEET_SPEED'   => $travel_data['fleet_speed'],
120
            'DISTANCE'      => $travel_data['distance'],
121
            'DURATION'      => $travel_data['duration'],
122
            'DURATION_TEXT' => $travel_data['duration'] ? pretty_time($travel_data['duration']) : $this->lang['flt_no_fuel'],
123
            'CONSUMPTION'   => $travel_data['consumption'],
124
          ]
125
          : []);
126
    } // end planet loop
127
128
    return $planet_list;
129
  }
130
131
  /**
132
   * @return array
133
   */
134
  protected function getTransportUnitsCapacity() {
135
    $transports = [];
136
    foreach (sn_get_groups('flt_transports') as $transport_id) {
137
      $transports[$transport_id] = get_unit_param($transport_id, P_CAPACITY);
138
    }
139
    arsort($transports);
140
141
    return $transports;
142
  }
143
144
  /**
145
   * @param array $user
146
   * @param array $resources_taken
147
   * @param array $planet_db_data
148
   *
149
   * @return float
150
   */
151
  protected function calcPlanetResources(&$user, $resources_taken, $planet_db_data) {
152
    $planet_resources = 0;
153
    foreach (sn_get_groups('resources_loot') as $resource_id) {
154
      if ($resources_taken[$planet_db_data['id']] == 1 || $resources_taken[$planet_db_data['id']][$resource_id] > 0) {
155
        $planet_resources += floor(mrc_get_level($user, $planet_db_data, $resource_id, true, true));
0 ignored issues
show
It seems like mrc_get_level($user, $pl...esource_id, true, true) can also be of type boolean; however, parameter $num of floor() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

155
        $planet_resources += floor(/** @scrutinizer ignore-type */ mrc_get_level($user, $planet_db_data, $resource_id, true, true));
Loading history...
156
      }
157
    }
158
159
    return $planet_resources;
160
  }
161
162
  /**
163
   * @param array $user
164
   * @param array $planet_db_data
165
   *
166
   * @return float[]
167
   */
168
  protected function calcFleetCapacity(&$user, $planet_db_data) {
169
    $fleetCapacityList = [];
170
    foreach ($this->infoTransportCapacity as $ship_id => $ship_capacity) {
171
      if (($ship_count = mrc_get_level($user, $planet_db_data, $ship_id, true, true)) > 0) {
172
        $fleetCapacityList[$ship_id] = $ship_count * $ship_capacity;
173
      }
174
    }
175
176
    return $fleetCapacityList;
177
  }
178
179
  /**
180
   * @param float[] $fleetCapacityList - List of capacities per ship
181
   * @param float   $maxResourcesToTake - Maximum resources that can be taken from this planet with whole transport fleet
182
   *
183
   * @return array
184
   */
185
  protected function calcShipAmount($fleetCapacityList, $maxResourcesToTake) {
186
    $fleet = [];
187
    foreach ($fleetCapacityList as $ship_id => $shipCapacity) {
188
      $can_take = min($maxResourcesToTake, $shipCapacity);
189
      if ($can_take <= 0) {
190
        continue;
191
      }
192
193
      $fleet[$ship_id] = ceil($can_take / $this->infoTransportCapacity[$ship_id]);
194
195
      $maxResourcesToTake -= $can_take;
196
      if ($maxResourcesToTake <= 0) {
197
        break;
198
      }
199
    }
200
201
    return $fleet;
202
  }
203
204
  /**
205
   * @param array $user
206
   * @param array $resources_taken
207
   * @param array $planetRecord
208
   * @param float $will_take
209
   * @param array $fleet
210
   */
211
  protected function fillFleetResources(&$user, $resources_taken, $planetRecord, $will_take, &$fleet) {
0 ignored issues
show
The parameter $fleet 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

211
  protected function fillFleetResources(&$user, $resources_taken, $planetRecord, $will_take, /** @scrutinizer ignore-unused */ &$fleet) {

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...
212
    $result = [];
213
    foreach (sn_get_groups('resources_loot') as $resource_id) {
214
      if ($resources_taken[$planetRecord['id']] != 1 && !$resources_taken[$planetRecord['id']][$resource_id]) {
215
        continue;
216
      }
217
218
      $resource_amount = floor(mrc_get_level($user, $planetRecord, $resource_id, true, true));
0 ignored issues
show
It seems like mrc_get_level($user, $pl...esource_id, true, true) can also be of type boolean; however, parameter $num of floor() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

218
      $resource_amount = floor(/** @scrutinizer ignore-type */ mrc_get_level($user, $planetRecord, $resource_id, true, true));
Loading history...
219
220
      $result[$resource_id] = min($will_take, $resource_amount);
221
      $will_take -= $resource_amount;
222
223
      if ($will_take <= 0) {
224
        break;
225
      }
226
    }
227
228
    return $result;
229
  }
230
231
232
  /**
233
   * @param array     $playerRecord
234
   * @param array     $planetRecord
235
   * @param \template $template
236
   */
237
  public function modelFleet5Gathering(&$playerRecord, &$planetRecord, $template) {
238
    if (empty($resources_taken = sys_get_param('resources')) || !is_array($resources_taken)) {
239
      return;
240
    }
241
242
    $planet_list = $this->flt_build_gathering($playerRecord, $planetRecord, $resources_taken);
243
244
    foreach ($planet_list as $planet_id => $planet_data) {
245
      if ($planet_data['RESULT'] == ATTACK_ALLOWED) {
246
        /** @noinspection PhpUnhandledExceptionInspection */
247
        $planet_data['RESULT'] = flt_t_send_fleet(
248
          $playerRecord,
249
          $planet_data['PLANET_DB_DATA'],
250
          $planetRecord,
251
          $planet_data['FLEET'],
252
          $planet_data['FLEET_RESOURCES'],
253
          MT_TRANSPORT);
254
      }
255
256
      $planet_data['MESSAGE'] = $this->lang['fl_attack_error'][$planet_data['RESULT']];
257
258
      $template->assign_block_vars('results', $planet_data);
259
      if (!empty($planet_data['FLEET']) && $planet_data['RESULT'] == ATTACK_ALLOWED) {
260
        foreach ($planet_data['FLEET'] as $unit_id => $amount) {
261
          $template->assign_block_vars('results.units', [
262
            'ID'     => $unit_id,
263
            'NAME'   => $this->lang['tech'][$unit_id],
264
            'AMOUNT' => $amount
265
          ]);
266
        }
267
      }
268
    }
269
  }
270
271
  /**
272
   * @param array     $user
273
   * @param array     $planetrow
274
   * @param \template $template
275
   */
276
  public function viewPage5Gathering(&$user, &$planetrow, $template) {
277
    $planet_list = $this->flt_build_gathering($user, $planetrow, []);
278
    foreach ($planet_list as $planet_data) {
279
//      $planet_data['DURATION'] = $planet_data['DURATION'] ? pretty_time($planet_data['DURATION']) : $this->lang['flt_no_fuel'];
280
      $template->assign_block_vars('colonies', $planet_data);
281
    }
282
283
    $template->assign_vars([
284
      'PAGE_HINT'      => $this->lang['fl_page5_hint'],
285
      'METAL_NEED'     => HelperString::numberFloorAndFormat(max(0, -sys_get_param_float('metal'))),
286
      'CRYSTAL_NEED'   => HelperString::numberFloorAndFormat(max(0, -sys_get_param_float('crystal'))),
287
      'DEUTERIUM_NEED' => HelperString::numberFloorAndFormat(max(0, -sys_get_param_float('deuterium'))),
288
    ]);
289
290
    tpl_set_resource_info($template, $planetrow, []);
291
292
    SnTemplate::display($template, $this->lang['fl_title']);
293
  }
294
295
}
296