set total scores less often - will savve data
All checks were successful
deploy / deploy (push) Successful in 29s

This commit is contained in:
Lee 2023-10-31 14:07:26 +00:00
parent ec0fe68eab
commit e0363de4ad

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