64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { PaxConfig } from '../configs/pax';
|
|
import { SharedConfig } from '../configs/shared';
|
|
|
|
const getSimBriefFlightPlan = async (simBriefUsername: string) => {
|
|
const flightPlanURL = `https://www.simbrief.com/api/xml.fetcher.php?username=${simBriefUsername}&json=1`;
|
|
let response: Response;
|
|
let success = false;
|
|
try {
|
|
response = await fetch(flightPlanURL);
|
|
success = true;
|
|
} catch (e: any) {
|
|
response = e.response;
|
|
}
|
|
return {
|
|
success,
|
|
data: await response.json(),
|
|
};
|
|
};
|
|
|
|
export const ImportFlightPlan = async (
|
|
username: string,
|
|
config: typeof PaxConfig,
|
|
unit: 'kg' | 'lbs',
|
|
isER: boolean
|
|
) => {
|
|
const flightPlan = await getSimBriefFlightPlan(username);
|
|
if (!flightPlan.success) {
|
|
return {
|
|
type: 'error',
|
|
message: flightPlan.data.status,
|
|
};
|
|
}
|
|
|
|
const data = flightPlan.data;
|
|
|
|
if (!['MD11', 'MD1F'].includes(data.aircraft.icao_code)) {
|
|
return {
|
|
type: 'error',
|
|
message: `Your SimBrief plan is not for a MD-11`,
|
|
};
|
|
}
|
|
|
|
let convFactor = 1;
|
|
if (data.params.units === 'kgs' && unit === 'lbs') convFactor = 2.20462262185;
|
|
if (data.params.units === 'lbs' && unit === 'kg') convFactor = 1 / 2.20462262185;
|
|
|
|
return {
|
|
type: 'data',
|
|
message: {
|
|
plannedZFW: Math.min(config.maxZWF[unit], Math.round(data.weights.est_zfw * convFactor)),
|
|
plannedGW: Math.min(
|
|
isER ? SharedConfig.maxTOW.er[unit] : SharedConfig.maxTOW.norm[unit],
|
|
Math.round(data.weights.est_ramp * convFactor)
|
|
),
|
|
pax: data.weights.pax_count_actual,
|
|
cargo: Math.round(data.weights.freight_added * convFactor),
|
|
fuel: Math.min(
|
|
isER ? SharedConfig.maxFuel.er[unit] : SharedConfig.maxFuel.norm[unit],
|
|
Math.round(data.fuel.plan_ramp * convFactor)
|
|
),
|
|
},
|
|
};
|
|
};
|