Completed
Push — work-fleets ( c97fe0...81e6e5 )
by SuperNova.WS
05:36
created

flotenajax.php (4 issues)

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 = db_user_by_id($user['id'], true);
63
  $planetrow = 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 = 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
  $fleet_array = array();
78
  switch($target_mission) {
79
    case MT_SPY:
80
      $fleet_array[SHIP_SPY] = min(mrc_get_level($user, $planetrow, SHIP_SPY), abs(classSupernova::$user_options[PLAYER_OPTION_FLEET_SPY_DEFAULT]));
0 ignored issues
show
It seems like $user defined by db_user_by_id($user['id'], true) on line 62 can also be of type false; however, mrc_get_level() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
81
      $unit_group = 'flt_spies';
82
    break;
83
84
    case MT_RECYCLE:
85
      foreach(sn_get_groups('flt_recyclers') as $unit_id) {
86
        if($unit_count = mrc_get_level($user, $planetrow, $unit_id)) {
0 ignored issues
show
It seems like $user defined by db_user_by_id($user['id'], true) on line 62 can also be of type false; however, mrc_get_level() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
87
          $fleet_array[$unit_id] = $unit_count;
88
        }
89
      }
90
      $transport_data = flt_calculate_fleet_to_transport($fleet_array, $target_row['debris_metal'] + $target_row['debris_crystal'], $planetrow, $target_row);
91
      $fleet_array = $transport_data['fleet'];
92
      $unit_group = 'flt_recyclers';
93
    break;
94
95
    case MT_MISSILE:
96
      $fleet_array[UNIT_DEF_MISSILE_INTERPLANET] = min(mrc_get_level($user, $planetrow, UNIT_DEF_MISSILE_INTERPLANET), abs(sys_get_param_float('missiles')));
0 ignored issues
show
It seems like $user defined by db_user_by_id($user['id'], true) on line 62 can also be of type false; however, mrc_get_level() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
97
      $unit_group = 'missile';
98
    break;
99
100
  }
101
102
  $isAttackAllowed = flt_can_attack(
103
    $planetrow,
104
    $target_row,
105
    $fleet_array,
106
    $target_mission,
107
    array(
108
      'target_structure' => $target_structure = sys_get_param_int('structures'),
109
    )
110
  );
111
  if($isAttackAllowed != FLIGHT_ALLOWED) {
112
    die(classLocale::$lang['fl_attack_error'][$isAttackAllowed]);
113
  }
114
115
  $db_changeset = array();
116
  foreach($fleet_array as $unit_id => $unit_count) {
117
    $db_changeset['unit'][] = sn_db_unit_changeset_prepare($unit_id, -$unit_count, $user, $planetrow);
118
  }
119
120
  if($target_mission == MT_MISSILE) {
121
    $distance = abs($target_coord['system'] - $planetrow['system']);
122
    $duration = round((30 + (60 * $distance)) / flt_server_flight_speed_multiplier());
123
    $arrival = SN_TIME_NOW + $duration;
124
    $travel_data['consumption'] = 0;
125
126
    db_missile_insert($target_coord, $user, $planetrow, $arrival, array_sum($fleet_array), $target_structure);
127
  } else {
128
    $travel_data = flt_travel_data($user, $planetrow, $target_coord, $fleet_array, 10);
129
130
    if($planetrow['deuterium'] < $travel_data['consumption']) {
131
      die(classLocale::$lang['gs_c13']);
132
    }
133
134
    $objFleet = new Fleet();
135
    $objFleet->set_times($travel_data['duration']);
136
    $objFleet->unitsSetFromArray($fleet_array);
137
    $objFleet->mission_type = $target_mission;
138
    $objFleet->set_start_planet($planetrow);
139
    $objFleet->set_end_planet($target_coord);
140
    $objFleet->playerOwnerId = $user['id'];
141
    $objFleet->group_id = 0;
142
    $objFleet->dbInsert();
143
  }
144
145
  db_planet_set_by_id($planetrow['id'], "`deuterium` = `deuterium` - {$travel_data['consumption']}");
146
  db_changeset_apply($db_changeset);
147
  sn_db_transaction_commit();
148
149
  $ships_sent = array();
150
  $ships_sent_js = 0;
151
  foreach($fleet_array as $unit_id => $unit_count) {
152
    $ships_sent[] = "{$unit_count} {$classLocale['tech'][$unit_id]}";
153
    $ships_sent_js += mrc_get_level($user, $planetrow, $unit_id, false, true);
0 ignored issues
show
It seems like $user defined by db_user_by_id($user['id'], true) on line 62 can also be of type false; however, mrc_get_level() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
154
  }
155
  $ships_sent = implode(', ', $ships_sent);
156
  $ships_sent_js = "{$unit_group}={$ships_sent_js}";
157
158
  $ResultMessage = "{$classLocale['gs_sending']} {$ships_sent} {$classLocale['gs_to']} {$target_coord['galaxy']}:{$target_coord['system']}:{$target_coord['planet']}|{$ships_sent_js}";
159
160
  die($ResultMessage);
161
}
162
163
fleet_ajax();
164