Issues (1369)

flotenajax.php (5 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\db_mysql;
18
use DBAL\OldDbChangeSet;
19
use Fleet\DbFleetStatic;
20
use Planet\DBStaticPlanet;
21
22
include('common.' . substr(strrchr(__FILE__, '.'), 1));
23
24
global $lang, $user;
25
26
define('IN_AJAX', true);
27
28
setHeader("Content-type: text/html; charset=utf-8");
29
lng_include('universe');
30
lng_include('fleet');
31
require_once('includes/includes/flt_functions.php');
32
33
$target_coord = array(
34
  'galaxy' => $target_galaxy = sys_get_param_int('galaxy'),
35
  'system' => $target_system = sys_get_param_int('system'),
36
  'planet' => $target_planet = sys_get_param_int('planet'),
37
);
38
39
if (!uni_coordinates_valid($target_coord)) {
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
  die($lang['gs_c00']);
47
}
48
49
$target_planet_type  = sys_get_param_int('planet_type');
50
$target_planet_check = $target_planet_type == PT_DEBRIS ? PT_PLANET : $target_planet_type;
51
$target_coord['planet_type'] = $target_planet_check;
52
53
// Now gathering info on db records we should lock
54
$target_row = DBStaticPlanet::db_planet_by_vector($target_coord);
55
// Finding enemy player record - if target planet exists
56
$enemy = !empty($target_row['id_owner']) ? db_user_by_id($target_row['id_owner']) : null;
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

56
$enemy = !empty($target_row['id_owner']) ? /** @scrutinizer ignore-deprecated */ db_user_by_id($target_row['id_owner']) : null;
Loading history...
57
58
db_mysql::db_transaction_start();
59
// Locking all necessary user/planet records
60
SN::$gc->db->lockRecords([
61
  'users'   => [$user['id'],] + (!empty($enemy['id']) ? [1 => $enemy['id'],] : []),
62
  'planets' => [$user['current_planet'],] + (!empty($target_row['id']) ? [1 => $target_row['id'],] : []),
63
]);
64
65
$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

65
$user      = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true);
Loading history...
66
$planetrow = DBStaticPlanet::db_planet_by_id($user['current_planet'], true);
67
68
// If target row is present - refreshing records to be absolutely sure that nothing happens with it
69
if (!empty($target_row)) {
70
  $target_row = DBStaticPlanet::db_planet_by_id($target_row['id']);
71
}
72
73
if (empty($target_row)) {
74
  $target_row = array(
75
    'galaxy'      => $target_coord['galaxy'],
76
    'system'      => $target_coord['system'],
77
    'planet'      => $target_coord['planet'],
78
    'planet_type' => $target_planet_check,
79
    'id_owner'    => 0
80
  );
81
}
82
83
$fleet_array = array();
84
switch ($target_mission) {
85
  case MT_SPY:
86
    // $fleet_array[SHIP_SPY] = min(mrc_get_level($user, $planetrow, SHIP_SPY), abs($user['spio_anz']));
87
    $fleet_array[SHIP_SPY] = min(mrc_get_level($user, $planetrow, SHIP_SPY), abs(SN::$user_options[PLAYER_OPTION_FLEET_SPY_DEFAULT]));
88
    $unit_group            = 'flt_spies';
89
  break;
90
91
  case MT_RECYCLE:
92
    foreach (sn_get_groups('flt_recyclers') as $unit_id) {
93
      if ($unit_count = mrc_get_level($user, $planetrow, $unit_id)) {
94
        $fleet_array[$unit_id] = $unit_count;
95
      }
96
    }
97
    $transport_data = flt_calculate_fleet_to_transport($fleet_array, $target_row['debris_metal'] + $target_row['debris_crystal'], $planetrow, $target_row);
98
    $fleet_array    = $transport_data['fleet'];
99
    $unit_group     = 'flt_recyclers';
100
  break;
101
102
  case MT_MISSILE:
103
    $fleet_array[UNIT_DEF_MISSILE_INTERPLANET] = min(mrc_get_level($user, $planetrow, UNIT_DEF_MISSILE_INTERPLANET), abs(sys_get_param_float('missiles')));
104
    $unit_group                                = 'missile';
105
  break;
106
107
}
108
109
$options     = [P_FLEET_ATTACK_TARGET_STRUCTURE => $target_structure = sys_get_param_int('structures')];
110
$cant_attack = flt_can_attack($planetrow, $target_row, $fleet_array, $target_mission, $options);
111
112
113
if ($cant_attack != ATTACK_ALLOWED) {
114
  die($lang['fl_attack_error'][$cant_attack]);
115
}
116
117
$FleetDBArray = array();
118
$db_changeset = array();
119
foreach ($fleet_array as $unit_id => $unit_count) {
120
  $FleetDBArray[]         = "{$unit_id},{$unit_count}";
121
  $db_changeset['unit'][] = OldDbChangeSet::db_changeset_prepare_unit($unit_id, -$unit_count, $user, $planetrow);
122
}
123
$FleetDBArray = implode(';', $FleetDBArray);
124
125
$fleet_ship_count = array_sum($fleet_array);
126
127
if ($target_mission == MT_MISSILE) {
128
  $distance                   = abs($target_coord['system'] - $planetrow['system']);
129
  $duration                   = round((30 + (60 * $distance)) / Universe::flt_server_flight_speed_multiplier());
130
  $arrival                    = SN_TIME_NOW + $duration;
131
  $travel_data['consumption'] = 0;
132
133
  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

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