Completed
Push — trunk ( d06df9...f48ee3 )
by SuperNova.WS
11:23
created

galaxy.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * galaxy.php
5
 *
6
 * Galaxy view
7
 *
8
 * History version
9
 *   2.1 - 'galaxy' table replaced with 'planets' by Gorlum for http://supernova.ws
10
 *   2.0 - Rewrote by Gorlum for http://supernova.ws
11
 *     [+] Template-related parts cutted from PHP and moved to TPL-code
12
 *   1.4 - Security checks & tests by Gorlum for http://supernova.ws
13
 *   1.3 - 2eme Nettoyage Chlorel ... Mise en fonction et debuging complet
14
 *   1.2 - 1er Nettoyage Chlorel ...
15
 *   1.1 - Modified by -MoF- (UGamela germany)
16
 *   1.0 - Created by Perberos
17
 * @copyright 2008 by Chlorel for XNova
18
 */
19
20
use Fleet\DbFleetStatic;
21
use Planet\DBStaticPlanet;
22
23
include('common.' . substr(strrchr(__FILE__, '.'), 1));
24
25
global $config, $template_result, $planetrow, $debug, $lang;
26
27
lng_include('universe');
28
lng_include('stat');
29
30
$mode = sys_get_param_str('mode');
31
$scan = sys_get_param_str('scan');
32
$uni_galaxy = sys_get_param_int('galaxy', $planetrow['galaxy']);
33
$uni_system = sys_get_param_int('system', $planetrow['system']);
34
$planet = sys_get_param_int('planet', $planetrow['planet']);
35
36
if ($mode == 'name') {
37
  require_once('includes/includes/uni_rename.php');
38
}
39
40
require_once('includes/includes/flt_functions.php');
41
42
$CurrentPlanetID = sys_get_param_id('current');
43
$POST_galaxyLeft = sys_get_param_str('galaxyLeft');
44
$POST_galaxyRight = sys_get_param_str('galaxyRight');
45
$POST_systemLeft = sys_get_param_str('systemLeft');
46
$POST_systemRight = sys_get_param_str('systemRight');
47
48
$fleetmax = GetMaxFleets($user);
49
$CurrentPlID = $planetrow['id'];
50
$CurrentMIP = mrc_get_level($user, $planetrow, UNIT_DEF_MISSILE_INTERPLANET, false, true);
51
$HavePhalanx = mrc_get_level($user, $planetrow, STRUC_MOON_PHALANX);
52
$CurrentSystem = $planetrow['system'];
53
$CurrentGalaxy = $planetrow['galaxy'];
54
55
//$maxfleet       = doquery("SELECT COUNT(*) AS flying_fleet_count FROM {{fleets}} WHERE `fleet_owner` = '{$user['id']}';", '', true);
56
//$maxfleet_count = $maxfleet['flying_fleet_count'];
57
$flying_fleet_count = DbFleetStatic::fleet_count_flying($user['id']);
58
59
if ($mode == 1) {
60
} elseif ($mode == 2 || $mode == 3) {
61
  $planet = $planetrow['planet'];
62
} else {
63
  $uni_galaxy = $planetrow['galaxy'];
64
  $uni_system = $planetrow['system'];
65
  $planet = $planetrow['planet'];
66
}
67
68
$uni_galaxy = $uni_galaxy < 1 ? 1 : ($uni_galaxy > SN::$config->game_maxGalaxy ? SN::$config->game_maxGalaxy : $uni_galaxy);
69
$uni_system = $uni_system < 1 ? 1 : ($uni_system > SN::$config->game_maxSystem ? SN::$config->game_maxSystem : $uni_system);
70
$planet = $planet < 1 ? 1 : ($planet > SN::$config->game_maxPlanet + 1 ? SN::$config->game_maxPlanet + 1 : $planet);
71
72
$planetcount = 0;
73
$lunacount = 0;
74
$CurrentRC = $planetrow['recycler'];
75
$cached = array('users' => array(), 'allies' => array());
76
77
78
$template = gettemplate('universe', true);
0 ignored issues
show
true of type true is incompatible with the type null|template expected by parameter $template of gettemplate(). ( Ignorable by Annotation )

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

78
$template = gettemplate('universe', /** @scrutinizer ignore-type */ true);
Loading history...
79
80
$CurrentPoints = $user['total_points'];
81
82
$MissileRange = flt_get_missile_range($user);
83
$PhalanxRange = GetPhalanxRange($HavePhalanx);
84
85
$planet_precache_query = DBStaticPlanet::db_planet_list_in_system($uni_galaxy, $uni_system);
86
if (!empty($planet_precache_query)) {
87
  foreach ($planet_precache_query as $planet_row) {
88
    if (CheckAbandonPlanetState($planet_row)) {
89
      continue;
90
    }
91
    $planet_list[$planet_row['planet']][$planet_row['planet_type']] = $planet_row;
92
  }
93
}
94
95
$system_fleet_list = DbFleetStatic::fleet_list_by_planet_coords($uni_galaxy, $uni_system);
96
foreach ($system_fleet_list as $fleet_row) {
97
  $fleet_planet = $fleet_row['fleet_mess'] == 0 ? $fleet_row['fleet_end_planet'] : $fleet_row['fleet_start_planet'];
98
  $fleet_type = $fleet_row['fleet_mess'] == 0 ? $fleet_row['fleet_end_type'] : $fleet_row['fleet_start_type'];
99
  $fleet_list[$fleet_planet][$fleet_type][] = $fleet_row;
100
}
101
102
$time_now_parsed = getdate(SN_TIME_NOW);
103
104
$recycler_info = array();
105
$planet_recyclers_orbiting = 0;
106
$recyclers_fleet = array();
107
foreach (sn_get_groups('flt_recyclers') as $recycler_id) {
108
  $recycler_info[$recycler_id] = get_ship_data($recycler_id, $user);
109
  $recyclers_fleet[$recycler_id] = mrc_get_level($user, $planetrow, $recycler_id);
110
  $planet_recyclers_orbiting += $recyclers_fleet[$recycler_id];
111
}
112
113
$user_skip_list = sys_stat_get_user_skip_list();
114
$fleetsTotalIncomeOwn = array();
115
$config_game_max_planet = SN::$config->game_maxPlanet + 1;
116
for ($Planet = 1; $Planet < $config_game_max_planet; $Planet++) {
117
  unset($uni_galaxyRowPlanet);
118
  unset($uni_galaxyRowMoon);
119
  unset($uni_galaxyRowUser);
120
  unset($uni_galaxyRowAlly);
121
122
  $templatizedMoon = [];
123
124
  if (
125
    empty($planet_list[$Planet][PT_PLANET]['id'])
126
    ||
127
    !($uni_galaxyRowPlanet = $planet_list[$Planet][PT_PLANET])
128
    ||
129
    (!empty($uni_galaxyRowPlanet['destruyed']) && CheckAbandonPlanetState($uni_galaxyRowPlanet))
130
  ) {
131
    $template->assign_block_vars('galaxyrow', ['PLANET_NUM' => $Planet,]);
132
    continue;
133
  }
134
135
  $planet_fleet_id = 0;
136
  if (!isset($cached['users'][$uni_galaxyRowPlanet['id_owner']])) {
137
    $cached['users'][$uni_galaxyRowPlanet['id_owner']] = db_user_by_id($uni_galaxyRowPlanet['id_owner']);
138
  }
139
  $uni_galaxyRowUser = $cached['users'][$uni_galaxyRowPlanet['id_owner']];
140
141
  // Checking if there is planet owner record
142
  if (empty($uni_galaxyRowUser)) {
143
    // If there is planet owner but planet not destroyed - marking as destroyed
144
    if ($uni_galaxyRowPlanet['id_owner'] && empty($uni_galaxyRowPlanet['destruyed'])) {
145
      $debug->warning("Planet '{$uni_galaxyRowPlanet['name']}' [{$uni_galaxy}:{$uni_system}:{$Planet}] has no owner!", 'Userless planet', 503);
146
      $uni_galaxyRowPlanet['destruyed'] = SN_TIME_NOW + 60 * 60 * 24;
147
      $uni_galaxyRowPlanet['id_owner'] = 0;
148
      DBStaticPlanet::db_planet_set_by_id($uni_galaxyRowPlanet['id'], "id_owner = 0, destruyed = {$uni_galaxyRowPlanet['destruyed']}");
149
    }
150
  } else {
151
    if ($uni_galaxyRowUser['ally_id'] && !isset($cached['allies'][$uni_galaxyRowUser['ally_id']])) {
152
      /** @noinspection SqlResolve */
153
      $cached['allies'][$uni_galaxyRowUser['ally_id']] = doquery("SELECT * FROM `{{alliance}}` WHERE `id` = '{$uni_galaxyRowUser['ally_id']}';", '', true);
154
    }
155
  }
156
157
  $planetcount++;
158
159
  $fleets_to_planet = flt_get_fleets_to_planet(false, $fleet_list[$Planet][PT_PLANET]);
160
  if (!empty($fleets_to_planet['own']['count'])) {
161
    $planet_fleet_id = getUniqueFleetId($planet_list[$Planet][PT_PLANET]);
162
    $fleetsTotalIncomeOwn[$planet_list[$Planet][PT_PLANET]['id']] = tpl_parse_fleet_sn($fleets_to_planet['own']['total'], $planet_fleet_id);
163
  }
164
165
  if (
166
    !empty($planet_list[$Planet][PT_MOON])
167
    &&
168
    ($uni_galaxyRowMoon = $planet_list[$Planet][PT_MOON])
169
    &&
170
    (
171
      empty($uni_galaxyRowMoon['destruyed'])
172
      ||
173
      !CheckAbandonPlanetState($uni_galaxyRowMoon)
174
    )
175
  ) {
176
    $fleets_to_planet = flt_get_fleets_to_planet(false, $fleet_list[$Planet][PT_MOON]);
177
    if (!empty($fleets_to_planet['own']['count'])) {
178
      $moon_fleet_id = getUniqueFleetId($uni_galaxyRowMoon);
179
      $fleetsTotalIncomeOwn[$uni_galaxyRowMoon['id']] = tpl_parse_fleet_sn($fleets_to_planet['own']['total'], $moon_fleet_id);
180
    } else {
181
      $moon_fleet_id = 0;
182
    }
183
    $templatizedMoon = [
184
      'MOON_NAME_JS'  => js_safe_string($uni_galaxyRowMoon['name']),
185
      'MOON_IMAGE'    => $uni_galaxyRowMoon['image'],
186
      'MOON_DIAMETER' => number_format($uni_galaxyRowMoon['diameter'], 0, '', '.'),
187
      'MOON_TEMP'     => number_format($uni_galaxyRowMoon['temp_min'], 0, '', '.'),
188
      'MOON_FLEET_ID' => $moon_fleet_id,
189
    ];
190
  }
191
192
  $debrisTemplatized = [];
193
  if ($debrisTotal = $uni_galaxyRowPlanet['debris_metal'] + $uni_galaxyRowPlanet['debris_crystal']) {
194
    $recyclers_incoming_capacity = 0;
195
    if ($fleet_list[$Planet][PT_DEBRIS]) {
196
      foreach ($fleet_list[$Planet][PT_DEBRIS] as $fleet_row) {
197
        if ($fleet_row['fleet_owner'] == $user['id']) {
198
          $fleet_data = sys_unit_str2arr($fleet_row['fleet_array']);
199
          foreach ($recycler_info as $recycler_id => $recycler_data) {
200
            $recyclers_incoming_capacity += $fleet_data[$recycler_id] * $recycler_data['capacity'];
201
          }
202
        }
203
      }
204
    }
205
206
    $debris_reserved = $uni_galaxyRowPlanet['debris_reserved'] = $recyclers_incoming_capacity;
207
    $debris_reserved_percent = min(100, floor($debris_reserved / $debrisTotal * 100));
208
209
    $debris_to_gather = max(0, $debrisTotal - $recyclers_incoming_capacity);
210
211
    $recyclers_fleet_data = flt_calculate_fleet_to_transport($recyclers_fleet, $debris_to_gather, $planetrow, $uni_galaxyRowPlanet);
212
213
    $debrisTemplatized = [
214
      'DEBRIS'         => $debrisTotal,
215
      'DEBRIS_METAL'   => $uni_galaxyRowPlanet['debris_metal'],
216
      'DEBRIS_CRYSTAL' => $uni_galaxyRowPlanet['debris_crystal'],
217
218
      'DEBRIS_RESERVED'             => $debris_reserved,
219
      'DEBRIS_RESERVED_PERCENT'     => $debris_reserved_percent,
220
      'DEBRIS_WILL_GATHER'          => $debris_will_gather = max(0, min($recyclers_fleet_data['capacity'], $debris_to_gather)),
221
      'DEBRIS_WILL_GATHER_PERCENT'  => $debris_to_gather
222
        ? floor($debris_will_gather / $debris_to_gather * (100 - $debris_reserved_percent))
223
        : 0,
224
      'DEBRIS_GATHER_TOTAL'         => $debris_gather_total = max(0, $debris_will_gather + $debris_reserved),
225
      'DEBRIS_GATHER_TOTAL_PERCENT' => min(100, floor($debris_gather_total / $debrisTotal * 100)),
226
    ];
227
  }
228
229
  $RowUserPoints = $uni_galaxyRowUser['total_points'];
230
  $birthday_array = $uni_galaxyRowUser['user_birthday'] ? date_parse($uni_galaxyRowUser['user_birthday']) : array();
231
  $playerSecondsInactive = SN_TIME_NOW - $uni_galaxyRowUser['onlinetime'];
232
  $user_activity_days = floor(($playerSecondsInactive) / (60 * 60 * 24));
233
234
  $templatizedPlanet = array_merge([
235
    'PLANET_ID'        => $uni_galaxyRowPlanet['id'],
236
    'PLANET_NUM'       => $Planet,
237
    'PLANET_NAME'      => $uni_galaxyRowPlanet['name'],
238
    'PLANET_NAME_JS'   => js_safe_string($uni_galaxyRowPlanet['name']),
239
    'PLANET_DESTROYED' => $uni_galaxyRowPlanet["destruyed"],
240
    'PLANET_TYPE'      => $uni_galaxyRowPlanet["planet_type"],
241
    'PLANET_ACTIVITY'  => floor((SN_TIME_NOW - $uni_galaxyRowPlanet['last_update']) / 60),
242
    'PLANET_IMAGE'     => $uni_galaxyRowPlanet['image'],
243
    'PLANET_FLEET_ID'  => $planet_fleet_id,
244
    'PLANET_DIAMETER'  => number_format($uni_galaxyRowPlanet['diameter'], 0, '', '.'),
245
246
    'IS_CAPITAL'      => $uni_galaxyRowUser['id_planet'] == $uni_galaxyRowPlanet['id'],
247
248
    'USER_ID'         => $uni_galaxyRowUser['id'],
249
    'USER_NAME'       => $renderedNick = player_nick_render_to_html($uni_galaxyRowUser, ['icons' => true,]),
250
    'USER_NAME_JS'    => js_safe_string($renderedNick),
251
    'USER_RANK'       => in_array($uni_galaxyRowUser['id'], $user_skip_list) ? '-' : $uni_galaxyRowUser['total_rank'],
252
    'USER_BANNED'     => $uni_galaxyRowUser['banaday'],
253
    'USER_VACATION'   => $uni_galaxyRowUser['vacation'],
254
    'USER_ACTIVITY'   => $user_activity_days,
255
    'USER_ATTACKABLE' => $playerSecondsInactive >= PLAYER_INACTIVE_TIMEOUT,
256
    'USER_INACTIVE'   => $playerSecondsInactive >= PLAYER_INACTIVE_TIMEOUT_LONG,
257
    'USER_PROTECTED'  => SN::$gc->general->playerIsNoobByPoints($RowUserPoints),
258
    'USER_NOOB'       => SN::$gc->general->playerIs1stStrongerThen2nd($CurrentPoints, $RowUserPoints),
259
    'USER_STRONG'     => SN::$gc->general->playerIs1stStrongerThen2nd($RowUserPoints, $CurrentPoints),
260
    'USER_AUTH'       => $uni_galaxyRowUser['authlevel'],
261
    'USER_ADMIN'      => $lang['user_level_shortcut'][$uni_galaxyRowUser['authlevel']],
262
    'USER_BIRTHDAY'   => $birthday_array['month'] == $time_now_parsed['mon'] && $birthday_array['day'] == $time_now_parsed['mday'] ? date(FMT_DATE, SN_TIME_NOW) : 0,
263
264
    'ALLY_ID'  => $uni_galaxyRowUser['ally_id'],
265
    'ALLY_TAG' => $uni_galaxyRowUser['ally_tag'],
266
  ], $templatizedMoon, $debrisTemplatized);
267
  $template->assign_block_vars('galaxyrow', $templatizedPlanet);
268
}
269
270
tpl_assign_fleet($template, $fleetsTotalIncomeOwn);
271
272
foreach (sn_get_groups('defense_active') as $unit_id) {
273
  $template->assign_block_vars('defense_active', array(
274
    'ID'   => $unit_id,
275
    'NAME' => $lang['tech'][$unit_id],
276
  ));
277
}
278
279
foreach ($cached['users'] as $PlanetUser) {
280
  if (!$PlanetUser) {
281
    continue;
282
  }
283
284
  $user_ally = $cached['allies'][$PlanetUser['ally_id']];
285
  if (isset($user_ally)) {
286
    if ($PlanetUser['id'] == $user_ally['ally_owner']) {
287
      $user_rank_title = $user_ally['ally_owner_range'];
288
    } else {
289
      $ally_ranks = explode(';', $user_ally['ranklist']);
290
      list($user_rank_title) = explode(',', $ally_ranks[$PlanetUser['ally_rank_id']]);
291
    }
292
  } else {
293
    $user_rank_title = '';
294
  }
295
296
  $birthday_array = $PlanetUser['user_birthday'] ? date_parse($PlanetUser['user_birthday']) : array();
297
  $template->assign_block_vars('users', array(
298
    'ID'         => $PlanetUser['id'],
299
    'NAME'       => $renderedNick = player_nick_render_to_html($PlanetUser, true),
300
    'NAME_JS'    => js_safe_string($renderedNick),
301
    'RANK'       => in_array($PlanetUser['id'], $user_skip_list) ? '-' : $PlanetUser['total_rank'],
302
    'AVATAR'     => $PlanetUser['avatar'],
303
    'ALLY_ID'    => $PlanetUser['ally_id'],
304
    'ALLY_TAG'   => js_safe_string($user_ally['ally_tag']),
305
    'ALLY_TITLE' => str_replace(' ', '&nbsp', js_safe_string($user_rank_title)),
306
  ));
307
}
308
309
foreach ($cached['allies'] as $PlanetAlly) {
310
  if ($PlanetAlly) {
311
    $template->assign_block_vars('alliances', array(
312
      'ID'      => $PlanetAlly['id'],
313
      'NAME_JS' => js_safe_string($PlanetAlly['ally_name']),
314
      'MEMBERS' => $PlanetAlly['ally_members'],
315
      'URL'     => $PlanetAlly['ally_web'],
316
      'RANK'    => $PlanetAlly['total_rank'],
317
      'AVATAR'  => $PlanetAlly['ally_image'],
318
    ));
319
  }
320
}
321
322
$is_missile = SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_MISSILE] && ($CurrentMIP > 0) && ($uni_galaxy == $CurrentGalaxy) && ($uni_system >= $CurrentSystem - $MissileRange) && ($uni_system <= $CurrentSystem + $MissileRange);
323
$colspan = SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_SPYING] + SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_PM] + SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_BUDDY] + $is_missile;
324
325
/** @noinspection SqlResolve */
326
$ally_count = doquery("SELECT COUNT(*) AS ally_count FROM `{{alliance}}`;", '', true);
327
/** @noinspection SqlResolve */
328
$galaxy_name = doquery("select `universe_name` from `{{universe}}` where `universe_galaxy` = {$uni_galaxy} and `universe_system` = 0 limit 1;", true);
329
/** @noinspection SqlResolve */
330
$system_name = doquery("select `universe_name` from `{{universe}}` where `universe_galaxy` = {$uni_galaxy} and `universe_system` = {$uni_system} limit 1;", true);
331
332
$template->assign_vars(array(
333
//     'rows'                => $Result,
334
    'userCount'             => SN::$config->users_amount,
335
    'ALLY_COUNT'            => $ally_count['ally_count'],
336
    'PLANET_EXPEDITION'     => SN::$config->game_maxPlanet + 1,
337
    'curPlanetID'           => $planetrow['id'],
338
    'curPlanetG'            => $planetrow['galaxy'],
339
    'curPlanetS'            => $planetrow['system'],
340
    'curPlanetP'            => $planetrow['planet'],
341
    'curPlanetPT'           => $planetrow['planet_type'],
342
    'deathStars'            => mrc_get_level($user, $planetrow, SHIP_HUGE_DEATH_STAR, false, true),
343
    'galaxy'                => $uni_galaxy,
344
    'system'                => $uni_system,
345
    'planet'                => $planet,
346
    'MIPs'                  => round($CurrentMIP),
347
    'MODE'                  => $mode,
348
    'planets'               => $planetcount,
349
    'SPs'                   => HelperString::numberFloorAndFormat(mrc_get_level($user, $planetrow, SHIP_SPY, false, true)),
350
    'SHOW_ADMIN'            => SHOW_ADMIN,
351
    'fleet_count'           => $flying_fleet_count,
352
    'fleet_max'             => $fleetmax,
353
    'ALLY_ID'               => $user['ally_id'],
354
    'USER_ID'               => $user['id'],
355
    'ACT_SPIO'              => SN::$user_options[PLAYER_OPTION_FLEET_SPY_DEFAULT],
356
    'ACT_SPY'               => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_SPYING],
357
    'ACT_WRITE'             => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_PM],
358
    'ACT_STATISTICS'        => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_STATS],
359
    'ACT_INFO'              => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_PROFILE],
360
    'ACT_FRIEND'            => SN::$user_options[PLAYER_OPTION_UNIVERSE_ICON_BUDDY],
361
    'opt_uni_tooltip_time'  => SN::$user_options[PLAYER_OPTION_TOOLTIP_DELAY],
362
    'opt_uni_avatar_user'   => $user['opt_uni_avatar_user'],
363
    'opt_uni_avatar_ally'   => $user['opt_uni_avatar_ally'],
364
    'ACT_MISSILE'           => $is_missile,
365
    'PLANET_PHALANX'        => $HavePhalanx && $uni_galaxy == $CurrentGalaxy && $uni_system >= $CurrentSystem - $PhalanxRange && $uni_system <= $CurrentSystem + $PhalanxRange,
366
    'PAGE_HINT'             => $lang['gal_sys_hint'],
367
    'PLANET_RECYCLERS'      => $planet_recyclers_orbiting,
368
    'PLANET_RECYCLERS_TEXT' => HelperString::numberFloorAndFormat($planet_recyclers_orbiting),
369
    'GALAXY_NAME'           => $galaxy_name['universe_name'],
370
    'SYSTEM_NAME'           => $system_name['universe_name'],
371
    'COL_SPAN'              => $colspan + 9,
372
    'COL_SPAN_PLUS'         => $colspan + 3,
373
374
    'COL_SPAN_NEW'          => $colspan + 4,
375
    'COL_SPAN_NEW_COLONIZE' => $colspan - 2,
376
377
    'PLAYER_OPTION_UNIVERSE_OLD'              => SN::$user_options[PLAYER_OPTION_UNIVERSE_OLD],
378
    'PLAYER_OPTION_UNIVERSE_DISABLE_COLONIZE' => SN::$user_options[PLAYER_OPTION_UNIVERSE_DISABLE_COLONIZE],
379
  )
380
);
381
382
if ($scan) {
383
  $template_result = array_merge($template_result, array(
384
    'GLOBAL_DISPLAY_MENU'   => false,
385
    'GLOBAL_DISPLAY_NAVBAR' => false,
386
    'UNIVERSE_SCAN_MODE'    => true,
387
  ));
388
389
  $template->assign_vars(array(
390
    'UNIVERSE_SCAN_MODE' => true,
391
  ));
392
}
393
394
display($template, $lang['sys_universe']);
395