diff --git a/.gitignore b/.gitignore index 5a47c0d..8b2fe56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules driver.txt -drivers.json \ No newline at end of file +drivers.json +.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8f008cf --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/stewardle.iml b/.idea/stewardle.iml new file mode 100644 index 0000000..0c8867d --- /dev/null +++ b/.idea/stewardle.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app.js b/app.js index f6be589..e39bbb8 100644 --- a/app.js +++ b/app.js @@ -5,8 +5,10 @@ const schedule = require("node-schedule") const express = require("express") const favicon = require("serve-favicon") const morgan = require("morgan") +const dayjs = require("dayjs") -const path = "./assets/drivers.json" +const driversPath = "./assets/drivers.json" +const statsPath = "./assets/stats.json" const flag = { "British": "gb", @@ -55,6 +57,11 @@ const team = { "Ferrari": "ferrari" } +let stats = { + "visits": 0, + "guesses": 0 +} + let drivers = {} let driver @@ -68,14 +75,24 @@ schedule.scheduleJob("0 0 * * *", async () => { dotd() }).catch(() => { console.log("API is unreachable! Not updating drivers...") - if (fs.existsSync(path)) { - let data = fs.readFileSync(path) + if (fs.existsSync(driversPath)) { + let data = fs.readFileSync(driversPath) drivers = JSON.parse(data) dotd() } else { throw "Ergast API is unreachable and the drivers.json cache has not been built. Please try again when the Ergast API is online." } }) + let rawStatsFile = fs.readFileSync(statsPath) + let statsFile = JSON.parse(rawStatsFile) + let date = dayjs.format("YYYY-MM-DD") + statsFile[date] = stats + let newStatsFile = JSON.stringify(statsFile) + fs.writeFileSync(statsPath, newStatsFile) + stats = { + "visits": 0, + "guesses": 0 + } }) async function main() { @@ -84,9 +101,13 @@ async function main() { dotd() }).catch(() => { console.log("API is unreachable! Not updating drivers...") - let data = fs.readFileSync(path) - drivers = JSON.parse(data) - dotd() + if (fs.existsSync(driversPath)) { + let data = fs.readFileSync(driversPath) + drivers = JSON.parse(data) + dotd() + } else { + throw "Ergast API is unreachable and the drivers.json cache has not been built. Please try again when the Ergast API is online." + } }) server() } @@ -164,6 +185,12 @@ function server() { app.get("/", (req, res) => { res.render("index") + stats.visits++ + }) + + app.get("/stats", (req, res) => { + let rawStatsFile = fs.readFileSync(statsPath) + res.json(JSON.parse(rawStatsFile)) }) app.get("/winner", (req, res) => { @@ -222,6 +249,7 @@ function server() { "firstYear": response[4], "wins": response[5] }) + stats.guesses++ }) let port = 3000 diff --git a/package-lock.json b/package-lock.json index d9f6694..8426213 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "axios": "^0.27.2", + "dayjs": "^1.11.7", "ejs": "^3.1.8", "express": "^4.18.1", "lodash": "^4.17.21", @@ -233,6 +234,11 @@ "node": ">=0.8" } }, + "node_modules/dayjs": { + "version": "1.11.7", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.7.tgz", + "integrity": "sha512-+Yw9U6YO5TQohxLcIkrXBeY73WP3ejHWVvx8XCk3gxvQDCTEmS48ZrSZCKciI7Bhl/uCMyxYtE9UqRILmFphkQ==" + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -1177,6 +1183,11 @@ "luxon": "^1.26.0" } }, + "dayjs": { + "version": "1.11.7", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.7.tgz", + "integrity": "sha512-+Yw9U6YO5TQohxLcIkrXBeY73WP3ejHWVvx8XCk3gxvQDCTEmS48ZrSZCKciI7Bhl/uCMyxYtE9UqRILmFphkQ==" + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/package.json b/package.json index 7b1e609..a2c6dd8 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "axios": "^0.27.2", + "dayjs": "^1.11.7", "ejs": "^3.1.8", "express": "^4.18.1", "lodash": "^4.17.21",