Completed
Push — work-fleets ( 45d3a1...6724fd )
by SuperNova.WS
09:14 queued 03:18
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(
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
  if ($target_mission == MT_MISSILE) {
117
    $distance = abs($target_coord['system'] - $planetrow['system']);
118
    $duration = round((30 + (60 * $distance)) / flt_server_flight_speed_multiplier());
119
    $arrival = SN_TIME_NOW + $duration;
120
    $travel_data['consumption'] = 0;
121
122
    DBStaticFleetMissile::db_missile_insert($target_coord, $user, $planetrow, $arrival, array_sum($fleet_array), $target_structure);
123
  } else {
124
    $objFleet = new Fleet();
125
    $objFleet->set_start_planet($planetrow);
126
    $objFleet->set_end_planet($target_coord);
127
    $objFleet->playerOwnerId = $user['id'];
128
    $objFleet->group_id = 0;
129
    $objFleet->unitsSetFromArray($fleet_array);
130
    $objFleet->mission_type = $target_mission;
131
132
    $travel_data = flt_travel_data($user, $planetrow, $target_coord, $fleet_array, 10);
133
134
    if ($planetrow['deuterium'] < $travel_data['consumption']) {
135
      die(classLocale::$lang['gs_c13']);
136
    }
137
138
    $objFleet->set_times($travel_data['duration']);
139
    $objFleet->dbInsert();
140
  }
141
142
  DBStaticPlanet::db_planet_update_adjust_by_id(
143
    $planetrow['id'],
144
    array(
0 ignored issues
show
array('deuterium' => -$t...el_data['consumption']) is of type array<string,integer|dou...ium":"integer|double"}>, but the function expects a string.

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...
145
      'deuterium' => -$travel_data['consumption'],
146
    )
147
  );
148
149
  foreach ($fleet_array as $unit_id => $unit_count) {
150
    DBStaticUnit::dbUpdateOrInsertUnit($unit_id, -$unit_count, $user, $planetrow);
151
  }
152
  sn_db_transaction_commit();
153
154
  $ships_sent = array();
155
  $ships_sent_js = 0;
156
  foreach ($fleet_array as $unit_id => $unit_count) {
157
    $ships_sent[] = "{$unit_count} {$classLocale['tech'][$unit_id]}";
158
    $ships_sent_js += mrc_get_level($user, $planetrow, $unit_id, false, true);
159
  }
160
  $ships_sent = implode(', ', $ships_sent);
161
  $ships_sent_js = "{$unit_group}={$ships_sent_js}";
162
163
  $ResultMessage = "{$classLocale['gs_sending']} {$ships_sent} {$classLocale['gs_to']} {$target_coord['galaxy']}:{$target_coord['system']}:{$target_coord['planet']}|{$ships_sent_js}";
164
165
  die($ResultMessage);
166
}
167
168
fleet_ajax();
169