2020-07-25 16:05:43 +00:00
|
|
|
|
const axios = require("axios");
|
|
|
|
|
const turfAlong = require("@turf/along").default;
|
|
|
|
|
const turfProjection = require("@turf/projection");
|
|
|
|
|
const network = require("./network.json");
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const server = "http://localhost:4321";
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
class Course {
|
|
|
|
|
constructor(data) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.id = data.id;
|
|
|
|
|
this.passings = {};
|
|
|
|
|
this.state = null;
|
|
|
|
|
|
|
|
|
|
// Attributes for the `stopped` state
|
|
|
|
|
this.currentStop = null;
|
|
|
|
|
|
|
|
|
|
// Attributes for the `moving` state
|
|
|
|
|
this.departureStop = null;
|
|
|
|
|
this.arrivalStop = null;
|
|
|
|
|
this.arrivalTime = 0;
|
|
|
|
|
this.traveledDistance = 0;
|
|
|
|
|
this.speed = 0;
|
|
|
|
|
|
|
|
|
|
this.position = [0, 0];
|
|
|
|
|
this.angle = 0;
|
2020-07-24 22:57:47 +00:00
|
|
|
|
|
|
|
|
|
this.history = [];
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
get currentSegment() {
|
|
|
|
|
if (this.state !== "moving") {
|
|
|
|
|
return null;
|
2020-07-24 17:05:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return network.segments[`${this.departureStop}-${this.arrivalStop}`];
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
updateData(data) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.line = data.line;
|
|
|
|
|
this.finalStop = data.finalStop;
|
|
|
|
|
Object.assign(this.passings, data.nextPassings);
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-24 17:05:43 +00:00
|
|
|
|
const now = Date.now();
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-24 17:05:43 +00:00
|
|
|
|
// Make sure we’re on the right `stopped`/`moving` state
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (this.state === null) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
let previousStop = null;
|
|
|
|
|
let departureTime = 0;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-24 17:05:43 +00:00
|
|
|
|
let nextStop = null;
|
|
|
|
|
let arrivalTime = Infinity;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const [stopId, time] of Object.entries(this.passings)) {
|
|
|
|
|
if (time > now && time < arrivalTime) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
nextStop = stopId;
|
|
|
|
|
arrivalTime = time;
|
|
|
|
|
}
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (time < now && time > departureTime) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
previousStop = stopId;
|
|
|
|
|
departureTime = time;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-17 17:17:06 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (nextStop === null) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (previousStop === null) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
// Teleport to the first known stop
|
|
|
|
|
this.arriveToStop(nextStop);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
2020-07-24 17:05:43 +00:00
|
|
|
|
// Teleport to the first known segment
|
|
|
|
|
this.arriveToStop(previousStop);
|
|
|
|
|
this.moveToStop(nextStop, arrivalTime);
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else if (this.state === "moving") {
|
|
|
|
|
if (this.passings[this.arrivalStop] <= now) {
|
|
|
|
|
// Should already be at the next stop
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.arriveToStop(this.arrivalStop);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
// On the right track, update the arrival time
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.arrivalTime = this.passings[this.arrivalStop];
|
|
|
|
|
}
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
// (this.state === 'stopped')
|
2020-07-24 17:05:43 +00:00
|
|
|
|
// Try moving to the next stop
|
|
|
|
|
let nextStop = null;
|
|
|
|
|
let arrivalTime = Infinity;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const [stopId, time] of Object.entries(this.passings)) {
|
|
|
|
|
if (time > now && time < arrivalTime) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
nextStop = stopId;
|
|
|
|
|
arrivalTime = time;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-24 17:05:43 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (nextStop === null) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
// This course is finished
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (nextStop !== this.currentStop) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.moveToStop(nextStop, arrivalTime);
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-24 17:05:43 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (this.state === "moving") {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.speed = this.computeTheoreticalSpeed();
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
2020-07-24 17:05:43 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
tick(time) {
|
|
|
|
|
if (this.state === "moving") {
|
|
|
|
|
|
2020-07-24 17:05:43 +00:00
|
|
|
|
// Integrate current speed in travelled distance
|
2020-07-25 14:28:51 +00:00
|
|
|
|
this.traveledDistance += this.speed * time;
|
2020-07-24 17:05:43 +00:00
|
|
|
|
const segment = this.currentSegment;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (this.traveledDistance >= segment.properties.length) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.arriveToStop(this.arrivalStop);
|
|
|
|
|
return;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
}
|
2020-07-23 15:29:35 +00:00
|
|
|
|
|
2020-07-25 14:28:51 +00:00
|
|
|
|
// Compute updated position and angle based on a small step
|
|
|
|
|
const step = 10; // In meters
|
2020-07-23 15:29:35 +00:00
|
|
|
|
|
2020-07-25 14:28:51 +00:00
|
|
|
|
const positions = [
|
|
|
|
|
Math.max(0, this.traveledDistance - step / 2),
|
|
|
|
|
this.traveledDistance,
|
|
|
|
|
this.traveledDistance + step / 2
|
|
|
|
|
].map(distance => turfProjection.toMercator(turfAlong(
|
|
|
|
|
segment,
|
|
|
|
|
distance / 1000
|
|
|
|
|
)).geometry.coordinates);
|
2020-07-23 22:18:30 +00:00
|
|
|
|
|
2020-07-25 14:28:51 +00:00
|
|
|
|
this.angle = Math.atan2(
|
|
|
|
|
positions[0][1] - positions[2][1],
|
2020-07-25 16:05:43 +00:00
|
|
|
|
positions[2][0] - positions[0][0]
|
2020-07-25 14:28:51 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.position = positions[1];
|
2020-07-17 17:17:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-24 17:05:43 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Transition this course to a state where it has arrived to a stop.
|
2020-07-25 16:05:43 +00:00
|
|
|
|
* @param {string} stop Identifier for the stop to which
|
|
|
|
|
* the course arrives.
|
|
|
|
|
* @returns {undefined}
|
2020-07-24 17:05:43 +00:00
|
|
|
|
*/
|
2020-07-25 16:05:43 +00:00
|
|
|
|
arriveToStop(stop) {
|
|
|
|
|
this.state = "stopped";
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.currentStop = stop;
|
2020-07-25 14:28:51 +00:00
|
|
|
|
this.position = (
|
|
|
|
|
turfProjection.toMercator(network.stops[stop])
|
|
|
|
|
.geometry.coordinates);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
this.history.push(["arriveToStop", stop]);
|
2020-07-24 17:05:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Transition this course to a state where it is moving to a stop.
|
2020-07-25 16:05:43 +00:00
|
|
|
|
* @param {string} stop Next stop for this course.
|
|
|
|
|
* @param {number} arrivalTime Planned arrival time to that stop.
|
|
|
|
|
* @returns {undefined}
|
2020-07-24 17:05:43 +00:00
|
|
|
|
*/
|
2020-07-25 16:05:43 +00:00
|
|
|
|
moveToStop(stop, arrivalTime) {
|
|
|
|
|
if (!(`${this.currentStop}-${stop}` in network.segments)) {
|
2020-07-25 11:46:54 +00:00
|
|
|
|
console.warn(`Course ${this.id} cannot go from stop
|
2020-07-24 22:57:47 +00:00
|
|
|
|
${this.currentStop} to stop ${stop}. Teleporting to ${stop}`);
|
|
|
|
|
this.arriveToStop(stop);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
this.state = "moving";
|
2020-07-24 17:05:43 +00:00
|
|
|
|
this.departureStop = this.currentStop;
|
|
|
|
|
this.arrivalStop = stop;
|
|
|
|
|
this.arrivalTime = arrivalTime;
|
|
|
|
|
this.traveledDistance = 0;
|
|
|
|
|
this.speed = 0;
|
2020-07-25 16:05:43 +00:00
|
|
|
|
this.history.push(["moveToStop", stop, arrivalTime]);
|
2020-07-24 17:05:43 +00:00
|
|
|
|
|
2020-07-24 22:57:47 +00:00
|
|
|
|
console.info(`Course ${this.id} leaving stop ${this.currentStop} \
|
2020-07-24 17:05:43 +00:00
|
|
|
|
with initial speed ${this.computeTheoreticalSpeed() * 3600} km/h`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Compute the speed that needs to be maintained to arrive on time.
|
2020-07-25 16:05:43 +00:00
|
|
|
|
* @returns {number} Speed in meters per millisecond.
|
2020-07-24 17:05:43 +00:00
|
|
|
|
*/
|
2020-07-25 16:05:43 +00:00
|
|
|
|
computeTheoreticalSpeed() {
|
|
|
|
|
if (this.state !== "moving") {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const segment = this.currentSegment;
|
|
|
|
|
const remainingTime = this.arrivalTime - Date.now();
|
2020-07-25 14:28:51 +00:00
|
|
|
|
const remainingDistance = (
|
|
|
|
|
segment.properties.length - this.traveledDistance
|
|
|
|
|
);
|
2020-07-24 17:05:43 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (remainingDistance <= 0) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (remainingTime <= 0) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
// We’re late, go to maximum speed
|
|
|
|
|
return 50 / 3600; // 50 km/h
|
|
|
|
|
}
|
2020-07-25 16:05:43 +00:00
|
|
|
|
|
|
|
|
|
return remainingDistance / remainingTime;
|
2020-07-24 17:05:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const updateData = async courses => {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
const dataset = (await axios.get(`${server}/courses`)).data;
|
|
|
|
|
|
|
|
|
|
// Update or create new courses
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const [id, data] of Object.entries(dataset)) {
|
|
|
|
|
if (id in courses) {
|
|
|
|
|
if (!courses[id].updateData(data)) {
|
2020-07-24 22:57:47 +00:00
|
|
|
|
console.info(`Course ${id} is finished.`);
|
|
|
|
|
delete courses[id];
|
|
|
|
|
}
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
2020-07-24 22:57:47 +00:00
|
|
|
|
const newCourse = new Course(data);
|
2020-07-24 17:05:43 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (!newCourse.updateData(data)) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
console.info(`Ignoring course ${id} which is outdated.`);
|
2020-07-25 16:05:43 +00:00
|
|
|
|
} else {
|
2020-07-24 22:57:47 +00:00
|
|
|
|
console.info(`Course ${id} starting.`);
|
|
|
|
|
courses[id] = newCourse;
|
|
|
|
|
}
|
2020-07-24 17:05:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove stale courses
|
2020-07-25 16:05:43 +00:00
|
|
|
|
for (const id of Object.keys(courses)) {
|
|
|
|
|
if (!(id in dataset)) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
delete courses[id];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const tick = (courses, time) => {
|
|
|
|
|
for (const course of Object.values(courses)) {
|
2020-07-24 17:05:43 +00:00
|
|
|
|
course.tick(time);
|
|
|
|
|
}
|
2020-07-17 17:17:06 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const start = () => {
|
2020-07-23 15:29:35 +00:00
|
|
|
|
const courses = {};
|
|
|
|
|
let lastFrame = null;
|
|
|
|
|
let lastUpdate = null;
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
const update = () => {
|
2020-07-23 15:29:35 +00:00
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
if (lastUpdate === null || lastUpdate + 5000 <= now) {
|
2020-07-23 15:29:35 +00:00
|
|
|
|
lastUpdate = now;
|
2020-07-24 17:05:43 +00:00
|
|
|
|
updateData(courses);
|
2020-07-23 15:29:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const time = lastFrame === null ? 0 : now - lastFrame;
|
2020-07-25 16:05:43 +00:00
|
|
|
|
|
2020-07-23 15:29:35 +00:00
|
|
|
|
lastFrame = now;
|
2020-07-24 17:05:43 +00:00
|
|
|
|
tick(courses, time);
|
2020-07-23 15:29:35 +00:00
|
|
|
|
};
|
2020-07-18 23:45:36 +00:00
|
|
|
|
|
2020-07-25 16:05:43 +00:00
|
|
|
|
return { courses, update };
|
2020-07-17 17:17:06 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-07-23 17:19:35 +00:00
|
|
|
|
exports.start = start;
|