From 590a3e95026c2663e8a88e004294c6ad5087d9b6 Mon Sep 17 00:00:00 2001 From: ssyyhhrr <44934807+ssyyhhrr@users.noreply.github.com> Date: Wed, 15 Mar 2023 20:29:55 +0000 Subject: [PATCH] Driver Generation Date Detection --- .gitignore | 1 - app.js | 75 +++++++++++++++++++++++++++-------------------- assets/stats.json | 1 - 3 files changed, 44 insertions(+), 33 deletions(-) delete mode 100644 assets/stats.json diff --git a/.gitignore b/.gitignore index a26f5ba..0701f44 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ node_modules drivers.json stats.json -past.json .idea \ No newline at end of file diff --git a/app.js b/app.js index b651133..195d61c 100644 --- a/app.js +++ b/app.js @@ -85,7 +85,7 @@ axios.get("https://ergast.com/api/f1/1950/driverStandings.json?limit=1000").then console.log(err) return process.exit(1) }).then(() => { - dotd(true) + dotd() server() }) @@ -100,35 +100,10 @@ schedule.scheduleJob("59 23 * * *", async () => { schedule.scheduleJob("0 0 * * *", () => { dotd() - stats = { - "visits": 0, - "guesses": 0 - } }) schedule.scheduleJob("* * * * *", () => { - let statsFile = JSON.parse(fs.readFileSync(statsPath)) - let date = dayjs().format("YYYY-MM-DD") - if (statsFile.hasOwnProperty(date)) { - if (statsFile[date]["visits"] < stats["visits"]) { - statsFile[date]["visits"] = stats["visits"] - } else { - statsFile[date]["visits"] += stats["visits"] - stats["visits"] = statsFile[date]["visits"] - } - if (statsFile[date]["guesses"] < stats["guesses"]) { - statsFile[date]["guesses"] = stats["guesses"] - } else { - statsFile[date]["guesses"] += stats["guesses"] - stats["guesses"] = statsFile[date]["guesses"] - } - if (!statsFile[date].hasOwnProperty("driver") && stats.hasOwnProperty("driver")) { - statsFile[date]["driver"] = stats["driver"] - } - } else { - statsFile[date] = stats - } - fs.writeFileSync(statsPath, JSON.stringify(statsFile)) + processStats() }) async function updateDrivers() { @@ -172,13 +147,46 @@ async function updateDrivers() { }) } -function dotd(cold = false) { +function processStats() { + let date = dayjs().format("YYYY-MM-DD") + let statsFile = {} + if (fs.existsSync(statsPath)) { + statsFile = JSON.parse(fs.readFileSync(statsPath)) + if (statsFile.hasOwnProperty(date)) { + if (statsFile[date]["visits"] < stats["visits"]) { + statsFile[date]["visits"] = stats["visits"] + } else { + statsFile[date]["visits"] += stats["visits"] + stats["visits"] = statsFile[date]["visits"] + } + if (statsFile[date]["guesses"] < stats["guesses"]) { + statsFile[date]["guesses"] = stats["guesses"] + } else { + statsFile[date]["guesses"] += stats["guesses"] + stats["guesses"] = statsFile[date]["guesses"] + } + if (!statsFile[date].hasOwnProperty("driver") && stats.hasOwnProperty("driver")) { + statsFile[date]["driver"] = stats["driver"] + } + } else { + statsFile[date] = stats + } + } else { + statsFile[date] = stats + } + fs.writeFileSync(statsPath, JSON.stringify(statsFile)) +} + +function dotd() { console.log("Selecting Driver of the Day...") - let pastDrivers + let date = dayjs().format("YYYY-MM-DD") + let pastDrivers = [] + let pastDates = [] if (fs.existsSync(statsPath)) { pastDrivers = Object.values(JSON.parse(fs.readFileSync(statsPath))).map(x => x.driver).filter((x) => { return typeof x === "string"}) + pastDates = Object.keys(JSON.parse(fs.readFileSync(statsPath))) } - if (cold && pastDrivers.length > 0) { + if (pastDrivers.length > 0 && pastDates.length > 0 && pastDates[pastDates.length - 1] === date) { driver = pastDrivers[pastDrivers.length - 1] } else { let newDriver = getRandomProperty(drivers) @@ -188,7 +196,12 @@ function dotd(cold = false) { } driver = newDriver } - stats["driver"] = driver + stats = { + "visits": 0, + "guesses": 0, + "driver": driver + } + processStats() console.log(`Driver of the Day is ${driver}!`) console.log(drivers[driver]) } diff --git a/assets/stats.json b/assets/stats.json deleted file mode 100644 index 696927d..0000000 --- a/assets/stats.json +++ /dev/null @@ -1 +0,0 @@ -{"2023-03-13":{"visits":4,"guesses":0},"2023-03-15":{"visits":2112,"guesses":1664,"driver":"sainz"}} \ No newline at end of file