Example #1
0
function flt_t_send_fleet($user, &$from, $to, $fleet, $mission, $options = array())
{
    //ini_set('error_reporting', E_ALL);
    $internal_transaction = !sn_db_transaction_check(false) ? sn_db_transaction_start() : false;
    //pdump($internal_transaction);
    // TODO Потенциальный дедлок - если успела залочится запись пользователя - хозяина планеты
    $user = db_user_by_id($user['id'], true);
    $from = sys_o_get_updated($user, $from['id'], SN_TIME_NOW);
    $from = $from['planet'];
    $can_attack = flt_can_attack($from, $to, $fleet, $mission, $options);
    if ($can_attack != ATTACK_ALLOWED) {
        $internal_transaction ? sn_db_transaction_rollback() : false;
        return $can_attack;
    }
    $fleet_group = isset($options['fleet_group']) ? floatval($options['fleet_group']) : 0;
    $travel_data = flt_travel_data($user, $from, $to, $fleet, $options['fleet_speed_percent']);
    $fleet_start_time = SN_TIME_NOW + $travel_data['duration'];
    if ($mission == MT_EXPLORE || $mission == MT_HOLD) {
        $stay_duration = $options['stay_time'] * 3600;
        $stay_time = $fleet_start_time + $stay_duration;
    } else {
        $stay_duration = 0;
        $stay_time = 0;
    }
    $fleet_end_time = $fleet_start_time + $travel_data['duration'] + $stay_duration;
    $fleet_ship_count = 0;
    $fleet_string = '';
    $db_changeset = array();
    $planet_fields = array();
    foreach ($fleet as $unit_id => $amount) {
        if (!$amount || !$unit_id) {
            continue;
        }
        if (in_array($unit_id, sn_get_groups('fleet'))) {
            $fleet_ship_count += $amount;
            $fleet_string .= "{$unit_id},{$amount};";
            $db_changeset['unit'][] = sn_db_unit_changeset_prepare($unit_id, -$amount, $user, $from['id']);
        } elseif (in_array($unit_id, sn_get_groups('resources_loot'))) {
            $planet_fields[pname_resource_name($unit_id)]['delta'] -= $amount;
        }
    }
    $to['id_owner'] = intval($to['id_owner']);
    $QryInsertFleet = "INSERT INTO {{fleets}} SET ";
    $QryInsertFleet .= "`fleet_owner` = '{$user['id']}', ";
    $QryInsertFleet .= "`fleet_mission` = '{$mission}', ";
    $QryInsertFleet .= "`fleet_amount` = '{$fleet_ship_count}', ";
    $QryInsertFleet .= "`fleet_array` = '{$fleet_string}', ";
    $QryInsertFleet .= "`fleet_start_time` = '{$fleet_start_time}', ";
    if ($from['id']) {
        $QryInsertFleet .= "`fleet_start_planet_id` = '{$from['id']}', ";
    }
    $QryInsertFleet .= "`fleet_start_galaxy` = '{$from['galaxy']}', ";
    $QryInsertFleet .= "`fleet_start_system` = '{$from['system']}', ";
    $QryInsertFleet .= "`fleet_start_planet` = '{$from['planet']}', ";
    $QryInsertFleet .= "`fleet_start_type` = '{$from['planet_type']}', ";
    $QryInsertFleet .= "`fleet_end_time` = '{$fleet_end_time}', ";
    $QryInsertFleet .= "`fleet_end_stay` = '{$stay_time}', ";
    if ($to['id']) {
        $QryInsertFleet .= "`fleet_end_planet_id` = '{$to['id']}', ";
    }
    $QryInsertFleet .= "`fleet_end_galaxy` = '{$to['galaxy']}', ";
    $QryInsertFleet .= "`fleet_end_system` = '{$to['system']}', ";
    $QryInsertFleet .= "`fleet_end_planet` = '{$to['planet']}', ";
    $QryInsertFleet .= "`fleet_end_type` = '{$to['planet_type']}', ";
    $QryInsertFleet .= "`fleet_resource_metal` = " . floatval($fleet[RES_METAL]) . ", ";
    $QryInsertFleet .= "`fleet_resource_crystal` = " . floatval($fleet[RES_CRYSTAL]) . ", ";
    $QryInsertFleet .= "`fleet_resource_deuterium` = " . floatval($fleet[RES_DEUTERIUM]) . ", ";
    $QryInsertFleet .= "`fleet_target_owner` = '{$to['id_owner']}', ";
    $QryInsertFleet .= "`fleet_group` = '{$fleet_group}', ";
    $QryInsertFleet .= "`start_time` = " . SN_TIME_NOW . ";";
    doquery($QryInsertFleet);
    $planet_fields[pname_resource_name(RES_DEUTERIUM)]['delta'] -= $travel_data['consumption'];
    $db_changeset['planets'][] = array('action' => SQL_OP_UPDATE, P_VERSION => 1, 'where' => array('id' => $from['id']), 'fields' => $planet_fields);
    db_changeset_apply($db_changeset);
    $internal_transaction ? sn_db_transaction_commit() : false;
    $from = db_planet_by_id($from['id']);
    return ATTACK_ALLOWED;
    //ini_set('error_reporting', E_ALL ^ E_NOTICE);
}
Example #2
0
function flt_t_send_fleet($user, &$from, $to, $fleet, $mission, $options = array())
{
    //ini_set('error_reporting', E_ALL);
    //doquery('SET autocommit = 0;');
    //doquery('LOCK TABLES {{users}} READ, {{planets}} WRITE, {{fleet}} WRITE, {{aks}} WRITE, {{statpoints}} READ;');
    doquery('START TRANSACTION;');
    $from = sys_o_get_updated($user, $from['id'], $GLOBALS['time_now']);
    $from = $from['planet'];
    $speed_factor = get_fleet_speed();
    $distance = GetTargetDistance($from['galaxy'], $to['galaxy'], $from['system'], $to['system'], $from['planet'], $to['planet']);
    $fleet_speed = min(GetFleetMaxSpeed($fleet, 0, $user));
    $duration = GetMissionDuration(10, $fleet_speed, $distance, $speed_factor);
    $consumption = GetFleetConsumption($fleet, $speed_factor, $duration, $distance, $fleet_speed, $user);
    //!!
    $fleet_group = 0;
    //!!
    $options = array();
    $can_attack = flt_can_attack($from, $to, $fleet, $mission, $options);
    if ($can_attack != ATTACK_ALLOWED) {
        doquery('ROLLBACK');
        return $can_attack;
    }
    global $time_now, $sn_data;
    $fleet_start_time = $time_now + $duration;
    if ($mission == MT_EXPLORE or $mission == MT_HOLD) {
        $stay_duration = $options['stay_time'] * 3600;
        $stay_time = $fleet_start_time + $stay_duration;
    } else {
        $stay_duration = 0;
        $stay_time = 0;
    }
    $fleet_end_time = $fleet_start_time + $duration + $stay_duration;
    $fleet_ship_count = 0;
    $fleet_string = '';
    $planet_sub_query = '';
    foreach ($fleet as $unit_id => $amount) {
        if (!$amount || !$unit_id) {
            continue;
        }
        if (in_array($unit_id, $sn_data['groups']['fleet'])) {
            $fleet_ship_count += $amount;
            $fleet_string .= "{$unit_id},{$amount};";
        }
        $planet_sub_query .= "`{$sn_data[$unit_id]['name']}` = `{$sn_data[$unit_id]['name']}` - {$amount},";
    }
    $to['id_owner'] = intval($to['id_owner']);
    $QryInsertFleet = "INSERT INTO {{fleets}} SET ";
    $QryInsertFleet .= "`fleet_owner` = '{$user['id']}', ";
    $QryInsertFleet .= "`fleet_mission` = '{$mission}', ";
    $QryInsertFleet .= "`fleet_amount` = '{$fleet_ship_count}', ";
    $QryInsertFleet .= "`fleet_array` = '{$fleet_string}', ";
    $QryInsertFleet .= "`fleet_start_time` = '{$fleet_start_time}', ";
    $QryInsertFleet .= "`fleet_start_galaxy` = '{$from['galaxy']}', ";
    $QryInsertFleet .= "`fleet_start_system` = '{$from['system']}', ";
    $QryInsertFleet .= "`fleet_start_planet` = '{$from['planet']}', ";
    $QryInsertFleet .= "`fleet_start_type` = '{$from['planet_type']}', ";
    $QryInsertFleet .= "`fleet_end_time` = '{$fleet_end_time}', ";
    $QryInsertFleet .= "`fleet_end_stay` = '{$stay_time}', ";
    $QryInsertFleet .= "`fleet_end_galaxy` = '{$to['galaxy']}', ";
    $QryInsertFleet .= "`fleet_end_system` = '{$to['system']}', ";
    $QryInsertFleet .= "`fleet_end_planet` = '{$to['planet']}', ";
    $QryInsertFleet .= "`fleet_end_type` = '{$to['planet_type']}', ";
    $QryInsertFleet .= "`fleet_resource_metal` = '{$fleet[RES_METAL]}', ";
    $QryInsertFleet .= "`fleet_resource_crystal` = '{$fleet[RES_CRYSTAL]}', ";
    $QryInsertFleet .= "`fleet_resource_deuterium` = '{$fleet[RES_DEUTERIUM]}', ";
    $QryInsertFleet .= "`fleet_target_owner` = '{$to['id_owner']}', ";
    $QryInsertFleet .= "`fleet_group` = '{$fleet_group}', ";
    $QryInsertFleet .= "`start_time` = '{$time_now}';";
    doquery($QryInsertFleet);
    $QryUpdatePlanet = "UPDATE {{planets}} SET {$planet_sub_query} `deuterium` = `deuterium` - '{$consumption}' WHERE `id` = '{$from['id']}' LIMIT 1;";
    doquery($QryUpdatePlanet);
    if (BE_DEBUG) {
        debug($QryInsertFleet);
        debug($QryUpdatePlanet);
    }
    doquery("COMMIT;");
    // doquery('SET autocommit = 1;');
    $from = doquery("SELECT * FROM {{planets}} WHERE `id` = '{$from['id']}' LIMIT 1;", '', true);
    return ATTACK_ALLOWED;
    //ini_set('error_reporting', E_ALL ^ E_NOTICE);
}
Example #3
0
        foreach (sn_get_groups('flt_recyclers') as $unit_id) {
            if ($unit_count = mrc_get_level($user, $planetrow, $unit_id)) {
                $fleet_array[$unit_id] = $unit_count;
            }
        }
        $transport_data = flt_calculate_fleet_to_transport($fleet_array, $target_row['debris_metal'] + $target_row['debris_crystal'], $planetrow, $target_row);
        $fleet_array = $transport_data['fleet'];
        $unit_group = 'flt_recyclers';
        break;
    case MT_MISSILE:
        $fleet_array[UNIT_DEF_MISSILE_INTERPLANET] = min(mrc_get_level($user, $planetrow, UNIT_DEF_MISSILE_INTERPLANET), abs(sys_get_param_float('missiles')));
        $unit_group = 'missile';
        break;
}
$options = array('target_structure' => $target_structure = sys_get_param_int('structures'));
$cant_attack = flt_can_attack($planetrow, $target_row, $fleet_array, $target_mission, $options);
if ($cant_attack != ATTACK_ALLOWED) {
    die($lang['fl_attack_error'][$cant_attack]);
}
$FleetDBArray = array();
$db_changeset = array();
foreach ($fleet_array as $unit_id => $unit_count) {
    $FleetDBArray[] = "{$unit_id},{$unit_count}";
    // $db_changeset[]  = "`{$unit_db_name}` = `{$unit_db_name}` - {$unit_count}";
    $db_changeset['unit'][] = sn_db_unit_changeset_prepare($unit_id, -$unit_count, $user, $planetrow);
}
$FleetDBArray = implode(';', $FleetDBArray);
// $db_changeset  = implode(',', $db_changeset);
$fleet_ship_count = array_sum($fleet_array);
if ($target_mission == MT_MISSILE) {
    $distance = abs($target_coord['system'] - $planetrow['system']);