Skip to content

Commit

Permalink
feat(boarding): mix emptying and loading seats
Browse files Browse the repository at this point in the history
  • Loading branch information
2hwk committed Dec 31, 2022
1 parent 9ef62eb commit 842e447
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-undef */
// TODO: Deprecate, move boarding backend to WASM
function airplaneCanBoard() {
const busDC2 = SimVar.GetSimVarValue("L:A32NX_ELEC_DC_2_BUS_IS_POWERED", "Bool");
const busDCHot1 = SimVar.GetSimVarValue("L:A32NX_ELEC_DC_HOT_1_BUS_IS_POWERED", "Bool");
Expand Down Expand Up @@ -29,41 +30,33 @@ class A32NX_Boarding {
}

async init() {
/*
const inDeveloperState = SimVar.GetSimVarValue("L:A32NX_DEVELOPER_STATE", "Bool");
if (!inDeveloperState) {
// Set default pax (0)
await this.setPax(0);
this.loadPaxPayload();
this.loadCargoZero();
this.loadCargoPayload();
}
*/
this.loadPaxPayload();
this.loadCargoZero();
this.loadCargoPayload();
}

// Shuffle passengers within same station
async shufflePax(station) {
station.activeFlags.setFlags(station.desiredFlags.toNumber());
await SimVar.SetSimVarValue(`L:${station.simVar}`, "string", station.desiredFlags.toString());
}

async fillPaxStation(station, paxToFill) {
const pax = Math.min(paxToFill, station.seats);
const paxDiff = pax - station.pax;
const paxDelta = Math.abs(pax - station.pax);

// bitwise SeatFlags implementation
if (paxDiff >= 0) {
console.log(`paxDiff positive, filling ${paxToFill} pax`);
// TODO FIXME: Fill from desired Flags
const toFill = station.desiredFlags.getFilledSeatIds();
console.log('toFill', toFill.toString());
station.activeFlags.fillSeats(paxDelta, toFill);
console.log('active flags', station.activeFlags.toString());
async fillPaxStation(station, paxTarget) {
const paxDiff = Math.min(paxTarget, station.seats) - station.activeFlags.getTotalFilledSeats();

if (paxDiff > 0) {
const fillChoices = station.desiredFlags.getFilledSeatIds()
.filter(seatIndex => !station.activeFlags.getFilledSeatIds().includes(seatIndex))
station.activeFlags.fillSeats(Math.abs(paxDiff), fillChoices);
} else if (paxDiff < 0) {
const emptyChoices = station.desiredFlags.getEmptySeatIds()
.filter(seatIndex => !station.activeFlags.getEmptySeatIds().includes(seatIndex))
station.activeFlags.emptySeats(Math.abs(paxDiff), emptyChoices);
} else {
console.log('paxDiff negative, emptying');
station.activeFlags.emptyFilledSeats(paxDelta);
this.shufflePax(station);
}
console.log(`setting L:${station.bitFlags} as ${station.activeFlags.toString()}`);
await SimVar.SetSimVarValue(`L:${station.bitFlags}`, "string", station.activeFlags.toString());
station.pax = pax;

// TODO REFACTOR: Change this
// await SimVar.SetSimVarValue(`L:${station.simVar}`, "Number", parseInt(pax));
await SimVar.SetSimVarValue(`L:${station.simVar}`, "string", station.activeFlags.toString());
}

async fillCargoStation(station, loadToFill) {
Expand All @@ -76,10 +69,9 @@ class A32NX_Boarding {

async function fillStation(station, percent, paxToFill) {
const pax = Math.min(Math.trunc(percent * paxToFill), station.seats);
station.pax = pax;
// SeatFlags implementation
station.activeFlags.setFlags(pax);
await SimVar.SetSimVarValue(`L:${station.bitFlags}_DESIRED`, "string", station.activeFlags.toString());
await SimVar.SetSimVarValue(`L:${station.simVar}_DESIRED`, "string", station.activeFlags.toString());
// await SimVar.SetSimVarValue(`L:${station.simVar}_DESIRED`, "Number", parseInt(pax));
paxRemaining -= pax;
}
Expand All @@ -94,7 +86,7 @@ class A32NX_Boarding {
loadPaxPayload() {
const PAX_WEIGHT = SimVar.GetSimVarValue("L:A32NX_WB_PER_PAX_WEIGHT", "Number");
return Promise.all(Object.values(this.paxStations).map(paxStation => {
return SimVar.SetSimVarValue(`PAYLOAD STATION WEIGHT:${paxStation.stationIndex}`, getUserUnit(), paxStation.pax * PAX_WEIGHT);
return SimVar.SetSimVarValue(`PAYLOAD STATION WEIGHT:${paxStation.stationIndex}`, getUserUnit(), paxStation.activeFlags.getTotalFilledSeats() * PAX_WEIGHT);
}));
}
loadCargoPayload() {
Expand Down Expand Up @@ -125,26 +117,20 @@ class A32NX_Boarding {
return;
}

// SeatFlags Implementation
let currentPax = 0;
let paxTarget = 0;
let isAllPaxStationFilled = true;
Object.values(this.paxStations).map((station) => {
station.activeFlags.setFlags(SimVar.GetSimVarValue(`L:${station.bitFlags}`, 'Number'));
station.activeFlags.setFlags(SimVar.GetSimVarValue(`L:${station.simVar}`, 'Number'));
const stationCurrentPax = station.activeFlags.getTotalFilledSeats();
currentPax += stationCurrentPax;

station.desiredFlags.setFlags(SimVar.GetSimVarValue(`L:${station.bitFlags}_DESIRED`, 'Number'));
station.desiredFlags.setFlags(SimVar.GetSimVarValue(`L:${station.simVar}_DESIRED`, 'Number'));
const stationCurrentPaxTarget = station.desiredFlags.getTotalFilledSeats();
paxTarget += stationCurrentPaxTarget;

if (stationCurrentPax !== stationCurrentPaxTarget) {
isAllPaxStationFilled = false;
/*
console.log('not pax filled');
console.log('station current passengers', station.name, stationCurrentPax);
console.log('station target passengers', station.name, 'target', stationCurrentPaxTarget);
*/
}
});

Expand Down Expand Up @@ -185,12 +171,6 @@ class A32NX_Boarding {
// Finish boarding
this.boardingState = "finished";

// SeatFlags: Balance pax flags
/*
station.desiredFlags.setFlags(station.activeFlags.toNumber());
await SimVar.SetSimVarValue(`L:${station.bitFlags}_DESIRED`, "string", station.activeFlags.toString());
*/

await SimVar.SetSimVarValue("L:A32NX_BOARDING_STARTED_BY_USR", "Bool", false);

} else if ((currentPax < paxTarget) || (currentLoad < loadTarget)) {
Expand All @@ -202,10 +182,7 @@ class A32NX_Boarding {
if (boardingRate == 'INSTANT') {
// Instant
for (const paxStation of Object.values(this.paxStations)) {
// const stationCurrentPaxTarget = SimVar.GetSimVarValue(`L:${paxStation.simVar}_DESIRED`, "Number");

// TODO: Replace with flags
paxStation.desiredFlags.setFlags(SimVar.GetSimVarValue(`L:${paxStation.bitFlags}_DESIRED`, "Number"));
paxStation.desiredFlags.setFlags(SimVar.GetSimVarValue(`L:${paxStation.simVar}_DESIRED`, "Number"));
const stationCurrentPaxTarget = paxStation.desiredFlags.getTotalFilledSeats();

await this.fillPaxStation(paxStation, stationCurrentPaxTarget);
Expand Down Expand Up @@ -234,15 +211,10 @@ class A32NX_Boarding {

// Stations logic:
for (const paxStation of Object.values(this.paxStations).reverse()) {

// TODO REFACTOR: Replace with flags
// const stationCurrentPax = SimVar.GetSimVarValue(`L:${paxStation.simVar}`, "Number");
// const stationCurrentPaxTarget = SimVar.GetSimVarValue(`L:${paxStation.simVar}_DESIRED`, "Number");

paxStation.activeFlags.setFlags(SimVar.GetSimVarValue(`L:${paxStation.bitFlags}`, "Number"));
paxStation.activeFlags.setFlags(SimVar.GetSimVarValue(`L:${paxStation.simVar}`, "Number"));
const stationCurrentPax = paxStation.activeFlags.getTotalFilledSeats();

paxStation.desiredFlags.setFlags(SimVar.GetSimVarValue(`L:${paxStation.bitFlags}_DESIRED`, "Number"));
paxStation.desiredFlags.setFlags(SimVar.GetSimVarValue(`L:${paxStation.simVar}_DESIRED`, "Number"));
const stationCurrentPaxTarget = paxStation.desiredFlags.getTotalFilledSeats();

if (stationCurrentPax < stationCurrentPaxTarget) {
Expand All @@ -252,6 +224,8 @@ class A32NX_Boarding {
this.fillPaxStation(paxStation, stationCurrentPax - 1);
break;
} else {
// TODO: Handle shuffling passengers with time delay more gracefully
this.shufflePax(paxStation);
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* eslint-disable no-undef */
class A32NX_PayloadConstructor {
constructor() {
this.paxStations = {
rows1_6: {
name: 'ROWS [1-6]',
seats: 36,
weight: Math.round(NXUnits.kgToUser(3024)),
pax: 0,
paxTarget: 0,
stationIndex: 0 + 1,
position: 21.98,
seatsRange: [1, 36],
simVar: "A32NX_PAX_TOTAL_ROWS_1_6",
bitFlags: "A32NX_PAX_FLAGS_A",
simVar: "A32NX_PAX_FLAGS_A",
activeFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_A', 'Number'), 36),
desiredFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_A_DESIRED', 'Number'), 36),

Expand All @@ -20,41 +17,29 @@ class A32NX_PayloadConstructor {
name: 'ROWS [7-13]',
seats: 42,
weight: Math.round(NXUnits.kgToUser(3530)),
pax: 0,
paxTarget: 0,
stationIndex: 1 + 1,
position: 2.86,
seatsRange: [37, 78],
simVar: "A32NX_PAX_TOTAL_ROWS_7_13",
bitFlags: "A32NX_PAX_FLAGS_B",
simVar: "A32NX_PAX_FLAGS_B",
activeFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_B', 'Number'), 42),
desiredFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_B_DESIRED', 'Number'), 42),
},
rows14_21: {
name: 'ROWS [14-21]',
seats: 48,
weight: Math.round(NXUnits.kgToUser(4032)),
pax: 0,
paxTarget: 0,
stationIndex: 2 + 1,
position: -15.34,
seatsRange: [79, 126],
simVar: "A32NX_PAX_TOTAL_ROWS_14_21",
bitFlags: "A32NX_PAX_FLAGS_C",
simVar: "A32NX_PAX_FLAGS_C",
activeFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_C', 'Number'), 48),
desiredFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_C_DESIRED', 'Number'), 48),
},
rows22_29: {
name: 'ROWS [22-29]',
seats: 48,
weight: Math.round(NXUnits.kgToUser(4032)),
pax: 0,
paxTarget: 0,
stationIndex: 3 + 1,
position: -32.81,
seatsRange: [127, 174],
simVar: "A32NX_PAX_TOTAL_ROWS_22_29",
bitFlags: "A32NX_PAX_FLAGS_D",
simVar: "A32NX_PAX_FLAGS_D",
activeFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_D', 'Number'), 48),
desiredFlags: new SeatFlags(SimVar.GetSimVarValue('L:A32NX_PAX_FLAGS_D_DESIRED', 'Number'), 48),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ class SeatFlags extends BitFlags {
this.toggleBitIndex(seatId);
}

fillEmptySeats(numFill) {
this.fillSeats(numFill, this.getEmptySeatIds());
}

fillSeats(numFill, choices) {
for (let i = 0; i < numFill; i++) {
if (choices.length > 0) {
Expand All @@ -130,8 +126,11 @@ class SeatFlags extends BitFlags {
}
}

emptyFilledSeats(numEmpty) {
const choices = this.getFilledSeatIds();
fillEmptySeats(numFill) {
this.fillSeats(numFill, this.getEmptySeatIds());
}

emptySeats(numEmpty, choices) {
for (let i = 0; i < numEmpty; i++) {
if (choices.length > 0) {
const chosen = ~~(Math.random() * choices.length);
Expand All @@ -141,6 +140,10 @@ class SeatFlags extends BitFlags {
}
}

emptyFilledSeats(numEmpty) {
this.emptySeats(numEmpty, this.getFilledSeatIds());
}

getTotalFilledSeats() {
return this.getTotalBits();
}
Expand Down
15 changes: 9 additions & 6 deletions src/shared/src/bitFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ export class SeatFlags extends BitFlags {
this.toggleBitIndex(seatId);
}

fillEmptySeats(numFill: number) {
this.fillSeats(numFill, this.getEmptySeatIds());
}

fillSeats(numFill: number, choices: number[]) {
for (let i = 0; i < numFill; i++) {
if (choices.length > 0) {
Expand All @@ -132,8 +128,11 @@ export class SeatFlags extends BitFlags {
}
}

emptyFilledSeats(numEmpty: number) {
const choices = this.getFilledSeatIds();
fillEmptySeats(numFill: number) {
this.fillSeats(numFill, this.getEmptySeatIds());
}

emptySeats(numEmpty: number, choices: number[]) {
for (let i = 0; i < numEmpty; i++) {
if (choices.length > 0) {
const chosen = ~~(Math.random() * choices.length);
Expand All @@ -143,6 +142,10 @@ export class SeatFlags extends BitFlags {
}
}

emptyFilledSeats(numEmpty: number) {
this.emptySeats(numEmpty, this.getFilledSeatIds());
}

getTotalFilledSeats(): number {
return this.getTotalBits();
}
Expand Down

0 comments on commit 842e447

Please sign in to comment.