Refonte du code de simulation
This commit is contained in:
parent
7a418f8ada
commit
6d76d874a1
|
@ -4,8 +4,8 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"back": "node src/back",
|
"back": "node src/back",
|
||||||
"front:dev": "vite",
|
"front:dev": "vite src/front",
|
||||||
"front:prod": "vite build",
|
"front:prod": "vite build src/front",
|
||||||
"lint": "eslint ."
|
"lint": "eslint ."
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -33,6 +33,6 @@
|
||||||
<body>
|
<body>
|
||||||
<aside id="panel"></aside>
|
<aside id="panel"></aside>
|
||||||
<div id="map"></div>
|
<div id="map"></div>
|
||||||
<script type="module" src="index.js"></script>
|
<script type="module" src="/index.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import network from "../tam/network.json";
|
import network from "../tam/network.json";
|
||||||
import * as simulation from "../tam/simulation";
|
import * as simulation from "../tam/simulation.js";
|
||||||
import * as map from "./map/index";
|
import * as map from "./map/index.js";
|
||||||
|
|
||||||
// Run courses simulation
|
// Run courses simulation
|
||||||
const coursesSimulation = simulation.start();
|
const coursesSimulation = simulation.start();
|
||||||
|
@ -15,6 +15,18 @@ const displayTime = date => [
|
||||||
date.getSeconds()
|
date.getSeconds()
|
||||||
].map(number => number.toString().padStart(2, "0")).join(":");
|
].map(number => number.toString().padStart(2, "0")).join(":");
|
||||||
|
|
||||||
|
const timeToHTML = time => {
|
||||||
|
const delta = Math.ceil((time - Date.now()) / 1000);
|
||||||
|
|
||||||
|
if (delta <= 0) {
|
||||||
|
return `Imminent`;
|
||||||
|
} else if (delta < 60) {
|
||||||
|
return `${delta} s`;
|
||||||
|
} else {
|
||||||
|
return `${Math.floor(delta / 60)} min ${delta % 60} s`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
let html = `
|
let html = `
|
||||||
<dl>
|
<dl>
|
||||||
|
@ -26,8 +38,6 @@ setInterval(() => {
|
||||||
if (courseId !== null && courseId in coursesSimulation.courses) {
|
if (courseId !== null && courseId in coursesSimulation.courses) {
|
||||||
const course = coursesSimulation.courses[courseId];
|
const course = coursesSimulation.courses[courseId];
|
||||||
|
|
||||||
const timeToHTML = time => Math.ceil((time - Date.now()) / 1000);
|
|
||||||
|
|
||||||
const stopToHTML = stopId => stopId in network.stops ?
|
const stopToHTML = stopId => stopId in network.stops ?
|
||||||
network.stops[stopId].properties.name :
|
network.stops[stopId].properties.name :
|
||||||
'<em>Arrêt inconnu</em>';
|
'<em>Arrêt inconnu</em>';
|
||||||
|
@ -39,6 +49,17 @@ setInterval(() => {
|
||||||
</tr>
|
</tr>
|
||||||
`).join("\n");
|
`).join("\n");
|
||||||
|
|
||||||
|
const state = (
|
||||||
|
course.traveledDistance === 0 && course.speed === 0
|
||||||
|
? "stopped" : "moving"
|
||||||
|
);
|
||||||
|
|
||||||
|
let prevPassings = course.prevPassings;
|
||||||
|
|
||||||
|
if (state === "moving") {
|
||||||
|
prevPassings = prevPassings.concat([[course.departureStop, course.departureTime]]);
|
||||||
|
}
|
||||||
|
|
||||||
html += `
|
html += `
|
||||||
<dl>
|
<dl>
|
||||||
<dt>ID</dt>
|
<dt>ID</dt>
|
||||||
|
@ -51,14 +72,14 @@ setInterval(() => {
|
||||||
<dd>${stopToHTML(course.finalStop)}</dd>
|
<dd>${stopToHTML(course.finalStop)}</dd>
|
||||||
|
|
||||||
<dt>État</dt>
|
<dt>État</dt>
|
||||||
<dd>${course.state === "moving"
|
<dd>${state === "moving"
|
||||||
? `Entre ${stopToHTML(course.departureStop)}
|
? `Entre ${stopToHTML(course.departureStop)}
|
||||||
et ${stopToHTML(course.arrivalStop)}`
|
et ${stopToHTML(course.arrivalStop)}`
|
||||||
: `À l’arrêt ${stopToHTML(course.currentStop)}`}</dd>
|
: `À l’arrêt ${stopToHTML(course.departureStop)}`}</dd>
|
||||||
|
|
||||||
${course.state === "moving" ? `
|
${state === "moving" ? `
|
||||||
<dt>Arrivée dans</dt>
|
<dt>Arrivée dans</dt>
|
||||||
<dd>${timeToHTML(course.arrivalTime)} s</dd>
|
<dd>${timeToHTML(course.arrivalTime - 10000)}</dd>
|
||||||
|
|
||||||
<dt>Distance parcourue</dt>
|
<dt>Distance parcourue</dt>
|
||||||
<dd>${Math.ceil(course.traveledDistance)} m</dd>
|
<dd>${Math.ceil(course.traveledDistance)} m</dd>
|
||||||
|
@ -67,12 +88,12 @@ setInterval(() => {
|
||||||
<dd>${Math.ceil(course.speed * 3600)} km/h</dd>
|
<dd>${Math.ceil(course.speed * 3600)} km/h</dd>
|
||||||
` : `
|
` : `
|
||||||
<dt>Départ dans</dt>
|
<dt>Départ dans</dt>
|
||||||
<dd>${timeToHTML(course.departureTime)} s</dd>
|
<dd>${timeToHTML(course.departureTime)}</dd>
|
||||||
`}
|
`}
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<h2>Arrêts précédents</h2>
|
<h2>Arrêts précédents</h2>
|
||||||
<table>${passingsToHTML(course.prevPassings)}</table>
|
<table>${passingsToHTML(prevPassings)}</table>
|
||||||
|
|
||||||
<h2>Arrêts suivants</h2>
|
<h2>Arrêts suivants</h2>
|
||||||
<table>${passingsToHTML(course.nextPassings)}</table>
|
<table>${passingsToHTML(course.nextPassings)}</table>
|
||||||
|
|
|
@ -1,204 +1,250 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import turfAlong from "@turf/along";
|
import turfAlong from "@turf/along";
|
||||||
|
import turfLength from "@turf/length";
|
||||||
|
import * as turfHelpers from "@turf/helpers";
|
||||||
import * as turfProjection from "@turf/projection";
|
import * as turfProjection from "@turf/projection";
|
||||||
import network from "./network.json";
|
import network from "./network.json";
|
||||||
|
|
||||||
const server = "http://localhost:4321";
|
const server = "http://localhost:4321";
|
||||||
|
|
||||||
const findRoute = (from, to) => {
|
// Number of milliseconds to stay at each stop
|
||||||
const queue = [[from, []]];
|
const stopTime = 10000;
|
||||||
|
|
||||||
while (queue.length) {
|
// Step used to compute the vehicle angle in meters
|
||||||
const [head, path] = queue.shift();
|
const angleStep = 10;
|
||||||
|
|
||||||
for (const successor of network.stops[head].properties.successors) {
|
// Maximum speed of a vehicle
|
||||||
if (successor === to) {
|
const maxSpeed = 60 / 3600;
|
||||||
return path.concat([head, successor]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!path.includes(successor)) {
|
// Minimum speed of a vehicle
|
||||||
queue.push([successor, path.concat([head])]);
|
const minSpeed = 10 / 3600;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
// Normal speed of a vehicle
|
||||||
};
|
const normSpeed = (2 * maxSpeed + minSpeed) / 3;
|
||||||
|
|
||||||
|
/** Simulate the evolution of a vehicle course in the network. */
|
||||||
class Course {
|
class Course {
|
||||||
constructor(data) {
|
constructor(id) {
|
||||||
this.id = data.id;
|
// Unique identifier of this course
|
||||||
this.prevPassings = [];
|
this.id = id;
|
||||||
this.nextPassings = [];
|
|
||||||
this.state = null;
|
|
||||||
|
|
||||||
// Attributes for the `stopped` state
|
// Line on which this vehicle operates
|
||||||
this.currentStop = null;
|
this.line = null;
|
||||||
|
|
||||||
|
// Line direction of this course
|
||||||
|
this.direction = null;
|
||||||
|
|
||||||
|
// Stop to which this course is headed
|
||||||
|
this.finalStop = null;
|
||||||
|
|
||||||
|
// Previous stops that this course left (with timestamps)
|
||||||
|
this.prevPassings = [];
|
||||||
|
|
||||||
|
// Next stops that this course will leave (with timestamps)
|
||||||
|
this.nextPassings = [];
|
||||||
|
|
||||||
|
// Stop that this course just left or will leave
|
||||||
|
this.departureStop = null;
|
||||||
|
|
||||||
|
// Time at which the last stop was left or will be left
|
||||||
this.departureTime = 0;
|
this.departureTime = 0;
|
||||||
|
|
||||||
// Attributes for the `moving` state
|
// Next stop that this course will reach
|
||||||
this.departureStop = null;
|
// (if equal to departureStop, the course has reached its last stop)
|
||||||
this.arrivalStop = null;
|
this.arrivalStop = null;
|
||||||
|
|
||||||
|
// Time at which the next stop will be left
|
||||||
this.arrivalTime = 0;
|
this.arrivalTime = 0;
|
||||||
|
|
||||||
|
// Segment of points between the current departure and arrival
|
||||||
|
this.segment = null;
|
||||||
|
|
||||||
|
// Number of meters travelled between the two stops
|
||||||
this.traveledDistance = 0;
|
this.traveledDistance = 0;
|
||||||
|
|
||||||
|
// Current vehicle speed in meters per millisecond
|
||||||
this.speed = 0;
|
this.speed = 0;
|
||||||
|
|
||||||
|
// Current vehicle latitude and longitude
|
||||||
this.position = [0, 0];
|
this.position = [0, 0];
|
||||||
|
|
||||||
|
// Current vehicle bearing
|
||||||
this.angle = 0;
|
this.angle = 0;
|
||||||
|
|
||||||
this.history = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get currentSegment() {
|
/** Retrieve information about the current segment used by the vehicle. */
|
||||||
if (this.state !== "moving") {
|
updateSegment() {
|
||||||
return null;
|
if (this.departureStop === null || this.arrivalStop === null) {
|
||||||
|
this.segment = null;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return network.segments[`${this.departureStop}-${this.arrivalStop}`];
|
const name = `${this.departureStop}-${this.arrivalStop}`;
|
||||||
|
|
||||||
|
if (name in network.segments) {
|
||||||
|
this.segment = network.segments[name];
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateData(data) {
|
if (!(this.departureStop in network.stops)) {
|
||||||
|
console.warn(`Unknown stop: ${this.departureStop}`);
|
||||||
|
this.segment = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(this.arrivalStop in network.stops)) {
|
||||||
|
console.warn(`Unknown stop: ${this.arrivalStop}`);
|
||||||
|
this.segment = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.segment = turfHelpers.lineString([
|
||||||
|
network.stops[this.departureStop].geometry.coordinates,
|
||||||
|
network.stops[this.arrivalStop].geometry.coordinates,
|
||||||
|
]);
|
||||||
|
this.segment.properties.length = turfLength(this.segment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Merge data received from the server. */
|
||||||
|
receiveData(data) {
|
||||||
this.line = data.line;
|
this.line = data.line;
|
||||||
this.direction = data.direction;
|
this.direction = data.direction;
|
||||||
this.finalStop = data.finalStopId;
|
this.finalStop = data.finalStopId;
|
||||||
this.nextPassings = data.passings;
|
|
||||||
|
|
||||||
|
const passings = Object.assign(
|
||||||
|
Object.fromEntries(this.nextPassings),
|
||||||
|
Object.fromEntries(data.passings),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Remove older passings from next passings
|
||||||
|
for (let [stop, time] of this.prevPassings) {
|
||||||
|
delete passings[stop];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update departure time if still announced
|
||||||
|
if (this.departureStop !== null) {
|
||||||
|
if (this.departureStop in passings) {
|
||||||
|
this.departureTime = passings[this.departureStop];
|
||||||
|
delete passings[this.departureStop];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update arrival time
|
||||||
|
if (this.arrivalStop !== null) {
|
||||||
|
if (this.arrivalStop in passings) {
|
||||||
|
// Use announced time if available
|
||||||
|
this.arrivalTime = passings[this.arrivalStop];
|
||||||
|
delete passings[this.arrivalStop];
|
||||||
|
} else {
|
||||||
|
// Otherwise, arrive using a normal speed from current position
|
||||||
|
const segment = this.segment;
|
||||||
|
const distance = segment.properties.length - this.traveledDistance;
|
||||||
|
const time = Math.floor(distance / normSpeed);
|
||||||
|
this.arrivalTime = Date.now() + time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.nextPassings = Object.entries(passings).sort(
|
||||||
|
([, time1], [, time2]) => time1 - time2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Update the vehicle state. */
|
||||||
|
update() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
if (this.state === null) {
|
// When initializing, use the first available passing as start
|
||||||
// Initialize the course on the first available segment
|
if (this.departureStop === null) {
|
||||||
const index = this.nextPassings.findIndex(
|
if (this.nextPassings.length > 0) {
|
||||||
([, time]) => time >= now
|
const [stopId, time] = this.nextPassings.shift();
|
||||||
);
|
this.departureStop = stopId;
|
||||||
|
this.departureTime = time;
|
||||||
if (index === -1) {
|
this.updateSegment();
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index === 0) {
|
|
||||||
this.arriveToStop(this.nextPassings[index][0]);
|
|
||||||
} else {
|
|
||||||
this.arriveToStop(this.nextPassings[index - 1][0]);
|
|
||||||
this.moveToStop(...this.nextPassings[index]);
|
|
||||||
}
|
|
||||||
} else if (this.state === "moving") {
|
|
||||||
const index = this.nextPassings.findIndex(
|
|
||||||
([stop]) => stop === this.arrivalStop
|
|
||||||
);
|
|
||||||
|
|
||||||
if (index === -1 || this.nextPassings[index][1] <= now) {
|
|
||||||
// Next stop is not announced or in the past,
|
|
||||||
// move towards it as fast as possible
|
|
||||||
this.arrivalTime = now;
|
|
||||||
} else {
|
|
||||||
// On the right track, update the arrival time
|
|
||||||
this.arrivalTime = this.nextPassings[index][1];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// (this.state === 'stopped')
|
|
||||||
// Try moving to the next stop
|
|
||||||
const index = this.nextPassings.findIndex(
|
|
||||||
([stop]) => stop === this.currentStop
|
|
||||||
);
|
|
||||||
|
|
||||||
if (index !== -1) {
|
|
||||||
if (this.nextPassings[index][1] <= now) {
|
|
||||||
// Current stop is still announced but in the past
|
|
||||||
if (index + 1 < this.nextPassings.length) {
|
|
||||||
// Move to next stop
|
|
||||||
this.moveToStop(...this.nextPassings[index + 1]);
|
|
||||||
} else {
|
|
||||||
// No next stop announced, end of course
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Cannot move yet, departure is in the future
|
|
||||||
this.departureTime = this.nextPassings[index][1];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Current stop is not announced, find the first stop
|
|
||||||
// announced in the future to which is connection is
|
|
||||||
// possible
|
|
||||||
let found = false;
|
|
||||||
|
|
||||||
for (
|
|
||||||
let nextIndex = 0;
|
|
||||||
nextIndex < this.nextPassings.length;
|
|
||||||
++nextIndex
|
|
||||||
) {
|
|
||||||
const [stop, arrivalTime] = this.nextPassings[nextIndex];
|
|
||||||
|
|
||||||
if (arrivalTime > now) {
|
|
||||||
const route = findRoute(this.currentStop, stop);
|
|
||||||
|
|
||||||
if (route !== null) {
|
|
||||||
// Move to the first intermediate stop, guess the
|
|
||||||
// arrival time based on the final arrival time and
|
|
||||||
// the relative distance of the stops
|
|
||||||
const midDistance = network.segments[
|
|
||||||
`${route[0]}-${route[1]}`
|
|
||||||
].properties.length;
|
|
||||||
|
|
||||||
let totalDistance = midDistance;
|
|
||||||
|
|
||||||
for (
|
|
||||||
let midIndex = 1;
|
|
||||||
midIndex + 1 < route.length;
|
|
||||||
++midIndex
|
|
||||||
) {
|
|
||||||
totalDistance += network.segments[
|
|
||||||
`${route[midIndex]}-${route[midIndex + 1]}`
|
|
||||||
].properties.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
const midTime = now + (arrivalTime - now) *
|
|
||||||
midDistance / totalDistance;
|
|
||||||
|
|
||||||
this.moveToStop(route[1], midTime);
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
// …and the second one as the arrival
|
||||||
// No valid next stop available
|
if (this.arrivalStop === null) {
|
||||||
return false;
|
if (this.nextPassings.length > 0) {
|
||||||
}
|
const [stopId, time] = this.nextPassings.shift();
|
||||||
|
this.arrivalStop = stopId;
|
||||||
|
this.arrivalTime = time;
|
||||||
|
this.updateSegment();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.state === "moving") {
|
if (this.segment !== null) {
|
||||||
const segment = this.currentSegment;
|
const segment = this.segment;
|
||||||
const distance = segment.properties.length - this.traveledDistance;
|
const distance = segment.properties.length - this.traveledDistance;
|
||||||
const duration = this.arrivalTime - Date.now();
|
const duration = this.arrivalTime - stopTime - now;
|
||||||
|
|
||||||
|
// Arrive to the next stop
|
||||||
|
if (distance === 0) {
|
||||||
|
this.prevPassings.push([this.departureStop, this.departureTime]);
|
||||||
|
this.departureStop = this.arrivalStop;
|
||||||
|
this.departureTime = this.arrivalTime;
|
||||||
|
|
||||||
|
if (this.nextPassings.length > 0) {
|
||||||
|
const [stopId, time] = this.nextPassings.shift();
|
||||||
|
this.arrivalStop = stopId;
|
||||||
|
this.arrivalTime = time;
|
||||||
|
} else {
|
||||||
|
this.arrivalStop = null;
|
||||||
|
this.arrivalTime = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.traveledDistance = 0;
|
||||||
|
this.updateSegment();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.departureTime > now) {
|
||||||
|
// Wait for departure
|
||||||
|
this.speed = 0;
|
||||||
|
} else {
|
||||||
|
if (this.traveledDistance === 0 && this.speed === 0) {
|
||||||
|
// We’re late, record the actual departure time
|
||||||
|
this.departureTime = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update current speed to arrive on time if possible
|
||||||
this.speed = Course.computeSpeed(distance, duration);
|
this.speed = Course.computeSpeed(distance, duration);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
tick(time) {
|
/** Integrate the current vehicle speed and update distance. */
|
||||||
if (this.state === "moving") {
|
move(time) {
|
||||||
// Integrate current speed in travelled distance
|
if (this.segment === null) {
|
||||||
this.traveledDistance += this.speed * time;
|
|
||||||
const segment = this.currentSegment;
|
|
||||||
|
|
||||||
if (this.traveledDistance >= segment.properties.length) {
|
|
||||||
this.arriveToStop(this.arrivalStop);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.speed > 0) {
|
||||||
|
this.traveledDistance = Math.min(
|
||||||
|
this.traveledDistance + this.speed * time,
|
||||||
|
this.segment.properties.length,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Compute updated position and angle based on a small step
|
// Compute updated position and angle based on a small step
|
||||||
const step = 10; // In meters
|
let positionBehind;
|
||||||
|
let positionInFront;
|
||||||
|
|
||||||
|
if (this.traveledDistance < angleStep / 2) {
|
||||||
|
positionBehind = this.traveledDistance;
|
||||||
|
positionInFront = angleStep;
|
||||||
|
} else {
|
||||||
|
positionBehind = this.traveledDistance - angleStep / 2;
|
||||||
|
positionInFront = this.traveledDistance + angleStep / 2;
|
||||||
|
}
|
||||||
|
|
||||||
const positions = [
|
const positions = [
|
||||||
Math.max(0, this.traveledDistance - step / 2),
|
positionBehind,
|
||||||
this.traveledDistance,
|
this.traveledDistance,
|
||||||
this.traveledDistance + step / 2
|
positionInFront,
|
||||||
].map(distance => turfProjection.toMercator(turfAlong(
|
].map(distance => turfProjection.toMercator(turfAlong(
|
||||||
segment,
|
this.segment,
|
||||||
distance / 1000
|
distance / 1000
|
||||||
)).geometry.coordinates);
|
)).geometry.coordinates);
|
||||||
|
|
||||||
|
@ -209,69 +255,21 @@ class Course {
|
||||||
|
|
||||||
this.position = positions[1];
|
this.position = positions[1];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transition this course to a state where it has arrived to a stop.
|
|
||||||
* @param {string} stop Identifier for the stop to which
|
|
||||||
* the course arrives.
|
|
||||||
* @returns {undefined}
|
|
||||||
*/
|
|
||||||
arriveToStop(stop) {
|
|
||||||
this.state = "stopped";
|
|
||||||
this.currentStop = stop;
|
|
||||||
this.departureTime = Date.now();
|
|
||||||
this.prevPassings.push([stop, Date.now()]);
|
|
||||||
this.position = (
|
|
||||||
turfProjection.toMercator(network.stops[stop])
|
|
||||||
.geometry.coordinates
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transition this course to a state where it is moving to a stop.
|
|
||||||
* @param {string} stop Next stop for this course.
|
|
||||||
* @param {number} arrivalTime Planned arrival time to that stop.
|
|
||||||
* @returns {undefined}
|
|
||||||
*/
|
|
||||||
moveToStop(stop, arrivalTime) {
|
|
||||||
const segmentId = `${this.currentStop}-${stop}`;
|
|
||||||
|
|
||||||
if (!(segmentId in network.segments)) {
|
|
||||||
console.warn(`Course ${this.id} cannot go from stop
|
|
||||||
${this.currentStop} to stop ${stop}. Teleporting to ${stop}`);
|
|
||||||
this.arriveToStop(stop);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const distance = network.segments[segmentId].properties.length;
|
|
||||||
const duration = arrivalTime - Date.now();
|
|
||||||
|
|
||||||
if (Course.computeSpeed(distance, duration) === 0) {
|
|
||||||
// Speed would be too low, better wait for some time
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.state = "moving";
|
|
||||||
this.departureStop = this.currentStop;
|
|
||||||
this.arrivalStop = stop;
|
|
||||||
this.arrivalTime = arrivalTime;
|
|
||||||
this.traveledDistance = 0;
|
|
||||||
this.speed = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static 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 maxSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (distance / duration <= 10 / 3600) {
|
const speed = distance / duration;
|
||||||
|
|
||||||
|
if (speed < minSpeed) {
|
||||||
// Too slow: pause until speed is sufficient
|
// Too slow: pause until speed is sufficient
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return distance / duration;
|
return Math.min(maxSpeed, speed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,29 +279,19 @@ const updateData = async courses => {
|
||||||
// Update or create new courses
|
// Update or create new courses
|
||||||
for (const [id, data] of Object.entries(dataset)) {
|
for (const [id, data] of Object.entries(dataset)) {
|
||||||
if (id in courses) {
|
if (id in courses) {
|
||||||
if (!courses[id].updateData(data)) {
|
courses[id].receiveData(data);
|
||||||
delete courses[id];
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const newCourse = new Course(data);
|
const newCourse = new Course(data.id);
|
||||||
|
newCourse.receiveData(data);
|
||||||
if (newCourse.updateData(data)) {
|
|
||||||
courses[id] = newCourse;
|
courses[id] = newCourse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Remove stale courses
|
|
||||||
for (const id of Object.keys(courses)) {
|
|
||||||
if (!(id in dataset)) {
|
|
||||||
delete courses[id];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const tick = (courses, time) => {
|
const tick = (courses, time) => {
|
||||||
for (const course of Object.values(courses)) {
|
for (const course of Object.values(courses)) {
|
||||||
course.tick(time);
|
course.update();
|
||||||
|
course.move(time);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -326,5 +314,7 @@ export const start = () => {
|
||||||
tick(courses, time);
|
tick(courses, time);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
window.__courses = courses;
|
||||||
|
|
||||||
return { courses, update };
|
return { courses, update };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue