From 41a92ff82605ff0dfe655a0cc5bbb08d8aaa5cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Sat, 25 Jul 2020 00:57:47 +0200 Subject: [PATCH] Simulation: Fix undefined segment, keep history --- src/tam/simulation.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/tam/simulation.js b/src/tam/simulation.js index 4bef094..7fd262d 100644 --- a/src/tam/simulation.js +++ b/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; + } } }