mirror of
https://github.com/Krafpy/KSP-MGA-Planner.git
synced 2025-12-05 20:40:13 -08:00
Check if initials match exactly a body's name
This commit is contained in:
20
dist/main/solvers/sequence.js
vendored
20
dist/main/solvers/sequence.js
vendored
@@ -23,19 +23,25 @@ export class FlybySequence {
|
|||||||
const initials = initialsList[i];
|
const initials = initialsList[i];
|
||||||
if (initials.length < 2)
|
if (initials.length < 2)
|
||||||
throw new Error("Body sequence initials must contain at least 2 characters.");
|
throw new Error("Body sequence initials must contain at least 2 characters.");
|
||||||
const bodiesWithInitials = [];
|
let candidateBodies = [];
|
||||||
for (const body of system.orbiting) {
|
for (const body of system.orbiting) {
|
||||||
if (body.name.toLowerCase().startsWith(initials.toLowerCase())) {
|
const bodyNameLowercase = body.name.toLowerCase();
|
||||||
bodiesWithInitials.push(body);
|
const initialsLowercase = initials.toLowerCase();
|
||||||
|
if (bodyNameLowercase == initialsLowercase) {
|
||||||
|
candidateBodies = [body];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (bodyNameLowercase.startsWith(initialsLowercase)) {
|
||||||
|
candidateBodies.push(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bodiesWithInitials.length >= 2) {
|
if (candidateBodies.length >= 2) {
|
||||||
const bodyNames = bodiesWithInitials.map(body => body.name);
|
const bodyNames = candidateBodies.map(body => body.name);
|
||||||
throw new Error(`Ambiguous initials \"${initials}\": ${joinStrings(bodyNames, ", ")}.`);
|
throw new Error(`Ambiguous initials \"${initials}\": ${joinStrings(bodyNames, ", ")}.`);
|
||||||
}
|
}
|
||||||
if (bodiesWithInitials.length == 0)
|
if (candidateBodies.length == 0)
|
||||||
throw new Error(`Invalid custom sequence body initials \"${initials}\".`);
|
throw new Error(`Invalid custom sequence body initials \"${initials}\".`);
|
||||||
const body = bodiesWithInitials[0];
|
const body = candidateBodies[0];
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
attractorId = body.attractor.id;
|
attractorId = body.attractor.id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,21 +37,29 @@ export class FlybySequence {
|
|||||||
if (initials.length < 2)
|
if (initials.length < 2)
|
||||||
throw new Error("Body sequence initials must contain at least 2 characters.");
|
throw new Error("Body sequence initials must contain at least 2 characters.");
|
||||||
|
|
||||||
|
let candidateBodies: OrbitingBody[] = [];
|
||||||
// check for ambiguity and validity
|
// check for ambiguity and validity
|
||||||
const bodiesWithInitials: OrbitingBody[] = [];
|
|
||||||
for (const body of system.orbiting) {
|
for (const body of system.orbiting) {
|
||||||
if (body.name.toLowerCase().startsWith(initials.toLowerCase())) {
|
const bodyNameLowercase = body.name.toLowerCase();
|
||||||
bodiesWithInitials.push(body);
|
const initialsLowercase = initials.toLowerCase();
|
||||||
|
// if initials match exactly the body's name, choose that body
|
||||||
|
if (bodyNameLowercase == initialsLowercase) {
|
||||||
|
candidateBodies = [body];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// otherwise keep track of bodies with same initials
|
||||||
|
if (bodyNameLowercase.startsWith(initialsLowercase)) {
|
||||||
|
candidateBodies.push(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bodiesWithInitials.length >= 2) {
|
if (candidateBodies.length >= 2) {
|
||||||
const bodyNames = bodiesWithInitials.map(body => body.name);
|
const bodyNames = candidateBodies.map(body => body.name);
|
||||||
throw new Error(`Ambiguous initials \"${initials}\": ${joinStrings(bodyNames, ", ")}.`);
|
throw new Error(`Ambiguous initials \"${initials}\": ${joinStrings(bodyNames, ", ")}.`);
|
||||||
}
|
}
|
||||||
if (bodiesWithInitials.length == 0)
|
if (candidateBodies.length == 0)
|
||||||
throw new Error(`Invalid custom sequence body initials \"${initials}\".`);
|
throw new Error(`Invalid custom sequence body initials \"${initials}\".`);
|
||||||
|
|
||||||
const body = bodiesWithInitials[0];
|
const body = candidateBodies[0];
|
||||||
// check for same attractor
|
// check for same attractor
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
attractorId = body.attractor.id;
|
attractorId = body.attractor.id;
|
||||||
|
|||||||
Reference in New Issue
Block a user