From 687f6b941a847bf546e05f5e6952d9273a3067a2 Mon Sep 17 00:00:00 2001 From: iamBadgers Date: Sun, 19 May 2024 13:53:00 -0700 Subject: [PATCH] wrap up game stats api --- backend/src/app.ts | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/backend/src/app.ts b/backend/src/app.ts index d8e029d..70ded53 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -14,19 +14,31 @@ app.get('/', (req, res) => { res.send('Hello World!') }) -app.post('/api/serverstats/gamestats', async (req, res) => { - const serverGameStats = - await database.query( - 'SELECT ' + - 'COUNT(*) as TotalGames, ' + - 'SUM(CASE WHEN status = "Complete" THEN 1 ELSE 0 END) as Complete, ' + - 'SUM(CASE WHEN status = "Postponed" THEN 1 ELSE 0 END) as Postponed, ' + - 'SUM(CASE WHEN status = "Pending" THEN 1 ELSE 0 END) as Pending, ' + - 'SUM(payoutEB) / SUM(CASE WHEN status = "Complete" THEN 1 ELSE 0 END) as AverageEB, ' + - 'SUM(payoutIP) / SUM(CASE WHEN status = "Complete" THEN 1 ELSE 0 END) as AverageIP ' + - 'FROM Games WHERE postdate BETWEEN 1706788520 AND 1709207720', - { type: QueryTypes.SELECT } - ) +app.post('/api/serverstats/gamestats', jsonParser, async (req, res) => { + + const month = req.body.monthId % 12 + const year = Math.floor(req.body.monthId / 12) + 2023 + + const startDate = new Date(year, month, 1) + const endDate = new Date(year, month + 1, -1) + + const serverGameStats = await database.query( + 'SELECT ' + + 'COUNT(*) as TotalGames, ' + + 'SUM(CASE WHEN status = "Complete" THEN 1 ELSE 0 END) as Complete, ' + + 'SUM(CASE WHEN status = "Postponed" THEN 1 ELSE 0 END) as Postponed, ' + + 'SUM(CASE WHEN status = "Pending" THEN 1 ELSE 0 END) as Pending, ' + + 'SUM(payoutEB) / SUM(CASE WHEN status = "Complete" THEN 1 ELSE 0 END) as AverageEB, ' + + 'SUM(payoutIP) / SUM(CASE WHEN status = "Complete" THEN 1 ELSE 0 END) as AverageIP ' + + 'FROM Games WHERE postdate BETWEEN :startSeconds AND :endSeconds', + { + type: QueryTypes.SELECT, + replacements: { + startSeconds: startDate.getTime() / 1000, + endSeconds: endDate.getTime() / 1000 + } + } + ) res.json(serverGameStats[0]) })