Remove passings duplicates
This commit is contained in:
		
							parent
							
								
									d9869ce2f7
								
							
						
					
					
						commit
						0b8963af99
					
				|  | @ -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(() => { | |||
|                 <td>${stopToHTML(stopId)}</td> | ||||
|                 <td>${displayTime(new Date(time))}</td> | ||||
|             </tr> | ||||
|         `).join('\n');
 | ||||
|         `).join("\n");
 | ||||
| 
 | ||||
|         html += ` | ||||
|             <dl> | ||||
|  | @ -52,12 +50,12 @@ setInterval(() => { | |||
|                 <dd>${stopToHTML(course.finalStop)}</dd> | ||||
| 
 | ||||
|                 <dt>État</dt> | ||||
|                 <dd>${course.state === 'moving' | ||||
|                         ? `Entre ${stopToHTML(course.departureStop)} | ||||
|                            et ${stopToHTML(course.arrivalStop)}` | ||||
|                         : `À l’arrêt ${stopToHTML(course.currentStop)}`}</dd> | ||||
|                 <dd>${course.state === "moving" | ||||
|         ? `Entre ${stopToHTML(course.departureStop)} | ||||
|             et ${stopToHTML(course.arrivalStop)}` | ||||
|         : `À l’arrêt ${stopToHTML(course.currentStop)}`}</dd> | ||||
| 
 | ||||
|                 ${course.state === 'moving' ? ` | ||||
|                 ${course.state === "moving" ? ` | ||||
|                     <dt>Arrivée dans</dt> | ||||
|                     <dd>${timeToHTML(course.arrivalTime)} s</dd> | ||||
| 
 | ||||
|  |  | |||
|  | @ -89,7 +89,7 @@ a “ref” tag`); | |||
|                         ], { | ||||
|                             name: stop.tags.name, | ||||
|                             routes: [[lineRef, routeRef]], | ||||
|                             successors: [], | ||||
|                             successors: [] | ||||
|                         }); | ||||
|                     } else { | ||||
|                         stops[stop.tags.ref].properties.routes.push([ | ||||
|  |  | |||
|  | @ -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 | ||||
|                 ) | ||||
|             ); | ||||
|         } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue