From b348969089a6b83b7fef40b34bd53fcb1f877a35 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 30 Oct 2023 15:27:10 +0000 Subject: [PATCH] update how countries and hmds are stored --- src/db/schemas/leaderboard.ts | 8 ++++++ src/db/schemas/score.ts | 1 - src/services/updateData.ts | 47 +++++++++++++++++++++++++++-------- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/db/schemas/leaderboard.ts b/src/db/schemas/leaderboard.ts index d443289..8cc7a78 100644 --- a/src/db/schemas/leaderboard.ts +++ b/src/db/schemas/leaderboard.ts @@ -4,6 +4,14 @@ const { Schema } = mongoose; const leaderboardSchema = new Schema({ _id: String, totalPlays: Number, + countries: { + type: Map, + of: Number, + }, + headsets: { + type: Map, + of: Number, + }, }); export const LeaderboardSchema = diff --git a/src/db/schemas/score.ts b/src/db/schemas/score.ts index ebd7cba..1cb0b17 100644 --- a/src/db/schemas/score.ts +++ b/src/db/schemas/score.ts @@ -5,7 +5,6 @@ const scoreSchema = new Schema({ _id: String, player: { id: String, - hmd: String, }, acc: Number, pp: Number, diff --git a/src/services/updateData.ts b/src/services/updateData.ts index 883fe42..768d730 100644 --- a/src/services/updateData.ts +++ b/src/services/updateData.ts @@ -26,6 +26,8 @@ async function connectWebsocket() { await LeaderboardSchema.create({ _id: "scoresaber", totalPlays: 0 }); } let totalScores = leaderboard?.totalPlays || 0; + let totalCountries = leaderboard?.countries || {}; + let totalHeadsets = leaderboard?.headsets || {}; const socket = new WebsocketClient("wss://scoresaber.com/ws"); socket.onopen = () => { @@ -55,10 +57,6 @@ async function connectWebsocket() { if (commandName == "score") { const { score, leaderboard } = commandData; totalScores++; - LeaderboardSchema.updateOne( - { _id: "scoresaber" }, - { totalPlays: totalScores } - ).exec(); const { id, @@ -71,6 +69,7 @@ async function connectWebsocket() { pp, } = score; const { maxScore, stars, id: leaderboardId } = leaderboard; + await Score.findByIdAndDelete(id).exec(); // Remove any existing score with the same id const data: any = { _id: id, @@ -94,15 +93,41 @@ async function connectWebsocket() { data.stars = stars; } - await Score.findByIdAndDelete(id).exec(); Score.create(data); + const countryId = country?.toLowerCase() || "unknown"; + totalCountries[countryId] = (totalCountries[countryId] || 0) + 1; + totalHeadsets[hmd] = (totalHeadsets[hmd] || 0) + 1; + await LeaderboardSchema.updateOne( + { _id: "scoresaber" }, + { + $set: { + totalPlays: totalScores, + countries: totalCountries, + headsets: totalHeadsets, + }, + } + ).exec(); - const point = new Point("scoresaber") - .tag("type", "score_count") - .intField("value", totalScores) - .stringField("country", country) - .timestamp(new Date()); - InfluxWriteAPI.writePoint(point); + InfluxWriteAPI.writePoint( + new Point("scoresaber") + .tag("type", "score_count") + .intField("value", totalScores) + .timestamp(new Date()) + ); + InfluxWriteAPI.writePoint( + new Point("scoresaber") + .tag("type", "headsets") + .tag("hmd", hmd) + .intField("value", totalHeadsets[hmd]) + .timestamp(new Date()) + ); + InfluxWriteAPI.writePoint( + new Point("scoresaber") + .tag("type", "countries") + .tag("country", countryId) + .intField("value", totalCountries[countryId]) + .timestamp(new Date()) + ); } }; }