Past Drivers Persistence

This commit is contained in:
ssyyhhrr
2023-03-15 17:02:11 +00:00
parent bec302b2c6
commit 09fbe44bf3
2 changed files with 9 additions and 6 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,5 @@
node_modules node_modules
driver.txt
drivers.json drivers.json
stats.json stats.json
past.json
.idea .idea

13
app.js
View File

@@ -13,7 +13,6 @@ const version = uuidv4()
const driversPath = "./assets/drivers.json" const driversPath = "./assets/drivers.json"
const statsPath = "./assets/stats.json" const statsPath = "./assets/stats.json"
const driverPath = "./assets/driver.txt"
const pastPath = "./assets/past.json" const pastPath = "./assets/past.json"
const flag = { const flag = {
@@ -69,11 +68,15 @@ let stats = {
} }
let drivers = {} let drivers = {}
let pastDrivers = []
let driver let driver
let pastDrivers
let year = new Date().getFullYear() let year = new Date().getFullYear()
if (fs.existsSync(pastPath)) {
pastDrivers = JSON.parse(fs.readFileSync(pastPath))
} else pastDrivers = []
axios.get("https://ergast.com/api/f1/1950/driverStandings.json?limit=1000").then(async () => { axios.get("https://ergast.com/api/f1/1950/driverStandings.json?limit=1000").then(async () => {
await updateDrivers() await updateDrivers()
}).catch(() => { }).catch(() => {
@@ -159,8 +162,8 @@ async function updateDrivers() {
function dotd(cold = false) { function dotd(cold = false) {
console.log("Selecting Driver of the Day...") console.log("Selecting Driver of the Day...")
if (cold && fs.existsSync(driverPath)) { if (cold && fs.existsSync(pastPath)) {
driver = fs.readFileSync(driverPath) driver = pastDrivers[pastDrivers.length - 1]
} else { } else {
let newDriver = getRandomProperty(drivers) let newDriver = getRandomProperty(drivers)
if (pastDrivers.includes(newDriver)) { if (pastDrivers.includes(newDriver)) {
@@ -170,7 +173,7 @@ function dotd(cold = false) {
driver = newDriver driver = newDriver
pastDrivers.push(driver) pastDrivers.push(driver)
if (pastDrivers.length > 7) pastDrivers.shift() if (pastDrivers.length > 7) pastDrivers.shift()
fs.writeFileSync(driverPath, driver) fs.writeFileSync(pastPath, JSON.stringify(pastDrivers))
} }
console.log(`Driver of the Day is ${driver}!`) console.log(`Driver of the Day is ${driver}!`)
console.log(drivers[driver]) console.log(drivers[driver])