Skip to content

Commit

Permalink
feat: race data file being written + session data
Browse files Browse the repository at this point in the history
  • Loading branch information
chaseconey committed Mar 10, 2022
1 parent 2b73c78 commit e1743e1
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 40 deletions.
44 changes: 40 additions & 4 deletions packages/main/src/mainWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { BrowserWindow } from 'electron';
import { join } from 'path';
import { URL } from 'url';
import { F1TelemetryClient, constants } from '@racehub-io/f1-telemetry-client';
import { mergeData } from './utils';
import { app } from 'electron';
import fs from 'fs';

const { PACKETS } = constants;

let session = {};
Expand Down Expand Up @@ -36,13 +40,41 @@ client.on(PACKETS.session, (data) => (session = data));
client.on(PACKETS.participants, (data) => (drivers = data));
// client.on(PACKETS.carTelemetry, console.log);
// client.on(PACKETS.carStatus, console.log);
// client.on(PACKETS.finalClassification, console.log);
client.on(PACKETS.finalClassification, (data) =>
handleFinalClassification(data),
);
// client.on(PACKETS.lobbyInfo, console.log);
// client.on(PACKETS.carDamage, console.log);
client.on(PACKETS.sessionHistory, (data) => {
lapHistory[data.m_carIdx] = data;
});

function handleFinalClassification(data) {
console.log('writing final data');
const driverData = mergeData(
lapData.m_lapData,
drivers.m_participants,
lapHistory,
data.m_classificationData,
);

const raceData = {
driverData,
session,
};

const downloadPath = app.getPath('downloads');
// TODO: use session id in name
const fileLocation = `${downloadPath}/race.json`;

BigInt.prototype['toJSON'] = function () {
return this.toString();
};
fs.writeFileSync(fileLocation, JSON.stringify(raceData));

console.log(`Race data written to ${fileLocation}`);
}

async function createWindow() {
const browserWindow = new BrowserWindow({
show: false, // Use 'ready-to-show' event to show window
Expand All @@ -66,12 +98,16 @@ async function createWindow() {

setInterval(() => {
browserWindow?.webContents.send('session', session);
browserWindow?.webContents.send('drivers', drivers);
}, 10000);

setInterval(() => {
browserWindow?.webContents.send('lapData', lapData);
browserWindow?.webContents.send('lapHistory', lapHistory);
// Merge drivers and other data together by driver
let merged = mergeData(
lapData.m_lapData,
drivers.m_participants,
lapHistory,
);
browserWindow?.webContents.send('drivers', merged);
browserWindow?.webContents.send('fastestLap', fastestLap);
}, 2000);

Expand Down
16 changes: 15 additions & 1 deletion packages/main/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
import fs from 'fs';
import { merge } from 'lodash';

export const out = (rawData, name) => {
BigInt.prototype['toJSON'] = function () {
Expand All @@ -8,3 +9,16 @@ export const out = (rawData, name) => {
let data = JSON.stringify(rawData, null, 2);
fs.writeFileSync(`data/${name}.json`, data);
};

export const mergeData = function () {
let combined = [];
if (!arguments) {
return combined;
}

for (let x = 0; x < arguments.length; x++) {
merge(combined, arguments[x]);
}

return combined;
};
8 changes: 1 addition & 7 deletions packages/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ export default {
window.api.handle('drivers', () => (event, data) => {
this.$store.state.drivers = data;
});
window.api.handle('lapData', () => (event, data) => {
this.$store.state.lapData = data;
});
window.api.handle('session', () => (_event, data) => {
this.$store.state.session = data;
});
window.api.handle('lapHistory', () => (_event, data) => {
this.$store.state.lapHistory = data;
});
window.api.handle('fastestLap', () => (_event, data) => {
this.$store.state.fastestLap = data;
});
Expand All @@ -31,7 +25,7 @@ export default {
<TopNav />
<router-view />
<div
v-if="$store.drivers"
v-if="!$store.state.drivers"
class="text-center"
>
Waiting for match ...
Expand Down
23 changes: 3 additions & 20 deletions packages/renderer/src/components/DriverTable.vue
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
<script>
import { mergeData } from '/@/utils';
import { mapState } from 'vuex';
export default {
computed: {
...mapState(['lapData', 'drivers', 'lapHistory', 'fastestLap']),
mergedDriverData() {
if (
!this.lapData?.m_lapData ||
!this.drivers?.m_participants ||
!this.lapHistory
) {
return [];
}
return mergeData(
this.lapData.m_lapData,
this.drivers.m_participants,
this.lapHistory,
);
},
...mapState(['drivers', 'fastestLap']),
sortedLapData() {
if (!this.mergedDriverData) {
if (!this.drivers) {
return [];
}
// Remove dead cars
const filtered = this.mergedDriverData?.filter(
(car) => car.m_carPosition > 0,
);
const filtered = this.drivers?.filter((car) => car.m_carPosition > 0);
// TODO: Map in driver names
Expand Down
47 changes: 41 additions & 6 deletions packages/renderer/src/components/SessionInfo.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
<script>
import { mapState } from 'vuex';
import { tracks, weatherTypes, sessionTypes } from '/@/constants';
export default {
computed: {
...mapState(['session', 'fastestLap']),
...mapState(['session']),
weatherTypes() {
return weatherTypes;
},
tracks() {
return tracks;
},
sessionTypes() {
return sessionTypes;
},
filteredWeatherSamples() {
if (!this.session?.m_weatherForecastSamples) {
return [];
}
return this.session.m_weatherForecastSamples.filter(
(sample) => sample.m_trackTemperature > 0,
);
},
},
};
</script>
<template>
<h1>
{{ tracks[session?.m_trackId] }}
<span class="fs-6">({{ sessionTypes[session?.m_sessionType] }})</span>
</h1>
<!-- <p>{{ session?.m_header }}</p> -->
<h2>Quick Stats</h2>
<div class="row my-2">
<div
class="col py-2"
Expand All @@ -19,11 +44,21 @@ export default {
<div class="col py-2">
Air Temp: {{ session?.m_airTemperature }}
</div>
<div class="col py-2">
Session Type: {{ session?.m_sessionType }}
</div>
<div class="col py-2">
Fastest Lap: {{ fastestLap }}
</div>
<h2>Forecast</h2>
<div class="row my-2">
<div
v-for="sample in filteredWeatherSamples"
:key="sample.m_timeOffset"
class="col"
>
<div class="d-flex flex-column text-center border">
<b>{{ weatherTypes[sample.m_weather] }}</b>
<div>{{ sample.m_trackTemperature }}°</div>
<div>{{ sample.m_rainPercentage }}%</div>
{{ sample.m_timeOffset }} m
</div>
</div>
</div>
</template>
57 changes: 57 additions & 0 deletions packages/renderer/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export const sessionTypes = [
'unknown', // 0
'P1', // 1
'P2', // 2
'P3', // 3
'Short P', // 4
'Q1', // 5
'Q2', // 6
'Q3', // 7
'Short Q', // 8
'OSQ', // 9
'R', // 10
'R2', // 11
'Time Trial', // 12
];

export const weatherTypes = [
'clear', // 0
'light cloud', // 1
'overcast', // 2
'light rain', // 3
'heavy rain', // 4
'storm', // 5
];

export const tracks = [
'Melbourne', // 0
'Paul Ricard', // 1
'Shanghai', // 2
'Sakhir (Bahrain)', // 3
'Catalunya', // 4
'Monaco', // 5
'Montreal', // 6
'Silverstone', // 7
'Hockenheim', // 8
'Hungaroring', // 9
'Spa', // 10
'Monza', // 11
'Singapore', // 12
'Suzuka', // 13
'Abu Dhabi', // 14
'Texas', // 15
'Brazil', // 16
'Austria', // 17
'Sochi', // 18
'Mexico', // 19
'Baku (Azerbaijan)', // 20
'Sakhir Short', // 21
'Silverstone Short', // 22
'Texas Short', // 23
'Suzuka Short', // 24
'Hanoi', // 25
'Zandvoort', // 26
'Imola', // 27
'Portimão', // 28
'Jeddah', // 29
];
2 changes: 0 additions & 2 deletions packages/renderer/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export default createStore({
return {
drivers: null,
session: null,
lapData: null,
lapHistory: [],
fastestLap: null,
};
},
Expand Down

0 comments on commit e1743e1

Please sign in to comment.