Test Failed
Branch trunk (d809b8)
by SuperNova.WS
05:48
created

PageImperium::viewStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 01.10.2017 17:53
4
 */
5
6
namespace Deprecated;
7
8
use \template;
9
use \classLocale;
10
use \DBStaticPlanet;
11
12
class PageImperium {
13
14
  const GROUPS_TO_NAMES = [
15
    UNIT_STRUCTURES         => 'structures',
16
    UNIT_STRUCTURES_SPECIAL => 'structures',
17
    UNIT_SHIPS              => 'fleet',
18
    UNIT_DEFENCE            => 'defense',
19
  ];
20
21
  /**
22
   * @var classLocale $lang ;
23
   */
24
  protected $lang;
25
26
27
  /**
28
   * @param template|null $template
29
   */
30
  public static function viewStatic(template $template = null) {
31
    $that = new static();
32
33
    $template = $that->view($template);
34
    unset($that);
35
36
    return $template;
37
  }
38
39
  /**
40
   * @param template|null $template
41
   */
42
  public static function modelStatic(template $template = null) {
43
    $that = new static();
44
45
    $that->modelAdjustMinePercent();
46
    unset($that);
47
48
    return $template;
49
  }
50
51
  public function __construct() {
52
    global $lang;
53
    $this->lang = $lang;
54
  }
55
56
  /**
57
   * @param template|null $template
58
   *
59
   * @return template
60
   */
61
  public function view(template $template = null) {
62
    global $user;
63
64
//    $this->modelAdjustMinePercent();
65
66
    list($planets, $ques) = $this->getUpdatedUserPlanetsAndQues($user);
67
68
    $template = gettemplate('imperium', $template);
69
70
    templateFillPercent($template);
71
72
    $this->tplRenderPlanetsAndFleets($template, $planets);
73
    $this->tplTotalPlanetInfo($template, $planets);
74
75
    foreach (self::GROUPS_TO_NAMES as $unit_group_id => $internalGroupName) {
76
      $template->assign_block_vars('prods', array(
77
        'NAME' => $this->lang['tech'][$unit_group_id],
78
      ));
79
      $this->imperiumTemplatizeUnitGroup($user, $template, $unit_group_id, $planets, $ques);
80
    }
81
82
    $template->assign_var('amount', count($planets) + 2);
83
    $this->tplAddGlobals($user, $template);
84
85
    return $template;
86
  }
87
88
  /**
89
   * Store current mines load in DB
90
   */
91
  protected function modelAdjustMinePercent() {
92
    global $user;
93
94
    if (!sys_get_param('save_production') || !is_array($production = sys_get_param('percent')) || empty($production)) {
95
      return;
96
    }
97
98
    $sn_group_factories = sn_get_groups('factories');
99
100
    foreach (DBStaticPlanet::db_planet_list_sorted($user, false, '*') as $planetId => $planet) {
101
      $query = [];
102
      foreach ($sn_group_factories as $factory_unit_id) {
103
        $unit_db_name_porcent = pname_factory_production_field_name($factory_unit_id);
104
        if (
105
          // Changes required to mine production
106
          isset($production[$factory_unit_id][$planet['id']])
107
          // If mine is managed
108
          && get_unit_param($factory_unit_id, P_MINING_IS_MANAGED)
109
          // Input value is valid
110
          && ($actual_porcent = intval($production[$factory_unit_id][$planet['id']] / 10)) >= 0
111
          && $actual_porcent <= 10
112
          // And changes really should be stored to DB
113
          && $actual_porcent != $planet[$unit_db_name_porcent]
114
        ) {
115
          $actual_porcent = intval($actual_porcent);
116
          $query[] = "`{$unit_db_name_porcent}` = {$actual_porcent}";
117
        }
118
      }
119
120
      if (!empty($query)) {
121
        DBStaticPlanet::db_planet_set_by_id($planet['id'], implode(',', $query));
122
      }
123
    }
124
  }
125
126
  /**
127
   * @param array $user
128
   *
129
   * @return array[]
130
   */
131
  protected function getUpdatedUserPlanetsAndQues($user) {
132
    $planets = array();
133
    $ques = array();
134
    $planet_row_list = DBStaticPlanet::db_planet_list_sorted($user);
135
    foreach ($planet_row_list as $planet) {
136
      sn_db_transaction_start();
137
      $global_data = sys_o_get_updated($user, $planet['id'], SN_TIME_NOW, false, true);
138
      $planets[$planet['id']] = $global_data['planet'];
139
      $ques[$planet['id']] = $global_data['que'];
140
      sn_db_transaction_commit();
141
    }
142
143
    return array($planets, $ques);
144
  }
145
146
  /**
147
   * @param array    $user
148
   * @param template $template
149
   * @param int      $unit_group_id
150
   * @param array[]  $planets
151
   * @param array[]  $ques
152
   */
153
  protected function imperiumTemplatizeUnitGroup(&$user, $template, $unit_group_id, $planets, $ques) {
154
    $sn_group_factories = sn_get_groups('factories');
155
156
//    $imperiumStats = [];
157
158
    foreach (get_unit_param('techtree', $unit_group_id) as $unit_id) {
159
      $unit_count = $unit_count_abs = 0;
160
      $block_vars = array();
161
      $unit_is_factory = in_array($unit_id, $sn_group_factories) && get_unit_param($unit_id, P_MINING_IS_MANAGED);
162
      foreach ($planets as $planet) {
163
        $unit_level_plain = mrc_get_level($user, $planet, $unit_id, false, true);
164
165
        $levelGreen = 0;
166
        $levelYellow = 0;
167
168
        switch ($unit_group_id) {
169
          /** @noinspection PhpMissingBreakStatementInspection */
170
          case UNIT_SHIPS:
171
            $levelYellow = floatval($planet['fleet_list']['own']['total'][$unit_id]);
172
173
          case UNIT_STRUCTURES:
174
          case UNIT_STRUCTURES_SPECIAL:
175
          case UNIT_DEFENCE:
176
            $levelGreen = floatval($ques[$planet['id']]['in_que'][que_get_unit_que($unit_id)][$user['id']][$planet['id']][$unit_id]);
177
          break;
178
179
          default:
180
          break;
181
        }
182
        $unitsPresentOrChanged = $unit_level_plain + abs($levelYellow) + abs($levelGreen);
183
184
        $unit_count += $unit_level_plain;
185
        $unit_count_abs += $unitsPresentOrChanged;
186
187
        $block_vars[] = [
188
          'ID'                     => $planet['id'],
189
          'TYPE'                   => $planet['planet_type'],
190
          'LEVEL'                  => $unitsPresentOrChanged ? $unit_level_plain : '-',
191
          'LEVEL_PLUS_YELLOW'      => $levelYellow,
192
          'LEVEL_PLUS_GREEN'       => $levelGreen,
193
          'LEVEL_TEXT'             => $unitsPresentOrChanged ? pretty_number($unit_level_plain) : '-',
194
          'LEVEL_PLUS_YELLOW_TEXT' => tplPrettyPlus($levelYellow),
195
          'LEVEL_PLUS_GREEN_TEXT'  => tplPrettyPlus($levelGreen),
196
          'PERCENT'                => $unit_is_factory ? ($unit_level_plain ? $planet[pname_factory_production_field_name($unit_id)] * 10 : -1) : -1,
197
          'FACTORY'                => $unit_is_factory,
198
        ];
199
      }
200
201
      if ($unit_count_abs) {
202
        $this->tplRenderUnitLine($template, $unit_id, $block_vars, $unit_count, $unit_is_factory);
203
      }
204
    }
205
  }
206
207
  /**
208
   * Renders line of unit for each planet
209
   *
210
   * @param template $template
211
   * @param int      $unit_id
212
   * @param array    $block_vars
213
   * @param int      $unit_count
214
   * @param bool     $unit_is_factory
215
   *
216
   */
217
  protected function tplRenderUnitLine($template, $unit_id, $block_vars, $unit_count, $unit_is_factory) {
218
    // Adding unit cell name
219
    $template->assign_block_vars('prods', [
220
      'ID'    => $unit_id,
221
      'FIELD' => 'unit_' . $unit_id, // TODO Делать это прямо в темплейте
222
      'NAME'  => $this->lang['tech'][$unit_id],
223
      'MODE'  => static::GROUPS_TO_NAMES[get_unit_param($unit_id, P_UNIT_TYPE)],
224
    ]);
225
226
    $imperiumYellows = [];
227
    $imperiumGreens = [];
228
    // Adding data for each planet for this unit
229
    foreach ($block_vars as $block_var) {
230
      $imperiumYellows[$unit_id] += $block_var['LEVEL_PLUS_YELLOW'];
231
      $imperiumGreens[$unit_id] += $block_var['LEVEL_PLUS_GREEN'];
232
      $template->assign_block_vars('prods.planet', $block_var);
233
    }
234
235
    // Adding final cell with Imperium total stat about this unit
236
    $template->assign_block_vars('prods.planet', [
237
      'ID'                     => 0,
238
      'LEVEL'                  => $unit_count,
239
      'LEVEL_TEXT'             => pretty_number($unit_count),
240
      'LEVEL_PLUS_YELLOW'      => $imperiumYellows[$unit_id],
241
      'LEVEL_PLUS_GREEN'       => $imperiumGreens[$unit_id],
242
      'LEVEL_PLUS_YELLOW_TEXT' => $imperiumYellows[$unit_id] == 0 ? '' : tplPrettyPlus($imperiumYellows[$unit_id]),
243
      'LEVEL_PLUS_GREEN_TEXT'  => $imperiumGreens[$unit_id] == 0 ? '' : tplPrettyPlus($imperiumGreens[$unit_id]),
244
      'PERCENT'                => $unit_is_factory ? '' : -1,
245
      'FACTORY'                => $unit_is_factory,
246
    ]);
247
  }
248
249
  /**
250
   * @param template $template
251
   * @param array[]  $planets
252
   */
253
  protected function tplRenderPlanetsAndFleets($template, &$planets) {
254
    $planet_density = sn_get_groups('planet_density');
255
256
    $fleets = [];
257
    foreach ($planets as $planetId => &$planet) {
258
      $templatizedPlanet = tpl_parse_planet($planet, $fleets);
259
260
      $planet['fleet_list'] = $templatizedPlanet['fleet_list'];
261
      $planet['BUILDING_ID'] = $templatizedPlanet['BUILDING_ID'];
262
      $planet['hangar_que'] = $templatizedPlanet['hangar_que'];
263
      $planet['full_que'] = $templatizedPlanet;
264
265
      foreach ([RES_METAL, RES_CRYSTAL, RES_DEUTERIUM] as $resourceId) {
266
        if (empty($templatizedPlanet['fleet_list']['own']['total'][$resourceId])) {
267
          $templatizedPlanet['RES_' . $resourceId] = 0;
268
        } else {
269
          $templatizedPlanet['RES_' . $resourceId] = $templatizedPlanet['fleet_list']['own']['total'][$resourceId];
270
          $templatizedPlanet['RES_' . $resourceId . '_TEXT'] = pretty_number($templatizedPlanet['fleet_list']['own']['total'][$resourceId]);
271
        }
272
      }
273
274
      $template->assign_block_vars('planet', array_merge($templatizedPlanet, [
275
        'METAL_CUR'  => pretty_number($planet['metal'], true, $planet['caps']['total_storage'][RES_METAL]),
276
        'METAL_PROD' => pretty_number($planet['caps']['total'][RES_METAL]),
277
278
        'CRYSTAL_CUR'  => pretty_number($planet['crystal'], true, $planet['caps']['total_storage'][RES_CRYSTAL]),
279
        'CRYSTAL_PROD' => pretty_number($planet['caps']['total'][RES_CRYSTAL]),
280
281
        'DEUTERIUM_CUR'  => pretty_number($planet['deuterium'], true, $planet['caps']['total_storage'][RES_DEUTERIUM]),
282
        'DEUTERIUM_PROD' => pretty_number($planet['caps']['total'][RES_DEUTERIUM]),
283
284
        'ENERGY_CUR' => pretty_number($planet['caps'][RES_ENERGY][BUILD_CREATE] - $planet['caps'][RES_ENERGY][BUILD_DESTROY], true, true),
285
        'ENERGY_MAX' => pretty_number($planet['caps'][RES_ENERGY][BUILD_CREATE]),
286
287
        'TEMP_MIN' => $planet['temp_min'],
288
        'TEMP_MAX' => $planet['temp_max'],
289
290
        'DENSITY_CLASS'      => $planet['density_index'],
291
        'DENSITY_RICHNESS'   => $planet_density[$planet['density_index']][UNIT_PLANET_DENSITY_RICHNESS],
292
        'DENSITY_CLASS_TEXT' => $this->lang['uni_planet_density_types'][$planet['density_index']],
293
      ]));
294
    }
295
296
    tpl_assign_fleet($template, $fleets);
297
  }
298
299
  /**
300
   * @param template $template
301
   * @param array    $planets
302
   */
303
  function tplTotalPlanetInfo($template, $planets) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
304
    $imperiumStats = [
305
      'temp_min' => 1000,
306
      'temp_max' => -999,
307
    ];
308
309
    foreach ($planets as $planetId => &$planet) {
310
      $imperiumStats['fields'] += $planet['field_current'];
311
      $imperiumStats['metal'] += $planet['metal'];
312
      $imperiumStats['crystal'] += $planet['crystal'];
313
      $imperiumStats['deuterium'] += $planet['deuterium'];
314
      $imperiumStats['energy'] += $planet['energy_max'] - $planet['energy_used'];
315
316
      $imperiumStats['fields_max'] += eco_planet_fields_max($planet);
317
      $imperiumStats['metal_perhour'] += $planet['caps']['total'][RES_METAL];
318
      $imperiumStats['crystal_perhour'] += $planet['caps']['total'][RES_CRYSTAL];
319
      $imperiumStats['deuterium_perhour'] += $planet['caps']['total'][RES_DEUTERIUM];
320
      $imperiumStats['energy_max'] += $planet['caps'][RES_ENERGY][BUILD_CREATE];
321
322
      $imperiumStats['temp_min'] = min($planet['temp_min'], $imperiumStats['temp_min']);
323
      $imperiumStats['temp_max'] = max($planet['temp_max'], $imperiumStats['temp_max']);
324
    }
325
326
    $template->assign_block_vars('planet', array_merge([
327
      'ID'   => 0,
328
      'NAME' => $this->lang['sys_total'],
329
330
      'FIELDS_CUR' => $imperiumStats['fields'],
331
      'FIELDS_MAX' => $imperiumStats['fields_max'],
332
333
      'METAL_CUR'  => pretty_number($imperiumStats['metal']),
334
      'METAL_PROD' => pretty_number($imperiumStats['metal_perhour']),
335
336
      'CRYSTAL_CUR'  => pretty_number($imperiumStats['crystal']),
337
      'CRYSTAL_PROD' => pretty_number($imperiumStats['crystal_perhour']),
338
339
      'DEUTERIUM_CUR'  => pretty_number($imperiumStats['deuterium']),
340
      'DEUTERIUM_PROD' => pretty_number($imperiumStats['deuterium_perhour']),
341
342
      'ENERGY_CUR' => pretty_number($imperiumStats['energy']),
343
      'ENERGY_MAX' => pretty_number($imperiumStats['energy_max']),
344
345
      'TEMP_MIN' => $imperiumStats['temp_min'],
346
      'TEMP_MAX' => $imperiumStats['temp_max'],
347
    ]));
348
  }
349
350
  /**
351
   * @param array    $user
352
   * @param template $template
353
   */
354
  protected function tplAddGlobals(&$user, $template) {
355
    $template->assign_vars([
356
      'COLONIES_CURRENT' => get_player_current_colonies($user),
357
      'COLONIES_MAX'     => get_player_max_colonies($user),
358
359
      'EXPEDITIONS_CURRENT' => fleet_count_flying($user['id'], MT_EXPLORE),
360
      'EXPEDITIONS_MAX'     => get_player_max_expeditons($user),
361
362
      'PLANET_DENSITY_RICHNESS_NORMAL'  => PLANET_DENSITY_RICHNESS_NORMAL,
363
      'PLANET_DENSITY_RICHNESS_AVERAGE' => PLANET_DENSITY_RICHNESS_AVERAGE,
364
      'PLANET_DENSITY_RICHNESS_GOOD'    => PLANET_DENSITY_RICHNESS_GOOD,
365
      'PLANET_DENSITY_RICHNESS_PERFECT' => PLANET_DENSITY_RICHNESS_PERFECT,
366
367
      'PAGE_HEADER' => $this->lang['imp_overview'],
368
    ]);
369
  }
370
371
}
372