Remove passings duplicates

This commit is contained in:
Mattéo Delabre 2020-07-27 21:21:07 +02:00
parent d9869ce2f7
commit 0b8963af99
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
4 changed files with 32 additions and 27 deletions

View File

@ -7,8 +7,6 @@ const map = require("./map/index.js");
// Run courses simulation // Run courses simulation
const coursesSimulation = simulation.start(); const coursesSimulation = simulation.start();
global.courses = coursesSimulation.courses;
const informations = document.querySelector("#informations"); const informations = document.querySelector("#informations");
let courseId = null; let courseId = null;
@ -16,7 +14,7 @@ const displayTime = date => [
date.getHours(), date.getHours(),
date.getMinutes(), date.getMinutes(),
date.getSeconds() date.getSeconds()
].map(number => number.toString().padStart(2, '0')).join(':'); ].map(number => number.toString().padStart(2, "0")).join(":");
setInterval(() => { setInterval(() => {
let html = ` let html = `
@ -38,7 +36,7 @@ setInterval(() => {
<td>${stopToHTML(stopId)}</td> <td>${stopToHTML(stopId)}</td>
<td>${displayTime(new Date(time))}</td> <td>${displayTime(new Date(time))}</td>
</tr> </tr>
`).join('\n'); `).join("\n");
html += ` html += `
<dl> <dl>
@ -52,12 +50,12 @@ setInterval(() => {
<dd>${stopToHTML(course.finalStop)}</dd> <dd>${stopToHTML(course.finalStop)}</dd>
<dt>État</dt> <dt>État</dt>
<dd>${course.state === 'moving' <dd>${course.state === "moving"
? `Entre ${stopToHTML(course.departureStop)} ? `Entre ${stopToHTML(course.departureStop)}
et ${stopToHTML(course.arrivalStop)}` et ${stopToHTML(course.arrivalStop)}`
: `À larrêt ${stopToHTML(course.currentStop)}`}</dd> : `À larrêt ${stopToHTML(course.currentStop)}`}</dd>
${course.state === 'moving' ? ` ${course.state === "moving" ? `
<dt>Arrivée dans</dt> <dt>Arrivée dans</dt>
<dd>${timeToHTML(course.arrivalTime)} s</dd> <dd>${timeToHTML(course.arrivalTime)} s</dd>

View File

@ -89,7 +89,7 @@ a “ref” tag`);
], { ], {
name: stop.tags.name, name: stop.tags.name,
routes: [[lineRef, routeRef]], routes: [[lineRef, routeRef]],
successors: [], successors: []
}); });
} else { } else {
stops[stop.tags.ref].properties.routes.push([ stops[stop.tags.ref].properties.routes.push([

View File

@ -53,10 +53,16 @@ const fetch = async() => {
id, id,
line, line,
finalStop, finalStop,
nextPassings: [[stopId, arrivalTime]]
// Initially accumulate passings in an object
// to prevent duplicates
nextPassings: { [stopId]: arrivalTime }
}; };
} else { } else if (!(stopId in courses[id].nextPassings) ||
courses[id].nextPassings.push([stopId, arrivalTime]); 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)) { if (!(course.line in network.lines)) {
delete courses[courseId]; delete courses[courseId];
} else { } else {
for (const [stopId,] of course.nextPassings) { for (const stopId of Object.keys(course.nextPassings)) {
if (!(stopId in network.stops)) { if (!(stopId in network.stops)) {
delete courses[courseId]; delete courses[courseId];
break; break;
@ -78,8 +84,10 @@ const fetch = async() => {
// Order next passings by increasing passing time // Order next passings by increasing passing time
for (const courseId of Object.keys(courses)) { for (const courseId of Object.keys(courses)) {
courses[courseId].nextPassings.sort( courses[courseId].nextPassings = (
Object.entries(courses[courseId].nextPassings).sort(
([, time1], [, time2]) => time1 - time2 ([, time1], [, time2]) => time1 - time2
)
); );
} }

View File

@ -5,8 +5,6 @@ const network = require("./network.json");
const server = "http://localhost:4321"; const server = "http://localhost:4321";
const stops = Object.keys(network.stops);
const findRoute = (from, to) => { const findRoute = (from, to) => {
const queue = [[from, []]]; const queue = [[from, []]];
@ -84,7 +82,7 @@ class Course {
} }
} else if (this.state === "moving") { } else if (this.state === "moving") {
const index = this.nextPassings.findIndex( const index = this.nextPassings.findIndex(
([stop, ]) => stop === this.arrivalStop ([stop]) => stop === this.arrivalStop
); );
if (index === -1 || this.nextPassings[index][1] <= now) { if (index === -1 || this.nextPassings[index][1] <= now) {
@ -99,7 +97,7 @@ class Course {
// (this.state === 'stopped') // (this.state === 'stopped')
// Try moving to the next stop // Try moving to the next stop
const index = this.nextPassings.findIndex( const index = this.nextPassings.findIndex(
([stop, ]) => stop === this.currentStop ([stop]) => stop === this.currentStop
); );
if (index !== -1) { if (index !== -1) {
@ -123,11 +121,11 @@ class Course {
let found = false; let found = false;
for ( for (
let index = 0; let nextIndex = 0;
index < this.nextPassings.length; nextIndex < this.nextPassings.length;
++index ++nextIndex
) { ) {
const [stop, arrivalTime] = this.nextPassings[index]; const [stop, arrivalTime] = this.nextPassings[nextIndex];
if (arrivalTime > now) { if (arrivalTime > now) {
const route = findRoute(this.currentStop, stop); const route = findRoute(this.currentStop, stop);
@ -173,7 +171,8 @@ class Course {
const segment = this.currentSegment; const segment = this.currentSegment;
const distance = segment.properties.length - this.traveledDistance; const distance = segment.properties.length - this.traveledDistance;
const duration = this.arrivalTime - Date.now(); const duration = this.arrivalTime - Date.now();
this.speed = this.computeSpeed(distance, duration);
this.speed = Course.computeSpeed(distance, duration);
} }
return true; return true;
@ -247,7 +246,7 @@ ${this.currentStop} to stop ${stop}. Teleporting to ${stop}`);
const distance = network.segments[segmentId].properties.length; const distance = network.segments[segmentId].properties.length;
const duration = arrivalTime - Date.now(); 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 // Speed would be too low, better wait for some time
return; return;
} }
@ -260,7 +259,7 @@ ${this.currentStop} to stop ${stop}. Teleporting to ${stop}`);
this.speed = 0; this.speed = 0;
} }
computeSpeed(distance, duration) { static computeSpeed(distance, duration) {
if (duration <= 0) { if (duration <= 0) {
// Late: go to maximum speed // Late: go to maximum speed
return 50 / 3600; return 50 / 3600;