Driver Generation Date Detection

This commit is contained in:
ssyyhhrr
2023-03-15 20:29:55 +00:00
parent 49d1b43979
commit 590a3e9502
3 changed files with 44 additions and 33 deletions

1
.gitignore vendored
View File

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

75
app.js
View File

@@ -85,7 +85,7 @@ axios.get("https://ergast.com/api/f1/1950/driverStandings.json?limit=1000").then
console.log(err) console.log(err)
return process.exit(1) return process.exit(1)
}).then(() => { }).then(() => {
dotd(true) dotd()
server() server()
}) })
@@ -100,35 +100,10 @@ schedule.scheduleJob("59 23 * * *", async () => {
schedule.scheduleJob("0 0 * * *", () => { schedule.scheduleJob("0 0 * * *", () => {
dotd() dotd()
stats = {
"visits": 0,
"guesses": 0
}
}) })
schedule.scheduleJob("* * * * *", () => { schedule.scheduleJob("* * * * *", () => {
let statsFile = JSON.parse(fs.readFileSync(statsPath)) processStats()
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))
}) })
async function updateDrivers() { 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...") console.log("Selecting Driver of the Day...")
let pastDrivers let date = dayjs().format("YYYY-MM-DD")
let pastDrivers = []
let pastDates = []
if (fs.existsSync(statsPath)) { if (fs.existsSync(statsPath)) {
pastDrivers = Object.values(JSON.parse(fs.readFileSync(statsPath))).map(x => x.driver).filter((x) => { return typeof x === "string"}) 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] driver = pastDrivers[pastDrivers.length - 1]
} else { } else {
let newDriver = getRandomProperty(drivers) let newDriver = getRandomProperty(drivers)
@@ -188,7 +196,12 @@ function dotd(cold = false) {
} }
driver = newDriver driver = newDriver
} }
stats["driver"] = driver stats = {
"visits": 0,
"guesses": 0,
"driver": driver
}
processStats()
console.log(`Driver of the Day is ${driver}!`) console.log(`Driver of the Day is ${driver}!`)
console.log(drivers[driver]) console.log(drivers[driver])
} }

View File

@@ -1 +0,0 @@
{"2023-03-13":{"visits":4,"guesses":0},"2023-03-15":{"visits":2112,"guesses":1664,"driver":"sainz"}}