Try something to sort locations along tracked trips

This commit is contained in:
2025-07-05 23:48:48 +02:00
parent 6571d053e0
commit edee295fe1
2 changed files with 55 additions and 0 deletions

View File

@@ -15,6 +15,8 @@ export function addJourneyToDraftingBoard(journey) {
}); });
} }
trackedTripsLocationsSorted();
let station_offset = 0; let station_offset = 0;
let trip_offset = 1; let trip_offset = 1;
@@ -129,3 +131,53 @@ export function addJourneyToDraftingBoard(journey) {
trip_offset += 1; trip_offset += 1;
} }
} }
export function getLocationWithLeastInboundTrips(locations, trips) {
function numberOfInboundTrips(location) {
return trips.filter(trip => trip.destination == location).length;
}
let locations_sorted_by_number_of_inbound_trips = locations.toSorted((location_a, location_b) => {
let n_a = numberOfInboundTrips(location_a);
let n_b = numberOfInboundTrips(location_b);
if (n_a == n_b) {
return 0;
} else if (n_a > n_b) {
return 1;
} else {
return -1;
}
});
return locations_sorted_by_number_of_inbound_trips[0];
}
export function sortLocations(sorted_locations, unsorted_locations, trips) {
if (unsorted_locations.length == 0) {
return sorted_locations;
}
let selected_location = getLocationWithLeastInboundTrips(unsorted_locations, trips);
unsorted_locations = unsorted_locations.filter(location => location != selected_location);
trips = trips.filter(trip => trip.destination != selected_location && trip.origin != selected_location);
return sortLocations(sorted_locations.concat([ selected_location ]), unsorted_locations, trips);
}
export function trackedTripsLocationsSorted() {
let tracked_trips = DataStore.tracked_trips.get();
let tracked_locations = Array.from(new Set(tracked_trips.map(trip => trip.origin).concat(tracked_trips.map(trip => trip.destination))));
console.log(tracked_trips);
console.log(tracked_locations);
let sorted_locations = sortLocations([], tracked_locations, tracked_trips);
console.log(sorted_locations);
console.log(sorted_locations.map(item => DataStore.locations.get()[item]?.name));
}

View File

@@ -2,6 +2,7 @@ import * as Api from './api.js';
import { setupPopups } from "./popup.js"; import { setupPopups } from "./popup.js";
import { setupLocationsSearch } from './locations-search.js'; import { setupLocationsSearch } from './locations-search.js';
import { setupJourneysSearch, attachJourneysSearch } from './journeys-search.js'; import { setupJourneysSearch, attachJourneysSearch } from './journeys-search.js';
import { trackedTripsLocationsSorted } from './drafting-board.js';
import { DataStore } from './datastore.js'; import { DataStore } from './datastore.js';
window.Api = Api; window.Api = Api;
@@ -12,3 +13,5 @@ setupLocationsSearch();
setupJourneysSearch(); setupJourneysSearch();
attachJourneysSearch(document.querySelector("#journeys-search-button")); attachJourneysSearch(document.querySelector("#journeys-search-button"));
trackedTripsLocationsSorted();