Completed
Push — trunk ( c8f413...ad61f6 )
by SuperNova.WS
04:45
created

includes/functions/tpl_helpers.php (6 issues)

1
<?php
2
3
// Compare function to sort fleet in time order
4
function tpl_assign_fleet_compare($a, $b) {
5
  if ($a['fleet']['OV_THIS_PLANET'] == $b['fleet']['OV_THIS_PLANET']) {
6
    if ($a['fleet']['OV_LEFT'] == $b['fleet']['OV_LEFT']) {
7
      return 0;
8
    }
9
10
    return ($a['fleet']['OV_LEFT'] < $b['fleet']['OV_LEFT']) ? -1 : 1;
11
  } else {
12
    return $a['fleet']['OV_THIS_PLANET'] ? -1 : 1;
13
  }
14
}
15
16
/**
17
 * @param array  $fleets
18
 * @param string $js_name
19
 *
20
 * @return array
21
 */
22
function tpl_assign_fleet_generate($fleets, $js_name = 'fleets') {
23
  $result = [];
24
  if (empty($fleets)) {
25
    return $result;
26
  }
27
28
  usort($fleets, 'tpl_assign_fleet_compare');
29
30
  foreach ($fleets as $fleet_data) {
31
    $temp = $fleet_data['fleet'];
32
33
    if ($fleet_data['ships']) {
34
      $temp['.']['ships'] = $fleet_data['ships'];
35
    }
36
37
    $result['.'][$js_name][] = $temp;
38
  }
39
40
  return $result;
41
}
42
43
/**
44
 * For backward compatibility
45
 *
46
 * @param template $template
47
 * @param array    $fleets
48
 * @param string   $js_name
49
 *
50
 * @deprecated
51
 */
52
function tpl_assign_fleet(&$template, $fleets, $js_name = 'fleets') {
53
  if (!$fleets) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fleets of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
    return;
55
  }
56
57
  $template->assign_recursive(tpl_assign_fleet_generate($fleets, $js_name));
58
}
59
60
/**
61
 * function that parses internal fleet representation (as array(id => count))
62
 *
63
 * @param $fleet
64
 * @param $fleet_id
65
 *
66
 * @return mixed
67
 */
68
function tpl_parse_fleet_sn($fleet, $fleet_id) {
69
  global $lang, $user;
70
71
  $user_data = &$user;
72
73
  $return['fleet'] = array(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.
Loading history...
74
    'ID' => $fleet_id,
75
76
    'METAL'     => $fleet[RES_METAL],
77
    'CRYSTAL'   => $fleet[RES_CRYSTAL],
78
    'DEUTERIUM' => $fleet[RES_DEUTERIUM],
79
  );
80
81
  foreach ($fleet as $ship_id => $ship_amount) {
82
    if (in_array($ship_id, sn_get_groups('fleet'))) {
83
      $single_ship_data = get_ship_data($ship_id, $user_data);
84
      $return['ships'][$ship_id] = array(
85
        'ID'          => $ship_id,
86
        'NAME'        => $lang['tech'][$ship_id],
87
        'AMOUNT'      => $ship_amount,
88
        'CONSUMPTION' => $single_ship_data['consumption'],
89
        'SPEED'       => $single_ship_data['speed'],
90
        'CAPACITY'    => $single_ship_data['capacity'],
91
      );
92
    }
93
  }
94
95
  return $return;
96
}
97
98
/**
99
 * @param array      $fleet
100
 * @param int        $index
101
 * @param array|bool $user_data
102
 *
103
 * @return mixed
104
 */
105
function tpl_parse_fleet_db($fleet, $index, $user_data = false) {
106
  $result = null;
107
108
  return sn_function_call('tpl_parse_fleet_db', [$fleet, $index, $user_data, &$result]);
109
}
110
111
/**
112
 * @param array      $fleet
113
 * @param int        $index
114
 * @param array|bool $user_data
115
 * @param            $result
116
 *
117
 * @return mixed
118
 */
119
function sn_tpl_parse_fleet_db($fleet, $index, $user_data = false, &$result) {
120
  global $lang, $user;
121
122
  if (!$user_data) {
123
    $user_data = $user;
124
  }
125
126
  if ($fleet['fleet_mess'] == 0 && $fleet['fleet_mission'] == MT_AKS) {
127
    $aks = doquery("SELECT * FROM {{aks}} WHERE id={$fleet['fleet_group']} LIMIT 1;", true);
128
  }
129
130
  $spy_level = $user['id'] == $fleet['fleet_owner'] ? 100 : GetSpyLevel($user);
131
132
  $result['fleet'] = isset($result['fleet']) ? $result['fleet'] : array();
133
134
  $result['fleet'] = array(
135
    'NUMBER' => $index,
136
137
    'ID'           => $fleet['fleet_id'],
138
    'OWNER'        => $fleet['fleet_owner'],
139
    'TARGET_OWNER' => $fleet['fleet_target_owner'],
140
141
    'MESSAGE'      => $fleet['fleet_mess'],
142
    'MISSION'      => $fleet['fleet_mission'],
143
    'MISSION_NAME' => $lang['type_mission'][$fleet['fleet_mission']],
144
    'ACS'          => $aks['name'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $aks does not seem to be defined for all execution paths leading up to this point.
Loading history...
145
    'AMOUNT'       => $spy_level >= 4 ? (HelperString::numberFloorAndFormat($fleet['fleet_amount']) . ($fleet['fleet_resource_metal'] + $fleet['fleet_resource_crystal'] + $fleet['fleet_resource_deuterium'] ? '+' : '')) : '?',
146
147
    'METAL'     => $spy_level >= 8 ? $fleet['fleet_resource_metal'] : 0,
148
    'CRYSTAL'   => $spy_level >= 8 ? $fleet['fleet_resource_crystal'] : 0,
149
    'DEUTERIUM' => $spy_level >= 8 ? $fleet['fleet_resource_deuterium'] : 0,
150
151
    'START_TYPE_TEXT_SH' => $lang['sys_planet_type_sh'][$fleet['fleet_start_type']],
152
    'START_COORDS'       => "[{$fleet['fleet_start_galaxy']}:{$fleet['fleet_start_system']}:{$fleet['fleet_start_planet']}]",
153
    'START_TIME_TEXT'    => date(FMT_DATE_TIME, $fleet['fleet_end_time'] + SN_CLIENT_TIME_DIFF),
154
    'START_LEFT'         => floor($fleet['fleet_end_time'] + 1 - SN_TIME_NOW),
155
    'START_URL'          => uni_render_coordinates_href($fleet, 'fleet_start_', 3),
156
    'START_NAME'         => $fleet['fleet_start_name'],
157
158
    'END_TYPE_TEXT_SH' => $lang['sys_planet_type_sh'][$fleet['fleet_end_type']],
159
    'END_COORDS'       => "[{$fleet['fleet_end_galaxy']}:{$fleet['fleet_end_system']}:{$fleet['fleet_end_planet']}]",
160
    'END_TIME_TEXT'    => date(FMT_DATE_TIME, $fleet['fleet_start_time'] + SN_CLIENT_TIME_DIFF),
161
    'END_LEFT'         => floor($fleet['fleet_start_time'] + 1 - SN_TIME_NOW),
162
    'END_URL'          => uni_render_coordinates_href($fleet, 'fleet_end_', 3),
163
    'END_NAME'         => $fleet['fleet_end_name'],
164
165
    'STAY_TIME' => date(FMT_DATE_TIME, $fleet['fleet_end_stay'] + SN_CLIENT_TIME_DIFF),
166
    'STAY_LEFT' => floor($fleet['fleet_end_stay'] + 1 - SN_TIME_NOW),
167
168
    'OV_LABEL'        => $fleet['ov_label'],
169
    'EVENT_TIME_TEXT' => date(FMT_DATE_TIME, $fleet['event_time'] + SN_CLIENT_TIME_DIFF),
170
    'OV_LEFT'         => floor($fleet['event_time'] + 1 - SN_TIME_NOW),
171
    'OV_THIS_PLANET'  => $fleet['ov_this_planet'],
172
  );
173
174
  $ship_list = explode(';', $fleet['fleet_array']);
175
176
  $ship_id = 0;
177
  if ($spy_level >= 6) {
178
    foreach ($ship_list as $ship_record) {
179
      if ($ship_record) {
180
        $ship_data = explode(',', $ship_record);
181
        if ($spy_level >= 10) {
182
          $single_ship_data = get_ship_data($ship_data[0], $user_data);
183
          $result['ships'][$ship_data[0]] = array(
184
            'ID'          => $ship_data[0],
185
            'NAME'        => $lang['tech'][$ship_data[0]],
186
            'AMOUNT'      => $ship_data[1],
187
            'AMOUNT_TEXT' => HelperString::numberFloorAndFormat($ship_data[1]),
188
            'CONSUMPTION' => $single_ship_data['consumption'],
189
            'SPEED'       => $single_ship_data['speed'],
190
            'CAPACITY'    => $single_ship_data['capacity'],
191
          );
192
        } else {
193
          $result['ships'][$ship_data[0]] = array(
194
            'ID'               => $ship_id++,
195
            'NAME'             => $lang['tech'][UNIT_SHIPS],
196
            'AMOUNT'           => $ship_data[1],
197
            'AMOUNT_TEXT'      => HelperString::numberFloorAndFormat($ship_data[1]),
198
            'CONSUMPTION'      => 0,
199
            'CONSUMPTION_TEXT' => '0',
200
            'SPEED'            => 0,
201
            'CAPACITY'         => 0,
202
          );
203
        }
204
      }
205
    }
206
  }
207
208
  return $result;
209
}
210
211
function tpl_parse_planet_que($que, $planet, $que_id) {
212
  $hangar_que = array();
213
  $que_hangar = $que['ques'][$que_id][$planet['id_owner']][$planet['id']];
214
  if (!empty($que_hangar)) {
215
    foreach ($que_hangar as $que_item) {
216
      $hangar_que['que'][] = array('id' => $que_item['que_unit_id'], 'count' => $que_item['que_unit_amount']);
217
      $hangar_que[$que_item['que_unit_id']] += $que_item['que_unit_amount'];
218
    }
219
  }
220
221
  return $hangar_que;
222
}
223
224
/**
225
 * @param array $planet
226
 * @param array $fleet_list
227
 *
228
 * @return array
229
 */
230
function tpl_parse_planet_result_fleet($planet, $fleet_list) {
231
  return [
232
    'FLEET_OWN'       => $fleet_list['own']['count'],
233
    'FLEET_ENEMY'     => $fleet_list['enemy']['count'],
234
    'FLEET_NEUTRAL'   => $fleet_list['neutral']['count'],
235
    'PLANET_FLEET_ID' => !empty($fleet_list['own']['count']) ? getUniqueFleetId($planet) : 0,
236
  ];
237
}
238
239
240
/**
241
 * @param int $parentPlanetId
242
 *
243
 * @return array
244
 */
245
function tpl_parse_planet_moon($parentPlanetId) {
246
  $moon_fill = 0;
247
  $moon_fleets = [];
248
249
  $moon = DBStaticPlanet::db_planet_by_parent($parentPlanetId);
250
  if ($moon) {
0 ignored issues
show
The condition $moon can never be true.
Loading history...
251
    $moon_fill = min(100, floor($moon['field_current'] / eco_planet_fields_max($moon) * 100));
252
    $moon_fleets = flt_get_fleets_to_planet($moon);
253
  }
254
255
  return [
256
    'MOON_ID'    => $moon['id'],
257
    'MOON_NAME'  => $moon['name'],
258
    'MOON_IMG'   => $moon['image'],
259
    'MOON_FILL'  => min(100, $moon_fill),
260
    'MOON_ENEMY' => !empty($moon_fleets['enemy']['count']) ? $moon_fleets['enemy']['count'] : 0,
261
262
    'MOON_PLANET' => $moon['parent_planet'],
263
  ];
264
}
265
266
/**
267
 * @param array $planet
268
 *
269
 * @return array
270
 */
271
function tpl_parse_planet($planet) {
272
  global $lang;
273
274
  $que = que_get($planet['id_owner'], $planet['id'], false);
275
  $structure_que = tpl_parse_planet_que($que, $planet, QUE_STRUCTURES); // TODO Заменить на que_tpl_parse_element($que_element);
276
  $structure_que_first = is_array($structure_que['que']) ? reset($structure_que['que']) : array();
277
  $hangar_que = tpl_parse_planet_que($que, $planet, SUBQUE_FLEET); // TODO Заменить на que_tpl_parse_element($que_element);
278
  $hangar_que_first = is_array($hangar_que['que']) ? reset($hangar_que['que']) : array();
279
  $defense_que = tpl_parse_planet_que($que, $planet, SUBQUE_DEFENSE); // TODO Заменить на que_tpl_parse_element($que_element);
280
  $defense_que_first = is_array($defense_que['que']) ? reset($defense_que['que']) : array();
281
282
  $result = [
283
    'ID'    => $planet['id'],
284
    'NAME'  => $planet['name'],
285
    'IMAGE' => $planet['image'],
286
287
    'GALAXY'      => $planet['galaxy'],
288
    'SYSTEM'      => $planet['system'],
289
    'PLANET'      => $planet['planet'],
290
    'TYPE'        => $planet['planet_type'],
291
    'COORDINATES' => uni_render_coordinates($planet),
292
293
    'METAL_PERCENT'     => $planet['metal_mine_porcent'] * 10,
294
    'CRYSTAL_PERCENT'   => $planet['crystal_mine_porcent'] * 10,
295
    'DEUTERIUM_PERCENT' => $planet['deuterium_sintetizer_porcent'] * 10,
296
297
    'STRUCTURE'       => isset($structure_que_first['id']) ? $lang['tech'][$structure_que_first['id']] : '',
298
    'STRUCTURE_SLOTS' => is_array($structure_que['que']) ? count($structure_que['que']) : 0,
299
    'HANGAR'          => isset($hangar_que_first['id']) ? $lang['tech'][$hangar_que_first['id']] : '',
300
    'HANGAR_SLOTS'    => is_array($hangar_que['que']) ? count($hangar_que['que']) : 0,
301
    'DEFENSE'         => isset($defense_que_first['id']) ? $lang['tech'][$defense_que_first['id']] : '',
302
    'DEFENSE_SLOTS'   => is_array($defense_que['que']) ? count($defense_que['que']) : 0,
303
304
    'FIELDS_CUR' => $planet['field_current'],
305
    'FIELDS_MAX' => eco_planet_fields_max($planet),
306
    'FILL'       => min(100, floor($planet['field_current'] / eco_planet_fields_max($planet) * 100)),
307
308
    'PLANET_GOVERNOR_ID'        => $planet['PLANET_GOVERNOR_ID'],
309
    'PLANET_GOVERNOR_NAME'      => $lang['tech'][$planet['PLANET_GOVERNOR_ID']],
310
    'PLANET_GOVERNOR_LEVEL'     => $planet['PLANET_GOVERNOR_LEVEL'],
311
    'PLANET_GOVERNOR_LEVEL_MAX' => get_unit_param($planet['PLANET_GOVERNOR_ID'], P_MAX_STACK),
312
  ];
313
314
  if (!empty($que['ques'][QUE_STRUCTURES][$planet['id_owner']][$planet['id']])) {
315
    $result['building_que'] = [];
316
    $building_que = &$que['ques'][QUE_STRUCTURES][$planet['id_owner']][$planet['id']];
317
    foreach ($building_que as $que_element) {
318
      $result['building_que'][] = que_tpl_parse_element($que_element);
319
    }
320
  }
321
322
  return $result;
323
}
324
325
function flt_get_fleets_to_planet($planet, $fleet_db_list = 0) {
326
  if (!($planet && $planet['id']) && !$fleet_db_list) {
327
    return $planet;
328
  }
329
330
  global $user;
331
332
  if ($fleet_db_list === 0) {
333
    $fleet_db_list = fleet_and_missiles_list_by_coordinates($planet);
334
  }
335
336
  foreach ($fleet_db_list as $fleet) {
337
    if ($fleet['fleet_owner'] == $user['id']) {
338
      if ($fleet['fleet_mission'] == MT_MISSILE) {
339
        continue;
340
      }
341
      $fleet_ownage = 'own';
342
    } else {
343
      switch ($fleet['fleet_mission']) {
344
        case MT_ATTACK:
345
        case MT_AKS:
346
        case MT_DESTROY:
347
        case MT_MISSILE:
348
          $fleet_ownage = 'enemy';
349
        break;
350
351
        default:
352
          $fleet_ownage = 'neutral';
353
        break;
354
355
      }
356
    }
357
358
    $fleet_list[$fleet_ownage]['fleets'][$fleet['fleet_id']] = $fleet;
359
360
    if ($fleet['fleet_mess'] == 1 || ($fleet['fleet_mess'] == 0 && $fleet['fleet_mission'] == MT_RELOCATE) || ($fleet['fleet_target_owner'] != $user['id'])) {
361
//      $fleet_sn = flt_expand($fleet);
362
      $fleet_sn = sys_unit_str2arr($fleet['fleet_array']);
363
      foreach ($fleet_sn as $ship_id => $ship_amount) {
364
        if (in_array($ship_id, sn_get_groups('fleet'))) {
365
          $fleet_list[$fleet_ownage]['total'][$ship_id] += $ship_amount;
366
        }
367
      }
368
    }
369
370
    $fleet_list[$fleet_ownage]['count']++;
371
    $fleet_list[$fleet_ownage]['amount'] += $fleet['fleet_amount'];
372
    $fleet_list[$fleet_ownage]['total'][RES_METAL] += $fleet['fleet_resource_metal'];
373
    $fleet_list[$fleet_ownage]['total'][RES_CRYSTAL] += $fleet['fleet_resource_crystal'];
374
    $fleet_list[$fleet_ownage]['total'][RES_DEUTERIUM] += $fleet['fleet_resource_deuterium'];
375
  }
376
377
  return $fleet_list;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fleet_list does not seem to be defined for all execution paths leading up to this point.
Loading history...
378
}
379
380
/**
381
 * @param template $template
382
 * @param array    $planetrow
383
 * @param array    $fleets_to_planet
384
 */
385
function tpl_set_resource_info(&$template, $planetrow, $fleets_to_planet = array()) {
386
  $template->assign_vars(array(
387
    'RESOURCE_ROUNDING' => 0,
388
389
    'PLANET_ENERGY'                   => $planetrow['energy_used'],
390
    'ENERGY_BALANCE_NUMBER'           => $planetrow['energy_max'] - $planetrow['energy_used'],
391
    'ENERGY_BALANCE'                  => prettyNumberStyledDefault($planetrow['energy_max'] - $planetrow['energy_used']),
392
    'ENERGY_MAX_NUMBER'               => $planetrow['energy_max'],
393
    'ENERGY_MAX_NUMBER_TEXT_NO_COLOR' => HelperString::numberFloorAndFormat($planetrow['energy_max']),
394
    'ENERGY_MAX_NUMBER_TEXT'          => Tools::numberPercentSpan($planetrow['energy_max'], $planetrow['energy_used']),
395
    'ENERGY_MAX'                      => prettyNumberStyledCompare($planetrow['energy_max'], -$planetrow['energy_used']),
396
    'ENERGY_FILL'                     => round(($planetrow["energy_used"] / ($planetrow["energy_max"] + 1)) * 100, 0),
397
398
    'PLANET_METAL'              => floor($planetrow["metal"]),
399
    'PLANET_METAL_TEXT'         => prettyNumberStyledCompare($planetrow["metal"], $planetrow["metal_max"]),
400
    'PLANET_METAL_MAX'          => floor($planetrow["metal_max"]),
401
    'PLANET_METAL_MAX_TEXT'     => Tools::numberPercentSpan($planetrow["metal_max"], $planetrow["metal"]),
402
    'PLANET_METAL_MAX_NO_COLOR' => HelperString::numberFloorAndFormat($planetrow["metal_max"]),
403
    'PLANET_METAL_FILL'         => round(($planetrow["metal"] / ($planetrow["metal_max"] + 1)) * 100, 0),
404
    'PLANET_METAL_PERHOUR'      => round($planetrow["metal_perhour"], 5),
405
    'PLANET_METAL_FLEET_TEXT'   => prettyNumberStyledDefault($fleets_to_planet[$planetrow['id']]['fleet']['METAL']),
406
407
    'PLANET_CRYSTAL'              => floor($planetrow["crystal"]),
408
    'PLANET_CRYSTAL_TEXT'         => prettyNumberStyledCompare($planetrow["crystal"], $planetrow["crystal_max"]),
409
    'PLANET_CRYSTAL_MAX'          => floor($planetrow["crystal_max"]),
410
    'PLANET_CRYSTAL_MAX_TEXT'     => Tools::numberPercentSpan($planetrow["crystal_max"], $planetrow["crystal"]),
411
    'PLANET_CRYSTAL_MAX_NO_COLOR' => HelperString::numberFloorAndFormat($planetrow["crystal_max"]),
412
    'PLANET_CRYSTAL_FILL'         => round(($planetrow["crystal"] / ($planetrow["crystal_max"] + 1)) * 100, 0),
413
    'PLANET_CRYSTAL_PERHOUR'      => round($planetrow["crystal_perhour"], 5),
414
    'PLANET_CRYSTAL_FLEET_TEXT'   => prettyNumberStyledDefault($fleets_to_planet[$planetrow['id']]['fleet']['CRYSTAL']),
415
416
    'PLANET_DEUTERIUM'              => floor($planetrow["deuterium"]),
417
    'PLANET_DEUTERIUM_TEXT'         => prettyNumberStyledCompare($planetrow["deuterium"], $planetrow["deuterium_max"]),
418
    'PLANET_DEUTERIUM_MAX'          => floor($planetrow["deuterium_max"]),
419
    'PLANET_DEUTERIUM_MAX_TEXT'     => Tools::numberPercentSpan($planetrow["deuterium_max"], $planetrow["deuterium"]),
420
    'PLANET_DEUTERIUM_MAX_NO_COLOR' => HelperString::numberFloorAndFormat($planetrow["deuterium_max"]),
421
    'PLANET_DEUTERIUM_FILL'         => round(($planetrow["deuterium"] / ($planetrow["deuterium_max"] + 1)) * 100, 0),
422
    'PLANET_DEUTERIUM_PERHOUR'      => round($planetrow["deuterium_perhour"], 5),
423
    'PLANET_DEUTERIUM_FLEET_TEXT'   => prettyNumberStyledDefault($fleets_to_planet[$planetrow['id']]['fleet']['DEUTERIUM']),
424
  ));
425
}
426
427
/**
428
 * @return int[][]
429
 */
430
function templateFillPercent() {
431
  $result = [];
432
  for ($i = 100; $i >= 0; $i -= 10) {
433
    $result[] = ['PERCENT' => $i];
434
  }
435
436
  return ['.' => ['percent' => $result]];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('.' => array('percent' => $result)) returns the type array<string,array<strin...tring,integer>>|array>> which is incompatible with the documented return type array<mixed,integer[]>.
Loading history...
437
}
438