Completed
Push — work-fleets ( 22b5bc...08ace7 )
by SuperNova.WS
06:13
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_set_by_id_DEPRECATED($planetrow['id'], "`deuterium` = `deuterium` - {$travel_data['consumption']}");
0 ignored issues
show
Deprecated Code introduced by
The method DBStaticPlanet::db_plane..._set_by_id_DEPRECATED() has been deprecated.

This method has been deprecated.

Loading history...
143
144
  foreach ($fleet_array as $unit_id => $unit_count) {
145
    DBStaticUnit::dbUpdateOrInsertUnit($unit_id, -$unit_count, $user, $planetrow);
146
  }
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);
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