Skip to content

Commit 18ed067

Browse files
committed
Handle display properly, and round to nearest second.
We don't have sub-second accuracy here, let's not pretend.
1 parent 757f32e commit 18ed067

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/messages/PitMessage.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { timeInSeconds } from '../formats.js';
1+
import dayjs from '../dates.js';
22
import { Stat } from '../racing.js';
33
import { Message } from './Message.js';
44

5+
const roundToSecond = (ts) => Math.round(ts / 1000) * 1000;
6+
57
export const PitMessage = (se, oldCar, newCar, cache) => {
68
const oldState = se.get(oldCar, Stat.STATE);
79
const newState = se.get(newCar, Stat.STATE);
@@ -41,14 +43,14 @@ export const PitMessage = (se, oldCar, newCar, cache) => {
4143
const { lastFuelIn, lastFuelOut, lastPitIn, lastPitOut } = cache['PitMessage'][carNum];
4244
let pitTimeMsg = '';
4345
if (Number.isInteger(lastPitIn) && Number.isInteger(lastPitOut)) {
44-
const pitTime = lastPitOut - lastPitIn;
46+
const pitTime = roundToSecond(lastPitOut) - roundToSecond(lastPitIn);
4547

46-
const fuelTime = (lastFuelOut || 0) - (lastFuelIn || 0);
48+
const fuelTime = roundToSecond(lastFuelOut || 0) - roundToSecond(lastFuelIn || 0);
4749
if (fuelTime > 0) {
48-
pitTimeMsg = `(total pit time: ${timeInSeconds(pitTime + fuelTime, 0)}, ${timeInSeconds(fuelTime, 0)} fuel)`;
50+
pitTimeMsg = `(total pit time: ${dayjs.duration(pitTime + fuelTime).format('m:ss')}, ${dayjs.duration(fuelTime).format('m:ss')} fuel)`;
4951
}
5052
else {
51-
pitTimeMsg = `(pit time: ${timeInSeconds(pitTime, 0)})`;
53+
pitTimeMsg = `(pit time: ${dayjs.duration(pitTime).format('m:ss')})`;
5254
}
5355

5456
delete cache['PitMessage'][carNum];

src/messages/__tests__/PitMessage.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ it('calculates a total pit time if both pit-in and pit-out have been seen', () =
4343
const generator = new MessageGenerator();
4444

4545
generator.generate({ colSpec }, { cars: oldCars, lastUpdated: 0 }, { cars: pitInCars, lastUpdated: 0 });
46-
const outMsgs = generator.generate({ colSpec }, { cars: pitInCars, lastUpdated: 0 }, { cars: pitOutCars, lastUpdated: 123 });
46+
const outMsgs = generator.generate({ colSpec }, { cars: pitInCars, lastUpdated: 0 }, { cars: pitOutCars, lastUpdated: 123000 });
4747

4848
expect(outMsgs.length).toEqual(1);
4949
expect(outMsgs[0][2]).toEqual('#1 (John Hindhaugh) has left the pits (pit time: 2:03)');
@@ -58,11 +58,11 @@ it('calculates a total pit time if both pit-in and pit-out have been seen and ca
5858
const generator = new MessageGenerator();
5959

6060
generator.generate({ colSpec }, { cars: oldCars, lastUpdated: 0 }, { cars: pitInCars, lastUpdated: 0 });
61-
generator.generate({ colSpec }, { cars: pitInCars, lastUpdated: 0 }, { cars: pitFuelCars, lastUpdated: 62 });
62-
const outMsgs = generator.generate({ colSpec }, { cars: pitInCars, lastUpdated: 0 }, { cars: pitOutCars, lastUpdated: 123 });
61+
generator.generate({ colSpec }, { cars: pitInCars, lastUpdated: 0 }, { cars: pitFuelCars, lastUpdated: 62000 });
62+
const outMsgs = generator.generate({ colSpec }, { cars: pitFuelCars, lastUpdated: 62000 }, { cars: pitOutCars, lastUpdated: 123000 });
6363

6464
expect(outMsgs.length).toEqual(1);
65-
expect(outMsgs[0][2]).toEqual('#1 (John Hindhaugh) has left the pits (pit time: 2:03)');
65+
expect(outMsgs[0][2]).toEqual('#1 (John Hindhaugh) has left the pits (total pit time: 2:03, 1:01 fuel)');
6666
});
6767

6868
it('calculates total pit time and fuel time if both pit-in and pit-out have been seen and car comes in via fuelling area', () => {
@@ -74,9 +74,10 @@ it('calculates total pit time and fuel time if both pit-in and pit-out have been
7474
const generator = new MessageGenerator();
7575

7676
generator.generate({ colSpec }, { cars: oldCars, lastUpdated: 0 }, { cars: pitFuelCars, lastUpdated: 0 });
77-
generator.generate({ colSpec }, { cars: pitFuelCars, lastUpdated: 0 }, { cars: pitInCars, lastUpdated: 62 });
78-
const outMsgs = generator.generate({ colSpec }, { cars: pitInCars, lastUpdated: 0 }, { cars: pitOutCars, lastUpdated: 123 });
77+
generator.generate({ colSpec }, { cars: pitFuelCars, lastUpdated: 0 }, { cars: pitInCars, lastUpdated: 52499 });
78+
// Also demonstrating rounding to the nearest second
79+
const outMsgs = generator.generate({ colSpec }, { cars: pitInCars, lastUpdated: 52499 }, { cars: pitOutCars, lastUpdated: 123499 });
7980

8081
expect(outMsgs.length).toEqual(1);
81-
expect(outMsgs[0][2]).toEqual('#1 (John Hindhaugh) has left the pits (total pit time: 2:03, 1:02 fuel)');
82+
expect(outMsgs[0][2]).toEqual('#1 (John Hindhaugh) has left the pits (total pit time: 2:03, 0:52 fuel)');
8283
});

0 commit comments

Comments
 (0)