update how countries and hmds are stored
All checks were successful
deploy / deploy (push) Successful in 39s

This commit is contained in:
Lee 2023-10-30 15:27:10 +00:00
parent 4be7a46a90
commit b348969089
3 changed files with 44 additions and 12 deletions

@ -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 =

@ -5,7 +5,6 @@ const scoreSchema = new Schema({
_id: String,
player: {
id: String,
hmd: String,
},
acc: Number,
pp: Number,

@ -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")
InfluxWriteAPI.writePoint(
new Point("scoresaber")
.tag("type", "score_count")
.intField("value", totalScores)
.stringField("country", country)
.timestamp(new Date());
InfluxWriteAPI.writePoint(point);
.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())
);
}
};
}