tracktracker/src/tam/simulation.js

369 lines
9.7 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
const turf = require('@turf/turf');
const network = require('./network.json');
const server = 'http://localhost:4321';
2020-07-24 17:05:43 +00:00
class Course
{
2020-07-24 17:05:43 +00:00
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;
this.history = [];
}
2020-07-24 17:05:43 +00:00
get currentSegment()
{
2020-07-24 17:05:43 +00:00
if (this.state !== 'moving')
{
return undefined;
}
return network.segments[`${this.departureStop}-${this.arrivalStop}`];
}
2020-07-24 17:05:43 +00:00
updateData(data)
{
this.line = data.line;
this.finalStop = data.finalStop;
Object.assign(this.passings, data.nextPassings);
2020-07-24 17:05:43 +00:00
const now = Date.now();
2020-07-24 17:05:43 +00:00
// Make sure were on the right `stopped`/`moving` state
if (this.state === null)
{
let previousStop = null;
let departureTime = 0;
2020-07-24 17:05:43 +00:00
let nextStop = null;
let arrivalTime = Infinity;
2020-07-24 17:05:43 +00:00
for (let [stopId, time] of Object.entries(this.passings))
{
if (time > now && time < arrivalTime)
{
nextStop = stopId;
arrivalTime = time;
}
2020-07-24 17:05:43 +00:00
if (time < now && time > departureTime)
{
previousStop = stopId;
departureTime = time;
}
}
2020-07-17 17:17:06 +00:00
2020-07-24 17:05:43 +00:00
if (nextStop === null)
{
return false;
}
2020-07-24 17:05:43 +00:00
if (previousStop === null)
{
// Teleport to the first known stop
this.arriveToStop(nextStop);
}
else
{
2020-07-24 17:05:43 +00:00
// Teleport to the first known segment
this.arriveToStop(previousStop);
this.moveToStop(nextStop, arrivalTime);
}
}
2020-07-24 17:05:43 +00:00
else if (this.state === 'moving')
{
2020-07-24 17:05:43 +00:00
// Should already be at the next stop
if (this.passings[this.arrivalStop] <= now)
{
this.arriveToStop(this.arrivalStop);
}
// On the right track, update the arrival time
else
{
this.arrivalTime = this.passings[this.arrivalStop];
}
}
2020-07-24 17: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-24 17:05:43 +00:00
for (let [stopId, time] of Object.entries(this.passings))
{
2020-07-24 17:05:43 +00:00
if (time > now && time < arrivalTime)
{
2020-07-24 17:05:43 +00:00
nextStop = stopId;
arrivalTime = time;
}
}
2020-07-24 17:05:43 +00:00
if (nextStop === null)
{
2020-07-24 17:05:43 +00:00
// This course is finished
return false;
}
if (nextStop !== this.currentStop)
{
this.moveToStop(nextStop, arrivalTime);
}
}
2020-07-24 17:05:43 +00:00
if (this.state === 'moving')
2020-07-17 17:17:06 +00:00
{
2020-07-24 17:05:43 +00:00
this.speed = this.computeTheoreticalSpeed();
}
2020-07-24 17:05:43 +00:00
return true;
}
2020-07-24 17:05:43 +00:00
tick(time)
{
2020-07-24 17:05:43 +00:00
if (this.state === null)
{
2020-07-24 17:05:43 +00:00
// Ignore uninitalized courses
}
2020-07-24 17:05:43 +00:00
else if (this.state === 'moving')
{
2020-07-24 17:05:43 +00:00
// Integrate current speed in travelled distance
const delta = this.speed * time;
2020-07-24 17:05:43 +00:00
const segment = this.currentSegment;
const length = segment.points[segment.points.length - 1].distance;
2020-07-24 17:05:43 +00:00
this.traveledDistance += delta;
2020-07-24 17:05:43 +00:00
if (this.traveledDistance >= length)
{
2020-07-24 17:05:43 +00:00
this.arriveToStop(this.arrivalStop);
return;
}
2020-07-23 15:29:35 +00:00
// Recompute updated position
2020-07-24 17:05:43 +00:00
const departureStop = network.stops[this.departureStop];
const arrivalStop = network.stops[this.arrivalStop];
2020-07-23 15:29:35 +00:00
const nextNodeIndex = segment.points.findIndex(
2020-07-24 17:05:43 +00:00
({distance}) => distance >= this.traveledDistance);
2020-07-23 15:29:35 +00:00
if (nextNodeIndex === 0)
{
2020-07-24 17:05:43 +00:00
this.position = turf.toMercator([
departureStop.lon,
departureStop.lat
]);
2020-07-23 15:29:35 +00:00
}
else
{
const previousNode = segment.points[nextNodeIndex - 1];
const nextNode = segment.points[nextNodeIndex];
const previousPoint = turf.toMercator([
previousNode.lon,
previousNode.lat
]);
const nextPoint = turf.toMercator([
nextNode.lon,
nextNode.lat
]);
2020-07-24 17:05:43 +00:00
const curLength = this.traveledDistance
2020-07-23 15:29:35 +00:00
- previousNode.distance;
const totalLength = nextNode.distance
- previousNode.distance;
const t = curLength / totalLength;
2020-07-23 15:29:35 +00:00
2020-07-24 17:05:43 +00:00
this.position = [
t * nextPoint[0] + (1 - t) * previousPoint[0],
t * nextPoint[1] + (1 - t) * previousPoint[1],
];
2020-07-23 22:18:30 +00:00
2020-07-24 17:05:43 +00:00
this.angle = Math.atan2(
2020-07-23 22:18:30 +00:00
previousPoint[1] - nextPoint[1],
nextPoint[0] - previousPoint[0],
);
2020-07-23 15:29:35 +00:00
}
}
2020-07-24 17:05:43 +00:00
else // this.state === 'stopped'
2020-07-23 15:29:35 +00:00
{
2020-07-24 17:05:43 +00:00
const currentNode = network.stops[this.currentStop];
2020-07-23 22:18:30 +00:00
2020-07-24 17:05:43 +00:00
this.position = turf.toMercator([
currentNode.lon,
currentNode.lat
]);
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.
*
* @param stop Identifier for the stop to which the course arrives.
*/
arriveToStop(stop)
{
this.state = 'stopped';
this.currentStop = stop;
this.position = turf.toMercator([
network.stops[this.currentStop].lon,
network.stops[this.currentStop].lat,
]);
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.
*
* @param stop Next stop for this course.
* @param arrivalTime Planned arrival time to that stop.
*/
moveToStop(stop, arrivalTime)
{
if (!(`${this.currentStop}-${stop}` in network.segments))
{
console.warn(`Course ${this.id} is cannot go from stop
${this.currentStop} to stop ${stop}. Teleporting to ${stop}`);
this.arriveToStop(stop);
return;
}
2020-07-24 17:05:43 +00:00
this.state = 'moving';
this.departureStop = this.currentStop;
this.arrivalStop = stop;
this.arrivalTime = arrivalTime;
this.traveledDistance = 0;
this.speed = 0;
this.position = turf.toMercator([
network.stops[this.departureStop].lon,
network.stops[this.departureStop].lat,
]);
this.history.push(['moveToStop', stop, arrivalTime]);
2020-07-24 17:05:43 +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.
*/
computeTheoreticalSpeed()
{
if (this.state !== 'moving')
{
return 0;
}
const segment = this.currentSegment;
const length = segment.points[segment.points.length - 1].distance;
const remainingTime = this.arrivalTime - Date.now();
const remainingDistance = length - this.traveledDistance;
if (remainingDistance <= 0)
{
return 0;
}
else if (remainingTime <= 0)
{
// Were late, go to maximum speed
return 50 / 3600; // 50 km/h
}
else
{
return remainingDistance / remainingTime;
}
}
}
const updateData = async (courses) =>
{
const dataset = (await axios.get(`${server}/courses`)).data;
// Update or create new courses
for (let [id, data] of Object.entries(dataset))
{
if (id in courses)
{
if (!courses[id].updateData(data))
{
console.info(`Course ${id} is finished.`);
delete courses[id];
}
2020-07-24 17:05:43 +00:00
}
else
{
const newCourse = new Course(data);
2020-07-24 17:05:43 +00:00
if (!newCourse.updateData(data))
2020-07-24 17:05:43 +00:00
{
console.info(`Ignoring course ${id} which is outdated.`);
}
else
{
console.info(`Course ${id} starting.`);
courses[id] = newCourse;
}
2020-07-24 17:05:43 +00:00
}
}
// Remove stale courses
for (let id of Object.keys(courses))
{
if (!(id in dataset))
{
delete courses[id];
}
}
};
const tick = (courses, time) =>
{
for (let course of Object.values(courses))
{
course.tick(time);
}
2020-07-17 17:17:06 +00:00
};
const start = () =>
2020-07-17 17:17:06 +00:00
{
2020-07-23 15:29:35 +00:00
const courses = {};
let lastFrame = null;
let lastUpdate = null;
const update = () =>
{
2020-07-23 15:29:35 +00:00
const now = Date.now();
if (lastUpdate === null || lastUpdate + 5000 <= now)
{
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;
lastFrame = now;
2020-07-24 17:05:43 +00:00
tick(courses, time);
2020-07-23 15:29:35 +00:00
};
return {courses, update};
2020-07-17 17:17:06 +00:00
};
exports.start = start;