diff --git a/web/drafting-board.js b/web/drafting-board.js index 3a33f34..19d188a 100644 --- a/web/drafting-board.js +++ b/web/drafting-board.js @@ -15,6 +15,8 @@ export function addJourneyToDraftingBoard(journey) { }); } + trackedTripsLocationsSorted(); + let station_offset = 0; let trip_offset = 1; @@ -129,3 +131,53 @@ export function addJourneyToDraftingBoard(journey) { 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)); + + +} diff --git a/web/traveldrafter.js b/web/traveldrafter.js index c33976b..5759f49 100644 --- a/web/traveldrafter.js +++ b/web/traveldrafter.js @@ -2,6 +2,7 @@ import * as Api from './api.js'; import { setupPopups } from "./popup.js"; import { setupLocationsSearch } from './locations-search.js'; import { setupJourneysSearch, attachJourneysSearch } from './journeys-search.js'; +import { trackedTripsLocationsSorted } from './drafting-board.js'; import { DataStore } from './datastore.js'; window.Api = Api; @@ -12,3 +13,5 @@ setupLocationsSearch(); setupJourneysSearch(); attachJourneysSearch(document.querySelector("#journeys-search-button")); + +trackedTripsLocationsSorted();