Test Failed
Push — trunk ( 769658...cc4c01 )
by SuperNova.WS
15:09
created

flotenajax.php (4 issues)

1
<?php
2
3
/**
4
 * flotenajax.php
5
 *
6
 * Fleet manager on Ajax (to work in Galaxy view)
7
 *
8
 * @version 2.0 Security checks by Gorlum for http://supernova.ws
9
 *  [!] Full rewrite
10
 *  [+] Added missile attack launch sequience
11
 *  [-] Moved almost all check code to flt_can_attack
12
 * @version 1.1 Security checks by Gorlum for http://supernova.ws
13
 * @version 1
14
 * @copyright 2008 By Chlorel for XNova
15
**/
16
17
use DBAL\OldDbChangeSet;
18
use Fleet\DbFleetStatic;
19
use Planet\DBStaticPlanet;
20
21
include('common.' . substr(strrchr(__FILE__, '.'), 1));
22
23
global $lang, $user;
24
25
define('IN_AJAX', true);
26
27
setHeader("Content-type: text/html; charset=utf-8");
28
lng_include('universe');
29
lng_include('fleet');
30
require_once('includes/includes/flt_functions.php');
31
32
$target_coord = array(
33
  'galaxy' => $target_galaxy = sys_get_param_int('galaxy'),
34
  'system' => $target_system = sys_get_param_int('system'),
35
  'planet' => $target_planet = sys_get_param_int('planet'),
36
);
37
38
if(!uni_coordinates_valid($target_coord))
39
{
40
  die($lang['gs_c02']);
41
}
42
43
$target_mission = sys_get_param_int('mission');
44
$sn_group_missions = sn_get_groups('missions');
45
if(!isset($sn_group_missions[$target_mission]['AJAX']) || !$sn_group_missions[$target_mission]['AJAX'])
46
{
47
  die($lang['gs_c00']);
48
}
49
50
SN::db_transaction_start();
51
52
$user = db_user_by_id($user['id'], true);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

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

52
$user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true);
Loading history...
53
$planetrow = DBStaticPlanet::db_planet_by_id($user['current_planet'], true);
54
55
$target_planet_type = sys_get_param_int('planet_type');
56
$target_planet_check = $target_planet_type == PT_DEBRIS ? PT_PLANET : $target_planet_type;
57
58
$target_coord['planet_type'] = $target_planet_check;
59
$target_row = DBStaticPlanet::db_planet_by_vector($target_coord);
60
61
if(empty($target_row))
62
{
63
  $target_row = array(
64
    'galaxy' => $target_coord['galaxy'],
65
    'system' => $target_coord['system'],
66
    'planet' => $target_coord['planet'],
67
    'planet_type' => $target_planet_check,
68
    'id_owner' => 0
69
  );
70
}
71
72
$fleet_array = array();
73
switch($target_mission)
74
{
75
  case MT_SPY:
76
    // $fleet_array[SHIP_SPY] = min(mrc_get_level($user, $planetrow, SHIP_SPY), abs($user['spio_anz']));
77
    $fleet_array[SHIP_SPY] = min(mrc_get_level($user, $planetrow, SHIP_SPY), abs(SN::$user_options[PLAYER_OPTION_FLEET_SPY_DEFAULT]));
78
    $unit_group = 'flt_spies';
79
  break;
80
81
  case MT_RECYCLE:
82
    foreach(sn_get_groups('flt_recyclers') as $unit_id)
83
    {
84
      if($unit_count = mrc_get_level($user, $planetrow, $unit_id))
85
      {
86
        $fleet_array[$unit_id] = $unit_count;
87
      }
88
    }
89
    $transport_data = flt_calculate_fleet_to_transport($fleet_array, $target_row['debris_metal'] + $target_row['debris_crystal'], $planetrow, $target_row);
90
    $fleet_array = $transport_data['fleet'];
91
    $unit_group = 'flt_recyclers';
92
  break;
93
94
  case MT_MISSILE:
95
    $fleet_array[UNIT_DEF_MISSILE_INTERPLANET] = min(mrc_get_level($user, $planetrow, UNIT_DEF_MISSILE_INTERPLANET), abs(sys_get_param_float('missiles')));
96
    $unit_group = 'missile';
97
  break;
98
99
}
100
101
$options = [P_FLEET_ATTACK_TARGET_STRUCTURE => $target_structure = sys_get_param_int('structures')];
102
$cant_attack = flt_can_attack($planetrow, $target_row, $fleet_array, $target_mission, $options);
103
104
105
if($cant_attack != ATTACK_ALLOWED)
106
{
107
  die($lang['fl_attack_error'][$cant_attack]);
108
}
109
110
$FleetDBArray   = array();
111
$db_changeset = array();
112
foreach($fleet_array as $unit_id => $unit_count)
113
{
114
  $FleetDBArray[] = "{$unit_id},{$unit_count}";
115
  $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit($unit_id, -$unit_count, $user, $planetrow);
116
}
117
$FleetDBArray = implode(';', $FleetDBArray);
118
119
$fleet_ship_count = array_sum($fleet_array);
120
121
if($target_mission == MT_MISSILE)
122
{
123
  $distance = abs($target_coord['system'] - $planetrow['system']);
124
  $duration = round((30 + (60 * $distance)) / Universe::flt_server_flight_speed_multiplier());
125
  $arrival = SN_TIME_NOW + $duration;
126
  $travel_data['consumption'] = 0;
127
128
  doquery(
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

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

128
  /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
129
    "INSERT INTO `{{iraks}}` SET
130
     `fleet_target_owner` = '{$target_row['id_owner']}', `fleet_end_galaxy` = '{$target_coord['galaxy']}', `fleet_end_system` = '{$target_coord['system']}', `fleet_end_planet` = '{$target_coord['planet']}',
131
     `fleet_owner` = '{$user['id']}', `fleet_start_galaxy` = '{$planetrow['galaxy']}', `fleet_start_system` = '{$planetrow['system']}', `fleet_start_planet` = '{$planetrow['planet']}',
132
     `fleet_end_time` = '{$arrival}', `fleet_amount` = '{$fleet_ship_count}', `primaer` = '{$target_structure}';"
133
  );
134
}
135
else
136
{
137
  $travel_data = flt_travel_data($user, $planetrow, $target_coord, $fleet_array, 10);
138
139
  if($planetrow['deuterium'] < $travel_data['consumption'])
140
  {
141
    die($lang['gs_c13']);
142
  }
143
144
  $fleet_start_time = SN_TIME_NOW + $travel_data['duration'];
145
  $fleet_end_time   = $fleet_start_time + $travel_data['duration'];
146
147
  $fleet_set = [
148
    'fleet_owner' => $user['id'],
149
    'fleet_mission' => $target_mission,
150
    'fleet_amount' => $fleet_ship_count,
151
    'fleet_array' => $FleetDBArray,
152
    'fleet_start_time' => $fleet_start_time,
153
    'fleet_start_planet_id' => !empty($planetrow['id']) ? $planetrow['id'] : null,
154
    'fleet_start_galaxy' => $planetrow['galaxy'],
155
    'fleet_start_system' => $planetrow['system'],
156
    'fleet_start_planet' => $planetrow['planet'],
157
    'fleet_start_type'   => $planetrow['planet_type'],
158
    'fleet_end_time' => $fleet_end_time,
159
    'fleet_end_planet_id' => !empty($target_row['id']) ? $target_row['id'] : null,
160
    'fleet_end_galaxy' => $target_coord['galaxy'],
161
    'fleet_end_system' => $target_coord['system'],
162
    'fleet_end_planet' => $target_coord['planet'],
163
    'fleet_end_type' => $target_planet_type,
164
    'fleet_target_owner' => $target_row['id_owner'],
165
    'start_time' => SN_TIME_NOW,
166
  ];
167
  DbFleetStatic::fleet_insert_set_dbq($fleet_set);
168
}
169
170
DBStaticPlanet::db_planet_set_by_id($planetrow['id'], "`deuterium` = `deuterium` - {$travel_data['consumption']}");
171
OldDbChangeSet::db_changeset_apply($db_changeset);
172
SN::db_transaction_commit();
173
174
$ships_sent = array();
175
//$ships_sent_js = array();
176
$ships_sent_js = 0;
177
foreach($fleet_array as $unit_id => $unit_count)
0 ignored issues
show
Comprehensibility Bug introduced by
$unit_id is overwriting a variable from outer foreach loop.
Loading history...
Comprehensibility Bug introduced by
$unit_count is overwriting a variable from outer foreach loop.
Loading history...
178
{
179
  $ships_sent[] = "{$unit_count} {$lang['tech'][$unit_id]}";
180
  $ships_sent_js += mrc_get_level($user, $planetrow, $unit_id, false, true);
181
}
182
$ships_sent = implode(', ', $ships_sent);
183
//$ships_sent_js = implode(',', $ships_sent_js);
184
$ships_sent_js = "{$unit_group}={$ships_sent_js}";
185
186
$ResultMessage  = "{$lang['gs_sending']} {$ships_sent} {$lang['gs_to']} {$target_coord['galaxy']}:{$target_coord['system']}:{$target_coord['planet']}|{$ships_sent_js}";
187
188
die($ResultMessage);
189