Browse Source

Simulation: Fix undefined segment, keep history

main
Mattéo Delabre 4 years ago
parent
commit
41a92ff826
Signed by: matteo GPG Key ID: AE3FBD02DC583ABB
  1. 35
      src/tam/simulation.js

35
src/tam/simulation.js

@ -24,6 +24,8 @@ class Course
this.position = [0, 0];
this.angle = 0;
this.history = [];
}
get currentSegment()
@ -223,6 +225,7 @@ class Course
network.stops[this.currentStop].lon,
network.stops[this.currentStop].lat,
]);
this.history.push(['arriveToStop', stop]);
}
/**
@ -233,6 +236,14 @@ class Course
*/
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;
}
this.state = 'moving';
this.departureStop = this.currentStop;
this.arrivalStop = stop;
@ -243,15 +254,10 @@ class Course
network.stops[this.departureStop].lon,
network.stops[this.departureStop].lat,
]);
this.history.push(['moveToStop', stop, arrivalTime]);
console.log(`Course ${this.id} leaving stop ${this.currentStop} \
console.info(`Course ${this.id} leaving stop ${this.currentStop} \
with initial speed ${this.computeTheoreticalSpeed() * 3600} km/h`);
if (this.currentSegment === undefined)
{
console.error(`Course ${this.id} is on an undefined segment from \
stop ${this.departureStop} to stop ${this.arrivalStop}`);
}
}
/**
@ -295,16 +301,25 @@ const updateData = async (courses) =>
{
if (id in courses)
{
courses[id].updateData(data);
if (!courses[id].updateData(data))
{
console.info(`Course ${id} is finished.`);
delete courses[id];
}
}
else
{
courses[id] = new Course(data);
const newCourse = new Course(data);
if (!courses[id].updateData(data))
if (!newCourse.updateData(data))
{
console.info(`Ignoring course ${id} which is outdated.`);
}
else
{
console.info(`Course ${id} starting.`);
courses[id] = newCourse;
}
}
}

Loading…
Cancel
Save