diff --git a/src/front/index.js b/src/front/index.js index dd1cd03..2ca7618 100644 --- a/src/front/index.js +++ b/src/front/index.js @@ -7,8 +7,6 @@ const map = require("./map/index.js"); // Run courses simulation const coursesSimulation = simulation.start(); -global.courses = coursesSimulation.courses; - const informations = document.querySelector("#informations"); let courseId = null; @@ -16,7 +14,7 @@ const displayTime = date => [ date.getHours(), date.getMinutes(), date.getSeconds() -].map(number => number.toString().padStart(2, '0')).join(':'); +].map(number => number.toString().padStart(2, "0")).join(":"); setInterval(() => { let html = ` @@ -38,7 +36,7 @@ setInterval(() => { ${stopToHTML(stopId)} ${displayTime(new Date(time))} - `).join('\n'); + `).join("\n"); html += `
@@ -52,12 +50,12 @@ setInterval(() => {
${stopToHTML(course.finalStop)}
État
-
${course.state === 'moving' - ? `Entre ${stopToHTML(course.departureStop)} - et ${stopToHTML(course.arrivalStop)}` - : `À l’arrêt ${stopToHTML(course.currentStop)}`}
+
${course.state === "moving" + ? `Entre ${stopToHTML(course.departureStop)} + et ${stopToHTML(course.arrivalStop)}` + : `À l’arrêt ${stopToHTML(course.currentStop)}`}
- ${course.state === 'moving' ? ` + ${course.state === "moving" ? `
Arrivée dans
${timeToHTML(course.arrivalTime)} s
diff --git a/src/tam/network.js b/src/tam/network.js index 62bbd57..cc679ed 100644 --- a/src/tam/network.js +++ b/src/tam/network.js @@ -89,7 +89,7 @@ a “ref” tag`); ], { name: stop.tags.name, routes: [[lineRef, routeRef]], - successors: [], + successors: [] }); } else { stops[stop.tags.ref].properties.routes.push([ diff --git a/src/tam/realtime.js b/src/tam/realtime.js index 4bf2e07..38efc77 100644 --- a/src/tam/realtime.js +++ b/src/tam/realtime.js @@ -53,10 +53,16 @@ const fetch = async() => { id, line, finalStop, - nextPassings: [[stopId, arrivalTime]] + + // Initially accumulate passings in an object + // to prevent duplicates + nextPassings: { [stopId]: arrivalTime } }; - } else { - courses[id].nextPassings.push([stopId, arrivalTime]); + } else if (!(stopId in courses[id].nextPassings) || + courses[id].nextPassings[stopId] < arrivalTime) { + // Only consider passings with an increased passing time + // or for stops not seen before + courses[id].nextPassings[stopId] = arrivalTime; } } @@ -67,7 +73,7 @@ const fetch = async() => { if (!(course.line in network.lines)) { delete courses[courseId]; } else { - for (const [stopId,] of course.nextPassings) { + for (const stopId of Object.keys(course.nextPassings)) { if (!(stopId in network.stops)) { delete courses[courseId]; break; @@ -78,8 +84,10 @@ const fetch = async() => { // Order next passings by increasing passing time for (const courseId of Object.keys(courses)) { - courses[courseId].nextPassings.sort( - ([, time1], [, time2]) => time1 - time2 + courses[courseId].nextPassings = ( + Object.entries(courses[courseId].nextPassings).sort( + ([, time1], [, time2]) => time1 - time2 + ) ); } diff --git a/src/tam/simulation.js b/src/tam/simulation.js index 05cfbf4..a6701bb 100644 --- a/src/tam/simulation.js +++ b/src/tam/simulation.js @@ -5,8 +5,6 @@ const network = require("./network.json"); const server = "http://localhost:4321"; -const stops = Object.keys(network.stops); - const findRoute = (from, to) => { const queue = [[from, []]]; @@ -84,7 +82,7 @@ class Course { } } else if (this.state === "moving") { const index = this.nextPassings.findIndex( - ([stop, ]) => stop === this.arrivalStop + ([stop]) => stop === this.arrivalStop ); if (index === -1 || this.nextPassings[index][1] <= now) { @@ -99,7 +97,7 @@ class Course { // (this.state === 'stopped') // Try moving to the next stop const index = this.nextPassings.findIndex( - ([stop, ]) => stop === this.currentStop + ([stop]) => stop === this.currentStop ); if (index !== -1) { @@ -123,11 +121,11 @@ class Course { let found = false; for ( - let index = 0; - index < this.nextPassings.length; - ++index + let nextIndex = 0; + nextIndex < this.nextPassings.length; + ++nextIndex ) { - const [stop, arrivalTime] = this.nextPassings[index]; + const [stop, arrivalTime] = this.nextPassings[nextIndex]; if (arrivalTime > now) { const route = findRoute(this.currentStop, stop); @@ -173,7 +171,8 @@ class Course { const segment = this.currentSegment; const distance = segment.properties.length - this.traveledDistance; const duration = this.arrivalTime - Date.now(); - this.speed = this.computeSpeed(distance, duration); + + this.speed = Course.computeSpeed(distance, duration); } return true; @@ -247,7 +246,7 @@ ${this.currentStop} to stop ${stop}. Teleporting to ${stop}`); const distance = network.segments[segmentId].properties.length; const duration = arrivalTime - Date.now(); - if (this.computeSpeed(distance, duration) === 0) { + if (Course.computeSpeed(distance, duration) === 0) { // Speed would be too low, better wait for some time return; } @@ -260,7 +259,7 @@ ${this.currentStop} to stop ${stop}. Teleporting to ${stop}`); this.speed = 0; } - computeSpeed(distance, duration) { + static computeSpeed(distance, duration) { if (duration <= 0) { // Late: go to maximum speed return 50 / 3600;