diff --git a/src/services/updateData.ts b/src/services/updateData.ts index 1c81c29..ccf6e1e 100644 --- a/src/services/updateData.ts +++ b/src/services/updateData.ts @@ -7,6 +7,8 @@ import { LeaderboardSchema } from "../db/schemas/leaderboard"; import { Headsets } from "../headsets"; import { normalizedRegionName } from "../utils/regionUtils"; +let totalScores: number | undefined; + async function update() { const response = await axios.get("https://scoresaber.com/api/players/count"); const count = response.data; @@ -17,6 +19,15 @@ async function update() { .timestamp(new Date()); InfluxWriteAPI.writePoint(point); + if (totalScores) { + InfluxWriteAPI.writePoint( + new Point("scoresaber") + .tag("type", "score_count") + .intField("value", totalScores) + .timestamp(new Date()) + ); + } + console.log(`Updated player count to ${count}`); } @@ -26,9 +37,8 @@ async function connectWebsocket() { if (!leaderboard) { await LeaderboardSchema.create({ _id: "scoresaber", totalPlays: 0 }); } - let totalScores = leaderboard?.totalPlays || 0; - let totalCountries = leaderboard?.countries || {}; - let totalHeadsets = leaderboard?.headsets || {}; + // set total scores to the current total plays in the database + totalScores = leaderboard?.totalPlays || 0; const socket = new WebsocketClient("wss://scoresaber.com/ws"); socket.onopen = () => { @@ -57,6 +67,9 @@ async function connectWebsocket() { const commandData = json.commandData; if (commandName == "score") { const { score, leaderboard } = commandData; + if (!totalScores) { + throw new Error("totalScores is undefined"); + } totalScores++; const { @@ -74,8 +87,6 @@ async function connectWebsocket() { const hmdName = Headsets[hmd] || "Unknown"; const countryId = normalizedRegionName(player.country) || "Unknown"; - totalCountries[countryId] = (totalCountries[countryId] || 0) + 1; - totalHeadsets[hmdName] = (totalHeadsets[hmdName] || 0) + 1; // Update the leaderboard data in the database await LeaderboardSchema.updateOne( @@ -83,8 +94,6 @@ async function connectWebsocket() { { $set: { totalPlays: totalScores, - countries: totalCountries, - headsets: totalHeadsets, }, } ).exec(); @@ -129,12 +138,6 @@ async function connectWebsocket() { // Write the data to influx InfluxWriteAPI.writePoint(scorePoint); - InfluxWriteAPI.writePoint( - new Point("scoresaber") - .tag("type", "score_count") - .intField("value", totalScores) - .timestamp(new Date()) - ); } }; }