Completed
Push — work-fleets ( d28adc...7496c9 )
by SuperNova.WS
06:01
created

flotenajax.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
include('common.' . substr(strrchr(__FILE__, '.'), 1));
18
19
define('IN_AJAX', true);
20
21
require_once('includes/includes/flt_functions.php');
22
23
/**
24
 * @throws Exception
25
 */
26
function fleet_ajax() {
27
  global $user;
28
29
  classLocale::$lang->lng_include('universe');
30
  classLocale::$lang->lng_include('fleet');
31
32
  $classLocale = classLocale::$lang;
33
34
  $travel_data = array();
35
36
  // TODO - change to JSON. Message can be sent in JSON-encoded field
37
  header("Content-type: text/html; charset=utf-8");
38
39
  $target_mission = sys_get_param_int('mission');
40
  $sn_group_missions = sn_get_groups('missions');
41
  if (empty($sn_group_missions[$target_mission]['AJAX'])) {
42
    die(classLocale::$lang['gs_c00']);
43
  }
44
45
  // Checking target coordinates validity
46
  $target_coord = array(
47
    'id'          => null,
48
    'id_owner'    => 0,
49
    'galaxy'      => sys_get_param_int('galaxy'),
50
    'system'      => sys_get_param_int('system'),
51
    'planet'      => sys_get_param_int('planet'),
52
    'planet_type' => sys_get_param_int('planet_type'),
53
  );
54
  // fleet_ajax now can send fleets only to existing planets/moons
55
  // TODO - sending colonization and expeditions in 1 click
56
  if (!uni_coordinates_valid($target_coord)) {
57
    die(classLocale::$lang['gs_c02']);
58
  }
59
60
  sn_db_transaction_start();
61
62
  $user = DBStaticUser::db_user_by_id($user['id'], true);
63
  $planetrow = DBStaticPlanet::db_planet_by_id($user['current_planet'], true);
64
65
  // TODO - DEADLOCK CAN BE HERE!!!! We should lock SOURCE and TARGET owners in one query
66
  $target_row = DBStaticPlanet::db_planet_by_vector($target_coord);
67
  if (empty($target_row)) {
68
    $target_row = $target_coord;
69
    $target_row['id_owner'] = 0;
70
    // If fleet destination is PT_DEBRIS - then it's actually destination is PT_PLANET // TODO - REMOVE! Debris should be valid DESTINATION planet_type!
71
    $target_row['planet_type'] = $target_row['planet_type'] == PT_DEBRIS ? PT_PLANET : $target_row['planet_type'];
72
  } else {
73
    $target_coord['id'] = $target_row['id'];
74
    $target_coord['id_owner'] = $target_row['id_owner'];
75
  }
76
77
  $unit_group = '';
78
  $fleet_array = array();
79
  switch ($target_mission) {
80
    case MT_SPY:
81
      $fleet_array[SHIP_SPY] = min(mrc_get_level($user, $planetrow, SHIP_SPY), abs(classSupernova::$user_options[PLAYER_OPTION_FLEET_SPY_DEFAULT]));
82
      $unit_group = 'flt_spies';
83
    break;
84
85
    case MT_RECYCLE:
86
      foreach (Fleet::$snGroupRecyclers as $unit_id) {
87
        if ($unit_count = mrc_get_level($user, $planetrow, $unit_id)) {
88
          $fleet_array[$unit_id] = $unit_count;
89
        }
90
      }
91
      $transport_data = flt_calculate_fleet_to_transport($fleet_array, $target_row['debris_metal'] + $target_row['debris_crystal'], $planetrow, $target_row);
92
      $fleet_array = $transport_data['fleet'];
93
      $unit_group = 'flt_recyclers';
94
    break;
95
96
    case MT_MISSILE:
97
      $fleet_array[UNIT_DEF_MISSILE_INTERPLANET] = min(mrc_get_level($user, $planetrow, UNIT_DEF_MISSILE_INTERPLANET), abs(sys_get_param_float('missiles')));
98
      $unit_group = GROUP_STR_MISSILES;
99
    break;
100
101
  }
102
103
  $isAttackAllowed = flt_can_attack(
104
    $planetrow,
105
    $target_row,
106
    $fleet_array,
107
    $target_mission,
108
    array(
0 ignored issues
show
array('target_structure'...aram_int('structures')) is of type array<string,integer,{"t..._structure":"integer"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
      'target_structure' => $target_structure = sys_get_param_int('structures'),
110
    )
111
  );
112
  if ($isAttackAllowed != FLIGHT_ALLOWED) {
113
    die(classLocale::$lang['fl_attack_error'][$isAttackAllowed]);
114
  }
115
116
  $db_changeset = array();
117
  foreach ($fleet_array as $unit_id => $unit_count) {
118
    $db_changeset['unit'][] = sn_db_unit_changeset_prepare($unit_id, -$unit_count, $user, $planetrow);
119
  }
120
121
  if ($target_mission == MT_MISSILE) {
122
    $distance = abs($target_coord['system'] - $planetrow['system']);
123
    $duration = round((30 + (60 * $distance)) / flt_server_flight_speed_multiplier());
124
    $arrival = SN_TIME_NOW + $duration;
125
    $travel_data['consumption'] = 0;
126
127
    DBStaticFleetMissile::db_missile_insert($target_coord, $user, $planetrow, $arrival, array_sum($fleet_array), $target_structure);
128
  } else {
129
    $objFleet = new Fleet();
130
    $objFleet->set_start_planet($planetrow);
131
    $objFleet->set_end_planet($target_coord);
132
    $objFleet->playerOwnerId = $user['id'];
133
    $objFleet->group_id = 0;
134
    $objFleet->unitsSetFromArray($fleet_array);
135
    $objFleet->mission_type = $target_mission;
136
137
    $travel_data = flt_travel_data($user, $planetrow, $target_coord, $fleet_array, 10);
138
139
    if ($planetrow['deuterium'] < $travel_data['consumption']) {
140
      die(classLocale::$lang['gs_c13']);
141
    }
142
143
    $objFleet->set_times($travel_data['duration']);
144
    $objFleet->dbInsert();
145
  }
146
147
  DBStaticPlanet::db_planet_set_by_id($planetrow['id'], "`deuterium` = `deuterium` - {$travel_data['consumption']}");
148
  classSupernova::db_changeset_apply($db_changeset);
149
  sn_db_transaction_commit();
150
151
  $ships_sent = array();
152
  $ships_sent_js = 0;
153
  foreach ($fleet_array as $unit_id => $unit_count) {
154
    $ships_sent[] = "{$unit_count} {$classLocale['tech'][$unit_id]}";
155
    $ships_sent_js += mrc_get_level($user, $planetrow, $unit_id, false, true);
156
  }
157
  $ships_sent = implode(', ', $ships_sent);
158
  $ships_sent_js = "{$unit_group}={$ships_sent_js}";
159
160
  $ResultMessage = "{$classLocale['gs_sending']} {$ships_sent} {$classLocale['gs_to']} {$target_coord['galaxy']}:{$target_coord['system']}:{$target_coord['planet']}|{$ships_sent_js}";
161
162
  die($ResultMessage);
163
}
164
165
fleet_ajax();
166