From 842e4478abb2df1a1ad63c1b3e50868ebbefec90 Mon Sep 17 00:00:00 2001 From: 2hwk Date: Sat, 31 Dec 2022 15:00:44 +0800 Subject: [PATCH] feat(boarding): mix emptying and loading seats --- .../Pages/A32NX_Core/A32NX_Boarding.js | 90 +++++++------------ .../Pages/A32NX_Core/A32NX_PayloadManager.js | 25 ++---- .../html_ui/Pages/A32NX_Utils/bitFlags.js | 15 ++-- src/shared/src/bitFlags.ts | 15 ++-- 4 files changed, 55 insertions(+), 90 deletions(-) diff --git a/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Boarding.js b/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Boarding.js index 7805006cd30..9dedc140c02 100644 --- a/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Boarding.js +++ b/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_Boarding.js @@ -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"); @@ -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) { @@ -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; } @@ -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() { @@ -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); - */ } }); @@ -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)) { @@ -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); @@ -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) { @@ -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; } } diff --git a/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_PayloadManager.js b/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_PayloadManager.js index b718416567a..2d160dd865f 100644 --- a/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_PayloadManager.js +++ b/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Core/A32NX_PayloadManager.js @@ -1,3 +1,4 @@ +/* eslint-disable no-undef */ class A32NX_PayloadConstructor { constructor() { this.paxStations = { @@ -5,13 +6,9 @@ class A32NX_PayloadConstructor { 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), @@ -20,13 +17,9 @@ 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), }, @@ -34,13 +27,9 @@ class A32NX_PayloadConstructor { 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), }, @@ -48,13 +37,9 @@ class A32NX_PayloadConstructor { 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), }, diff --git a/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/bitFlags.js b/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/bitFlags.js index 5dad8bf1efa..e78210f6094 100644 --- a/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/bitFlags.js +++ b/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/bitFlags.js @@ -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) { @@ -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); @@ -141,6 +140,10 @@ class SeatFlags extends BitFlags { } } + emptyFilledSeats(numEmpty) { + this.emptySeats(numEmpty, this.getFilledSeatIds()); + } + getTotalFilledSeats() { return this.getTotalBits(); } diff --git a/src/shared/src/bitFlags.ts b/src/shared/src/bitFlags.ts index df38872e4c1..798a3be0916 100644 --- a/src/shared/src/bitFlags.ts +++ b/src/shared/src/bitFlags.ts @@ -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) { @@ -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); @@ -143,6 +142,10 @@ export class SeatFlags extends BitFlags { } } + emptyFilledSeats(numEmpty: number) { + this.emptySeats(numEmpty, this.getFilledSeatIds()); + } + getTotalFilledSeats(): number { return this.getTotalBits(); }