From 28dce7952c2a279109d32397517adefb8c94151f Mon Sep 17 00:00:00 2001 From: Krafpy Date: Wed, 11 Dec 2024 19:03:48 +0100 Subject: [PATCH 1/2] Refactored custom sequence parsing Initials no longer limited to 2 characters, but require enough characters avoid ambiguity between bodies. --- dist/main/editor/editor.js | 9 ++++--- dist/main/solvers/sequence.js | 38 ++++++++++++++++------------- src/main/editor/editor.ts | 8 ++++--- src/main/solvers/sequence.ts | 45 ++++++++++++++++++++++------------- 4 files changed, 62 insertions(+), 38 deletions(-) diff --git a/dist/main/editor/editor.js b/dist/main/editor/editor.js index 6dfab0a..382b29d 100644 --- a/dist/main/editor/editor.js +++ b/dist/main/editor/editor.js @@ -264,10 +264,13 @@ export async function initEditorWithSystem(systems, systemIndex) { systemSelector.disable(); try { let sequence; - if (customSequence.value != "") + if (customSequence.value != "") { sequence = FlybySequence.fromString(customSequence.value, system); - else + console.debug(sequence.seqStringFullNames); + } + else { sequence = sequenceSelector.sequence; + } updateAltitudeRange(depAltitude, sequence.bodies[0]); const slen = sequence.length; updateAltitudeRange(destAltitude, sequence.bodies[slen - 1]); @@ -294,7 +297,7 @@ export async function initEditorWithSystem(systems, systemIndex) { maxDurationSeconds = maxDuration.value * 24 * 3600; } } - console.log(maxDurationSeconds); + console.debug(maxDurationSeconds); resetFoundTrajectory(); const userSettings = { startDate: startDate, diff --git a/dist/main/solvers/sequence.js b/dist/main/solvers/sequence.js index d3fa789..55f88f5 100644 --- a/dist/main/solvers/sequence.js +++ b/dist/main/solvers/sequence.js @@ -16,27 +16,33 @@ export class FlybySequence { } static fromString(str, system) { str = str.trim(); - const initials = str.split('-'); + const initialsList = str.split('-').map(s => s.trim()); const ids = []; - let attractor = 0; - for (let i = 0; i < initials.length; i++) { - let valid = false; + let attractorId = 0; + for (let i = 0; i < initialsList.length; i++) { + const initials = initialsList[i]; + if (initials.length < 2) + throw new Error("Body sequence initials must contain at least 2 characters."); + const bodiesWithInitials = []; for (const body of system.orbiting) { - if (body.name.substring(0, 2) == initials[i]) { - if (i == 0) { - attractor = body.attractor.id; - } - else if (body.attractor.id != attractor) { - throw new Error("All bodies of the sequence must orbit around the same body."); - } - ids.push(body.id); - valid = true; - break; + if (body.name.toLowerCase().startsWith(initials.toLowerCase())) { + bodiesWithInitials.push(body); } } - if (!valid) { - throw new Error("Invalid custom sequence input."); + if (bodiesWithInitials.length >= 2) { + const bodyNames = bodiesWithInitials.map(body => body.name); + throw new Error(`Ambiguous initials \"${initials}\": ${joinStrings(bodyNames, ", ")}.`); } + if (bodiesWithInitials.length == 0) + throw new Error(`Invalid custom sequence body initials \"${initials}\".`); + const body = bodiesWithInitials[0]; + if (i == 0) { + attractorId = body.attractor.id; + } + else if (body.attractor.id != attractorId) { + throw new Error("All bodies of the sequence must orbit around the same body."); + } + ids.push(body.id); } if (ids.length <= 1) { throw new Error("The sequence must contain at least two bodies."); diff --git a/src/main/editor/editor.ts b/src/main/editor/editor.ts index 471a641..a2cc6f0 100644 --- a/src/main/editor/editor.ts +++ b/src/main/editor/editor.ts @@ -343,10 +343,12 @@ export async function initEditorWithSystem(systems: SolarSystemData[], systemInd systemSelector.disable(); try { let sequence: FlybySequence; - if(customSequence.value != "") + if(customSequence.value != "") { sequence = FlybySequence.fromString(customSequence.value, system); - else + console.debug(sequence.seqStringFullNames); + } else { sequence = sequenceSelector.sequence; + } updateAltitudeRange(depAltitude, sequence.bodies[0]); const slen = sequence.length; @@ -378,7 +380,7 @@ export async function initEditorWithSystem(systems: SolarSystemData[], systemInd maxDurationSeconds = maxDuration.value * 24*3600; } } - console.log(maxDurationSeconds); + console.debug(maxDurationSeconds); resetFoundTrajectory(); diff --git a/src/main/solvers/sequence.ts b/src/main/solvers/sequence.ts index 21437d6..5ba6eef 100644 --- a/src/main/solvers/sequence.ts +++ b/src/main/solvers/sequence.ts @@ -25,28 +25,41 @@ export class FlybySequence { static fromString(str: string, system: SolarSystem){ str = str.trim(); - const initials = str.split('-'); + const initialsList = str.split('-').map(s => s.trim()); + const ids: number[] = []; + let attractorId = 0; - let attractor = 0; + for(let i = 0; i < initialsList.length; i++) { + const initials = initialsList[i]; - for(let i = 0; i < initials.length; i++){ - let valid = false; - for(const body of system.orbiting){ - if(body.name.substring(0, 2) == initials[i]){ - if(i == 0) { - attractor = body.attractor.id; - } else if(body.attractor.id != attractor) { - throw new Error("All bodies of the sequence must orbit around the same body."); - } - ids.push(body.id); - valid = true; - break; + // at least 2 characters + if (initials.length < 2) + throw new Error("Body sequence initials must contain at least 2 characters."); + + // check for ambiguity and validity + const bodiesWithInitials: OrbitingBody[] = []; + for (const body of system.orbiting) { + if (body.name.toLowerCase().startsWith(initials.toLowerCase())) { + bodiesWithInitials.push(body); } } - if(!valid){ - throw new Error("Invalid custom sequence input."); + if (bodiesWithInitials.length >= 2) { + const bodyNames = bodiesWithInitials.map(body => body.name); + throw new Error(`Ambiguous initials \"${initials}\": ${joinStrings(bodyNames, ", ")}.`); } + if (bodiesWithInitials.length == 0) + throw new Error(`Invalid custom sequence body initials \"${initials}\".`); + + const body = bodiesWithInitials[0]; + // check for same attractor + if (i == 0) { + attractorId = body.attractor.id; + } else if (body.attractor.id != attractorId) { + throw new Error("All bodies of the sequence must orbit around the same body."); + } + + ids.push(body.id); } if(ids.length <= 1){ From 16ab459ee90c30c8e70b6ac335f8a129ed5d1b1b Mon Sep 17 00:00:00 2001 From: Krafpy Date: Wed, 11 Dec 2024 19:08:42 +0100 Subject: [PATCH 2/2] Updated index.html --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index c879c2c..53a80c4 100644 --- a/index.html +++ b/index.html @@ -359,7 +359,7 @@ The flyby sequence is the sequence of bodies encountered during the mission's flight time. It starts with the departure body and ends with the destination body. All intermediate bodies of the sequence are used for gravity assist.
- Sequences are represented using the first two characters of each body's name. For example, a mission + Sequences are represented using the first characters (at least two) of each body's name. For example, a mission going from Kerbin to Jool, with a first flyby of Eve followed by a flyby of Duna would be written: Ke-Ev-Du-Jo.